1

这是我的代码:

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

}

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

它们现在都在旋转,但我希望矩形保持静止并仅旋转三角形。我在这里做错了什么?我是否需要更改推送流行线或其他内容。我尝试了很多组合,但我做不到。

4

3 回答 3

2

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

编辑:要旋转三角形,您需要更改角度。如果您保持固定角度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
}
于 2012-04-21T13:04:44.210 回答
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();

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

于 2012-04-22T15:20:09.713 回答
0

您的问题是围绕正方形的旋转是唯一实际复合每一帧的旋转。因为您推动/弹出围绕三角形的旋转,所以每次绘制它时它都会重置为默认状态。

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

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

首先将 a 定义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
于 2012-04-22T17:29:20.073 回答