#include <QMessageBox>
#include "3ds.h"
#include "3dmath.h"
//-------------------------------- CLOADS3DS ------------------------------------
// This constructor initializes the tChunk data
//-------------------------------- CLOADS3DS ------------------------------------
CLoad3DS::CLoad3DS()
{
m_CurrentChunk = new tChunk;
m_TempChunk = new tChunk;
}
//---------------------------------- IMPORT 3DS ----------------------------------
// This is called by the client to open the .3ds file, read it, then clean up
//---------------------------------- IMPORT 3DS -----------------------------------
bool CLoad3DS::Import3DS(t3DModel *pModel, char *strFileName)
{
char strMessage[255] = {0};
m_FilePointer = fopen(strFileName, "rb");
if(!m_FilePointer)
{
sprintf(strMessage, "Unable to find the file: %s!", strFileName);
// MessageBox(NULL, strMessage, "Error", MB_OK);
return false;
}
ReadChunk(m_CurrentChunk);
if (m_CurrentChunk->ID != PRIMARY)
{
sprintf(strMessage, "Unable to load PRIMARY chuck from file: %s!", strFileName);
//MessageBox(NULL, strMessage, "Error", MB_OK);
return false;
}
ProcessNextChunk(pModel, m_CurrentChunk);
ComputeNormals(pModel);
CleanUp();
return 0;
}
//---------------------------------- CLEAN UP ----------------------------------
// This function cleans up our allocated memory and closes the file
//---------------------------------- CLEAN UP ----------------------------------
void CLoad3DS::CleanUp()
{
fclose(m_FilePointer);
delete m_CurrentChunk;
delete m_TempChunk;
}
//---------------------------------- PROCESS NEXT CHUNK-----------------------------------
// This function reads the main sections of the .3DS file, then dives deeper with recursion
//---------------------------------- PROCESS NEXT CHUNK-----------------------------------
void CLoad3DS::ProcessNextChunk(t3DModel *pModel, tChunk *pPreviousChunk)
{
t3DObject newObject = {0};
tMaterialInfo newTexture = {0};
unsigned short version[10] = {0};
int buffer[50000] = {0};
m_CurrentChunk = new tChunk;
while (pPreviousChunk->bytesRead < pPreviousChunk->length)
{
ReadChunk(m_CurrentChunk);
switch (m_CurrentChunk->ID)
{
case VERSION:
m_CurrentChunk->bytesRead += fread(&version, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer);
if (version[0] > 0x03)
QMessageBox::information(NULL, QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("verson > 0x03"), QMessageBox::Ok);
//MessageBox(NULL, "This 3DS file is over version 3 so it may load incorrectly", "Warning", MB_OK);
break;
case OBJECTINFO:
ReadChunk(m_TempChunk);
m_TempChunk->bytesRead += fread(&version, 1, m_TempChunk->length - m_TempChunk->bytesRead, m_FilePointer);
m_CurrentChunk->bytesRead += m_TempChunk->bytesRead;
ProcessNextChunk(pModel, m_CurrentChunk);
break;
case MATERIAL:
pModel->numOfMaterials++;
pModel->pMaterials.push_back(newTexture);
ProcessNextMaterialChunk(pModel, m_CurrentChunk);
break;
case OBJECT:
pModel->numOfObjects++;
pModel->pObject.push_back(newObject);
memset(&(pModel->pObject[pModel->numOfObjects - 1]), 0, sizeof(t3DObject));
m_CurrentChunk->bytesRead += GetString(pModel->pObject[pModel->numOfObjects - 1].strName);
ProcessNextObjectChunk(pModel, &(pModel->pObject[pModel->numOfObjects - 1]), m_CurrentChunk);
break;
case EDITKEYFRAME:
m_CurrentChunk->bytesRead += fread(buffer, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer);
break;
default:
m_CurrentChunk->bytesRead += fread(buffer, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer);
break;
}
pPreviousChunk->bytesRead += m_CurrentChunk->bytesRead;
}
delete m_CurrentChunk;
m_CurrentChunk = pPreviousChunk;
}
//---------------------------------- PROCESS NEXT OBJECT CHUNK -----------------------------------
// This function handles all the information about the objects in the file
//---------------------------------- PROCESS NEXT OBJECT CHUNK -----------------------------------
void CLoad3DS::ProcessNextObjectChunk(t3DModel *pModel, t3DObject *pObject, tChunk *pPreviousChunk)
{
int buffer[50000] = {0};
m_CurrentChunk = new tChunk;
while (pPreviousChunk->bytesRead < pPreviousChunk->length)
{
ReadChunk(m_CurrentChunk);
switch (m_CurrentChunk->ID)
{
case OBJECT_MESH:
ProcessNextObjectChunk(pModel, pObject, m_CurrentChunk);
break;
case OBJECT_VERTICES:
ReadVertices(pObject, m_CurrentChunk);
break;
case OBJECT_FACES:
ReadVertexIndices(pObject, m_CurrentChunk);
break;
case OBJECT_MATERIAL:
ReadObjectMaterial(pModel, pObject, m_CurrentChunk);
break;
case OBJECT_UV:
ReadUVCoordinates(pObject, m_CurrentChunk);
break;
default:
m_CurrentChunk->bytesRead += fread(buffer, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer);
break;
}
pPreviousChunk->bytesRead += m_CurrentChunk->bytesRead;
}
delete m_CurrentChunk;
m_CurrentChunk = pPreviousChunk;
}
//---------------------------------- PROCESS NEXT MATERIAL CHUNK -----------------------------------
// This function handles all the information about the material (Texture)
//---------------------------------- PROCESS NEXT MATERIAL CHUNK -----------------------------------
void CLoad3DS::ProcessNextMaterialChunk(t3DModel *pModel, tChunk *pPreviousChunk)
{
int buffer[50000] = {0};
m_CurrentChunk = new tChunk;
while (pPreviousChunk->bytesRead < pPreviousChunk->length)
{
ReadChunk(m_CurrentChunk);
switch (pModel, m_CurrentChunk->ID)
{
case MATNAME:
m_CurrentChunk->bytesRead += fread(pModel->pMaterials[pModel->numOfMaterials - 1].strName, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer);
break;
case MATDIFFUSE:
ReadColorChunk(&(pModel->pMaterials[pModel->numOfMaterials - 1]), m_CurrentChunk);
break;
case MATMAP:
ProcessNextMaterialChunk(pModel, m_CurrentChunk);
break;
case MATMAPFILE:
m_CurrentChunk->bytesRead += fread(pModel->pMaterials[pModel->numOfMaterials - 1].strFile, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer);
break;
default:
m_CurrentChunk->bytesRead += fread(buffer, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer);
break;
}
pPreviousChunk->bytesRead += m_CurrentChunk->bytesRead;
}
delete m_CurrentChunk;
m_CurrentChunk = pPreviousChunk;
}
//---------------------------------- READ CHUNK ----------------------------------
// This function reads in a chunk ID and it's length in bytes
//---------------------------------- READ CHUNK -----------------------------------
void CLoad3DS::ReadChunk(tChunk *pChunk)
{
pChunk->bytesRead = fread(&pChunk->ID, 1, 2, m_FilePointer);
pChunk->bytesRead += fread(&pChunk->length, 1, 4, m_FilePointer);
}
//---------------------------------- GET STRING -----------------------------------
// This function reads in a string of characters
//---------------------------------- GET STRING -----------------------------------
int CLoad3DS::GetString(char *pBuffer)
{
int index = 0;
fread(pBuffer, 1, 1, m_FilePointer);
while (*(pBuffer + index++) != 0)
{
fread(pBuffer + index, 1, 1, m_FilePointer);
}
return strlen(pBuffer) + 1;
}
//---------------------------------- READ COLOR ----------------------------------
// This function reads in the RGB color data
//---------------------------------- READ COLOR -----------------------------------
void CLoad3DS::ReadColorChunk(tMaterialInfo *pMaterial, tChunk *pChunk)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本例导入3ds的类是从其它网络资源中找的源码 界面是用Qt做的,界面类中只用了一个QGLWidget类 注意:你需要找一个glaux库,在本例中也提供了这个库,lib和dll文件在Debug中,头文件直接放在Program Files\Microsoft SDKs\Windows\v6.0A\Include\gl这里面了,在本代码中只是简单的将gl这个文件夹考过来了,你可以自己重配一下路径
资源推荐
资源详情
资源评论
收起资源包目录
Import3DScsdn.zip (56个子文件)
Import3DScsdn
Import3DScsdn.sln 905B
Import3DScsdn.ncb 18.78MB
Import3DScsdn.suo 17KB
Debug
vc90.idb 1.96MB
GLAUX.LIB 1.13MB
Import3DScsdn.pdb 2.54MB
qrc_import3dscsdn.obj 8KB
vc90.pdb 1.82MB
mt.dep 67B
3DMath.obj 43KB
Import3DScsdn.ilk 1.05MB
GLShow3DS.obj 178KB
Import3DScsdn.exe.intermediate.manifest 621B
BuildLog.htm 9KB
3Ds.obj 245KB
3dsloader.obj 196KB
moc_GLShow3DS.obj 144KB
main.obj 141KB
Import3DScsdn.exe 182KB
glaux.dll 1.32MB
CJPEGFile.obj 10KB
Import3DScsdn
Import3DScsdn.vcproj 11KB
Import3DScsdn.vcproj.qinan-PC.qinan.user 1KB
CJPEGFile.h 337B
3Ds.cpp 14KB
data
model
FACE.3DS 77KB
model.3DS 10KB
zhishengji.3ds 32KB
texture
image2.jpg 6KB
image1.bmp 68KB
hind011.bmp 250KB
cwall02.jpg 18KB
FACE.BMP 900KB
Thumbs.db 16KB
include
jpeglib.h 46KB
jconfig.h 1KB
JMORECFG.H 13KB
define.h 985B
lib
jpeg.lib 248KB
CJPEGFile.cpp 3KB
GLShow3DS.ui 534B
Resources
import3dscsdn.qrc 73B
3DMath.cpp 3KB
GeneratedFiles
Release
qrc_import3dscsdn.cpp 739B
Debug
moc_GLShow3DS.cpp 2KB
ui_GLShow3DS.h 1KB
GLShow3DS.cpp 1KB
3DMath.h 925B
3dsloader.cpp 5KB
3dsloader.h 705B
main.cpp 203B
GLShow3DS.h 437B
gl
GL.h 67KB
GLAUX.H 12KB
GLU.h 18KB
3Ds.h 5KB
共 56 条
- 1
资源评论
- 开着拖拉机迎接春天2014-12-29在网上找了很久,资源很好,帮助很大,谢谢分享
- TIANQIBUCUO_h2014-07-01在VS 下 编的qt
- ahhrb02152014-04-03资源很好,帮助很大,谢谢分享
sky_calls00
- 粉丝: 3
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功