📄 3dsmodel.cpp
字号:
// 3DSModel.cpp: implementation of the C3DSModel class.
//
// 沈阳蓝雨视景科技
// 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::t3DObject *pObject;
// Since we know how many objects our model has, go through each of them.
for(int i = 0; i < m_3DModel.numOfObjects; i++)
{
// Get the current object that we are displaying
pObject = &m_3DModel.vctObjects[i];
//对所有的子材料
for(int imat=0;imat<pObject->numOfMaterials;imat++)
{
pmatref=&pObject->pMaterialREFS[imat];;
if(pmatref->bHasTexture)
{
glEnable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
glBindTexture(GL_TEXTURE_2D, m_3DModel.vctMaterials[pmatref->nMaterialID].texureId);//g_Texture[pObject->materialID]);
}
else
{
glDisable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
}
//开始绘制一个子物体
glBegin(GL_TRIANGLES);// Begin drawing with our selected mode (triangles or lines)
// Go through all of the faces (polygons) of the object and draw them
for(int nfindex = 0,j=0; nfindex < pmatref->nFaceNum; nfindex++)
{
j=int(pmatref->pFaceIndexs[nfindex]);
// Go through each corner of the triangle and draw it.
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
// Get the index for each point of the face
int index = pObject->pFaces[j].vertIndex[whichVertex];
// Give OpenGL the normal for this vertex.
glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
// If the object has a texture associated with it, give it a texture coordinate.
if(pmatref->bHasTexture)// pObject->bHasTexture) {
{
// Make sure there was a UVW map applied to the object or else it won't have tex coords.
if(pObject->pTexVerts) {
glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
}
}
else
{
if(m_3DModel.numOfMaterials>0 && pmatref->nMaterialID>=0)//pObject->materialID >= 0)
{
// Get and set the color that the object is, since it must not have a texture
BYTE *pColor = m_3DModel.vctMaterials[pmatref->nMaterialID].color;
// Assign the current color to this model
glColor3ub(pColor[0], pColor[1], pColor[2]);
}
}
glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
}
}
glEnd();
}
}
return TRUE;
}
// 载入模型文件
BOOL C3DSModel::LoadModelFromFile(char* sfilename)
{
//静态序号
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);
//此处改动,设置材料号
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -