www.pudn.com > DialogOpenGL.rar > OpenGL.h


#if !defined(AFX_OPENGL_H__38B5D1C8_2DFF_4A7D_9A99_3AC401C19D72__INCLUDED_) 
#define AFX_OPENGL_H__38B5D1C8_2DFF_4A7D_9A99_3AC401C19D72__INCLUDED_ 
 
#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 
// OpenGL.h : header file 
// 
 
#ifndef _3DS_H 
#define _3DS_H 
 
#include  
#include  
#include  
#include  
using namespace std; 
 
#define  BYTE unsigned char 
#define  WORD unsigned short  
#define  UINT unsigned int 
 
// 定义3ds的一些有使用到的ChunkID 
// 根Chunk,在每个文件的开始位置 
const WORD PRIMARY=0x4D4D; 
	const WORD PRIM_EDIT = 0x3D3D;					// ChunkID:3ds模型 
		const WORD EDIT_MAT = 0xAFFF;				// ChunkID:材质 
			const WORD MAT_NAME = 0xA000;			// ChunkID:材质名称 
			const WORD MAT_AMB  = 0xA010;			// ChunkID:材质环境光属性(没使用到) 
			const WORD MAT_DIF  = 0xA020;			// ChunkID:材质漫反射属性 
			const WORD MAT_SPE  = 0xA030;			// ChunkID:材质镜面反射属性(没使用到) 
			const WORD MAT_MAP =  0xA200;			// ChunkID:材质的纹理 
				const WORD MAP_NAME = 0xA300;		// ChunkID:纹理的名称 
 
		const WORD EDIT_OBJECT = 0x4000;			// ChunkID:3ds对象的面、点等信息 
			const WORD OBJECT_INFO = 0x4100;		// ChunkID:对象的主要信息 
				const WORD OBJECT_VERTEX = 0x4110;	// ChunkID:物体的顶点信息 
				const WORD OBJECT_FACET = 0x4120;	// ChunkID:物体的面信息 
					const WORD FACET_MAT = 0x4130;	// ChunkID:物体具有的材质 
					const WORD FACET_SMOOTH =0x4150;// ChunkID:面光滑信息(没使用到) 
				const WORD OBJECT_UV = 0x4140;		// ChunkID:纹理坐标信息 
				const WORD OBJECT_LOCAL = 0x4160; 
	const WORD PRIM_KEY=0xB000;						// ChunkID:所有的关键帧信息(没使用到) 
 
const WORD COLOR_BYTE=0x0011;						// ChunkID:颜色 
 
// 保存字符串 
typedef struct 
{ 
	char string[128]; 
} STRING; 
 
// 2维向量 
struct Vector2 
{ 
	float x, y; 
}; 
 
// 3维向量 
struct Vector3 
{ 
public: 
	// 向量初始化 
	Vector3() {} 
	Vector3(float X, float Y, float Z)	{ x = X; y = Y; z = Z; } 
	// 向量相加 
	Vector3 operator+(Vector3 vVector)	{ return Vector3(vVector.x + x, vVector.y + y, vVector.z + z); } 
	// 向量相加 
	Vector3 operator-(Vector3 vVector)	{ return Vector3(x - vVector.x, y - vVector.y, z - vVector.z); } 
	// 向量点乘 
	Vector3 operator*(float num)		{ return Vector3(x * num, y * num, z * num); } 
	Vector3 operator/(float num)		{ return Vector3(x / num, y / num, z / num); } 
 
	float x, y, z; 
}; 
 
// 保存Chunk信息 
typedef struct  
{ 
	WORD ID;										// Chunk的ID 
	UINT length;									// Chunk的长度 
} tChunk; 
 
// 保存面信息:顶点与纹理坐标的索引值。 
typedef struct 
{ 
	int vertIndex[3];								// 3个顶点的索引值 
	int	matID;										// 该面对应的材质ID 
} tFace; 
 
// 保存材质信息。 
typedef struct 
{ 
	STRING  matName;								// 材质的名称 
	STRING  mapName;								// 纹理的名称(bmp,jpg等的文件名) 
	BYTE	color[3];								// 材质颜色 
	UINT	texureId;								// 纹理的ID(指向载入的纹理) 
	bool	isTexMat;								// 该材质是不是包含有纹理 
} tMaterial; 
 
// 保存单个3ds对象 
typedef struct 
{ 
	int  numOfVerts;								// 该对象顶点的个数 
	int  numOfFaces;								// 该对象面的个数 
	int  numTexVertex;								// 该对象纹理坐标的个数 
	STRING	 objName;								// 保存对象的名称 
	Vector3  *pVerts;								// 保存顶点坐标 
	Vector3  *pNormals;								// 保存点的法线量 
	Vector2  *pTexVerts;							// 保存纹理坐标 
	tFace	 *pFaces;								// 保存面信息(顶点索引及面对应的材质) 
} t3DObject; 
 
