就在下方網址照著之前的步驟下載下來
http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
成功開啟檔案!!!(複習一下程式碼Shapes.exe、Transformation.exe)
今天學的是最右邊車子的程式
這三道程式碼
glTranslatef 調整左右前後上下
glRotatef 調整旋轉
glScalef 調整大小
再複習一下如何開啟一個專案,我們開啟CodeBlocks,在moodle或是下列網址下載軟體
https://www.transmissionzero.co.uk/software/freeglut-devel/
選擇 Download freeglut 3.0.0 for MinGW
第一步:New->project->GLUT project
第二步:填檔名、填位置、填目錄
第三步:創建且執行
之後我們開始我們的主題
---------------------------------------------------------------------------------------------------------
滑鼠拖移以及設置物件頂點
還記得我們之前如何做茶壺的嗎?
.
.
.
忘記了沒關係下方給你
----------------------------------------------------------------------------------------
.
.
.
忘記了沒關係下方給你
----------------------------------------------------------------------------------------
#include<GL/glut.h>
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04");
glutDisplayFunc(display);
glutMainLoop();
}
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04");
glutDisplayFunc(display);
glutMainLoop();
}
---------------------------------------------------------------------------------------------------------
我們如何讓茶壺動起來呢?
這時候就要加入Mouse的函式與上面由提到的移動程式碼
void mouse(int button, int state, int x, int y){}
glTranslatef(x,y,z);
說一下觀念
視窗的XY軸交點(左上角)跟數值(0~300)與圖示的XY軸交點(正中間)跟數值(0~1)是不同的
所以這時候就需要設個變數把視窗的中心點改成與圖示一樣
所以程式就像下面這這樣
-------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>#include <stdio.h>///可以用printf找出滑鼠點擊視窗的位置
float teapotX=-1 ,teapotY=1;///我們設在外面這樣每個函式都用的到
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glTranslatef(teapotX, teapotY, 0);///移動的程式碼
glutSolidTeapot(0.3);
glPopMatrix();///備份矩陣
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
teapotX =(x-150)/150.0;///滑鼠點擊的換算座標
teapotY =-(y-150)/150.0;
///printf("%d %d %d %d\n", button, state, x, y);
///如果不知道視窗的中心點在哪,可以用這個印出數值(滑鼠按鍵、點擊狀態、X軸、Y軸)
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("Week03");
glutDisplayFunc(display);
glutMouseFunc(mouse);///寫了這麼多Mouse,記得要引入它
glutMainLoop();
}
--------------------------------------------------------------------------------------------------
再來是畫圖,多邊形的方法是畫出各頂點然後連線
先用小畫家找出各個頂點
下面有描茶壺的範例:
-----------------------------------------------------------------------------------------------------------#include<GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);///畫圖開始
glVertex2f( (227-150)/150.0, -(132-150)/150.0 );///各個頂點位置(X,Y),一樣要換算
glVertex2f( (151-150)/150.0, -(115-150)/150.0 );///除數要注意" .0 "
glVertex2f( (85-150)/150.0, -(141-150)/150.0 );
glVertex2f( (118-150)/150.0, -(188-150)/150.0 );
glVertex2f( (180-150)/150.0, -(186-150)/150.0 );
glEnd();///畫圖結束
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("HomeWork");
glutDisplayFunc(display);
glutMainLoop();
}
-----------------------------------------------------------------------------------------------------------
最後是這樣呈現:如果要上色的程式碼為glColor3f( /255.0 , /255.0 , /255.0 );
-----------------------------------------------------------------------------------------------------------
最後是這樣呈現:如果要上色的程式碼為glColor3f( /255.0 , /255.0 , /255.0 );


回家有作業,如果要畫圓是以下程式碼:
--------------------------------------------------------------
#include<math.h>///sin()cos()需要用到
glBegin(GL_POLYGON);
for ( float a = 0 ; a <= 3.14 * 2 ; a + = 0.01 ) {
glVertex2f ( x + 大小 * cos ( ), y + 大小 * sin ( ) );
}/// x,y為圓的位置
glEnd();






沒有留言:
張貼留言