來源 : http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
1.下載 data
2.下載 win32 並解壓縮(放到你想放的地方
3.下載 glut32.dll 並複製到與win32同一個地方
4.解data壓縮至與win32同一個地方
5.執行Transformation.exe
試著挑整方向
用安培右手定則去試者想如何旋轉
glTranslate f(x , y , z) ; //移動
glRotatef ( angle , x , y , z ); // 轉動
glScalef ( x , y , z ) ; // 放大縮小
glBegin ( GL_POLYGON ) ; //開始畫
glColor3f ( r , g , b ) ; // 顏色
glVertex3f (x , y , z ) ; //頂點
glEnd() ; // 結束畫 glBegin()對應
glPopMatrix() ; //還原矩陣
(期中重點
茶壺轉動(自動):
display()內新增
glPushMatrix();
glRotatef(angle,0,0,1); ///選轉angle度
glutSolidTeapot(0.3);
glPopMatrix();
新增void motion()
void motion(int x,int y){
angle = x;
glutPostRedisplay();
}
main()內新增:
glutMotionFunc(motion);
執行結果:
#include <stdio.h>
#include <GL/glut.h>
float angle = 0 ;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
glPushMatrix();///備份矩陣
glRotatef(angle , 0 , 0 , 1);
glutSolidTeapot(0.3);
glPopMatrix(); /// 還原矩陣
glutSwapBuffers();
printf("%.1f\n",angle);
angle++ ; /// 每次角度加1度
}
int main(int argc , char**argv)
{
glutInit(&argc ,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH) ;
glutCreateWindow("Week04 transformation");
glutIdleFunc(display);///很閒/Idle,去重畫.
glutDisplayFunc(display);
glutMainLoop();
}
茶壺轉動(手動):
display()內新增
glPushMatrix();
glRotatef(angle,0,0,1); ///選轉angle度
glutSolidTeapot(0.3);
glPopMatrix();
新增void motion()
void motion(int x,int y){
angle = x;
glutPostRedisplay();
}
main()內新增:
glutMotionFunc(motion);
display()內新增
glPushMatrix();
glRotatef(angle,0,0,1); ///選轉angle度
glutSolidTeapot(0.3);
glPopMatrix();
新增void motion()
void motion(int x,int y){
angle = x;
glutPostRedisplay();
}
main()內新增:
glutMotionFunc(motion);
茶壺旋轉、移動(手動):
程式外宣告:
float angle = 0, teapotX = 0, teapotY = 0;
int oldX = 0, oldY = 0, nowRotate = 0;
display()內新增:
glTranslatef(teapotX,teapotY,0);
新增void mouse():
void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) nowRotate = 1;
///如果是右鍵就旋轉
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) nowRotate = 0;
///如果是左鍵就移動
oldX = x; oldY = Y; ///按下的時候,把oldX, oldY記住
}
void motion()內修改:
void motion(int x,int y){
if(nowRotate == 1) angle += (x - oldX); ///去改變angle值 ///加上改變量
else{
teapotX += (x - oldX)/150.0;
teapotY += (oldY - y)/150.0;
}
oldX = x; oldY = y;
glutPostRedisplay(); ///畫面重畫
}
執行結果:

#include <GL/glut.h>
float angle = 0,teapotX = 0,teapotY = 0;
int oldX = 0, oldY = 0, nowRotate = 0;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y){
if(nowRotate == 1) angle += (x - oldX);
else{
teapotX += (x - oldX)/150.0;
teapotY += (oldY - y)/150.0;
}
oldX = x; oldY = y;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) nowRotate = 1;
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) nowRotate = 0;
oldX = x; oldY = y;
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
程式外宣告:
float angle = 0, teapotX = 0, teapotY = 0;
int oldX = 0, oldY = 0, nowRotate = 0;
display()內新增:
glTranslatef(teapotX,teapotY,0);
新增void mouse():
void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) nowRotate = 1;
///如果是右鍵就旋轉
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) nowRotate = 0;
///如果是左鍵就移動
oldX = x; oldY = Y; ///按下的時候,把oldX, oldY記住
}
void motion()內修改:
void motion(int x,int y){
if(nowRotate == 1) angle += (x - oldX); ///去改變angle值 ///加上改變量
else{
teapotX += (x - oldX)/150.0;
teapotY += (oldY - y)/150.0;
}
oldX = x; oldY = y;
glutPostRedisplay(); ///畫面重畫
}
執行結果:
#include <GL/glut.h>
float angle = 0,teapotX = 0,teapotY = 0;
int oldX = 0, oldY = 0, nowRotate = 0;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y){
if(nowRotate == 1) angle += (x - oldX);
else{
teapotX += (x - oldX)/150.0;
teapotY += (oldY - y)/150.0;
}
oldX = x; oldY = y;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) nowRotate = 1;
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) nowRotate = 0;
oldX = x; oldY = y;
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}

沒有留言:
張貼留言