week03_1 下載上課用檔案
下載位置:https://jsyeh.org/3dcg10/
下載檔案及名稱:1. data.zip、2. win32.zip、3. glut32.dll
解壓縮
windows.zip => 獨立解出一個資料夾
data.zip => 將data解壓縮到window資料夾內
執行時可移動xyz的座標
Week03-1 開啟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送到顯示的地方
}
---------------------------------------------------------------------------------------------
欲加入滑鼠的函式,於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();/// 壓最後
}
------------------------------------------------------------------------------------------------------------
Week03-1_按滑鼠左鍵移動茶壺
#include <GL/glut.h>
float X=0, Y=0;
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glTranslatef( X, Y, 0);
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
void mouse (int button, int state, int x, int y)
{
X = ( x-150 ) / 150.0 ;
Y = -( y-150 ) / 150.0 ;
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
沒有留言:
張貼留言