// glutEx1.cpp : 定义控制台应用程序的入口点。
//
#include <stdlib.h>
#include "glut.h"
float fTranslate;
float fRotate;
float fScale = 1.0f; // set inital scale value to 1.0f
bool bPersp = false;
bool bAnim = false;
bool bWire = false;
bool lightcolor = false;
float lx=5,ly=5,lz=5; //光源初始位置
bool bjuguang = false;
float spl=45; //裁剪角度参数
float spx=0,spy=0; //光源朝向参数
int wHeight = 0;
int wWidth = 0;
void Draw_Leg();
void Draw_Triangle() // This function draws a triangle with RGB colors
{
glPushMatrix();
float mat_diffuse[] = {0.85f, 0.65f, 0.2f, 1.0f};
float mat_specular[] = {0.6f, 0.6f, 0.6f, 1.0f};
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMateriali(GL_FRONT_AND_BACK,GL_SHININESS,50);
glTranslatef(0, 0, 4+1);
glRotatef(90, 1, 0, 0);
glutSolidTeapot(1);
glPopMatrix();
glPushMatrix();
float specular1[] = {0 ,0, 0, 1.0f};
float color1[]={1,0,0,1};
glMaterialfv(GL_FRONT, GL_SPECULAR, specular1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, color1);
glTranslatef(0, 0, 3.5);
glScalef(5, 4, 1);
glColor3f(1.0,0.0,0.0);
glutSolidCube(1.0);
glPopMatrix();
glPushMatrix();
float color2[]={0,1,0,1};
glMaterialfv(GL_FRONT, GL_DIFFUSE, color2);
glTranslatef(1.5, 1, 1.5);
glColor3f(0.0,1.0,0.0);
Draw_Leg();
glPopMatrix();
glPushMatrix();
float color3[]={1,1,0,1};
glMaterialfv(GL_FRONT, GL_DIFFUSE, color3);
glTranslatef(-1.5, 1, 1.5);
glColor3f(1.0,1.0,0.0);
Draw_Leg();
glPopMatrix();
glPushMatrix();
float color4[]={0,1,1,1};
glMaterialfv(GL_FRONT, GL_DIFFUSE, color4);
glTranslatef(1.5, -1, 1.5);
glColor3f(0.0,1.0,1.0);
Draw_Leg();
glPopMatrix();
glPushMatrix();
float color5[]={0,0,1,1};
glMaterialfv(GL_FRONT, GL_DIFFUSE, color5);
glTranslatef(-1.5, -1, 1.5);
glColor3f(0.0,0.0,1.0);
Draw_Leg();
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
}
void Draw_Leg()
{
glScalef(1, 1, 3);
glutSolidCube(1.0);
}
void updateView(int width, int height)
{
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
float whRatio = (GLfloat)width/(GLfloat)height;
if (bPersp) {
gluPerspective(45.0f, whRatio,0.1f,100.0f);
//glFrustum(-3, 3, -3, 3, 3,100);
} else {
glOrtho(-3 ,3, -3, 3,-100,100);
}
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
}
void reshape(int width, int height)
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
wHeight = height;
wWidth = width;
updateView(wHeight, wWidth);
}
void idle()
{
glutPostRedisplay();
}
float eye[] = {0, 0, 8};
float center[] = {0, 0, 0};
void key(unsigned char k, int x, int y)
{
switch(k)
{
case 27:
case 'q': {exit(0); break; }
case 'p': {bPersp = !bPersp; break; }
case ' ': {bAnim = !bAnim; break;}
case 'o': {bWire = !bWire; break;}
case 'd': {
eye[0] -= 0.2f;
center[0] -= 0.2f;
break;
}
case 'a': {
eye[0] += 0.2f;
center[0] += 0.2f;
break;
}
case 'w': {
eye[1] -= 0.2f;
center[1] -= 0.2f;
break;
}
case 's': {
eye[1] += 0.2f;
center[1] += 0.2f;
break;
}
case 'z': {
eye[2] -= 0.2f;
center[2] -= 0.2f;
break;
}
case 'c': {
eye[2] += 0.2f;
center[2] += 0.2f;
break;
}
case 'D': { lx++; break; }
case 'A': { lx--; break; }
case 'W': { ly++; break; }
case 'S': { ly--; break; }
case 'Z': { lz++; break; }
case 'C': { lz--; break; }
case 'X': { lightcolor = !lightcolor; break; }
case 'T':{ bjuguang=!bjuguang; break; } //聚光灯开关
case 'J' :{ spx+=0.5 ; break; } //光源方向参数
case 'L' :{ spx-=0.5; break; }
case 'I' :{ spy+=0.5; break; }
case 'K' :{ spy-=0.5; break; }
case 'U' :{ spl+=0.5; break; } //裁剪角度参数
case 'O' :{ spl-=0.5; break; }
}
updateView(wHeight, wWidth);
}
void redraw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // Reset The Current Modelview Matrix
gluLookAt(eye[0], eye[1], eye[2],
center[0], center[1], center[2],
0, 1, 0); // 场景(0,0,0)的视点中心 (0,5,50),Y轴向上
if (bWire) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat light_pos[] = {lx,ly,lz,1};
// GLfloat light_pos[] = {5,5,5,1};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos); //光源位置函数
if(lightcolor)
glLightfv(GL_LIGHT0, GL_AMBIENT, blue);
if(!lightcolor)
glLightfv(GL_LIGHT0, GL_AMBIENT, white); //光源颜色
glEnable(GL_LIGHT0);
float jcolor[]={10,9,8,1}; //聚光灯颜色
// GLfloat light_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
// glLightfv(GL_LIGHT1, GL_AMBIENT,light_ambient); //设置环境光成分
glLightfv(GL_LIGHT1, GL_SPECULAR,jcolor); //设置镜面光成分
glLightfv(GL_LIGHT1, GL_DIFFUSE,jcolor); //设置漫射光成分
GLfloat position[]={0,10,0,1};
GLfloat lightDir[]={spx,-1,spy}; //聚光灯光源方向数组
//glLightf(GL_LIGHT1, GL_SPOT_CUTOFF,spotangle); //裁减角度
glLightfv(GL_LIGHT1, GL_POSITION,position);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF,spl); //裁减角度
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightDir); //光源方向
glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,2.0); //聚集度
if(bjuguang)
glEnable(GL_LIGHT1);
else
glDisable(GL_LIGHT1);
// glTranslatef(0.0f, 0.0f,-6.0f); // Place the triangle at Center
glRotatef(fRotate, 0, 1.0f, 0); // Rotate around Y axis
glRotatef(-90, 1, 0, 0);
glScalef(0.2, 0.2, 0.2);
Draw_Triangle(); // Draw triangle
if (bAnim) fRotate += 0.5f;
glutSwapBuffers();
}
int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(480,480);
int windowHandle = glutCreateWindow("Simple GLUT App");
glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}