## 【初学者】openGL的物体转速不一样

Michaelwpf 发布于 2016/05/17 15:50

#include <GL/glut.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

static float day = 0; // day 的变化：从 0 到 359

void myDisplay(void)
{

glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

gluPerspective(75, 1, 1, 400);

glMatrixMode(GL_MODELVIEW);
gluLookAt(3, 5,1, 0, 5, 0, 0, 1,0);
int count = 0;

for (float i = 0.0;i < 10.0;i += 0.01) {
count++;

if (count % 500 == 0) {

glPushMatrix();

glTranslatef(0.0, i, 0.0);
glRotatef(360-(i + day)+360, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, -1.0);

count = 0;
glColor3f(1.0f, 1.0f, 1.0f);
gluCylinder(gluQ, 0.05, 0.05, 2.0, 20, 20); //圆锥
glPopMatrix();
glFlush();

printf("    angle %f \n", i + day);
}

}

for (float i = 0.0;i <10.0;i += 0.01) {

glPushMatrix();
glTranslatef(-cos(i + day), i, -sin(i + day));
//        glRotatef(90.0, 0.0, 1.0, 0.0);
glRotatef(90.0, 1.0, 0.0, 0.0);
glColor3f(0.0f, 1.0f, 0.0f);
gluCylinder(gluP, 0.08, 0.08, 0.1, 20, 20); //圆锥

glPopMatrix();

glPushMatrix();
glTranslatef(cos(i + day), i, sin(i + day));
glRotatef(90.0, 1.0, 0.0, 0.0);
//        glRotatef(90.0, 0.0, 1.0, 0.0);
glColor3f(1.0f, 0.0f, 0.0f);
gluCylinder(gluP, 0.08, 0.08, 0.1, 20, 20); //圆锥

glPopMatrix();
glFlush();
}

glFlush();
glutSwapBuffers();
}

void myIdle(void)
{
day+=0.1;
if( day >= 360)
day = 0;
myDisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glEnable(GL_LIGHTING);
glutCreateWindow("first_Try");
glutDisplayFunc(&myDisplay);
glutIdleFunc(&myIdle);
glutMainLoop();
return 0;
}