//------------------------------------------------------------------------------
// Name: oglu_3ds_model_loading.cpp
// Author: Anudhyan Boral
// Last Modified: 02/27/06
// Description: This sample loads and displays a 3ds model
//
// Control Keys: F1 - Toggles wire frame mode
//
// Note: The 3ds object "box.3ds" is loaded in init();
// This sample has been coded on top of "ogl_initialization"
// This sample uses the stone texture from "ogl_dot3_bump_mapping"
// In order to load the 3ds object properly, all geometry should be
// attached together. Many other chunks have to be inserted into
// Load3dsObject() in order to load data such as lights,vertex colors
// multiple objects,material info etc.This code has been tested on
// 3ds objects from 3dsMax 4,6 and 7...
//------------------------------------------------------------------------------
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include "resource.h"
#include <io.h>
//-----------------------------------------------------------------------------
// CLASSES
//-----------------------------------------------------------------------------
//Basic Classes:
//Required for representing the loaded model
class vertex{
public:
float x,y,z;
};
//MapCoord - for storing texture mapping coords
class mapcoord{
public:
float u,v;
};
//The three ints for the polygon
//represent the no.s(or rank) of it's 3 vertices
class polygon{
public:
int a,b,c;
};
//Our object consists of a name,the vertex and polygons quantity,
//3000 vertices,3000 polygons, and 3000 mapping coords
//The value 3000 should be increased in order to load high-res models.
class object{
public:
char name[20];
int numVerts,numPolys;
vertex v[3000];
polygon p[3000];
mapcoord m[3000];
};
//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
HWND g_hWnd = NULL;
HDC g_hDC = NULL;
HGLRC g_hRC = NULL;
float g_fSpinX = 0.0f;
float g_fSpinY = 0.0f;
bool g_bRenderInWireFrame = false;
unsigned int g_textureID = 1;
object g_Obj3ds; //our object
//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow);
LRESULT CALLBACK WindowProc(HWND g_hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void init(void);
void render(void);
void shutDown(void);
void Load3dsObject(object *, char *);
void DrawObject(object *);
//-----------------------------------------------------------------------------
// Name: Load3dsObject (object *, char *)
// Desc: Loads a 3ds object given a filename
//-----------------------------------------------------------------------------
void Load3dsObject (object *obj, char *filename)
{
FILE *file; //Our file pointer
char temp; //Temporary char for reading name of object
short chunkID; //Stores ID of current chunk.
int chunkLength;
short useless;
//Open our file for reading(r) and in binary mode(b)- "rb"
file=fopen (filename, "rb");
int i;
//While current position is lesser than the total length
while (ftell(file) < filelength (fileno (file)))
{
fread (&chunkID, 2, 1, file);
fread (&chunkLength, 4, 1,file);
switch (chunkID)
{
case 0x4d4d: //Skip these chunks
break;
case 0x3d3d:
break;
case 0x4000: //Chunk containing name
for(i=0;i<20;i++)
{
fread (&temp, 1, 1, file);
obj->name[i]=temp;
if(temp == '\0')break;
}
break;
case 0x3f80: //Skip again
break;
case 0x4100:
break;
case 0x4110: //Chunk with num of vertices
//followed by their coordinates
fread (&obj->numVerts, sizeof (unsigned short), 1, file);
for (i=0; i<obj->numVerts; i++)
{
fread (&obj->v[i].x, sizeof(float), 1, file);
fread (&obj->v[i].y, sizeof(float), 1, file);
fread (&obj->v[i].z, sizeof(float), 1, file);
}
break;
case 0x4120: //Chunk with numPolys and
//the indices
fread (&obj->numPolys, sizeof (unsigned short), 1, file);
for (i=0; i<obj->numPolys; i++)
{
fread (&obj->p[i].a, sizeof (unsigned short), 1, file);
fread (&obj->p[i].b, sizeof (unsigned short), 1, file);
fread (&obj->p[i].c, sizeof (unsigned short), 1, file);
fread (&useless, sizeof (unsigned short), 1, file);
}
break;
case 0x4140: //Chunk with texture coords
fread (&useless, sizeof (unsigned short), 1, file);
for (i=0; i<obj->numVerts; i++)
{
fread (&obj->m[i].u, sizeof (float), 1, file);
fread (&obj->m[i].v, sizeof (float), 1, file);
}
break;
default:
fseek(file,chunkLength-6, SEEK_CUR);
}
}
fclose (file);
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
WNDCLASSEX winClass;
MSG uMsg;
memset(&uMsg,0,sizeof(uMsg));
winClass.lpszClassName = "MY_WINDOWS_CLASS";
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
winClass.lpfnWndProc = WindowProc;
winClass.hInstance = hInstance;
winClass.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_OPENGL_ICON);
winClass.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_OPENGL_ICON);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
if( !RegisterClassEx(&winClass) )
return E_FAIL;
g_hWnd = CreateWindowEx( NULL, "MY_WINDOWS_CLASS",
"OpenGL - Loading and Viewing 3ds Objects",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, 640, 480, NULL, NULL, hInstance, NULL );
if( g_hWnd == NULL )
return E_FAIL;
ShowWindow( g_hWnd, nCmdShow );
UpdateWindow( g_hWnd );
init();
while( uMsg.message != WM_QUIT )
{
if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &uMsg );
DispatchMessage( &uMsg );
}
else
render();
}
shutDown();
UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance );
return uMsg.wParam;
}
//-----------------------------------------------------------------------------
// Name: WindowProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT CALLBACK WindowProc( HWND g_hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam )
{
static POINT ptLastMousePosit;
static POINT ptCurrentMousePosit;
static bool bMousing;
switch( msg )
{
case WM_KEYDOWN:
{
switch( wParam )
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
case VK_F1:
g_bRenderInWireFrame=!g_bRenderInWireFrame;
break;
}
}
break;
case WM_LBUTTONDOWN:
{
ptLastMousePosit.x = ptCurrentMousePosit.x = LOWORD (lParam);
ptLastMousePosit.y = ptCurrentMousePosit.y = HIWORD (lParam);
bMousing = true;
}
3ogllu_3ds_mod.rar_3ds mod_MOD
版权申诉
159 浏览量
2022-09-21
17:14:23
上传
评论
收藏 72KB RAR 举报
御道御小黑
- 粉丝: 61
- 资源: 1万+
最新资源
- STC15单片机串口2使用程序例子
- 读取日志的excel生成周报 用python3开发weekplan-master.zip
- python 读取excel数据导入dbimport-data-master.zip
- K折交叉验证BP神经网络,多输入多输出BP神经网络(代码完整,数据齐全)
- B07训练原图.zip
- python-对Excel数据处理做可视化分析.zip
- 人工智能大作业-无人机图像目标检测的python源代码+文档说明.zip
- 基于GoogLeNet实现Cifar-10图像分类项目python源码(高分项目).zip
- 数据库 sql 面试题目及答案解析.docx
- 汽车常见 10 种传感器故障后的表现与解决措施.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