// 保存整个3ds模型 
typedef struct 
{ 
	int numOfObjects;								// 3ds对象的个数 
	int numOfMaterials;								// 3ds材质的个数 
	vector pMaterials;					// 保存3ds材质 
	vector pObject;						// 保存3ds对象 
} t3DModel; 
 
// C3DSModel类 
class C3DSModel 
{ 
public: 
	C3DSModel(); 
	~C3DSModel(); 
	BOOL Load(char *);								// 载入3ds文件 
	void Render(void);								// 绘制3ds模型 
	void Release(void);								// 释放3ds模型资源 
	void SetFirstTextureSpot(float x,  float y, float z); 
	void SetSecondTextureSpot(float x,  float y, float z); 
	void AddTextureSpot();							//加入纹理点 
 
public: 
	int g_ViewMode; 
	bool addTexture; 
	float textureSpots[2000]; 
	int spotNum; 
	float fx, fy, fz; 
	float sx, sy, sz; 
	float width;									// Width of the brush 
 
private: 
	void ReadChunk(tChunk *);						// 读取3ds的一个Chunk信息(Chunk的ID及长度) 
 
	UINT ReadPrimary(UINT n);						// 读取3ds文件主要Chunk 
		UINT ReadEdit(UINT n);						// 读取3ds物体主编辑Chunk 
			UINT ReadObject(UINT n);						// 读取3ds对象 
				UINT ReadObjectInfo(t3DObject *,UINT n);	// 读取3ds对象信息 
					UINT ReadFacetInfo(t3DObject *,UINT n);	// 读取面信息 
		UINT ReadMaterial(UINT n);					// 读取材质 
			UINT ReadMatDif(tMaterial *, UINT n);	// 读取材质的漫反射属性 
			UINT ReadMatMap(tMaterial *, UINT n);	// 读取材质的纹理 
		UINT ReadKeyframe(UINT n);					// 读取帧信息(未使用) 
 
	BYTE ReadByte(void);							// 从文件中读取1个字节 
	WORD ReadWord(void);							// 从文件中读取2个字节 
	UINT ReadUint(void);							// 从文件中读取4个字节 
	float ReadFloat(void);							// 从文件中读取浮点数 
	UINT ReadString(STRING *);						// 从文件中读取字符串(返回字符串长度) 
 
	Vector3 Cross(Vector3, Vector3);				// 计算两向量的叉积 
	Vector3 Normalize(Vector3);						// 向量单位化 
	void ComputeNormals(void);						// 计算顶点法线量 
 
private: 
	FILE *m_FilePtr;								// 3ds文件指针 
	t3DModel m_3DModel;								// 保存3ds模型 
}; 
 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// 
 
#ifndef GL_TEXTURE_LOADER 
#define GL_TEXTURE_LOADER 
 
typedef struct													// 建立一个结构体 
{ 
	GLubyte	*imageData;											// 图像数据 (最高32bit) 
	GLuint	bpp;												// 每一象素的图像颜色深度 
	GLuint	width;												// 图像宽度 
	GLuint	height;												// 图像高度 
	GLuint	texID;												// 纹理ID 
} TextureTga; 
 
// 载入BMP,JPG,GIF等文件 
BOOL BuildTexture(char *szPathName, GLuint &texid); 
 
// 载入TGA文件 
BOOL BuildTexture(char *filename, TextureTga *texture); 
 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// COpenGL window 
 
class COpenGL : public CWnd 
{ 
// Construction 
public: 
	COpenGL(); 
 
// Attributes 
public: 
	C3DSModel model; 
	bool doRotate, firstSpot, secondSpot, mouseDown; 
	float dx, dy, dz; 
	double mouse_x, mouse_y; 
	float deep; 
 
// Operations 
public: 
 
// Overrides 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(COpenGL) 
	//}}AFX_VIRTUAL 
 
// Implementation 
public: 
	BOOL SetNormScreen(); 
	BOOL SetFullScreen(int width, int height, int depth); 
	virtual void RenderGLScene(); 
 	void Create(CRect rect, CWnd *parent); 
	void Load3dsModel(CString filePath); 
	void LoadTexture(CString filePath); 
	virtual ~COpenGL(); 
 
	// Generated message map functions 
protected: 
	CRect m_rect; 
	CWnd* m_parent; 
	BOOL m_bFullScreen; 
	DEVMODE m_DMsaved; 
	BOOL m_bInit; 
	int InitGL(); 
	void KillGLWindow(); 
	HDC m_hDC; 
	HGLRC m_hRC; 
	//{{AFX_MSG(COpenGL) 
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); 
	afx_msg void OnPaint(); 
	afx_msg void OnSize(UINT nType, int cx, int cy); 
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point); 
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point); 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
///////////////////////////////////////////////////////////////////////////// 
 
//{{AFX_INSERT_LOCATION}} 
// Microsoft Visual C++ will insert additional declarations immediately before the previous line. 
 
#endif // !defined(AFX_OPENGL_H__38B5D1C8_2DFF_4A7D_9A99_3AC401C19D72__INCLUDED_)