電腦圖學---5
下載
data (裡面有MAYA圖案,移動到Windows資料夾)
win32 (解壓縮會有Windows資料夾)
glut32.dll (移動到Windows資料夾)
點選 Windows資料夾 裡面的 Light Material
⧭打光
左上角的圖片按右鍵可以換 屬性
左下角的圖片按右鍵可以換 材質、顏色
光源
GLfloat light_pos [ ] Position 光源座標 ( X , Y , Z , 數值)
GLfloat light_Ka [ ] Ambient light 到處都有的光
GLfloat light_Kd [ ] Diffuse light 主要的光
GLfloat light_Ks [ ] Specular light 特別凸顯得光
物體
GLfloat material_Ka [ ] Ambient light 到處都有的光
GLfloat material_Kd [ ] Diffuse light 主要的光
GLfloat material_Ks [ ] Specular light 特別凸顯得光
GLfloat material_Ke [ ] Emission light 自體發光 (少見)
GLfloat material_Se [ ] Shininess light 特別亮的光 ( 之後再教 )
➧fv 指的是 浮點數 v 很多
➟打開codeblocks
看起來變立體了
程式碼
#include <GL/glut.h>
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 };
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);///實心茶壺
glutSwapBuffers();///交換buffer顯示
}
int main(int argc, char**argv) ///argc代表參數有幾個,argv**二維字串(很多字串)
{
glutInit(&argc, argv);///初始化GLUT功能Init是初始化
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///啟動GLUT的Display的Mod模式:顯示的方式,GLUT_DOUBLE =>兩倍buffer ; GLUT_DEPTH =>可以用出3D
glutCreateWindow("06161162");///顯示一個視窗
glutDisplayFunc(display);///呼叫上面的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();///迴圈
}
加入 利用滑鼠移動
程式碼
#include <GL/glut.h>
float x=0,y=0; ///設X Y軸變數
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 };
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
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);
glutMotionFunc(motion); ///滑鼠有動作motion時,要叫motion()
glutMainLoop();
}
加入 自動旋轉
程式碼
#include <GL/glut.h>
float angle=0; ///新加一個變數
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 };
void display ()
{
///glut開頭,表示是GLUT的程式
/// glPushMatrix(); glPopMatrix(); 通常會做縮排
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///備份矩陣,保護
glRotatef(angle , 0, 1 ,0);
glutSolidTeapot(0.3);
glPopMatrix(); ///還原矩陣,保護
angle++;
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
glRotatef(x, 0, 1 ,0);
glutIdleFunc(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();
}
➽補充
程式模擬器
沒有留言:
張貼留言