電腦圖學_week04
2021/03/15(一)
今日的上課內容:
- 在github重新開啟上週檔案
- 產生茶壺圖形且視窗名稱取名為week04 mouse(圖一)
- 加入#includ <stdio.h>
- 加入 glutMouseFunc(mouse)
- 加入 mouse函式
- 產生圖形並顯示滑鼠座標及狀態(圖二)
- 遇到找不到視窗可以開啟工作管理員
- 找到codeblocks旁邊點下去找到cb..console關閉它(圖三)
- 新增找座標的程式碼
- 看座標並複製結果產生多邊形圖形(圖五)
- 新增茶壺移動程式(圖六、圖七)
- 運用範例程式了解圖形轉動(translatef、scalef、rotatef)(圖八)
- 如果不懂的看圖,指向為手指方向(圖九)
今日的程式碼:
- #include <GL/glut.h> ///使用GLUT外掛
- void display()
- {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清空
glutSolidTeapot(0.3); ///實心茶壺
glutSwapBuffers(); ///交換2倍的buffers
- }
- void mouse(int button, int state, int x, int y)
- {
printf("button:%d state:%d x:%d y:%d\n", button, state, x, y);
- }
- int main(int argc, char ** argv)
- {
glutInit(&argc, argv); ///GLUT初始設定
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///顯示模式
glutCreateWindow("week04 mouse"); ///開窗
glutMouseFunc(mouse);///滑鼠函式
glutDisplayFunc(display); ///等一下要顯示的函式
glutMainLoop(); ///主要迴圈
- }
///看滑鼠座標
- #include <GL/glut.h> ///使用GLUT外掛
- #include <stdio.h>
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清空
- glutSolidTeapot(0.3); ///實心茶壺
- glutSwapBuffers(); ///交換2倍的buffers
- }
- void mouse(int button, int state, int x, int y)
- {
- if( state == GLUT_DOWN)
- {
- printf(" glVertex3f( (%d-150)/150.0, -(%d-150)/150.0, 0);\n", x, y);
- }
- }
- int main(int argc, char ** argv)
- {
- glutInit(&argc, argv); ///GLUT初始設定
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///顯示模式
- glutCreateWindow("week04 mouse"); ///開窗
- glutMouseFunc(mouse);
- glutDisplayFunc(display); ///等一下要顯示的函式
- glutMainLoop(); ///主要迴圈
- }
///多邊形
- #include <GL/glut.h> ///使用GLUT外掛
- #include <stdio.h>
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清空
- glBegin(GL_POLYGON);
- glVertex3f( (108-150)/150.0, -(61-150)/150.0, 0);
- glVertex3f( (63-150)/150.0, -(80-150)/150.0, 0);
- glVertex3f( (71-150)/150.0, -(122-150)/150.0, 0);
- glVertex3f( (49-150)/150.0, -(136-150)/150.0, 0);
- glVertex3f( (39-150)/150.0, -(168-150)/150.0, 0);
- glVertex3f( (58-150)/150.0, -(183-150)/150.0, 0);
- glVertex3f( (72-150)/150.0, -(206-150)/150.0, 0);
- glVertex3f( (75-150)/150.0, -(246-150)/150.0, 0);
- glVertex3f( (107-150)/150.0, -(232-150)/150.0, 0);
- glVertex3f( (134-150)/150.0, -(246-150)/150.0, 0);
- glVertex3f( (164-150)/150.0, -(224-150)/150.0, 0);
- glVertex3f( (210-150)/150.0, -(216-150)/150.0, 0);
- glVertex3f( (225-150)/150.0, -(176-150)/150.0, 0);
- glVertex3f( (225-150)/150.0, -(156-150)/150.0, 0);
- glVertex3f( (245-150)/150.0, -(138-150)/150.0, 0);
- glVertex3f( (238-150)/150.0, -(109-150)/150.0, 0);
- glVertex3f( (210-150)/150.0, -(91-150)/150.0, 0);
- glEnd();
- glutSwapBuffers(); ///交換2倍的buffers
- }
- void mouse(int button, int state, int x, int y)
- {
- if( state == GLUT_DOWN)
- {
- printf(" glVertex3f( (%d-150)/150.0, -(%d-150)/150.0, 0);\n", x, y);
- }
- }
- int main(int argc, char ** argv)
- {
- glutInit(&argc, argv); ///GLUT初始設定
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///顯示模式
- glutCreateWindow("week04 mouse"); ///開窗
- glutMouseFunc(mouse);
- glutDisplayFunc(display); ///等一下要顯示的函式
- glutMainLoop(); ///主要迴圈
- }
///茶壺移動程式
- #include <GL/glut.h> ///使用GLUT外掛
- #include <stdio.h>
- float teapotX=0, teapotY=0;///茶壺的座標
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清空
- glPushMatrix();///矩陣備份
- glTranslatef(teapotX, teapotY, 0);///照著座標移動
- glutSolidTeapot(0.3);
- glPopMatrix();///矩陣還原
- glutSwapBuffers(); ///交換2倍的buffers
- }
- ///void mouse(int button, int state, int x, int y)
- ///{
- ///}
- void motion(int x, int y)
- {
- teapotX = (x-150)/150.0;
- teapotY = -(y-150)/150.0;
- display();///顯示刷新速度變快
- }
- int main(int argc, char ** argv)
- {
- glutInit(&argc, argv); ///GLUT初始設定
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///顯示模式
- glutCreateWindow("week04 mouse"); ///開窗
- ///glutMouseFunc(mouse);
- glutDisplayFunc(display); ///等一下要顯示的函式
- glutMotionFunc(motion);///滑鼠移動的函式
- glutMainLoop(); ///主要迴圈
- }
今日的上課成果:









沒有留言:
張貼留言