Ahmed_mosutafa
Ahmed_mosutafa

Reputation: 15

Rotate a simple polygon in OpenGL

I follow the code tutorial from the OpenGL programming book, but it doesn't work. It is showing white rectangle at the top left of my window. Could you please tell me what could be wrong with it?

#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();
}

Upvotes: 1

Views: 1930

Answers (1)

vallentin
vallentin

Reputation: 26157

First of all, you're not calling glutInit(&argc, argv) in main() before all the other GLUT related calls. Second of all, you're not calling glutSwapBuffers() in Render().

Besides that you aren't changing the projection matrix, and thus don't have the same resize function as the one presented in the beginning of the tutorial.

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();
}

Changing those things and your code should work.

Upvotes: 2

Related Questions