複習上周期中考內容 |
glPushMatrix();備份矩陣
glRotatef(angle,x,y,z);旋轉
glTranslatef(x,y,z);移動
glScalef(x,y,z);縮放
glBeing(GL_POLYGON);開始畫多邊形
glColor3f(r,g,b);顏色
glNormal3f(nx,ny,nz);打光的法向量
glTexCoord2f(tx,ty);貼圖座標
glVertex3f(x,y,z);頂點
glEnd();結束畫多邊形
glPopMatrix();還原矩陣
用CodeBlocks產生GLUT project。
打光的茶壺 |
將GLUT project多餘的程式碼刪掉,要留下打光的程式碼(關鍵字light),把display裡的程式碼進行修改。
以下是程式碼(紅色是重點):
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);~~~去除之前的顏色,去除之前的深度
glPushMatrix();~~~備份矩陣
glutSolidTeapot(0.3);~~~畫茶壺
glPopMatrix();~~~還原矩陣
glutSwapBuffers();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
改了數值,打光的茶壺 |
將上面打光的茶壺中的const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };改變數值,改成const GLfloat light_position[] = { 2.0f, -5.0f, 0.0f, 0.0f };就能得到不同的打光效果
會旋轉的打光茶壺 |
將上面打光的茶壺程式碼,在display函式中加入glTranslatef、glRotatef,在int main()的主程式碼裡加入glutIdleFunc。
程式碼如下:(紅色是新加入的程式碼)
#include <GL/glut.h>
float angle = 0;~~~宣告角度
static void display(void)
{
angle++;~~~每跑一次就加一
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,0.5,0);~~~移動
glRotatef(angle,0,1,0);~~~旋轉
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, -5.0f, 0.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutIdleFunc(display);~~~閒置(沒有動作),就從跑display。
glutDisplayFunc(display);
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
有兩個茶壺 |
在上面的旋轉茶壺程式碼中加入glPushMatrix();、glutSolidTeapot(0.3);、glPopMatrix();,就能再多產生一個不會動的茶壺,兩者就能比較。
程式碼如下:(紅色是新加入的程式碼)
static void display(void)
{
angle++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,0.5,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();~~~備份矩陣
glutSolidTeapot(0.3);~~~畫茶壺
glPopMatrix();~~~還原矩陣
glutSwapBuffers();
}
一個茶壺,一個正方形 |
把display函式裡資料進行修改,把原本不會動的茶壺改成正方形,程式碼茶壺glutSolidTeapot(0.3);改成正方形glutSolidCube(0.3);。
程式碼如下:(紅色是新加入的程式碼)
static void display(void)
{
angle++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,0.5,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,1);
glutSolidCube(0.3);~~~畫正方形
glPopMatrix();
glutSwapBuffers();
}
一個茶壺,一個圓錐 |
把display函式裡資料進行修改,把原本正方形改成圓錐,程式碼正方形glutSolidCube(0.3);改成圓錐glutSolidCone(0.4,0.4,5,15);。
程式碼如下:(紅色是新加入的程式碼)
static void display(void)
{
angle++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,0.5,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,1);
glRotatef(angle,0,1,0);
glutSolidCone(0.4,0.4,5,15);~~~畫圓錐(底半徑,高度,slice柳丁切,stack堆疊)
glPopMatrix();
glutSwapBuffers();
}
一個茶壺,一個透明圓錐 |
把display函式裡資料進行修改,把原本實體圓錐改成透明圓錐,程式碼實體圓錐glutSolidCone(0.4,0.4,5,15);改成透明圓錐glutWireCone(0.4,0.4,5,15);。
程式碼如下:(紅色是新加入的程式碼)
static void display(void)
{
angle++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,0.5,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,1);
glRotatef(angle,0,1,0);
glutWireCone(0.4,0.4,5,15);~~~畫透明圓錐(底半徑,高度,slice柳丁切,stack堆疊)
glPopMatrix();
glutSwapBuffers();
}
沒有留言:
張貼留言