電腦圖學 ---3
輸入網址 http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
⧭移動
下載
data (裡面有MAYA圖案,移動到Windows資料夾)
win32 (解壓縮會有Windows資料夾)
glut32.dll (移動到Windows資料夾)
data (移動到Windows資料夾) )
glut32.dll (移動到Windows資料夾)
點取 Transformation
就可以移動 glTranslatef( X , Y , Z ) ;
⧭利用滑鼠移動茶壺
程式碼
#include <GL/glut.h>
float x=0,y=0; ///設X Y軸變數
void display ()
{
///glut開頭,表示是GLUT的程式
/// glPushMatrix(); glPopMatrix(); 通常會做縮排
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///備份矩陣,保護
glTranslatef((x-150)/150.0, -(y-150)/150.0 ,0);///Translate 移動 X Y Z,f是浮點數
glutSolidTeapot(0.3);
glPopMatrix(); ///還原矩陣,保護
glutSwapBuffers();
}
void motion(int nowx, int nowy)
{
x=nowx; y=nowy; ///更新X Y
display(); ///再畫display(),用滑鼠移動畫面會跟著動
}
int main(int argc,char**argv) ///main參數
{
glutInit(&argc, argv); ///啟動GLUT
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///啟動GLUT的Display的Mode模式 : 兩倍Buffer,有3D的深度
glutCreateWindow("Week03 translate");
glutDisplayFunc(display); ///Display的函式是display
glutMotionFunc(motion); ///滑鼠有動作motion時,要叫motion()
glutMainLoop();
}
⟰第三周回家作業
今天的主角 --- Yo Yo Man
程式碼
#include <GL/glut.h> ///使用GL/glut外掛#include <stdio.h>
#include <math.h>
float nowx=0,nowy=0; ///設X Y軸變數
void mouse(int button, int state, int x,int y)
{
nowx = (x-150)/150.0; nowy = -(y-150)/150.0 ,0;
printf("glTranslatef(%f,%f,0;\n",nowx,nowy);
}
void display ()
{
///glut開頭,表示是GLUT的程式
/// glPushMatrix(); glPopMatrix(); 通常會做縮排
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///備份矩陣,保護
glTranslatef(nowx,nowy,0);
glColor3f(255,215,0);
glBegin(GL_POLYGON); ///頭
for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
{
glVertex2f(0.8*cos(angle)/* /數字+位置 */,0.8*sin(angle));
///園的大小 ///數字越大圓越大,-0.4 指移動的位置
}
glEnd();
glColor3f(255,255,255);
glBegin(GL_POLYGON);///頭
for(float angle=0; angle<3.14159265358979; angle+=0.1)
{
glVertex2f(0.8*cos(angle),0.8*-sin(angle));
}
glEnd();
glBegin(GL_POLYGON); ///Y
glColor3f(255,0,0); ///顏色
glVertex2f(-0.5,0.75); ///座標
glVertex2f(-0.4,0.8);
glVertex2f(0,0.5);
glVertex2f(-0.2,0.45);
glEnd();
glBegin(GL_POLYGON);///Y
glColor3f(255,0,0);
glVertex2f(0,0.5); ///座標
glVertex2f(0.4,0.8);
glVertex2f(0.5,0.75);
glVertex2f(0.2,0.45);
glEnd();
glBegin(GL_POLYGON);///Y
glColor3f(255,0,0);
glVertex2f(-0.1,0.5); ///座標
glVertex2f(0.1,0.5);
glVertex2f(0.1,0.2);
glVertex2f(-0.1,0.2);
glEnd();
glColor3f(0,0,0);
glBegin(GL_POLYGON);///眼睛邊框
for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
{
glVertex2f(0.45*-cos(angle)/1.4995+0.4,0.45*sin(angle)/1.4995+0.15);
///園的大小 ///數字越大圓越大,-0.4 指移動的位置
}
glEnd();
glColor3f(0,0,0);
glBegin(GL_POLYGON);///眼睛邊框
for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
{
glVertex2f(0.45*cos(angle)/1.4995-0.4,0.45*sin(angle)/1.4995+0.15);
///圓的大小 ///數字越大圓越大,-0.4 指移動的位置
}
glEnd();
glColor3f(245,222,179);
glBegin(GL_POLYGON);///眼睛外
for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
{
glVertex2f(0.4*-cos(angle)/1.5+0.4,0.4*sin(angle)/1.5+0.15);
}
glEnd();
glColor3f(245,222,179);
glBegin(GL_POLYGON);///眼睛外
for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
{
glVertex2f(0.4*cos(angle)/1.5-0.4,0.4*sin(angle)/1.5+0.15);
}
glEnd();
glColor3f(0,0,0);
glBegin(GL_POLYGON);///眼睛內
for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
{
glVertex2f(0.3*-cos(angle)/2+0.4,0.3*sin(angle)/2+0.15);
}
glEnd();
glColor3f(0,0,0);
glBegin(GL_POLYGON);///眼睛內
for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
{
glVertex2f(0.3*cos(angle)/2-0.4,0.3*sin(angle)/2+0.15);
}
glEnd();
glColor3f(255,255,255);
glBegin(GL_POLYGON);///眼睛內
for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
{
glVertex2f(0.1*-cos(angle)/2+0.5,0.1*sin(angle)/2+0.1);
}
glEnd();
glColor3f(255,255,255);
glBegin(GL_POLYGON);///眼睛內
for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
{
glVertex2f(0.1*cos(angle)/2-0.3,0.1*sin(angle)/2+0.1);
}
glEnd();
glColor3f(255,0,0);
glBegin(GL_POLYGON);///嘴巴
for(float angle=0; angle<3.14159265358979; angle+=0.1)
{
glVertex2f(0.4*cos(angle)/1,0.4*-sin(angle)/1-0.3);
}
glEnd();
glColor3f(255/255.0,128/255.0,128/255.0);
glBegin(GL_POLYGON);///嘴巴
for(float angle=0; angle<3.14159265358979; angle+=0.1)
{
glVertex2f(0.2*cos(angle)/1,0.2*-sin(angle)/1-0.4);
}
glEnd();
glPopMatrix(); ///還原矩陣,保護
glutSwapBuffers();
}
int main(int argc,char**argv) ///main參數
{
glutInit(&argc, argv); ///啟動GLUT
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///啟動GLUT的Display的Mode模式 : 兩倍Buffer,有3D的深度
glutCreateWindow("Week03 translate");
glutDisplayFunc(display); ///Display的函式是display
glutMouseFunc(mouse); ///滑鼠有動作motion時,要叫motion()
glutMainLoop();
}
沒有留言:
張貼留言