opengl导入3DS文件(带纹理)之填坑

本文介绍如何在OpenGL中导入3DS模型并处理纹理,包括使用CLoad3DS工具,注意模型制作与纹理资源路径设置,以及解决可能出现的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、3ds导入工具

CLoad3DS.h和CLoad3DS.cpp (见文末)

二、使用方法

1、在你工程中加入头文件

      #include "CLoad3DS.h"  

2、在定义全局变量的地方加入以下代码

CLoad3DS *gothicLoader=new(CLoad3DS);  
t3DModel gothicModel;  

float gothicTrans[10] = {   
    0, 0 , -30 ,     //表示在世界矩阵的位置  
       0.2 , 0.2 , 0.2 ,      //表示xyz放大倍数  
        0 , 0 , 0 , 0  //表示旋转  
};

这里要注意一个问题,若你需要封装代码,想在内里面定义t3DModel gothicModel;  一定要记得初始化其gothicModel.numOfMaterials =0和gothicModel.numOfObject=0。否则程序会报错。上面没有初始化,是因为是全局变量,C++里,全局变量是默认初始化的,也就是其成员默认初始化为0.


3、在opengl初始化的地方加入以下代码

gothicLoader->Import3DS(&gothicModel, "Data/3ds/GUTEMB_L.3DS");//导入模型,第二个参数是3ds文件的路径,

这里使用的是相对路径,你也可以使用绝对路径

4、在opengl绘制函数里加入如下代码

changeObject( gothicTrans );  
drawModel(gothicModel,true,false);  

三、3ds模型制作注意事项

其实没什么注意事项,唯一要注意的是,一定要有贴图的原素材,如果用的3dmax的系统贴图素材,需要去安装目录

找出原始图片,共后面opengl导入时使用。

四、3ds导入工具解析

在CLoad3DS.h中存在一个宏    #define PICPATH "\\Data\\pic\\"     //定义了纹理资源的相对地址,即相对工程目录

的。结合下面三句代码,共同决定了从何处加载到纹理资源。

GetCurrentDirectory(MAX_PATH, szPath);              // Get Our Working Directory  获得工程的目录
strcat(szPath, PICPATH);                                        // Append "\" After The Working Directory
strcat(szPath, szPathName);                                  // Append The PathName

因此,若你使用的纹理资源不放在当前工程目录下的Data\pic文件夹下,会导致最终显示模型丢失纹理。此外,你也

可以根据你纹理资源位置,调整这两处的代码,也可以实现纹理资源的加载。

3ds导入工具模板

#ifndef _CLoad3DS_h_
#define _CLoad3DS_h_



#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include                   

#include               
#include   
#include 
#include 


//初始化OpenGL环境

#include 
#include 
//#include 

#include 

#pragma   comment(lib,"opengl32.lib")
#pragma	  comment(lib,"glu32.lib")
//#pragma   comment(lib,"glaux.lib")


#define PICPATH "\\Data\\pic\\"     //纹理资源的地址



// 基本块(Primary Chunk),位于文件的开始
#define PRIMARY 0x4D4D

// 主块(Main Chunks)
#define OBJECTINFO 0x3D3D        // 网格对象的版本号
#define VERSION 0x0002        // .3ds文件的版本
#define EDITKEYFRAME 0xB000        // 所有关键帧信息的头部

// 对象的次级定义(包括对象的材质和对象)
#define MATERIAL   0xAFFF        // 保存纹理信息
#define OBJECT     0x4000        // 保存对象的面、顶点等信息

// 材质的次级定义
#define MATNAME 0xA000        // 保存材质名称
#define MATDIFFUSE 0xA020        // 对象/材质的颜色
#define MATMAP 0xA200        // 新材质的头部
#define MATMAPFILE 0xA300        // 保存纹理的文件名

#define OBJECT_MESH 0x4100        // 新的网格对象

// OBJECT_MESH的次级定义
#define OBJECT_VERTICES 0x4110      // 对象顶点
#define OBJECT_FACES    0x4120      // 对象的面
#define OBJECT_MATERIAL    0x4130      // 对象的材质
#define OBJECT_UV      0x4140      // 对象的UV纹理坐标


// 下面的宏定义计算一个矢量的长度
#define Mag(Normal) (sqrt(Normal.x*Normal.x + Normal.y*Normal.y + Normal.z*Normal.z))


#define MAX_TEXTURES 100                // 最大的纹理数目



using namespace std;
class NBVector3
{
public:
	NBVector3() {}
	NBVector3(float X, float Y, float Z) 
	{ 
		x = X; y = Y; z = Z;
	}
	inline NBVector3 operator+(NBVector3 vVector)
	{
		return NBVector3(vVector.x + x, vVector.y + y, vVector.z + z);
	}
	inline NBVector3 operator-(NBVector3 vVector)
	{
		return NBVector3(x - vVector.x, y - vVector.y, z - vVector.z);
	}
	inline NBVector3 operator-()
	{
		return NBVector3(-x, -y, -z);
	}
	inline NBVector3 operator*(float num)
	{
		return NBVector3(x * num, y * num, z * num);
	}
	inline NBVector3 operator/(float num)
	{
		return NBVector3(x / num, y / num, z / num);
	}

	inline NBVector3 operator^(const NBVector3 &rhs) const
	{
		return NBVector3(y * rhs.z - rhs.y * z, rhs.x * z - x * rhs.z, x * rhs.y - rhs.x * y);
	}

	union
	{
		struct
		{
			float x;
			float y;
			float z;
		};
		float v[3];
	};				
};

// 定义2D点类,用于保存模型的UV纹理坐标
class CVector2 
{
public:
	float x, y;
};

// 面的结构定义
struct tFace
{
	int vertIndex[3];      // 顶点索引
	int coordIndex[3];      // 纹理坐标索引
};

// 材质信息结构体
s
AppWizard has created this 3DSLoader application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your 3DSLoader application. MySDOpenGL.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. 3DSLoader.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CMy3DSLoaderApp application class. 3DSLoader.cpp This is the main application source file that contains the application class CMy3DSLoaderApp. 3DSLoader.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. 3DSLoader.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. res\3DSLoader.ico This is an icon file, which is used as the application's icon. This icon is included by the main resource file 3DSLoader.rc. res\3DSLoader.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file. ///////////////////////////////////////////////////////////////////////////// For the main frame window: MainFrm.h, MainFrm.cpp These files contain the frame class CMainFrame, which is derived from CFrameWnd and controls all SDI frame
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值