這裏是我的代碼:OpenGL的三角形旋轉 - 需要一個簡單的修正
#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);
}
我繪製一個矩形和一個三角形。
他們兩個都在旋轉,但我希望矩形靜止不動,只旋轉三角形。我在這裏做錯了什麼?我需要更改推彈出線或其他東西嗎? 我嘗試了很多組合,但我無法做到。
謝謝,但是當我嘗試它時,它們都停止了,這就是我不瞭解的。應該就像你告訴我的一樣,但他們都停下來了。 – showbiz 2012-04-22 16:10:57