试图启动并运行一个非常简单的 opengl/glut/glew 程序。当前未display()
调用传递给的函数glutDisplayFunc()
。执行时,init()
设置所有内容并给我一个空白的白色窗口,但显示永远不会用生成的点填充它。这是init()
在进入之前调用的函数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
}
这里是main()
和display()
功能:
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;
}
将 Eclipse CDT C/C++ 与 MinGW 一起使用。调试表明 glutMainLoop 确实被调用了,但我无法超越它。会不会是着色器问题?据报道,他们正在编译和链接良好,但他们在这里
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);
}
编辑:着色器绝对有效。通过插入glutIdleFunc(display);
,main()
程序正确执行并绘制所有预期点。所以就像我最初想的那样,出于某种原因glutMainLoop()
只是不想调用传递给的函数glutDisplayFunc()
?还是我做错了什么?