OLIGHT.C
上传用户:tengyuc
上传日期:2007-08-14
资源大小:722k
文件大小:4k
- #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
// 函数说明
void drawScene(void), setMatrix(void), initLightAndMaterial(void),
- animation(void), resize(int w, int h), menu(int choice),
- keyboard(unsigned char c, int x, int y);
-
// 全局变量
float ax, ay, az; // 动画角度
GLUquadricObj *quadObj; // 二次曲面对象
static float lmodel_twoside[] = {GL_TRUE};
static float lmodel_oneside[] = {GL_FALSE};
int main(int argc, char **argv)
{
- glutInit(&argc, argv);
- quadObj = gluNewQuadric();
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("双面光照");
- ax = 10.0;
- ay = -10.0;
- az = 0.0;
- initLightAndMaterial();
- glutDisplayFunc(drawScene);
- glutReshapeFunc(resize);
- glutCreateMenu(menu);
- glutAddMenuEntry("动画", 3);
- glutAddMenuEntry("双面光照", 1);
- glutAddMenuEntry("单面光照", 2);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- return 0;
}
void drawScene(void)
{
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- gluQuadricDrawStyle(quadObj, GLU_FILL);
- glColor3f(1.0, 1.0, 0.0);
- glRotatef(ax, 1.0, 0.0, 0.0);
- glRotatef(-ay, 0.0, 1.0, 0.0);
- gluCylinder(quadObj, 2.0, 5.0, 10.0, 20, 8); // 绘制一个圆锥
- glPopMatrix();
- glutSwapBuffers();
}
void setMatrix(void)
{
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-15.0, 15.0, -15.0, 15.0, -10.0, 10.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
}
int count = 0;
void animation(void)
{
- ax += 5.0;
- ay -= 2.0;
- az += 5.0;
- if (ax >= 360)
- ax = 0.0;
- if (ay <= -360)
- ay = 0.0;
- if (az >= 360)
- az = 0.0;
- drawScene();
- count++;
- if (count >= 60)
- glutIdleFunc(NULL);
}
void keyboard(unsigned char c, int x, int y)
{
- switch (c)
- {
- case 27:
- exit(0);
- break;
- default:
- break;
- }
}
void menu(int choice)
{
- switch (choice)
- {
- case 3:
- count = 0;
- glutIdleFunc(animation);
- break;
- case 2:
- glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
- glutSetWindowTitle("单面光照");
- glutPostRedisplay();
- break;
- case 1:
- glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
- glutSetWindowTitle("双面光照");
- glutPostRedisplay();
- break;
- }
}
void resize(int w, int h)
{
- glViewport(0, 0, w, h);
- setMatrix();
}
void initLightAndMaterial(void)
{
- // 光照于材质参数的数值
- static float ambient[] = {0.1, 0.1, 0.1, 1.0};
- static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
- static float position[] = {90.0, 90.0, 150.0, 0.0};
- static float front_mat_shininess[] = {60.0};
- static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
- static float front_mat_diffuse[] = {0.5, 0.5, 0.28, 1.0};
- static float back_mat_shininess[] = {60.0};
- static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
- static float back_mat_diffuse[] = {1.0, 0.9, 0.2, 1.0};
- static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
- setMatrix();
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- // 设置光照
- glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
- glLightfv(GL_LIGHT0, GL_POSITION, position);
- // 设置材质
- glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
- glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
- glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
- glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
- glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
- // 设置光照模型
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
- // 设置双面光照
- glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
- // 启动光照
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glShadeModel(GL_SMOOTH);
}