再度來到 http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
下載 data.zip, windows.zip, glut32.dll 三個
一樣將 glut32.dll、data資料夾複製到 windows 資料夾內
打開 Transformation可以調整物品大小及位子可以使我們了解程式碼在電腦圖學中的功用
旋轉茶壺杯杯
KEY上可愛的程式碼
#include <stdio.h>
#include <GL/glut.h> //整GLUT外掛
float teapotX=0, teapotY=0, angle=0; //茶壺的座標,rotate旋轉要的angle
int oldX=0, oldY=0, nowRotate=0; //紀錄mouse在哪裡按下去,oldX, oldY 幫忙了解你走了多少地方
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();//備份矩陣
glTranslatef(teapotX, teapotY, 0); //依照茶壺的座標(teapotX, teapotY)移動
glRotatef(angle, 0,0,1); //茶壺要對著 (0,0,1)做旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix();//還原矩陣
glutSwapBuffers(); //搭配GLUT_DOUBLE兩倍顯示
}
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;//紀錄按下去的點在哪裡
}
void motion(int x, int y) //mouse motion 事件
{
if(nowRotate==1) angle += (x-oldX); //滑鼠的座標,就是璇轉的角度,加上新的增加量
else
{
teapotX += (x-oldX)/150.0; //依照motion時的x來改teapot的座標,加上新的增加量
teapotY += (oldY-y)/150.0;//依照motion時的y來改teapot的座標,加上新的增加量
}
oldX = x; oldY = y; //紀錄移動後新的位置
glutPostRedisplay(); //重畫畫面
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);//初始的參數,照著丟進去
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);//2個顯示的參數
glutCreateWindow("Week04 Mouse");//設定視窗的名稱
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();//主要GLUT迴圈
}



沒有留言:
張貼留言