1.jsyeh.org/3dcg10 <----小葉給的下載檔案網址
2.下載windows.zip >> 下載\windows\Tranformation.exe
3.下載 data.zip >> 將裡面的data 資料夾丟進去 windows 資料夾裡面,這樣就可以打開Tranformation.exe,就會出現下面的畫面
下一步:
1.在 CodeBlocks 中開啟GLUT新專案 Week04_Translate
2.先做出一個圖案並使用 PushMatrix 備份矩陣,再將圖案移動座標寫出來,這樣就不會一直往設定的座標移動,因為不先存進矩陣中移動會一直累績,後續使用 PopMatrix 把矩陣還原,就可以往設定的座標移動然後停下,而不會一直移動.
3.畫面
4.程式碼:
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0.3,0.2,0);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(& argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("HW2 bonus");
glutDisplayFunc(display);
glutMainLoop();
}
下一步:
1.將 Teapot 圖案另外寫成一個函式,這樣可以在 Display 函式中,寫入好幾個 Teapot 圖案,並把他們的位置分散開來
2.畫面
3.程式碼:
#include <GL/glut.h>
void myTeapot(float x, float y)
{
glPushMatrix();
glTranslatef(x,y,0);
glutSolidTeapot(0.3);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0);
myTeapot(+0.5,+0.5);
myTeapot(+0.5,-0.5);
myTeapot(-0.5,-0.5);
myTeapot(-0.5,+0.5);
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(& argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("HW2 bonus");
glutDisplayFunc(display);
glutMainLoop();
}
下一步:
1. 新增一個專案 week04_MouseFunc 進入後,需要多寫一個 mouse 函式,並讓這個函式能夠在大黑視窗中,每點擊一下就產生 mouse 函式中宣告的4個項目,且透過這個函式來顯示滑鼠左鍵是否按下去了,下去是0,上來是1.
2.畫面
3.程式碼:
#include <GL/glut.h>
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int button, int state, int x,int y)
{
printf("%d %d %d %d\n",button,state,x,y);
}
int main(int argc , char**argv)
{
glutInit( &argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
下一步:
1.從剛剛那個檔案修改程式碼,一開始宣告滑鼠的座標位置從 (0,0) 開始,然後在剛剛 void display 中的 Teapot 位置及矩陣中,將讀取座標位置的程式碼改成從滑鼠座標位置開始,這邊就是指滑鼠點到哪裡 Teapot 就會出現在哪裡.
2.畫面
3.程式碼:
#include <GL/glut.h>
#include <stdio.h>
int mouseX=0,mouseY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
glPushMatrix();///座標位置:減一半>除一半,y要加負號
glTranslatef((mouseX-150)/150.0,-(mouseY-150)/150.0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button, int state, int x,int y)
{
//printf("%d %d %d %d\n",button,state,x,y);
mouseX=x;mouseY=y;
}
int main(int argc , char**argv)
{
glutInit( &argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 mouse");
glutDisplayFunc(display);///Display顯示
glutMouseFunc(mouse);///Mouse滑鼠
glutMainLoop();
}
下一步:
1.使用剛剛的確認滑鼠座標位置的程式碼繼續改寫,這次要讓滑鼠點下去時可以產生一個點 (X,Y),而這個則是從剛剛的 display 函式中加入迴圈配陣列,使用迴圈配陣列來達成點兩個地方產生一個線段,並在大黑視窗中印出座標的確切位置,這個是在 void mouse 函式中改寫
2.畫面
3.程式碼:
#include <GL/glut.h>
#include <stdio.h>
int mouseX=0,mouseY=0,N=0;///N個點
int mx[100],my[100];///用來記錄
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
glBegin(GL_LINE_LOOP);
for(int i=0;i<N;i++){
glVertex2f((mx[i]-150)/150.0,-(my[i]-150)/150.0);
}
glEnd();
glutSwapBuffers();
}
void mouse(int button, int state, int x,int y)
{
//printf("%d %d %d %d\n",button,state,x,y);
mouseX=x;mouseY=y;
if(state==GLUT_DOWN){
printf(" glVertex2f((%d-150)/150.0,-(%d-150)/150.0;\n",x,y);
N++;
mx[N-1]=x;my[N-1]=y;
}
}
int main(int argc , char**argv)
{
glutInit( &argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 mouse");
glutDisplayFunc(display);///Display顯示
glutMouseFunc(mouse);///Mouse滑鼠
glutMainLoop();
}
沒有留言:
張貼留言