旋轉範例
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidTeapot(1);
glPopMatrix();

p.s. glutSolidIcosahedron();

glutSolidOctahedron();

glutSolidDodecahedron();

分開製作圖形
glPushMatrix(); //備份矩陣
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix(); //還原矩陣
移動
glPushMatrix();
glTranslated(-2.4,1.2,-6);
//glRotated(60,1,0,0);
//glRotated(a,0,0,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
p.s.glTranslated(移動x座標,移動y座標,移動z座標);
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
//glRotated(a,0,0,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
p.s. glRotated(轉動角度,x座標,y座標,z座標);
以(x座標,y座標,z座標)為軸轉動

旋轉(隨時間改變)
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;const double a = t*90.0;
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);//一毫秒轉90度
glutSolidSphere(1,slices,stacks);
glPopMatrix();
p.s. glRotated(轉動角度,x座標,y座標,z座標);
以(x座標,y座標,z座標)為軸轉動
放大縮小
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glScalef(0.5,0.5,0.5);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
p.s. glScalef(x軸的大小乘以倍數,y軸的大小乘以倍數,z軸的大小乘以倍數); //倍數小於1即縮小
float dx=0,dy=0,dz=0;//要畫的3D位置
int oldx=0,oldy=0,oldz=0;//舊的x,y,z位置
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //設為單位矩陣
glTranslatef(dx,dy,dz);
glColor3d(1,0,0);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void motion(int x,int y)//用來拖曳移動
{
dx+= (x-oldx)/150.0;
dy+= -(y-oldy)/150.0;
oldx=x;
oldy=y;
glutPostRedisplay(); //貼便利貼,請電腦要重畫畫面
}
void mouse(int button,int state,int x,int y) //按下去時記錄在哪裡開始
{
if(state==GLUT_DOWN)
{
oldx=x;
oldy=y;
}
//GLUT_DOWN--->滑鼠按下去時為1
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glClearColor(1,1,1,1);
glutMainLoop();
return 0;
}



沒有留言:
張貼留言