资源简介

opengl和c++实现的三维物体有光照 ,两个物体 ,场景

资源截图

代码片段和文件信息


#include 
#include 
#include 
#include 
#include “3dobject.h“

typedef float GLvector4f[4]; //  VMatMult Procedure类型
typedef float GLmatrix16f[16]; // For VMatMult Procedure类型

HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance=NULL;

bool keys[256] keypressed[256]; // 键盘数组
bool active=TRUE;

globject obj; // 对象
GLfloat xrot=0.0 xspeed=0.1; // X 转动 & X 速度
GLfloat yrot=0.0 yspeed=-0.1; // Y 转动 & Y 速度

float LightPos[] = { 0.0f 3.0f-4.0f 1.0f}; // 灯的位置
float LightAmb[] = { 0.2f 0.2f 0.2f 1.0f};
float LightDif[] = { 0.6f 0.6f 0.6f 1.0f};
float LightSpc[] = {-0.2f -0.2f -0.2f 1.0f};

float MatAmb[] = {0.4f 0.4f 0.4f 1.0f}; // 环境
float MatDif[] = {0.2f 0.6f 0.9f 1.0f}; // 反射
float MatSpc[] = {0.0f 0.0f 0.0f 1.0f}; // 漫射
float MatShn[] = {0.0f}; // 辉度

float ObjPos[] = {2.0f-2.0f-7.0f}; // 对象位置

GLUquadricObj *q; // 绘制球体
float SpherePos[] = {-4.0f-2.0f-6.0f}; //球的位置

LRESULT CALLBACK WndProc(HWND UINT WPARAM LPARAM); // 消息过程

void VMatMult(GLmatrix16f M GLvector4f v)
{
GLfloat res[4]; // 保存中间计算结果
res[0]=M[ 0]*v[0]+M[ 4]*v[1]+M[ 8]*v[2]+M[12]*v[3];
res[1]=M[ 1]*v[0]+M[ 5]*v[1]+M[ 9]*v[2]+M[13]*v[3];
res[2]=M[ 2]*v[0]+M[ 6]*v[1]+M[10]*v[2]+M[14]*v[3];
res[3]=M[ 3]*v[0]+M[ 7]*v[1]+M[11]*v[2]+M[15]*v[3];
v[0]=res[0]; // 把结果保存在 v[]
v[1]=res[1];
v[2]=res[2];
v[3]=res[3]; // 同质协同
}

GLvoid ReSizeGLScene(GLsizei width GLsizei height)
{
if (height==0)
{
height=1;
}

glViewport(00widthheight); // 重置 The Current Viewport

glMatrixMode(GL_PROJECTION); // 选择 The Projection 矩阵
glLoadIdentity();

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f(GLfloat)width/(GLfloat)height0.001f100.0f);

glMatrixMode(GL_MODELVIEW); // 选择 The Modelview 矩阵
glLoadIdentity(); // 重置 The Modelview 矩阵
}

int InitGLobjects() // 初始化对象
{
if (!Readobject(“object.txt“ &obj)) // 读取模型数据
{
return FALSE; // 失败返回FALSE
}

SetConnectivity(&obj); // 设置相邻点的信息

for (unsigned int i=0;i CalcPlane(obj &(obj.planes[i])); // 计算所有面的等式

return TRUE;
}

int InitGL(GLvoid)
{
if (!InitGLobjects()) return FALSE; // 是否初始化对象
glShadeModel(GL_SMOOTH);
glClearColor(0.0f 0.0f 0.0f 0.5f); // 黑色背景
glClearDepth(1.0f);
glClearStencil(0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST);

glLightfv(GL_LIGHT1 GL_POSITION LightPos); // 设置灯的位置
glLightfv(GL_LIGHT1 GL_AMBIENT LightAmb); // 灯的环境
glLightfv(GL_LIGHT1 GL_DIFFUSE LightDif); // 灯的漫射

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6726  2012-12-27 09:38  Lesson27\3Dobject.h

     文件        403  2000-05-04 11:55  Lesson27\Data\object.txt

     文件        902  2000-05-05 02:11  Lesson27\Data\object1.txt

     文件       1374  2000-05-05 02:16  Lesson27\Data\object2.txt

     文件         82  2000-05-18 02:04  Lesson27\Data\Simpleobject.txt

     文件    2016256  2012-12-27 11:40  Lesson27\Debug\Lesson27.bsc

     文件     225355  2012-12-27 11:39  Lesson27\Debug\Lesson27.exe

     文件     290024  2012-12-27 11:39  Lesson27\Debug\Lesson27.ilk

     文件      48213  2012-12-27 11:39  Lesson27\Debug\Lesson27.obj

     文件    4665248  2012-12-27 11:39  Lesson27\Debug\Lesson27.pch

     文件     558080  2012-12-27 11:39  Lesson27\Debug\Lesson27.pdb

     文件          0  2012-12-27 11:40  Lesson27\Debug\Lesson27.sbr

     文件     140288  2012-12-27 11:40  Lesson27\Debug\vc60.idb

     文件      77824  2012-12-27 11:39  Lesson27\Debug\vc60.pdb

     文件      15137  2012-12-27 11:39  Lesson27\Lesson27.cpp

     文件       4135  2012-12-26 21:17  Lesson27\Lesson27.dsp

     文件        541  2002-04-23 18:30  Lesson27\Lesson27.dsw

     文件      50176  2012-12-27 11:40  Lesson27\Lesson27.ncb

     文件      53760  2012-12-27 11:40  Lesson27\Lesson27.opt

     文件       1508  2012-12-27 11:40  Lesson27\Lesson27.plg

     文件        403  2012-12-27 11:37  Lesson27\object.txt

     文件     152064  2012-12-27 11:35  Lesson27\课程设计.doc

     目录          0  2012-12-26 19:13  Lesson27\Data

     目录          0  2012-12-27 11:39  Lesson27\Debug

     目录          0  2012-12-27 11:43  Lesson27

----------- ---------  ---------- -----  ----

              8308499                    25


评论

共有 条评论