▴TRT轉動程式碼:
#include <GL/glut.h>
void myCube()
{
glPushMatrix();
glColor3f(1,0,0);///紅色
glScalef(0.5, 0.2, 0.2);///調大小
glutSolidCube(1);///正方塊
glPopMatrix();
}
float angle=0;///轉動 變數 (角度)
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0,0,1);///轉動
myCube();
glPopMatrix();
angle++;///每次角度增加
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display);///有空idel時,在重畫畫面
glutDisplayFunc(display);
glutMainLoop();
}

階層轉動程式碼:
固定一個軸轉動
#include <GL/glut.h>
void myCube()
{
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.5, 0.2, 0.2);
glutSolidCube(1);
glPopMatrix();
}
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0,0,1);
glTranslatef(0.22, 0,0);///把轉動的中心點,放到畫面的中心
myCube();
glPopMatrix();
angle++;
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}

對綠色方塊右上角進行定點轉動
#include <GL/glut.h>
void myCube()
{
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.5, 0.2, 0.2);
glutSolidCube(1);
glPopMatrix();
}
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);///綠色
glutSolidCube(0.6);///身體(方塊)
glPushMatrix();
glTranslatef(0.3, 0.3, 0);///把轉中的手臂掛到右上角
glRotatef(angle, 0,0,1);
glTranslatef(0.22, 0,0);
myCube();
glPopMatrix();
angle++;
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言