整个对象旋转

时间:2013-05-09 20:16:06

标签: opengl freeglut

这段代码的想法是让风车般的结构旋转,问题是整个物体旋转而不是风车风扇本身(不仅仅是红色三角形)。这是代码(我使用键来控制速度)

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

4 个答案:

答案 0 :(得分:1)

你需要在这里实现某种层次结构(通常是场景图),它使用转换矩阵来进行转换。

基本上,创建一个具有自己的变换矩阵的“Windmill”对象。然后创建一个有自己的“风车扇”对象。让粉丝成为父母的孩子。转换本质上是向下传播的。改造风车,然后改造风车风扇。

Post on Scene Graphs

您可能还想查看stackoverflow的Game Development部分。这些问题通常不会在这里张开双臂。

答案 1 :(得分:0)

在尝试绘制新框架之前,您需要清除转换。因此,在glLoadIdentity()函数后添加glClear

  void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity(); //  <-- put it here
    glBegin(GL_TRIANGLES);
    ...

答案 2 :(得分:0)

您需要在绘制旋转对象之前调用glRotate 。在此之前,您需要使用glPushMatrix将当前模型视图矩阵推入堆栈,然后在绘制其余风车之前将其弹出glPopMatrix

一旦你有一个更复杂的场景,你可以考虑使用一个场景图。请注意,现在不推荐使用glRotate等矩阵函数。您可以考虑切换到例如glm尽快。

答案 3 :(得分:0)

试试这个:

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