2018年3月22日 星期四

Week03 欣儀的blog~~~

Part1 動手玩Shape.exe
1.先搜尋網址:http://jsyeh.org/3dcg10

2.下載 data.zip, windows.zip, glut32.dll 3個檔案

3.將windows解壓縮


4.接著把剛剛下載的glut32.dll複製,在windows解壓縮後的資料夾貼上,點兩下Shapes就會出現第二張圖的視窗


    
5.左邊和右邊的視窗按下右鍵可以切換其他的模式 

Part2 用滑鼠找出茶壺座標
1.先建立GLUT專案,輸入程式碼畫出茶壺(可參考week01和week02的文章)
2.運用滑鼠按下後輸出位置座標,程式碼如下:

#include <stdio.h>
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT |  GL_DEPTH_BUFFER_BIT);///清畫面
    glutSolidTeapot(0.3);///大小為0.3的茶壺
    glutSwapBuffers();///搭配GLUT_DOUBLE兩倍顯示
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN) printf("glVertex2f(%f, %f);\n",(x-150)/150.0,(150-y)/150.0);///如果mouse往下,才印出程式
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);///初始Initialize你的glut參數設定
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);///2個顯示參數
    glutCreateWindow("Week03 Mouse");///建立視窗
    glutDisplayFunc(display);///顯示函式display()用來畫圖的
    glutMouseFunc(mouse);///上面會有mouse()滑鼠事件
    glutMainLoop();///主要的迴圈,用來控制程式
}
3.沿著茶壺邊緣點下,將所有座標點全選複製 

4.將剛剛複製的座標點貼上,程式碼如下:
#include <stdio.h>
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT |           GL_DEPTH_BUFFER_BIT);///清畫面
  //glutSolidTeapot(0.3);///大小為0.3的茶壺
    glBegin(GL_LINES);
    glVertex2f(-0.060000, 0.246667);
    glVertex2f(-0.020000, 0.246667);
    glVertex2f(0.013333, 0.253333);
    ....貼上所有座標點
    glEnd();
    glutSwapBuffers();///搭配GLUT_DOUBLE兩倍顯示
}

Part3  移動茶壺
第一種:在茶壺上按滑鼠左鍵,就能夠移動(都只從茶壺中心點去移動),程式碼如下:
#include <stdio.h>
#include <GL/glut.h>
float teatopX=0, teapotY=0;///Now:茶壺的座標
void display()
{
    glClear(GL_COLOR_BUFFER_BIT |  GL_DEPTH_BUFFER_BIT);///清畫面
    glPushMatrix();///Now:備份矩陣
        glTranslatef(teatopX,teapotY, 0);///Now:依照茶壺的座標移動
        glutSolidTeapot(0.3);///大小為0.3的茶壺
    glPopMatrix();///Now:還原矩陣
    glutSwapBuffers();///搭配GLUT_DOUBLE兩倍顯示
}
void mouse(int button, int state, int x, int y)
{

}
void motion (int x, int y)///mouse motion事件
{
    teatopX=(x-150)/150.0; teapotY=(150-y)/150.0;///Now:依照motion時的x,y來改teapot的座標
    glutPostRedisplay(); ///記得要Redisplay()
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);///初始Initialize你的glut參數設定
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);///2個顯示參數
    glutCreateWindow("Week03 Mouse");///建立視窗
    glutDisplayFunc(display);///顯示函式display()用來畫圖的
    glutMouseFunc(mouse);///上面會有mouse()滑鼠事件
    glutMotionFunc(motion);///等一下上面會有mouse()滑鼠motion事件
    glutMainLoop();///主要的迴圈,用來控制程式
}

第二種:由滑鼠點下的位置去做移動,程式碼如下:
float teatopX=0, teapotY=0;///Now:茶壺的座標
int oldX=0, oldY=0;///Now2:要用的變數,記得mouse在哪按下去
void display()
{
    glClear(GL_COLOR_BUFFER_BIT |  GL_DEPTH_BUFFER_BIT);///清畫面
    glPushMatrix();///Now:備份矩陣
        glTranslatef(teatopX,teapotY, 0);///Now:依照茶壺的座標移動
        glutSolidTeapot(0.3);///大小為0.3的茶壺
    glPopMatrix();///Now:還原矩陣
    glutSwapBuffers();///搭配GLUT_DOUBLE兩倍顯示
}
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;///Now2:記得按下去的點在哪裡
}
void motion (int x, int y)///mouse motion事件
{
    teatopX+=(x-oldX)/150.0; teapotY+=(oldY-y)/150.0;///Now2:換一下移動方式
    oldX=x; oldY=y;///Now2:記下來你已移到哪裡
    glutPostRedisplay(); ///記得要Redisplay()
}

附註:兩者差別在滑鼠點下的移動位置
第一種不管滑鼠點在哪,都是從中間移動
第二種是看滑鼠點在哪,就由那位置移動(紅色點為滑鼠點下位置)

沒有留言:

張貼留言