?? 3dsmodel.cpp
字號:
// 3DSModel.cpp: implementation of the C3DSModel class.
//
// 沈陽藍(lán)雨視景科技
// http://www.bvrain.com
// bvrain@163.com chglei@163.com
// 13998383976
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "3DSModel.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
C3DSModel::C3DSModel()
{
m_sFileName[0]=0;
m_bModelOK=FALSE;
m_3DModel.numOfMaterials=0;
m_3DModel.numOfObjects=0;
m_3DModel.vctMaterials.clear();
m_3DModel.vctObjects.clear();
}
C3DSModel::~C3DSModel()
{
for(int i = 0; i < m_3DModel.numOfObjects; i++)
{
// Free the faces, normals, vertices, and texture coordinates.
delete [] m_3DModel.vctObjects[i].pFaces;
delete [] m_3DModel.vctObjects[i].pNormals;
delete [] m_3DModel.vctObjects[i].pVerts;
delete [] m_3DModel.vctObjects[i].pTexVerts;
for(int j=0;j<m_3DModel.vctObjects[i].numOfMaterials;j++)
delete[] m_3DModel.vctObjects[i].pMaterialREFS[j].pFaceIndexs;
delete [] m_3DModel.vctObjects[i].pMaterialREFS;
}
}
//繪制模型
BOOL C3DSModel::Draw()
{
CLoad3DS::tMatREF *pmatref;
CLoad3DS::tMaterialInfo *pmat;
CLoad3DS::t3DObject *pObject;
// 繪制所有的物體
for(int i = 0; i < m_3DModel.numOfObjects; i++)
{
// 當(dāng)前要顯示的物體指針
pObject = &m_3DModel.vctObjects[i];
//對所有的子材料遍歷
for(int imat=0;imat<pObject->numOfMaterials;imat++)
{
//當(dāng)前材料引用
pmatref=&pObject->pMaterialREFS[imat];
//當(dāng)前材料
pmat=&(m_3DModel.vctMaterials[pmatref->nMaterialID]);
//使用材料
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,pmat->ambient);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,pmat->diffuse);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,pmat->specular);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,pmat->emissive);
//是否綁定紋理
if(pmatref->bHasTexture)
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
glColor3ub(255, 255, 255);
glBindTexture(GL_TEXTURE_2D, m_3DModel.vctMaterials[pmatref->nMaterialID].texureId);//g_Texture[pObject->materialID]);
}
else
{
glDisable(GL_TEXTURE_2D);
glDisable(GL_COLOR_MATERIAL);
}
//開始繪制一個子物體
glBegin(GL_TRIANGLES);
//循環(huán)紋理相關(guān)的所有的面
for(int nfindex = 0,j=0; nfindex < pmatref->nFaceNum; nfindex++)
{
//得到面索引號
j=int(pmatref->pFaceIndexs[nfindex]);
//繪制該面的三個頂點(diǎn)
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
// 得到頂點(diǎn)號
int index = pObject->pFaces[j].vertIndex[whichVertex];
// 設(shè)置法線
glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
// 如果改材料有紋理,就設(shè)置紋理坐標(biāo)
if(pmatref->bHasTexture)
{
if(pObject->pTexVerts)
{
glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
}
}
//繪制頂點(diǎn)
glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
}
}
glEnd();
}
}
return TRUE;
}
// 載入模型文件
BOOL C3DSModel::LoadModelFromFile(char* sfilename)
{
//靜態(tài)序號
static int ntextID=0;
char stmp[512];
//把文件名連接到媒體路徑,成為完整路徑
strcpy(m_sFileName,sfilename);
strcpy(stmp,g_sMediaPath);
strcat(stmp,sfilename);
//在這使用加載類
CLoad3DS cLoad3ds;
//載入文件
if(cLoad3ds.Import3DS(&m_3DModel, stmp)==false)
{
//失敗
m_bModelOK=FALSE;
return FALSE;
}
//成功
for(int i = 0; i < m_3DModel.numOfMaterials; i++)
{
// Check to see if there is a file name to load in this material
if(strlen(m_3DModel.vctMaterials[i].strFile) > 0)
{
strcpy(stmp,g_sMediaPath);
strcat(stmp,m_3DModel.vctMaterials[i].strFile);
//此處改動,設(shè)置材料號
CreateTexture(g_Texture,stmp , ntextID);
m_3DModel.vctMaterials[i].texureId = g_Texture[ntextID];
ntextID++;
}
else
m_3DModel.vctMaterials[i].texureId = 0;
}
m_bModelOK=TRUE;
return TRUE;
}
//對齊到最低點(diǎn)(Y值)
void C3DSModel::AlignBottom()
{
CLoad3DS::t3DObject *pObject;
float fminy=10000;
float foffset=0;
int i=0;
//找到最小Y
for(i = 0; i < m_3DModel.numOfObjects; i++)
{
pObject = &m_3DModel.vctObjects[i];
for(int j=0;j<pObject->numOfVerts;j++)
{
if(fminy>pObject->pVerts[j].y)fminy=pObject->pVerts[j].y;
}
}
foffset=-fminy;
//對齊所有點(diǎn)(平移)
for(i = 0; i < m_3DModel.numOfObjects; i++)
{
pObject = &m_3DModel.vctObjects[i];
for(int j=0;j<pObject->numOfVerts;j++)
{
pObject->pVerts[j].y+=foffset;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -