Week06:打光
範例 http://jsyeh.org/3dcg10
1. 下載網頁中的data, win32
2. 將windows解壓縮, data.zip/data丟進解壓縮後的windows資料夾
右方函式glLightfv():
fv = float vector (浮點數陣列)
fv = float vector (浮點數陣列)
glLightfv(GL_LIGHT0,GL_POSITION,light_pos)
第0道光 位置 陣列
打光Light
```
#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);
glColor3f(1, 1, 0);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week06_light");
glutDisplayFunc(display);
///以下是複製的程式碼(函式呼叫)
///複製的程式碼要放在glutCreateWindow()之後才有效
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 <stdio.h>///印出用
#include <GL/glut.h>
///打光陣列
/*這裡放陣列宣告*/
float x=150, y=150, z=0, scale=1.0;///座標設定為150, 150在視窗正中央
int oldX=0, oldY=0;
void display()
{
glClearColor(0.5, 0.5, 0.5, 1);///灰色背景R,G,B,A 其中A為半透明Alpha值
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備分矩陣
glTranslatef((x-150)/150.0, -(y-150)/150.0, z);
glScalef(scale, scale, scale);///x,y,z縮放為 scale倍,維持原有比例
glColor3f(1, 1, 0);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
void mouse(int button, int state, int mouseX, int mouseY)
{///重設圖案位置,避免瞬間移動的問題
oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
if(mouseX-oldX >0) scale *=1.01;
if(mouseX-oldX <0) scale *=0.99;///+-百分之一
oldX = mouseX;
oldY = mouseY;
display();
}
void keyboard( unsigned char key, int mouseX, int mouseY )
{
}
int main(int argc, char** argv)
{
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week05 keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);///鍵盤事件函式
glutMouseFunc(mouse);
glutMotionFunc(motion);
/*這裡放函式呼叫*/
glutMainLoop();
}
```
續上週:旋轉
加上新的變數(angle)與glRoataef()函式
```#include <stdio.h>///印出用
#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 };
float x=150, y=150, z=0, scale=1.0, angle=0.0;///座標設定為150, 150在視窗正中央
int oldX=0, oldY=0;
void display()
{
glClearColor(0.5, 0.5, 0.5, 1);///灰色背景R,G,B,A 其中A為半透明Alpha值
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備分矩陣
glTranslatef((x-150)/150.0, -(y-150)/150.0, z);
glRotatef(angle, 0, 1, 0);///對Y軸轉動
glScalef(scale, scale, scale);///x,y,z縮放為 scale倍,維持原有比例
glColor3f(1, 1, 0);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
void mouse(int button, int state, int mouseX, int mouseY)
{///重設圖案位置,避免瞬間移動的問題
oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
angle += (mouseX-oldX);///轉動
///if(mouseX-oldX >0) scale *=1.01;///縮放
///if(mouseX-oldX <0) scale *=0.99;///+-百分之一
///oldX = mouseX;///移動
///oldY = mouseY;
display();
}
void keyboard( unsigned char key, int mouseX, int mouseY )
{
}
int main(int argc, char** argv)
{
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week05 keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);///鍵盤事件函式
glutMouseFunc(mouse);
glutMotionFunc(motion);
///打光函式
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();
}
```
Keybaord()函式輸入切換功能
#include <stdio.h>///印出用
#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 };
float x=150, y=150, z=0, scale=1.0, angle=0.0;///座標設定為150, 150在視窗正中央
int oldX=0, oldY=0, now=1;///設定now變數切換功能
void display()
{
glClearColor(0.5, 0.5, 0.5, 1);///灰色背景R,G,B,A 其中A為半透明Alpha值
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備分矩陣
glTranslatef((x-150)/150.0, -(y-150)/150.0, z);
glRotatef(angle, 0, 1, 0);///對Y軸轉動
glScalef(scale, scale, scale);///x,y,z縮放為 scale倍,維持原有比例
glColor3f(1, 1, 0);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
void keyboard( unsigned char key, int mouseX, int mouseY )
{
if(key=='1' || key=='w' || key=='W') now=1;///移動
if(key=='2' || key=='e' || key=='E') now=2;///轉動
if(key=='3' || key=='r' || key=='R') now=3;///縮放
}
void mouse(int button, int state, int mouseX, int mouseY)
{///重設圖案位置,避免瞬間移動的問題
oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
if(now==1)
{
x += (mouseX-oldX);///移動
y += (mouseY-oldY);
}
else if(now==2)
{
angle += (mouseX-oldX);///轉動
}
else if(now==3)
{
if(mouseX-oldX >0) scale *=1.01;///縮放
if(mouseX-oldX <0) scale *=0.99;///+-百分之一
}
display();
}
int main(int argc, char** argv)
{
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week05 keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);///鍵盤事件函式
glutMouseFunc(mouse);
glutMotionFunc(motion);
///打光函式
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();
}
```
沒有留言:
張貼留言