Reputation: 31
Trying to get a very simple opengl/glut/glew program up and running. Currently the display()
function passed to glutDisplayFunc()
is not being called. When executed, init()
sets everything up and gives me a blank white window, but display never fills it with the generated points. Here is the init()
function which is called just before entering the glutMainLoop()
:
void init()
{
//generate points
const int NumPoints = 5000;
point3 points[NumPoints];
point3 vertices[3] = {point3(-1.0, -1.0, 0.0),
point3(0.0, 1.0, 0.0),
point3(1.0, -1.0, 0.0)};
points[0] = point3(0.25, 0.5, 0.0);
for(int k = 1; k < NumPoints; k++)
{
int j = rand() % 3;
points[k] = (points[k-1]+vertices[j])/2.0;
}
//load shaders and use the resulting shader program
GLuint program = InitShader("shaders/vshader.glsl", "shaders/fshader.glsl");
GLint linked;
glGetProgramiv( program, GL_LINK_STATUS, &linked );
if( !linked ){
std::cerr << "Shader program failed to link" << std::endl;
GLint logSize;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
char *logMsg = new char[logSize];
glGetProgramInfoLog( program, logSize, NULL, logMsg);
std::cerr << logMsg << std::endl;
delete [] logMsg;
exit( EXIT_FAILURE );
}
glUseProgram( program );
//create Vertex-Array object
GLuint aBuffer;
glGenVertexArrays(1, &aBuffer);
glBindVertexArray((GLuint)&aBuffer);
//create Buffer object
GLuint buffer;
//glGenBuffers(1, &buffer);
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points),
points, GL_STATIC_DRAW);
//initialize the vertex position attribute from the vertex shader
GLuint loc = glGetAttribLocation( program, "vPosition");
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glClearColor( 1.0, 1.0, 1.0, 1.0); // white background
}
and here are the main()
and display()
functions:
void display()
{
fprintf(stderr, "display called!\n");
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_POINTS, 0, 5000);
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutDisplayFunc(display);
glutCreateWindow("Program 1");
glewInit();
init();
glutMainLoop();
return 0;
}
using eclipse CDT C/C++ with MinGW. Debugging shows that glutMainLoop is indeed being called but I can't follow it past that. Could it be a shader issue? They are reportedly compiling and linking fine, but here they are
vshader.glsl
#version 150
in vec4 vPosition;
void main() {
gl_Position = vPosition;
}
.
fshader.glsl
#version 150
out vec4 fColor;
void main() {
fColor = vec4(1.0, 0.0, 0.0, 1.0);
}
edit: the shaders definitely work. By inserting glutIdleFunc(display);
into the main()
, the program executes properly and draws all the expected points. So like I originally thought, for some reason glutMainLoop()
just doesnt want to call the function passed to glutDisplayFunc()
? Or am I doing something terribly wrong?
Upvotes: 1
Views: 1045
Reputation: 162164
GLUT can create multiple windows. glutDisplayFunc operates on the currently active window, so you must call glutCreateWindow before the glut…Func functions.
Upvotes: 2