前言:
下載 。data 。windows 。glut32.dll
http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
_________________________________________________________________________
先備份
ctrl+F(Find)
找light
前面都刪
加入藍色圈圈內的程式碼
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
///打光也需要3D的gepth test
glEnable(GL_DEPTH_TEST);
#include<GL/glut.h>
///打光第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 };
/* Program entry point */
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
///打光也需要3D的gepth test
glEnable(GL_DEPTH_TEST);
///打光:要開啟光線,原來範例就寫好了
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
///打光2:要設定light的設定值(前面有陣列,這裡拿來用)
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);
///2.2:要打物體設定好它的meterial
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);
///其實原本177行程裡已經有打光需要的程式
glutMainLoop();
return EXIT_SUCCESS;
}
________________________________________________________________________________
{
GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f};///TODO:陣列,放位置
light_position[0]=(x-150)/150.0*10;///TODO:用motion的x座標算光ㄉ位置
light_position[1]=-(y-150)/150.0*10;
glLightfv(GL_LIGHT0, GL_POSITION, light_position);///TODO:用函式把光設好
glutPostRedisplay();///TODO:貼個post請glut重畫畫面
}
glutMotionFunc(motion);///TODO:現在我們想要讓光源動,用montion函式做http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
跟著滑鼠移動的光
void motion(int x,int y)///讓光源移動{
GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f};///TODO:陣列,放位置
light_position[0]=(x-150)/150.0*10;///TODO:用motion的x座標算光ㄉ位置
light_position[1]=-(y-150)/150.0*10;
glLightfv(GL_LIGHT0, GL_POSITION, light_position);///TODO:用函式把光設好
glutPostRedisplay();///TODO:貼個post請glut重畫畫面
}
glutMotionFunc(motion);///TODO:現在我們想要讓光源動,用montion函式做http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/





沒有留言:
張貼留言