(1)開啟小葉老師的網站>試試看車子能旋轉(Rotate) 、移動(Translate) 、縮放(Scale)

(2)開啟Glut專案>試試茶壺 移動(Translate)
----------------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///backup the matrix
glTranslatef(0.5,0,0); ///Today's Most Important line
glutSolidTeapot(0.3);
glPopMatrix(); ///restore the matrix
glutSwapBuffers();
}
int main (int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160355 Translate Rotate Scale");
glutDisplayFunc(display);
///glutMouseFunc(mouse);
///glutMotionFunc(motion);
glutMainLoop();
}
-------------------------------------------------------------------------------------------------------------------------
(3)加 motion >茶壺能跟著游標點擊來移動
-----------------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>
float mouseX=0,mouseY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///backup the matrix
glTranslatef(mouseX,mouseY,0); ///Today's Most Important line
glutSolidTeapot(0.3);
glPopMatrix(); ///restore the matrix
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX = (x-150)/150.0;
mouseY = -(y-150)/150.0;
glutPostRedisplay();
}
int main (int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160355 Translate Rotate Scale");
glutDisplayFunc(display);
///glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
-----------------------------------------------------------------------------------------------------------------------
↓↓老師截圖講解座標系統不同問題
(4)黃色小精靈會開口
- display() 裡的 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 會把畫面清空,才能再重畫的時候沒有殘影
- display() 裡的 glutSwapBuffers() 會更新畫面/交換繪圖的 memory buffer
- motion() 裡的 glutPostRedisplay() 會貼個 3M Post便利貼,告訴GLUT要重畫畫面 Re-display
- for(float angle=0 +mouth; angle< 3.1415926 *2 -mouth; angle++) 可以讓 angle 的開始&結束的角度
- for(float angle=0; angle< 3.1415926 *2; angle++) 可以讓 angle從0到 2*PI
- float mouth=0 表示嘴巴張開的角度
- void motion(int x, int y) 可以把現在的mouth位置做改變
- glutMotionFunc(motion) 可以註冊 motion的函式
------------------------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>
#include <math.h>
float mouth=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0,0);
for(float angle=0+mouth; angle<= 3.1415926 *2-mouth; angle+=0.01)
{
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
void motion(int x,int y)
{
mouth = x/300.0;
glutPostRedisplay();
}
int main (int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160355 Translate Rotate Scale");
glutDisplayFunc(display);
///glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
(5)茶壺能夠旋轉 (Rotate) 、放大(Scale)
- glScalef()的參數是 x,y,z 分別表示3個方向的縮放比例
- glScalef() 開頭的 gl表示是 OpenGL的函式
- glScalef() 結尾的f表示是float的參數
- glScalef(0,0,0) 會讓東西變不見
- glScalef(-1,-1, -1) 會讓東西的上下反過來, 左右反過來
-------------------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>
float mouseX=0,mouseY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///backup the matrix
glScalef(mouseX,mouseY,0); ///Today's Most Important line
glutSolidTeapot(0.3);
glPopMatrix(); ///restore the matrix
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX = (x-150)/150.0;
mouseY = -(y-150)/150.0;
glutPostRedisplay();
}
int main (int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160355 Translate Rotate Scale");
glutDisplayFunc(display);
///glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
----------------------------------------------------------------------------------------------------------------------------






沒有留言:
張貼留言