使用过剩的OpenGL三角旋转 - 需要简单的校正

时间:2012-04-21 12:48:39

标签: opengl rotation glut glrotate

这是我的代码:

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

}

我画了一个矩形和一个三角形。

它们现在都在旋转,但我想让矩形静止并仅旋转三角形。我在这做错了什么?我是否需要更改推送弹出线或其他内容。 我尝试了很多组合,但我不能这样做。

3 个答案:

答案 0 :(得分:2)

如果您不想旋转矩形,请在绘制矩形之前删除旋转调用glRotatef(0.09,0.0,0.0,0.09);。你需要在绘制三角形,矩形之前的push-pop矩阵。要停止旋转,您必须删除旋转调用,而不是push-pop矩阵。

编辑:要旋转三角形,您需要更改角度。如果你保持固定的角度0.09,那么将没有动画。一种简单的方法是将角度跟踪为全局变量。像这样:

double ang = 0;  // global variable

void renderScene(){
    // other codes

    glRotatef(ang++,0.0,0.0,-1.0);  // use and change ang instead of using fixed 0.09
    glBegin(GL_TRIANGLE_STRIP);

    // remaining codes
}

答案 1 :(得分:1)

你应该做的是替换

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

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

推动/弹出矩阵不会对矩形的变换做任何改变,它只会阻止矩形的变换影响三角形。

答案 2 :(得分:0)

你的问题是广场周围的旋转是唯一一个实际上复合每一帧的旋转。因为您在三角形周围按下/弹出旋转,所以每次绘制它时都会重置为默认状态。

而不是让它随着时间的推移而旋转,你只是做一个小的旋转,弹出它(从而去除旋转),然后当你绘制下一帧时,你做的完全相同的小旋转,所以三角形没有看起来它一直在移动,它每次都会以相同的旋转状态绘制。

因为您没有围绕四边形弹出旋转,所以它会在每个帧上复合。你想要做的是每帧增加一个变量,然后按变量的数量旋转。

首先将float rotationAngle定义为您班级的成员变量。

然后在绘图调用中,运行如下:

rotationAngle += rotationSpeed;
glPushMatrix();
  //do quad specific transformations
  //draw quad
glPopMatrix(); //remove quad specific transforms from stack

glPushMatrix();
   glRotatef(rotationAngle, 0, 0, 1);
   //do other triangle specific rotations
   //draw triangle
glPopMatrix(); //remove triangle's rotation and other transforms