http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
首先,先跟上次一樣到這個網址下載 window data glut32.dll 這三個檔案
解壓縮放到桌面,並把檔案丟進window
點入Transformation就可開啟旋轉模擬
//接下來老師又複習了之前的程式,還有移動的茶壺
(1)
#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0, angle=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)
{
///teapotX=(x-150)/150.0;
///teapotY=(150-y)/150.0;
angle=x;
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03 Mouse");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
*並把之前的程式碼加上angle之後 ,勉強做出一個可以轉動的茶壺,但還是有點卡卡的
-------------------------------------------------------------------------------------------------------------------------
(2)
#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0, angle=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;
}
///teapotX=(x-150)/150.0;
///teapotY=(150-y)/150.0;
///angle=x;
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("week03 Mouse");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
再從剛剛那些程式碼中,加入一些old XY的概念,並寫出滑鼠左右鍵得轉動和移動,就可以做出一個可以簡單轉動移動的茶壺
--------------------------------------------------------------------------------------------------------------------------
老師用了很幾個禮拜的時間,一步步教導大家,讓茶壺可以到現在輕易的轉動,也一直不斷的複習,加深我們的印象,並且也告訴我們了一些期中考題,讓學習寫程式的同時,也增加了許多聽的動力,還有玩樂的樂趣
沒有留言:
張貼留言