Skip to content

Commit

Permalink
增加光照和光照开关
Browse files Browse the repository at this point in the history
  • Loading branch information
Azard committed Nov 29, 2014
1 parent ebadabf commit 239f4e5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
Binary file modified World-of-Garden/Data/plantdata
Binary file not shown.
13 changes: 13 additions & 0 deletions World-of-Garden/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ void select_this_land()
fprintf(stdout, "select, x:%d, z:%d\n", select_x, select_z);
}

void onlight() {
glEnable(GL_LIGHTING);
}

void offlight() {
glDisable(GL_LIGHTING);
}

void initNormalKeys(unsigned char key, int x, int y) {
switch (key) {
Expand All @@ -139,6 +146,12 @@ void initNormalKeys(unsigned char key, int x, int y) {
case 'c':
select_this_land();
break;
case 'l':
onlight();
break;
case 'k':
offlight();
break;
}
}

Expand Down
3 changes: 2 additions & 1 deletion World-of-Garden/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ void flush_view(float ang_p, float ang_u);
void move_ab(float i);
void move_ud(float i);
void move_lr(float i);

void onlight();
void offlight();

void initNormalKeys(unsigned char key, int x, int y);
void initSpecialKeys(int key, int x, int y);
Expand Down
25 changes: 25 additions & 0 deletions World-of-Garden/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,28 @@ void initScene() {
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

snowman_display_list = createSnowman();
}

void initLight() {
GLfloat ambientLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };//白色主光源环境光
GLfloat diffuseLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };//白色主光源漫反射
GLfloat specularLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };//白色主光源镜面光

GLfloat lightPos[] = { 50.0f, 10.0f, 50.0f, 1.0f }; //光源位置
GLfloat spotLightPos[] = { 0.0f, 0.0f, 200.0f, 1.0f }; //射灯位置
GLfloat spotDir[] = { 0.0f, 0.0f, -1.0f }; //射灯方向

glEnable(GL_LIGHTING); //启用光照
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); //设置环境光源
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); //设置漫反射光源
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); //设置镜面光源
glLightfv(GL_LIGHT0, GL_POSITION, lightPos); //设置灯光位置
glEnable(GL_LIGHT0); //打开白色主光源

glEnable(GL_COLOR_MATERIAL); //启用材质的颜色跟踪
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); //指定材料着色的面
glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight); //指定材料对镜面光的反应
glMateriali(GL_FRONT, GL_SHININESS, 100); //指定反射系数
}

void render_scene(void) {
Expand Down Expand Up @@ -199,10 +220,14 @@ int main(int argc, char** argv) {

initMapTexture();
initTerran();
initLight();

glutReshapeFunc(reshape_init);
glutDisplayFunc(render_scene);
//glutIdleFunc(render_scene);





// GLUI terrain部分
Expand Down

0 comments on commit 239f4e5

Please sign in to comment.