1. 移動
。以下程式碼為例
glPushMatrix();
glTranslated(-2.4,1.2,-6); /// 移動(X,Y,Z) d表double
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glScalef(1,1,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(-2.4,1.2,-6); /// 移動(X,Y,Z) d表double
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glScalef(1,1,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
2. 旋轉
。以下程式碼為例
glPushMatrix();
glTranslated(-2.4,1.2,-6);
/// 旋轉(度數,X,Y,Z) X,Y,Z軸表向量
glRotated(60,1,0,0); /// 沿著x軸轉動60度
glRotated(a,0,0,1); /// 沿著z軸每秒轉動90度
glScalef(1,1,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
✤依照右手座標系統旋轉glPushMatrix();
glTranslated(-2.4,1.2,-6);
/// 旋轉(度數,X,Y,Z) X,Y,Z軸表向量
glRotated(60,1,0,0); /// 沿著x軸轉動60度
glRotated(a,0,0,1); /// 沿著z軸每秒轉動90度
glScalef(1,1,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
3. 縮放
。以下程式碼為例
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glScalef(1,1,1); /// 縮放比率(X,Y,Z)
glutSolidSphere(1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glScalef(1,1,1); /// 縮放比率(X,Y,Z)
glutSolidSphere(1,slices,stacks);
glPopMatrix();
4. 用滑鼠移動
。以下程式碼為例
#include <GL/glut.h>
#include <stdio.h>
float dx=0.0,dy=0.0,dz=0.0;
int oldX=0,oldY=0,oldZ=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(dx,dy,dz);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y){
if(state==GLUT_DOWN){ /// 如果按下滑鼠
oldX=x;
oldY=y;
}
}
void motion(int x, int y){
dx += (x-oldX)/150.0;
dy += -(y-oldY)/150.0; /// Y座標上下顛倒所以要加負號
oldX = x;
oldY = y;
glutPostRedisplay(); /// 畫面刷新
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week 04");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return EXIT_SUCCESS;
}
#include <GL/glut.h>
#include <stdio.h>
float dx=0.0,dy=0.0,dz=0.0;
int oldX=0,oldY=0,oldZ=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(dx,dy,dz);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y){
if(state==GLUT_DOWN){ /// 如果按下滑鼠
oldX=x;
oldY=y;
}
}
void motion(int x, int y){
dx += (x-oldX)/150.0;
dy += -(y-oldY)/150.0; /// Y座標上下顛倒所以要加負號
oldX = x;
oldY = y;
glutPostRedisplay(); /// 畫面刷新
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week 04");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return EXIT_SUCCESS;
}
沒有留言:
張貼留言