0

The idea from this code is to let a windmill like structure to rotate, the problem is that the entire object rotates instead of the windmill fan itself (not the red triangles only). here is the code (I use the keys to control speed)

  #include <windows.h>  // for MS Windows
  #include <GL/glut.h>  // GLUT, include glu.h and gl.h

  float angle = 0.00002f;  
  int refreshMills = 30;

  void initGL() {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
  }

  void Timer(int value) {
        glutPostRedisplay();      // Post re-paint request to activate display()
        glutTimerFunc(refreshMills, Timer, 0); // next Timer call milliseconds later
  }

  void display() {
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_TRIANGLES);
              glColor3f(1.0f, 0.0f, 0.0f)
              glVertex2f(0.0f, 0.0f);
              glVertex2f(-0.4f, 0.2f);
              glVertex2f(-0.2f, 0.4f);
              glColor3f(1.0f, 0.0f, 0.0f);
              glVertex2f(0.0f, 0.0f);
              glVertex2f(0.4f, -0.2f);
              glVertex2f(0.2f, -0.4f);
              glColor3f(1.0f, 0.0f, 0.0f);
              glVertex2f(0.0f, 0.0f);
              glVertex2f(-0.4f, -0.2f)
              glVertex2f(-0.2f, -0.4f);
              glColor3f(1.0f, 0.0f, 0.0f);
              glVertex2f(0.0f, 0.0f);
              glVertex2f(0.4f, 0.2f);    
              glVertex2f(0.2f, 0.4f);
        glEnd();

        glRotatef(angle, 0.0f, 0.0f, 1.0f); 
        angle=angle+0.000002f;
        glutPostRedisplay();
        glBegin(GL_TRIANGLES);
              glColor3f(1.0f, 1.0f, 1.0f);
              glVertex2f(0.0f, 0.0f);
              glVertex2f(-0.4f, -0.6f);
              glVertex2f(0.4f,  -0.6f);
        glEnd();
        glFlush();
        glutSwapBuffers();
  }

  void keyboard(unsigned char key, int x, int y) {
        switch (key) {
        case 'a':{      
              angle+=1;
              glutPostRedisplay();
        }

        case 's':
              angle+=2;
        glutPostRedisplay();

        case 'd':      
              angle+=3 
        glutPostRedisplay();

        case 'f':    
              angle=0;
        }
  }

  }

  int main(int argc, char** argv) {
        glutInit(&argc, argv);          // Initialize GLUTx
        glutCreateWindow("Windmill");  // Create window with the given title
        glutInitWindowSize(320, 320);   // Set the window's initial width & height
        glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
        glutDisplayFunc(display);
        glutTimerFunc(0, Timer, 0);
        glutSpecialFunc(specialKeys);
        glutKeyboardFunc(keyboard);
        initGL();                       // Our own OpenGL initialization
        glutMainLoop();                 // Enter the event-processing loop

        return 0;
  }
genpfault
  • 51,148
  • 11
  • 85
  • 139

4 Answers4

1

You need to implement some sort of hierarchy(commonly a scene graph) here that uses transformation matrixes to do your transformations.

Basically, create a "Windmill" object that has its own transformation matrix. Then create a "Windmill Fan" object that has its own. Make the fan a child of the parent. The transformations essentially propagate down. Transform the Windmill, then transform the Windmill Fan.

Post on Scene Graphs

You may also want to check out the Game Development section of stackoverflow. These questions are usually not met with open arms here.

Community
  • 1
  • 1
Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
  • Quote from that post: "2D Scene Graphs: Use of a scene graph for 2D may be useful if your content is sufficiently complex and if your objects have a number of sub components not rigidly fixed to the larger body. Otherwise, as others have mentioned, it's probably overkill." – Andreas Haferburg May 09 '13 at 20:40
0

You need to clear your transformations before trying to draw a new frame. So, put a glLoadIdentity() after you glClear function.

  void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity(); //  <-- put it here
    glBegin(GL_TRIANGLES);
    ...
Amadeus
  • 10,199
  • 3
  • 25
  • 31
0

You need to call glRotate before drawing the rotating object. And before that, you need to push the current model view matrix onto the stack with glPushMatrix, then pop it with glPopMatrix before drawing the rest of the windmill.

Once you have a more complex scene you might consider using a scene graph. Note that the matrix functions like glRotate are now deprecated. You might consider switching to e.g. glm as soon as possible.

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
0

Try this:

#include <GL/glut.h>  // GLUT, include glu.h and gl.h

void Timer(int value) 
{
    glutPostRedisplay();      // Post re-paint request to activate display()
    glutTimerFunc(16, Timer, 0); // next Timer call milliseconds later
}

float rate = 1.0f;  
void keyboard(unsigned char key, int x, int y) 
{
    switch (key) 
    {
    case 'a':
        rate=2;
        break;
    case 's':
        rate=3;
        break;
    case 'd':      
        rate=4;
        break;
    case 'f':    
        rate=0;
        break;
    }
    glutPostRedisplay();
}

float angle = 0.0f;
void display() 
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    // base    
    glPushMatrix();
        glBegin(GL_TRIANGLES);
            glColor3f(1.0f, 1.0f, 1.0f);
            glVertex2f(0.0f, 0.0f);
            glVertex2f(-0.4f, -0.6f);
            glVertex2f(0.4f,  -0.6f);
        glEnd();
    glPopMatrix();

    // prop
    glPushMatrix();
        glRotatef(angle, 0.0f, 0.0f, 1.0f); 
        angle=angle+rate;
        glBegin(GL_TRIANGLES);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex2f(0.0f, 0.0f);
            glVertex2f(-0.4f, 0.2f);
            glVertex2f(-0.2f, 0.4f);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex2f(0.0f, 0.0f);
            glVertex2f(0.4f, -0.2f);
            glVertex2f(0.2f, -0.4f);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex2f(0.0f, 0.0f);
            glVertex2f(-0.4f, -0.2f);
            glVertex2f(-0.2f, -0.4f);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex2f(0.0f, 0.0f);
            glVertex2f(0.4f, 0.2f);    
            glVertex2f(0.2f, 0.4f);
        glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv) 
{
    glutInit(&argc, argv);          // Initialize GLUTx
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutInitWindowSize(320, 320);   // Set the window's initial width & height
    glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
    glutCreateWindow("Windmill");  // Create window with the given title
    glutDisplayFunc(display);
    glutTimerFunc(0, Timer, 0);
    glutKeyboardFunc(keyboard);
    glutMainLoop();                 // Enter the event-processing loop
    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139