/*
这是一个数组创建图形的程序
*/
//#include <windows.h>
#include "glee.h"
//#include <gl/gl.h>
//#include <gl/glu.h>
#include <gl/glaux.h>
#include <glut.h>
#include <math.h>
#include <stdio.h>
#include "math3d.h"
HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;
GLUquadricObj * quadShape;
bool keys[256]; //全局的BOOL默认为0
bool active=TRUE;
bool fullscreen=TRUE;
int iState = 0; //场景状态
float material_ambient[4] = {1.0f,0.0f,0.0f,1.0f};
float material_diffuse[4] = {1.0f,1.0f,0.0f,1.0f};
float material_shiness[4] = {1.0f,1.0f,1.0f,1.0f};
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f};
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f};
GLfloat noLight[] = { 0.0f, 0.0f, 0.0f, 1.0f};
GLfloat lightPos[] = { -50.0f, 80.0f, 100.0f, 1.0f};
float cameraPos[4] = {50.0f,80.0f,100.0f,1.0f};
float cameraZoom = 0.3f;
float nfactor = 4.0f;
M3DMatrix44f fMatrix;
unsigned int shadowTextureID;
unsigned int texturePlane;
unsigned int textureSphere;
unsigned int textureJingse;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
AUX_RGBImageRec *LoadBMP(CHAR *Filename) // 载入位图图象
{
FILE *File=NULL; // 文件句柄
if (!Filename) // 确保文件名已提供
{
return NULL; // 如果没提供,返回 NULL
}
File=fopen(Filename,"r"); // 尝试打开文件
if (File) // 文件存在么?
{
fclose(File); // 关闭句柄
return auxDIBImageLoadA(Filename); // 载入位图并返回指针
}
return NULL; // 如果载入失败,返回 NULL
}
int LoadGLTextures() // 载入位图(调用上面的代码)并转换成纹理
{
int Status=FALSE; // 状态指示器
AUX_RGBImageRec *TextureImage[3]; // 创建纹理的存储空间
memset(TextureImage,0,sizeof(void *)*1); // 将指针设为 NULL
// 载入位图,检查有无错误,如果位图没找到则退出
if (TextureImage[0]=LoadBMP("Data/Cube.bmp"))
{
Status=TRUE; // 将 Status 设为 TRUE
glGenTextures(1, &texturePlane); // 创建纹理
glBindTexture(GL_TEXTURE_2D,texturePlane);
// 生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // 线形滤波
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // 线形滤波
delete[] TextureImage[0]->data; // 释放纹理图像占用的内存
delete[] TextureImage[0];
}
if (TextureImage[1]=LoadBMP("Data/lanqiu.bmp"))
{
Status=TRUE; // 将 Status 设为 TRUE
glGenTextures(1, &textureSphere); // 创建纹理
glBindTexture(GL_TEXTURE_2D,textureSphere);
// 生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // 线形滤波
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // 线形滤波
delete[] TextureImage[1]->data; // 释放纹理图像占用的内存
delete[] TextureImage[1];
}
if (TextureImage[2]=LoadBMP("Data/jingse.bmp"))
{
Status=TRUE; // 将 Status 设为 TRUE
glGenTextures(1, &textureJingse); // 创建纹理
glBindTexture(GL_TEXTURE_2D,textureJingse);
// 生成纹理
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[2]->sizeX, TextureImage[2]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[2]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // 线形滤波
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // 线形滤波
delete[] TextureImage[2]->data; // 释放纹理图像占用的内存
delete[] TextureImage[2];
}
return Status; // 返回 Status
}
GLvoid SetWorld(bool IfPlane)
{
glColor3f(1.0f,1.0f,1.0f);
if(IfPlane)
{
glNormal3f(0.0f,1.0f,0.0f);
glBegin(GL_QUADS);
glVertex3f( 50.0f,0.0f,-50.0f);
glVertex3f(-50.0f,0.0f,-50.0f);
glVertex3f(-50.0f,0.0f, 50.0f);
glVertex3f( 50.0f,0.0f, 50.0f);
glEnd();
}
//圆柱体 -20,10,0
glColor3f(1.0f,0.0f,0.0f);
glPushMatrix();
glTranslatef(-20.0f,20.0f,0.0f);
glRotatef(90.0f,1.0f,0.0f,0.0f);
gluCylinder(quadShape,5.0f,5.0f,20.0f,36,36);
glPopMatrix();
//圆体 20,10,0
glColor3f(0.0f,1.0f,0.0f);
glPushMatrix();
glTranslatef( 20.0f,10.0f,0.0f);
gluSphere(quadShape,10.0f,36,36);
glPopMatrix();
//圆锥体 0,0,0
glColor3f(1.0f,0.0f,1.0f);
glPushMatrix();
glRotatef(-90.0f,1.0f,0.0f,0.0f);
gluCylinder(quadShape,5.0f,0.0f,20.0f,36,36);
glPopMatrix();
//立方体 0,10,20
glColor3f(1.0f,1.0f,0.0f);
glPushMatrix();
glTranslatef( 0.0f,10.0f,20.0f);
auxSolidCube(10.0f);
glPopMatrix();
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0)
{
height=1;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble ar = (GLdouble)width / (GLdouble)height;
glFrustum(-ar * cameraZoom, ar * cameraZoom, -cameraZoom, cameraZoom, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid)
{
quadShape = gluNewQuadric();
gluQuadricNormals(quadShape,GLU_SMOOTH);
gluQuadricTexture(quadShape,GL_TRUE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glPolygonOffset(nfactor,0.0f);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
LoadGLTextures();
glGenTextures(1,&shadowTextureID);
glBindTexture(GL_TEXTURE_2D,shadowTextureID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_FAIL_VALUE_ARB,0.5f);
glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGeni(GL_R,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGeni(GL_Q,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
// glEnable(GL_COLORMATERIAL);
//生成深度纹理图
//场景与深度纹理图进行比较
//绘制场景
{
GLfloat lightToSceneDistance; //光源到屏幕距离
GLfloat nearPlane; //近剪切面
GLfloat fieldOfView; //视觉领
GLfloat lightView[16],lightProjection[16]; //基于光源的透视图
GLfloat sR = 95.0f; //场景中物体范围
lightToSceneDistance = sqrt(lightPos[0]*lightPos[0]+lightPos[1]*lightPos[1]+lightPos[2]*lightPos[2]);
nearPlane = lightToSceneDistance - sR;
//保证场景全部贴满深度图
fieldOfView = (GLfloat)m3dRadToDeg(2.0f*atan(sR/lightToSceneDistance));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fieldOfView,1.0f,nearPlane,nearPlane+(2.0f * sR));
glGetFloatv(GL_PROJECTION_MATRIX,lightProjection);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(lightPos[0],lightPos[1],lightPos[2],
0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX ,lightView);
glViewport(0,0,640,480);
glClear(GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_FLAT);
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_NORMALIZE);
glColorMask(0,0,0,0);
glEnable(GL_POLYGON_OFFSET_FILL);
SetWorld(false);
glCopyTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,0,0,640,480,0);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glColorMask(1,1,1,1);
glDisable(GL_POLYGON_OFFSET_FILL);
M3DMatrix44f fMatrixs;
m3dLoadIdentity44(fMatrixs);
m3dTranslateMatrix44(fMatrixs,0.5f,0.5f,0.5f);
m3dScaleMatrix44(fMatrixs,0.5f,0.5f,0.5f);
m3dMatrixMultiply