2019年3月21日 星期四

珮珮的學習日誌 Week05

電腦圖學

(1) 主題:旋轉 (Rotation)
(2) 實作:glRotatef()
(3) 滑鼠轉動、自動轉動


TODO:首先先到http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/ 這個網站

下載:windows.zip解壓縮並下載\windows\Transformation.exe

data.zip 解壓縮並下載\windows\data\模型

glut32.dll 下載\windows\glut32.dll

下面為glRotatef(角度,x軸,y軸,z軸);所呈現的畫面

備註:可以運用安培右手定律來做glRotatef的角度判斷


接下來在Google Chrome下載freeglut(window的mingw版) 


https://www.transmissionzero.co.uk/software/freeglut-devel/ 並把他解壓縮到桌面


之後再把解壓縮的freeglut資料夾打開來,並找到lib資料夾


再把lib裡面的libfreeglut.a複製成libglut32.a)




再來打開Codeblocks來創立GLUT project 連續上禮拜的接續動作

弄完之後呢 就要開始打程式碼了~

程式碼如下:

#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);
    glutSolidTeapot(0.3);
    glPopMatrix();///Pop還原Matrix
    glutSwapBuffers();
    angle+=3;///每次執行到這行,才加一點點
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week05 rotate");
    glutDisplayFunc(display);
    glutIdleFunc(display);///今天新教!!! Idle 閒閒沒事就重畫
    glutMainLoop();
}

更新:加了motion之後所顯現的程式碼及畫面
#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);
    glutSolidTeapot(0.3);
    glPopMatrix();///Pop還原Matrix
    glutSwapBuffers();
    ///angle+=3;///每次執行到這行,才加一點點
}
void motion(int x,int y)///新加的
{///當你的滑鼠去drag做motion動作時
   angle=x;///新加的
    display();///新加的
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week05 rotate");
    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);
    glutSolidTeapot(0.3);
    glPopMatrix();///Pop還原Matrix
    glutSwapBuffers();
    ///angle+=3;///每次執行到這行,才加一點點
}
void motion(int x,int y)///新加的
{///當你的滑鼠去drag做motion動作時
   angle=x;///新加的
    display();///新加的
}
#include <stdio.h>///新加的
void mouse(int botton,int state,int x,int y)///新加的
{///要把大象放到冰箱(1)mouse按下去(2)mouse放開來
    printf("%d %d %d %d\n",botton,state,x,y);///新加的
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week05 rotate");
    glutDisplayFunc(display);
    glutIdleFunc(display);///今天新教!!! Idle 閒閒沒事就重畫
    glutMotionFunc(motion);///新加的
    glutMouseFunc(mouse);///新加的
    glutMainLoop();
}

沒有留言:

張貼留言