Mer*_*kiz 3 c++ opengl sdl glu
我试图在屏幕上画一个正方形,但它清楚地绘制了一个矩形.

这是我的渲染代码:
glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(0,0,-0.1);
glBegin(GL_QUADS);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(1,1,0);
glVertex3f(0,1,0);
glEnd();
SDL_GL_SwapBuffers();
Run Code Online (Sandbox Code Playgroud)
和OpenGL初始化代码:
glClearColor(0,0,0,0.6f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30,640.0/480.0,.3f,200.0);
glMatrixMode(GL_MODELVIEW);
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我没有在你的代码中看到你设置glViewport的任何地方.我宁愿在你的init方法中写这样的东西:
glViewport(0,0,640,480); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(30.0f,(GLfloat)640/(GLfloat)480,0.3f,200.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity();
Run Code Online (Sandbox Code Playgroud)
还要检查第二个Nehe教程,它将帮助你开始使用OpenGL来获取非常基本的东西,比如绘制三角形,正方形等基元......