電腦圖學 2019-03-21 Week05
(1).主題 : 旋轉 Rotation(2).示範 : glRotatef()
(3).滑鼠轉 . 自動轉
(一)與上週一樣
先到http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載3個並且解壓縮

把data和glut32.dll丟到windows裡面

(二)試用glRotatef(角度 , x軸 , y軸 , z軸 )

(三)讓茶壺自動轉
#include <GL/glut.h>
float angle=0;
void display()
{ ////沒有使用glClear()去清理畫面,會有殘影
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ////push備份保護Matrix
glRotatef(angle,0,1,0); ////對y軸,轉angle度
glutSolidTeapot( 0.3 ); ////畫實心的茶壺
glPopMatrix(); ////Pop還原Matrix
glutSwapBuffers(); ////最後buffer做交易秀出
angle+=3; ////每次執行道這行,才加一點點
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("06160150-W5");
glutDisplayFunc(display);
glutIdleFunc(display); ////今天新教!!!Idle閒閒沒事,就重畫
glutMainLoop();
}


(三)讓茶壺跟著滑鼠轉動
#include <GL/glut.h>
float angle=0;
void display()
{ ////沒有使用glClear()去清理畫面,會有殘影
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ////push備份保護Matrix
glRotatef(angle,0,1,0); ////對y軸,轉angle度
glutSolidTeapot( 0.3 ); ////畫實心的茶壺
glPopMatrix(); ////Pop還原Matrix
glutSwapBuffers(); ////最後buffer做交易秀出
}
void motion(int x,int y)
{ ////當你mouse去drag做motion動作時
angle=x; ////現在新加的!!
display(); ////現在新加的!!
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("06160150-W5");
glutDisplayFunc(display);
glutIdleFunc(display); ////今天新教!!!Idle閒閒沒事,就重畫
glutMotionFunc(motion); ////現在新加的!!
glutMainLoop();
}

(四)讓茶壺跟著滑鼠轉動
#include <GL/glut.h>
float angle=0;
void display()
{ ////沒有使用glClear()去清理畫面,會有殘影
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ////push備份保護Matrix
glRotatef(angle,0,1,0); ////對y軸,轉angle度
glutSolidTeapot( 0.3 ); ////畫實心的茶壺
glPopMatrix(); ////Pop還原Matrix
glutSwapBuffers(); ////最後buffer做交易秀出
}
void motion(int x,int y)
{ ////當你mouse去drag做motion動作時
angle=x; ////現在新加的!!
display(); ////現在新加的!!
}
#include <stdio.h>
void mouse(int button, int state, int x,int y)
{
printf("%d %d %d %d\n",button,state,x,y);
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("06160150-W5");
glutDisplayFunc(display);
glutIdleFunc(display); ////今天新教!!!Idle閒閒沒事,就重畫
glutMouseFunc(mouse); ////現在新加的!!
glutMainLoop();
}

沒有留言:
張貼留言