📄 main.cpp
字号:
#include "main.h"
#include "3ds.h"
#include "bitmap.h"
#include "mmsystem.h"
#include "resource.h"
bool g_bFullScreen = true;
HWND g_hWnd;
RECT g_rRect;
HDC g_hDC;
HGLRC g_hRC;
HINSTANCE g_hInstance;
string fileName[17] =
{
"grab\\dizu.3ds", /*底座*/
"grab\\pingtai.3ds", /*平台*/
"grab\\allglass.3ds", /*玻璃*/
"grab\\wanbilink.3ds", /*弯臂连接座*/
"grab\\wanbixuanzhuantuiganda.3DS", /*弯臂旋转推杆大端*/
"grab\\wanbixuanzhuantuiganxiao.3DS", /*弯臂旋转推杆小端*/
"grab\\wanbi.3ds", /*弯臂*/
"grab\\wanbixiayouganda.3ds", /*弯臂下油杆大端*/
"grab\\wanbixiayouganxiao.3ds", /*弯臂下油杆小端*/
"grab\\wanbishangyouganda.3ds", /*弯臂上油杆大端*/
"grab\\wanbishangyouganxiao.3ds", /*弯臂上油杆小端*/
"grab\\qianbi.3ds", /*前臂*/
"grab\\qianyouganda.3ds", /*前油杆大端*/
"grab\\qianyouganxiao.3ds", /*前油杆小端*/
"grab\\liangan1.3ds", /*连杆1*/
"grab\\liangan2.3ds", /*连杆2*/
"grab\\dou.3ds" /*挖斗*/
};
#define FILENAMEALLSCENE "scene\\scene.3ds" /*总场景的模型文件名*/
UINT g_Texture[MAX_TEXTURES] = {0}; /*材质的纹理号*/
CLoad3DS *load3DS = NULL;
t3DModel g_3DModel[17]; /*挖掘机的模型数据结构*/
t3DModel g_3DModelAllScene; /*场景的模型数据结构*/
GLuint iList_grab = 17; /*挖掘机的显示列表号*/
GLuint iList_AllScene; /*总场景的显示列表号*/
GLuint iList_LoadScene; /*装车场景的显示列表号*/
//GLuint iList_SandParticles;
GLuint iList_SoilParticles;
GLuint iList_StoneParticles;
//GLuint iList_Truck; //卡车显示列表
GLfloat g_RotateWanbi = 0.0f;
GLfloat g_RotateWanbishangyouganda = 0.0f;
GLfloat g_RotateWanbishangyouganxiao = 0.0f;
GLfloat g_RotateWanbixiayouganda = 0.0f;
GLfloat g_RotateWanbixiayouganxiao = 0.0f;
GLfloat g_RotateQianyouganda = 0.0f;
GLfloat g_RotateQianyouganxiao = 0.0f;
GLfloat g_RotateLiangan1 = 0.0f;
GLfloat g_RotateLiangan2 = 0.0f;
GLfloat g_RotateWhole = 0.0f;
GLfloat g_RotatePingtai = 0.0f;
GLfloat g_RotateQianbi = 0.0f;
GLfloat g_RotateDou = 0.0f;
int g_CountWanbishangyouganxiao = 0;
GLfloat light_pos[] = { 0.0f, 100.0f, 100.0f, 0.0f }; /*光源位置*/
GLfloat lightModelAmbientScene[] = { 0.7f, 0.7f, 0.7f, 1.0f};/*场景的光的模型*/
GLfloat lightModelAmbientObject[] = { 0.2f, 0.2f, 0.2f, 1.0f };/*挖掘机的光的模型*/
GLfloat xpos = 0.0f;
GLfloat zpos = 0.0f;
bool bPressedButton1 = false; /*操纵杆按下的八个标志*/
bool bPressedButton2 = false;
bool bPressedButton3 = false;
bool bPressedButton4 = false;
bool bPressedButton5 = false;
bool bPressedButton6 = false;
bool bPressedButton7 = false;
bool bPressedButton8 = false;
//GLboolean bSelectScene[6] = { true, false, false, false, false, false };
int iSceneFlag = 0;
bool bFirstEntryAll = true;
bool bFirstEntrySand = true;
bool bFirstEntrySoil = true;
bool bFirstEntryRam = true;
bool bFirstEntryLoad = true;
bool bFirstEntryStone = true;
bool bHasObject = true;//是否有物体标志
bool keys[256];
bool bChangeCamera = false;
GLfloat eyex = -0.524f;
GLfloat eyey = 0.225f;
GLfloat eyez = 1.5f;
void Init(HWND hWnd)
{
g_hWnd = hWnd;
GetClientRect(g_hWnd, &g_rRect);
InitializeOpenGL(g_rRect.right, g_rRect.bottom);
InitGrab( );
if( iSceneFlag == 1 )
{
InitAllScene();
}
// else if( iSceneFlag == 2 )
// {
// InitSandScene();
// }
// else if( iSceneFlag == 3 )
// {
// InitSoilScene();
// }
// else if( iSceneFlag == 4 )
// {
// InitRamScene();
// }
// else if( iSceneFlag == 5 ) //
// {
// InitLoadScene();
// }
// else if( iSceneFlag = 6 )
// {
// InitStoneScene();
// }
// CreateSceneTexture( "sand.bmp", iSand );/*载入沙子的位图文件*/
glLightfv( GL_LIGHT0, GL_POSITION, light_pos ); /*设置光源*/
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING); /*启用光源*/
glEnable(GL_COLOR_MATERIAL);
}
void InitGrab( void )
{
/*将3ds文件装入到模型结构体中*/
for( int loop=0; loop<17; loop++ )
{
const char *temp = fileName[loop].data();/*将字符串转换成字符*/
load3DS = new ( CLoad3DS );
load3DS->Import3DS( &g_3DModel[loop], temp );
delete( load3DS );
}
/*读挖掘机的纹理文件*/
for( loop=0; loop<17; loop++ )
{
/* 遍历所有的材质*/
for(int i = 0; i < g_3DModel[loop].numOfMaterials; i++)
{
/* 判断是否是一个文件名*/
if(strlen(g_3DModel[loop].pMaterials[i].strFile) > 0)
{
/* 使用纹理文件名称来装入位图*/
CreateTexture(g_Texture, g_3DModel[loop].pMaterials[i].strFile, i);
}
/* 设置材质的纹理ID*/
g_3DModel[loop].pMaterials[i].texureId = i;
}
}
/*创建挖掘机的显示列表*/
iList_grab = glGenLists( 17 );
for( loop=0; loop<17; loop++ )
{
glNewList( iList_grab+loop, GL_COMPILE );
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, lightModelAmbientObject );
if( loop == 2 )/*判断是否是玻璃*/
{
/*设置玻璃的材质*/
glEnable( GL_BLEND );
glDepthMask( GL_FALSE );
glShadeModel( GL_FLAT );
glBlendFunc( GL_SRC_ALPHA, GL_ONE );
}
for( int i = 0; i < g_3DModel[loop].numOfObjects; i++)
{
/* 如果对象的大小小于0,则退出*/
if(g_3DModel[loop].pObject.size() <= 0) break;
/*获得当前显示的对象*/
t3DObject *pObject = &g_3DModel[loop].pObject[i];
/* 判断该对象是否有纹理映射*/
if(pObject->bHasTexture)
{
/* 打开纹理映射*/
glEnable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
glBindTexture(GL_TEXTURE_2D, g_Texture[pObject->materialID]);
}
else
{
/* 关闭纹理映射*/
glDisable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
}
/*开始以三角形模式绘制*/
glBegin( GL_TRIANGLES );
/*遍历所有的面*/
for(int j = 0; j < pObject->numOfFaces; j++)
{
/*遍历三角形的所有点*/
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
/*获得面对每个点的索引*/
int index = pObject->pFaces[j].vertIndex[whichVertex];
/* 给出法向量*/
glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
/*如果对象具有纹理*/
if(pObject->bHasTexture)
{
/*确定是否有UVW纹理坐标*/
if(pObject->pTexVerts)
{
glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
}
}
else
{
if(g_3DModel[loop].pMaterials.size() && pObject->materialID >= 0)
{
BYTE *pColor = g_3DModel[loop].pMaterials[pObject->materialID].color;
if( loop == 2 )
{
glColor4f(pColor[0]/255.0f, pColor[1]/255.0f, pColor[2]/255.0f, 0.3f);
}
else
{
glColor3ub(pColor[0], pColor[1], pColor[2]);
}
}
}
glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
}
}
glEnd(); /*绘制结束*/
}
if( loop == 2 )/*判断是否是玻璃*/
{
/*玻璃绘制结束,关闭其相应的材质设置*/
glDisable( GL_BLEND );
glShadeModel( GL_SMOOTH );
glDepthMask( GL_TRUE );
}
glEndList();/*显示列表创建结束*/
}
}
void InitAllScene( void )
{
load3DS = new( CLoad3DS );
load3DS->Import3DS( &g_3DModelAllScene, FILENAMEALLSCENE );
delete( load3DS );
/*读入总场景的纹理文件*/
for(int i = 0; i < g_3DModelAllScene.numOfMaterials; i++)
{
/*判断是否是一个文件名*/
if(strlen(g_3DModelAllScene.pMaterials[i].strFile) > 0)
{
/* 使用纹理文件名称来装入位图*/
CreateTexture(g_Texture, g_3DModelAllScene.pMaterials[i].strFile, i);
}
/*设置材质的纹理ID*/
g_3DModelAllScene.pMaterials[i].texureId = i;
}
/*创建总场景的显示列表*/
iList_AllScene = glGenLists( 1 );
glNewList( iList_AllScene, GL_COMPILE );
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, lightModelAmbientScene );
for( i = 0; i < g_3DModelAllScene.numOfObjects; i++)
{
/* 如果对象的大小小于0,则退出*/
if(g_3DModelAllScene.pObject.size() <= 0) break;
/* 获得当前显示的对象*/
t3DObject *pObject = &g_3DModelAllScene.pObject[i];
/* 判断该对象是否有纹理映射*/
if(pObject->bHasTexture)
{
/*打开纹理映射*/
glEnable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
glBindTexture(GL_TEXTURE_2D, g_Texture[pObject->materialID]);
}
else
{
/*关闭纹理映射*/
glDisable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
}
/*开始以三角形模式绘制*/
glBegin( GL_TRIANGLES );
/*遍历所有的面*/
for(int j = 0; j < pObject->numOfFaces; j++)
{
/*遍历三角形的所有点*/
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
/*获得面对每个点的索引*/
int index = pObject->pFaces[j].vertIndex[whichVertex];
/*给出法向量*/
glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
/* 如果对象具有纹理*/
if(pObject->bHasTexture)
{
/* 确定是否有UVW纹理坐标*/
if(pObject->pTexVerts)
{
glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
}
}
else
{
if(g_3DModelAllScene.pMaterials.size() && pObject->materialID >= 0)
{
BYTE *pColor = g_3DModelAllScene.pMaterials[pObject->materialID].color;
glColor3ub(pColor[0], pColor[1], pColor[2]);
}
}
glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
}
}
glEnd(); /* 绘制结束*/
}
glEndList(); /*显示创建结束 */
}
//void InitSandScene( void )
//{
//}
void InitSoilScene( void )
{
}
void InitLoadScene( void )
{
}
WPARAM MainLoop()
{
MSG msg;
while(1)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
RenderScene();
}
}
return(msg.wParam);
}
void RenderScene()
{
GLfloat atx, aty = 0.0, atz;
GLfloat tempxg = 0.0f;
GLfloat tempzg = 0.0f;
GLfloat tempxc = 0.0f;
GLfloat tempzc = 0.0f;
glClearColor( 0.5f, 0.5f, 0.5f, 1.0f );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /*设置背景颜色*/
glLoadIdentity(); /*重置为单位矩阵*/
if( keys[VK_RIGHT] )
{
if( g_RotateWhole <= -360.0f )
{
g_RotateWhole = 0.0f;
}
g_RotateWhole -= 1.0f;
}
else if( keys[VK_LEFT] )
{
if( g_RotateWhole >= 360.0f )
{
g_RotateWhole = 0.0f;
}
g_RotateWhole += 1.0f;
}
if( keys[VK_UP] ) //&& ((eyex*eyex+eyez*eyez) < 6.4*6.4)
{
tempxg = -(GLfloat)sin( g_RotateWhole * piover180 ) * 0.1f;
tempzg = -(GLfloat)cos( g_RotateWhole * piover180 ) * 0.1f;
tempxc = -(GLfloat)sin( g_RotateWhole * piover180 ) * 0.1f;
tempzc = -(GLfloat)cos( g_RotateWhole * piover180 ) * 0.1f;
}
else if( keys[VK_DOWN] ) //&& ((eyex*eyex+eyez*eyez) < 6.4*6.4)
{
tempxg = (GLfloat)sin( g_RotateWhole * piover180 ) * 0.1f;
tempzg = (GLfloat)cos( g_RotateWhole * piover180 ) * 0.1f;
tempxc = (GLfloat)sin( (g_RotateWhole) * piover180 ) * 0.1f;
tempzc = (GLfloat)cos( (g_RotateWhole) * piover180 ) * 0.1f;
}
/* if( ((xpos*xpos+zpos*zpos) < 6.3*6.3))
{
xpos += tempxg;
zpos += tempzg;
eyex += tempxc;
eyez += tempzc;
}
*/
xpos += tempxg;
zpos += tempzg;
eyex += tempxc;
eyez += tempzc;
atx = eyex + 100*(GLfloat)sin( (-g_RotateWhole - g_RotatePingtai) * piover180 );
atz = eyez - 100*(GLfloat)cos( (-g_RotateWhole - g_RotatePingtai) * piover180 );
if( !bChangeCamera )//视点切换设置
{
gluLookAt( 0, 1.5f, 8, 0, 0.5f, 0, 0.0f, 1.0f, 0.0f );/*设定视点位置*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -