我一直在关注 opengl 教程,但由于某种原因,当我添加了重塑功能(用于调整窗口大小以修复比率)时。opengl 窗口根本不显示对象(形状),只是一个黑屏。我不知道我是否拼写错误或其他什么。
重塑功能是否搞砸了?
#include <GL/gl.h>
#include <GL/glut.h>
void renderScene(void);
void changeSize(int w, int h);
int main(int argc, char **argv){
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Testing");
// register callbacks
glutDisplayFunc(renderScene);
// animation in reshaping
glutReshapeFunc(changeSize);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0.0);
glEnd();
glutSwapBuffers();
}
void changeSize(int w, int h){
if (h==0)
h = 1;
float ratio = 1.0*w/h;
// use the projection matrix
glMatrixMode(GL_PROJECTION);
//reset matrix
glLoadIdentity();
// set the viewpoint to be the entire window
glViewport(0,0,w,h);
// set the correct perspective
gluPerspective(45, ratio, 1, 1000);
// get back to the modelview
glMatrixMode(GL_MODELVIEW);
}
谢谢你的帮助!这是来自 Lighthouse3d 教程。
PS:如果我取出重塑功能,三角形就会出现(工作正常)。