Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.cc @ 4080

Last change on this file since 4080 was 4080, checked in by patrick, 19 years ago

orxonox/branches/md2_loader: implemented md2 import function, smooth an ez

File size: 5.2 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
13*/
14
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
16
17#include "md2Model.h"
18
19#include <fstream>
20
21#include "list.h"
22
23
24using namespace std;
25
26/********************************************************************************
27 *   MD2MODEL                                                                   *
28 ********************************************************************************/
29
30/**
31   \brief standard constructor
32   
33        creates a new model
34*/
35MD2Model::MD2Model () 
36{
37   this->setClassName ("MD2Model");
38}
39
40
41/**
42   \brief standard deconstructor
43
44*/
45MD2Model::~MD2Model () 
46{
47  // delete what has to be deleted here
48}
49
50
51/********************************************************************************
52 *   MD2LOADER                                                                  *
53 ********************************************************************************/
54
55/**
56   \brief standard deconstructor
57        creates a new model loader
58*/
59MD2Loader::MD2Loader()
60{
61   this->setClassName ("MD2Loader");
62   /* initialize all data to initial state */
63   memset(&this->header, 0, sizeof(tMd2Header));
64   this->pSkins = NULL;
65   this->pTexCoords = NULL;
66   this->pTriangles = NULL;
67   this->pFrames = NULL;
68}
69
70/**
71   \brief standard deconstructor
72*/
73MD2Loader::~MD2Loader()
74{}
75 
76
77/**
78        \brief this is called by the client to open the .Md2 file, read it, then clean up
79        \param model to load in
80        \param file name to load
81        \param texture name to load
82*/
83bool MD2Loader::importMD2(t3DModel *pModel, char *fileName, char *textureName)
84{
85        this->pFile = fopen(fileName, "rb");
86        if( unlikely(!pFile)) 
87        {
88                PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n");
89                return false;
90        }
91        fread(&this->header, 1, sizeof(tMd2Header), pFile);
92        /* check for the header version: make sure its a md2 file :) */
93        if( likely(this->header.version != 8))
94        {
95                PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName);
96                return false;
97        }
98       
99        this->readMD2Data();
100        this->convertDataStructures(pModel);
101
102        if( likely((int)textureName))
103        {
104                tMaterialInfo* textureInfo = new tMaterialInfo;
105                strcpy(textureInfo->strFile, textureName);
106                /* since there is only one texture for a .Md2 file, the ID is always 0 */
107                textureInfo->texureId = 0;
108                textureInfo->uTile = 1;
109                /* we only have 1 material for a model */
110                pModel->numOfMaterials = 1;
111                pModel->materialList->add(textureInfo);
112        }
113       
114        this->cleanUp();
115        return true;
116}
117
118/**
119        \brief This function reads in all of the model's data, except the animation frames
120*/
121void MD2Loader::readMD2Data()
122{
123        unsigned char buffer[MD2_MAX_FRAMESIZE];
124        this->pSkins     = new tMd2Skin[this->header.numSkins];
125        this->pTexCoords = new tMd2TexCoord[this->header.numTexCoords];
126        this->pTriangles = new tMd2Face[this->header.numTriangles];
127        this->pFrames    = new tMd2Frame[this->header.numFrames];
128
129        /* we read the skins */
130        fseek(this->pFile, this->header.offsetSkins, SEEK_SET);
131        fread(this->pSkins, sizeof(tMd2Skin), this->header.numSkins, this->pFile);
132        /* read all vertex data */
133        fseek(this->pFile, this->header.offsetTexCoords, SEEK_SET);
134        fread(this->pTexCoords, sizeof(tMd2TexCoord), this->header.numTexCoords, this->pFile);
135        /* read face data for each triangle (normals) */
136        fseek(this->pFile, this->header.offsetTriangles, SEEK_SET);
137        fread(this->pTriangles, sizeof(tMd2Face), this->header.numTriangles, this->pFile);
138        /* reading all frame data */
139        fseek(this->pFile, this->header.offsetFrames, SEEK_SET);
140        for(int i = 0; i < this->header.numFrames; i++)
141        {
142                tMd2AliasFrame *pFrame = (tMd2AliasFrame *) buffer;
143                this->pFrames[i].pVertices = new tMd2Triangle [this->header.numVertices];
144
145                /* read the frame animation data */
146                fread(pFrame, 1, this->header.frameSize, this->pFile);
147                strcpy(this->pFrames[i].strName, pFrame->name);
148                tMd2Triangle *pVertices = this->pFrames[i].pVertices;
149                /*
150                   scale translations, store vertices: since id used a non-opengl xyz notation, we have to swap y and z
151                   and negate z axis
152                */
153                for(int j = 0; j < this->header.numVertices; j++)
154                {
155                        pVertices[j].vertex[0] = pFrame->aliasVertices[j].vertex[0] * pFrame->scale[0] + pFrame->translate[0];
156                        pVertices[j].vertex[2] = -1 * (pFrame->aliasVertices[j].vertex[1] * pFrame->scale[1] + pFrame->translate[1]);
157                        pVertices[j].vertex[1] = pFrame->aliasVertices[j].vertex[2] * pFrame->scale[2] + pFrame->translate[2];
158                }
159        }
160}
161
162/**
163        \brief this function fills in the animation list for each animation by name and frame
164        \param model
165*/
166void MD2Loader::parseAnimations(t3DModel *pModel)
167{}
168
169/**
170        \brief this function converts the .md2 structures to our own model and object structures: decompress
171        \param model
172*/
173void MD2Loader::convertDataStructures(t3DModel *pModel)
174{}
175
176
177/**
178        \brief this function conputes the normals of the model
179        \param model
180*/
181void MD2Loader::computeNormals(t3DModel *pModel)
182{}
183
184/**
185        \brief This function cleans up our allocated memory and closes the file
186*/
187void MD2Loader::cleanUp()
188{}
Note: See TracBrowser for help on using the repository browser.