在OpenGL中旋转简单多边形

在OpenGL中旋转简单多边形,opengl,Opengl,我遵循中的代码教程,但它不起作用。它在我的窗口左上角显示白色矩形。你能告诉我它有什么问题吗 #include<windows.h> #include <GL/glut.h> float yRot=0.0; void Render() { //clear color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();//load identi

我遵循中的代码教程,但它不起作用。它在我的窗口左上角显示白色矩形。你能告诉我它有什么问题吗

#include<windows.h>
#include <GL/glut.h>
float yRot=0.0;
void Render()
{
  //clear color and depth buffer
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();//load identity matrix

  glTranslatef(0.0f,0.0f,-4.0f);//move forward 4 units

  //rotate along the y-axis
  glRotatef(yRot,0.0f,1.0f,0.0f);

  glColor3f(0.0f,0.0f,1.0f); //blue color

  glBegin(GL_POLYGON);//begin drawing of polygon
    glVertex3f(-0.5f,0.5f,0.0f);//first vertex
    glVertex3f(0.5f,0.5f,0.0f);//second vertex
    glVertex3f(1.0f,0.0f,0.0f);//third vertex
    glVertex3f(0.5f,-0.5f,0.0f);//fourth vertex
    glVertex3f(-0.5f,-0.5f,0.0f);//fifth vertex
    glVertex3f(-1.0f,0.0f,0.0f);//sixth vertex
  glEnd();//end drawing of polygon

  yRot+=0.1f;//increment the yRot variable
}
//method the reshape the entire figure.
void reshape(int x, int h){
    glViewport(0,0,x,h);
}
void init()
{
    glClearColor(0.0,0.0,0.2,0.8);
}
int main(int argc, char** argv)
{
   glutCreateWindow("simple triangles");
   glutDisplayFunc(Render);
   glutReshapeFunc(reshape);
   init();

   glutMainLoop();
}
#包括
#包括
浮动yRot=0.0;
void Render()
{
//清晰的颜色和深度缓冲区
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
glLoadIdentity();//加载标识矩阵
glTranslatef(0.0f,0.0f,-4.0f);//向前移动4个单位
//沿y轴旋转
glRotatef(yRot,0.0f,1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);//蓝色
glBegin(GL_多边形);//开始绘制多边形
glVertex3f(-0.5f,0.5f,0.0f);//第一个顶点
glVertex3f(0.5f,0.5f,0.0f);//第二个顶点
glVertex3f(1.0f,0.0f,0.0f);//第三个顶点
glVertex3f(0.5f,-0.5f,0.0f);//第四个顶点
glVertex3f(-0.5f,-0.5f,0.0f);//第五个顶点
glVertex3f(-1.0f,0.0f,0.0f);//第六个顶点
glEnd();//多边形的结束图
yRot+=0.1f;//增加yRot变量
}
//方法重塑整个图形。
空洞重塑(整数x,整数h){
glViewport(0,0,x,h);
}
void init()
{
glClearColor(0.0,0.0,0.2,0.8);
}
int main(int argc,字符**argv)
{
glutCreateWindow(“简单三角形”);
glutDisplayFunc(渲染);
GLUTREFORUNC(重塑);
init();
glutMainLoop();
}

首先,在所有其他与GLUT相关的调用之前,您没有在
main()
中调用
glutInit(&argc,argv)
。其次,您没有在
Render()
中调用
glutSwapBuffers()

此外,您没有更改投影矩阵,因此没有与本教程开头介绍的相同的调整大小功能

void Resize(int width, int height)
{
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 1.0f, 1000.0f); 

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

更改这些内容,您的代码应该可以工作。

现在您开始使用OpenGL是学习现代OpenGL(版本>=3.3)的好建议。请看我更新的代码@Ahmed_mosutafa我现在根本看不到任何调整大小的功能。再添加一次,它就会工作。