#include <vector>
#include <iostream>
#include <fstream>
#include <cmath>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#define PI 3.14159265 // Should be used from mathlib
inline float sqr(float x) { return x*x; }
//****************************************************
// Some Classes
//****************************************************
class Viewport;
class Viewport {
public:
int w, h; // width and height
};
//****************************************************
// shader
//****************************************************
const GLchar* vertexShaderSource = "#version 330 core
"
"layout (location = 0) in vec2 position;
"
"layout (location = 1) in vec3 color;
"
"out vec3 ourcolor;
"
"void main()
"
"{
"
"gl_Position = vec4(position.x, position.y, 1.0f, 1.0f);
"
"ourcolor = color;
"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core
"
"in vec3 ourcolor;
"
"out vec4 color;
"
"void main()
"
"{
"
"color = vec4(ourcolor, 1.0f);
"
"}
\0";
//****************************************************
// Global Variables
//****************************************************
Viewport viewport;
std::vector<GLfloat> col;
std::vector<GLfloat> pos;
GLuint colbuffer;
GLuint VAO;
GLuint posbuffer;
GLuint shaderProgram;
//****************************************************
// Simple init function
//****************************************************
void initScene(){
// Nothing to do here for this simple example.
}
//****************************************************
// reshape viewport if the window is resized
//****************************************************
void myReshape(int w, int h) {
viewport.w = w;
viewport.h = h;
glViewport (0,0,viewport.w,viewport.h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, viewport.w, 0, viewport.h);
}
void CreateVertexBuffer()
{
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &posbuffer);
glBindBuffer(GL_ARRAY_BUFFER, posbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GL_FLOAT) * pos.size(), &pos[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GL_FLOAT), (GLvoid*)0);
//glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glGenBuffers(1, &colbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GL_FLOAT) * col.size(), &col[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (GLvoid*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
}
//****************************************************
// A routine to set a pixel by drawing a GL point. This is not a
// general purpose routine as it assumes a lot of stuff specific to
// this example.
//****************************************************
void setPixel(float x, float y, GLfloat r, GLfloat g, GLfloat b) {
col.push_back(r);
col.push_back(g);
col.push_back(b);
pos.push_back(x);
pos.push_back(y); // The 0.5 is to target pixel
// centers
// Note: Need to check for gap
// bug on inst machines.
}
//****************************************************
// Draw a filled circle.
//****************************************************
void circle(float centerX, float centerY, float radius) {
// We could eliminate wasted work by only looping over the pixels
// inside the sphere's radius. But the example is more clear this
// way. In general drawing an object by loopig over the whole
// screen is wasteful.
int i,j; // Pixel indices
for (i=0;i<viewport.w;i++) {
for (j=0;j<viewport.h;j++) {
// Location of the center of pixel relative to center of sphere
float x = (i-centerX);
float y = (j-centerY);
float dist = sqrt(sqr(x) + sqr(y));
if (dist<=radius) {
setPixel(i,j, 0.5, 0.5, 0.0);
// This is amusing, but it assumes negative color values are treated reasonably.
// setPixel(i,j, x/radius, y/radius, z/radius );
}
}
}
}
//****************************************************
// Compile shader.
//****************************************************
void compile() {
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// Check for compile time errors
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED
" << infoLog << std::endl;
}
// Fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// Check for compile time errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED
" << infoLog << std::endl;
}
// Link shaders
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// Check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED
" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
//****************************************************
// function that does the actual drawing of stuff
//***************************************************
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT); // clear the color buffer
glMatrixMode(GL_MODELVIEW); // indicate we are specifying camera transformations
glLoadIdentity(); // make sure transformation is "zero'd"
glUseProgram(shaderProgram); //Use shader
// Start drawing
//circle(viewport.w / 2.0 , viewport.h / 2.0 , min(viewport.w, viewport.h) / 3.0);
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glDrawArrays(GL_POINTS, 0, (int)pos.size() / 2);
glBindVertexArray(VAO);
//glFlush();
glutSwapBuffers(); // swap buffers (we earlier set double buffer)
}
//****************************************************
// the usual stuff, nothing exciting here
//****************************************************
int main(int argc, char *argv[]) {
//Set core version
//This initializes glut
glutInitContextVersion (3, 3);
glutInit(&argc, argv);
//This tells glut to use a double-buffered window with red, green, and blue channels
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
// Initalize theviewport size
viewport.w = 400;
viewport.h = 400;
//The size and position of the window
glutInitWindowSize(viewport.w, viewport.h);
glutInitWindowPosition(0,0);
glutCreateWindow(argv[0]);
initScene();
glewExperimental = GL_TRUE;
GLenum res = glewInit();
if (res != GLEW_OK) {
std::cout << "Error: '%s'
" << glewGetErrorString(res) << std::endl;
return 1;
}
compile(); // quick function to set up scene
circle(viewport.w / 2.0 , viewport.h / 2.0 , std::min(viewport.w, viewport.h) / 3.0); // use shader
CreateVertexBuffer();
glutDisplayFunc(myDisplay); // function to run when its time to draw something
glutReshapeFunc(myReshape); // function to run when the window gets resized
glutMainLoop(); // infinite loop that will keep drawing and resizing
// and whatever else
return 0;
}
Hi. I want to make this program to draw a circle, so I make two vectors, one store the position and the other one store the color values, but I can’t draw a circle as expected. can anyone help , thanks!