Search code examples
openglrotationglutglrotate

OpenGL triangle rotation using glut - need a simple correction


Here is my code :

#include <stdio.h>
#include "glut.h"

float width = 800.0;
float height = 600.0;

void changeSize(int w,int h){
    if(h==0)
        h=1;
    width = w;
    height = h;
    GLdouble ratio = 1.0f*w/h;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0,0,w,h);

    gluPerspective(45,ratio,1,1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,10,
              0,0,-1,
              0,1,0);
}

void renderScene(){
    glClearColor(0,0,255,1);
    glClearDepth(1.0f);
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

    //glPushMatrix();


    glRotatef(0.09,0.0,0.0,0.09); 
    glColor3f(1.0,0.0,0.0);
        glBegin(GL_QUADS);
        glVertex3f(-1.0f,-1.0f,0.0f);
        glVertex3f(1.0f,-1.0f,0.0f);
        glVertex3f(1.0f,1.0f,0.0f);
        glVertex3f(-1.0f,1.0f,0.0f);
    glEnd();
    //glPopMatrix();


    glPushMatrix();
    glColor3f(0.0,1.0,0.0);
    glTranslatef(-3.0,0.0,0.0);

    glRotatef(0.09,0.0,0.0,-1.0);
    glBegin(GL_TRIANGLE_STRIP);
        glVertex2f(-1,-1);
        glVertex2f(0,1);
        glVertex2f(1,-1);
        glEnd();

    glPopMatrix();



    glutSwapBuffers();

}

int main(int argc, char **argv){

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,200);
    glutInitWindowSize(width,height);
    glutCreateWindow("OpenGL");

    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);

    glutReshapeFunc(changeSize);

    glutMainLoop();

    return(0);

}

I draw a rectangle and a triangle.

Both of them are rotating right now but I want the rectangle to stand still and rotate the triangle only. What am I doing wrong here? Do I need to change push pop lines or something else. I tried many combinations but I can't do it.


Solution

  • what you should do is replace

    glRotatef(0.09,0.0,0.0,0.09); 
    glColor3f(1.0,0.0,0.0);
        glBegin(GL_QUADS);
        glVertex3f(-1.0f,-1.0f,0.0f);
        glVertex3f(1.0f,-1.0f,0.0f);
        glVertex3f(1.0f,1.0f,0.0f);
        glVertex3f(-1.0f,1.0f,0.0f);
    glEnd();
    

    with

    glColor3f(1.0,0.0,0.0);
        glBegin(GL_QUADS);
        glVertex3f(-1.0f,-1.0f,0.0f);
        glVertex3f(1.0f,-1.0f,0.0f);
        glVertex3f(1.0f,1.0f,0.0f);
        glVertex3f(-1.0f,1.0f,0.0f);
    glEnd();
    

    Pushing/popping the matrix will not do any change to the rectangle's transformation, it will only prevent the rectangle's transformation from affecting the triangle's.