本周繼上周所學內容,繼續延伸以下的步驟:
1.(先開取程式)
2.
自行試transformation.exe.並回憶上周所學習到的旋轉及移動功能
並且利用右鍵swap交換兩行,如此就可以觀察出自轉與公轉的差別
將glBegin以藍色車子為視角
將glScalef視為變胖的車子
將glTranslatef視為移到右邊的車子
將glRotatef視為在餐桌中心移動的車子
利用以上假設的設定,便能更加清楚明白車子移動的方向及自轉與公轉...等等的差別
程式碼:
#include <GL/glut.h>
#include<stdio.h>
float vx[2000],vy[2000];準備一堆頂點,等一下要畫!(介於-1~+1)
int N=0;有N個頂點
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
for(int i=0;i<N;i++){
glVertex2f(vx[i],vy[i]);
}
glEnd();
glutSwapBuffers();
}
void mouse(int buttin,int state,int x,int y)
{
}
void motion(int x,int y)
{
printf("%d %d\n",x,y);把頂點記起來等一下要畫
vx[N]=(x-150)/150.0;很像這樣的寫法記錄下來
vy[N]=-(y-150)/150.0;
N++;
display();
}///減一半,再除一半,y加負號
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week05 drawing");
glutDisplayFunc(display);
glutMouseFunc(mouse);按下去.彈起來
glutMotionFunc(motion);mouse motion在動(拖曳)/在畫
glutMainLoop();
}
以上程式碼撰寫完成後便可以自行自由的畫圖!!!
1. glPushMatrix();//備份矩陣
2. glTranslatef(x,y,z);//移動
3. glRotatef(angle,x,y,z);//轉動
4. glScalef(x,y,z);///放大縮小
5. glBegin(GL_POLYGON);//開始畫
6. glColor3f(r,g,b);//色彩
7. glTexCoord2f(tx,ty);//貼圖座標
8. glNormal3f(nx,ny,nz);//打光的法向量
9. glVertex3f(x,y,z);//頂點
10. glEnd();//結束畫
11. glPopMatrix();//還原矩陣
以上為期中考考題!!!!!!!!!





沒有留言:
張貼留言