Week04
主題:Rotate旋轉
1.至 jsyeh.org/3dgc10 下載
data.zip =>解壓縮放到windows
windows.zip =>解壓縮 => 開啟Transfomation.exe
旋轉茶壺
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BITS);
glPushMatrix();
glRotatef(90,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main (int argc,char**argv)
{
glutInit( &argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 Rotate");
glutDisplayFunc(display);
glutMainLoop();
}
1.建立新專案,將程式碼貼上並執行
2.建立一個新專案 week04_motion 用滑鼠控制茶壺旋轉
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
void motion (int x, int y)
{
angle=x;
display();///重畫畫面
}
int main (int argc,char**argv)
{
glutInit( &argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 Rotate");
glutDisplayFunc(display);
glutMotionFunc(motion); ///滑鼠動
glutMainLoop();
}
3.因為上個程式碼做出來的茶壺在旋轉時會卡(每次旋轉角度都從新開始),所以要修改程式碼,讓茶壺旋轉更順暢。
變順暢的原因: 多建立一個oldx座標,當程式旋轉第二次時,將新座標(x)減掉第一次轉的座標(oldx)就可以從第一次旋轉完的座標在旋轉下去(不用從頭開始轉)
***********************************************
複習上禮拜的滑鼠座標,並讓軌跡顯示出來
#include <GL/glut.h>
#include <stdio.h>
int mx[1000],my[1000];
int N=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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)
{
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;
}
display();
}
int main (int argc,char**argv)
{
glutInit( &argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 Rotate");
glutDisplayFunc(display);
glutMouseFunc(mouse);/// 上週教過: mouse 按下去、放開來
glutMainLoop();
}
沒有留言:
張貼留言