檔案資源下載:http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
1.下載 data、win32、glut32.dll這三個檔案
| (PS:滑鼠右鍵→另存連結) |
| 沒有同一個目錄下的話可能會無法開啟或閃退新增說明文字 |
Part2
旋轉體驗
glRotatef(角度, X軸, Y軸, Z軸)
gl=>OpenGL 、 f=>float
(用右手安培去旋轉來判斷)









(用右手安培去旋轉來判斷)
Part3
選轉

程式碼
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 0, 1);
glColor3f(0, 1, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}
旋轉滑鼠

程式碼
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 0, 1);
glColor3f(0, 1, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
//angle++;
}
void mouse(int button, int state, int x, int y)
{
}
void motion(int x, int y)
{
angle=x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04");
glutMouseFunc(mouse);
glutMotionFunc(motion);
//glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}
(旋轉+移動)滑鼠

程式碼
#include <GL/glut.h>
float angle=0, teapotY=0, teapotX=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);
glColor3f(0, 1, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
//angle++;
}
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)
{
if(nowRotate==1) angle+=(x-oldx);
else
{
teapotX +=(x-oldx)/150.0;
teapotY += (oldy-y)/150.0;
}
oldx=x;
oldy=y;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04");
glutMouseFunc(mouse);
glutMotionFunc(motion);
//glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}

(移動和旋轉這地方比較容易寫錯)
沒有留言:
張貼留言