step01-1_下載上課用檔案
下載位置:https://jsyeh.org/3dcg10/
下載檔案及名稱:1. data.zip、2. win32.zip、3. glut32.dll
解壓縮
windows.zip => 獨立解出一個資料夾
data.zip => 將data解壓縮到window資料夾內
執行時可移動xyz的座標
step01-2_開啟Code::Blocks 建立新專案命名:week03-1_translate
建立上週的茶壺並加入glTranslatef(x,y,z);調整位置
---------------------------------------------------------------------------------------------
void display()
{
glPushMatrix(); ///備份矩陣
glTranslatef( 0.5, 0, 0); /// 讓位於正中心的茶壺往右方移動
glutSolidTeapot(0.3); ///劃出實心茶壺;大小0.3
glPopMatrix(); ///還原矩陣
glutSwapBuffers(); ///請GLUT畫面swap送到顯示的地方
}
---------------------------------------------------------------------------------------------
step02-1_加入滑鼠的操作-1
欲加入滑鼠的函式,於main裡面加入監聽滑鼠的glutMouseFunc();
------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void mouse(int button, int state, int x, int y)
{/// step 02-1 請mouse函式幫忙
printf("%d,%d,%d,%d\n", button,state,x,y); ///印出點擊的座標
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);///開啟GLUT
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
///設定顯示模式
glutCreateWindow("week03");///開視窗
glutDisplayFunc(display);///要顯示的對應函示"display()"
glutMouseFunc(mouse); /// step 02-1 請mouse函式幫忙
glutMainLoop();/// 壓最後
}
------------------------------------------------------------------------------------------------------------
結果展示:取得點擊的座標
實作點擊即移動
------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>
float X=0,Y=0; /// step02-2 利用global變數,在函示之間傳值
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清背景
glPushMatrix(); ///step01-2 備份矩陣
glTranslatef( X, Y, 0); /// 讓位於正中心的茶壺往右方移動
glutSolidTeapot(0.3); ///劃出實心茶壺;大小0.3
glPopMatrix(); ///還原矩陣
glutSwapBuffers(); ///請GLUT畫面swap送到顯示的地方
}
///#include<stdio.h>/// step 02-1
void mouse(int button, int state, int x, int y)
{/// step 02-1 請mouse函式幫忙
///printf("%d,%d,%d,%d\n", button,state,x,y);
X = (x-150)/150.0;
Y = -(y-150)/150.0;
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);///開啟GLUT
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
///設定顯示模式
glutCreateWindow("week03");///開視窗
glutDisplayFunc(display);///要顯示的對應函示"display()"
glutMouseFunc(mouse); /// step 02-1 請mouse函式幫忙
glutMainLoop();/// 壓最後
}
------------------------------------------------------------------------------------------------------------
step03-1_實作滑鼠畫圖
新建一個專案,實作用滑鼠畫圖
----------------------------------------------------------------------------------------------
#include <GL/glut.h>
#include <stdio.h>
void display()
{
glutSolidTeapot(0.3); ///劃出實心茶壺;大小0.3
glutSwapBuffers(); ///請GLUT畫面swap送到顯示的地方
}
void mouse(int button, int state, int x, int y)
{
float X = (x-250)/250.0; /// 根據視窗大小作調整(預設視窗300)
float Y = -(y-250)/250.0;
if(state==GLUT_DOWN){ ///step03-1 取得點擊座標
printf(" glVertex2f(%.3f, %.3f);\n",X ,Y);
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);///開啟GLUT
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
///設定顯示模式
glutInitWindowSize(500,500); /// 設定視窗大小 500*500
glutCreateWindow("week03");///開視窗
glutDisplayFunc(display);///要顯示的對應函示"display()"
glutMouseFunc(mouse); /// step 02-1 請mouse函式幫忙
glutMainLoop();/// 壓最後
}
--------------------------------------------------------------------------------------------------
另外在display裡加入取得的座標
glBegin(GL_POLYGON); ///多邊形
glVertex2f(-0.232, 0.128);
glVertex2f(-0.240, 0.108);
glVertex2f(-0.264, 0.100);
glVertex2f(-0.296, 0.100);
glVertex2f(-0.340, 0.100);
glVertex2f(-0.384, 0.100);
glVertex2f(-0.420, 0.100);
glVertex2f(-0.448, 0.064);
glVertex2f(-0.452, 0.012);
glVertex2f(-0.440, -0.024);
glVertex2f(-0.388, -0.080);
glVertex2f(-0.308, -0.116);
glVertex2f(-0.296, -0.140);
glVertex2f(-0.212, -0.228);
glVertex2f(-0.124, -0.228);
glVertex2f(-0.052, -0.228);
glVertex2f(0.068, -0.240);
glVertex2f(0.172, -0.240);
glVertex2f(0.228, -0.220);
glVertex2f(0.276, -0.136);
glVertex2f(0.344, -0.100);
glVertex2f(0.376, -0.080);
glVertex2f(0.332, -0.004);
glVertex2f(0.328, 0.048);
glVertex2f(0.296, 0.044);
glVertex2f(0.292, -0.012);
glVertex2f(0.256, 0.060);
glVertex2f(0.228, 0.108);
glVertex2f(0.216, 0.140);
glEnd();
-----------------------------------------------------------------------------------------------------
課外技巧:如何嵌入好看又整齊的程式碼?
利用Github的直觀(?)模式
沒有留言:
張貼留言