1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
// VlE 3D Model Format outline
#include <iostream>
#include <string>
#include <fstream>
#define VERSION 1
namespace binary
{
Uint32 Read32(std::ifstream &input)
{
Uint32 value;
Uint8 bytes[4];
input.read((char*)bytes, 4);
// Reconstruct the bytes into a 32 bit integer
value = (bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24))
return value;
}
}
enum a_humanStates
{
hIDLE,
hWALK,
hWALK
hCROUCH;
};
enum a_WeaponAnim
{
wIDLE,
wFIRE,
wRELOAD,
wBLOCK1,
wBLOCK2,
wBLOCK3,
wMELEE1,
wMELEE2,
wMELEE3;
;
};
struct Vertex
{
float x, y, z;
};
Vertex -operator(Vertex a, Vertex b)
{
float x, y, z;
x = a.x - b.x;
y = a.y - b.y;
z = a.z - b.z;
Vertex ret = {x, y, z};
return ret;
}
Vertex +operator(Vertex a, Vertex b)
{
float x, y, z;
x = a.x + b.x;
y = a.y + b.y;
z = a.z + b.z;
Vertex ret = {x, y, z};
return ret;
}
Vertex -operator(Vertex a, Vertex b)
{
float x, y, z;
x = a.x - b.x;
y = a.y - b.y;
z = a.z - b.z;
Vertex ret = {x, y, z};
return ret;
}
Vertex *operator(Vertex a, Vertex b)
{
float x, y, z;
x = a.x * b.x;
y = a.y * b.y;
z = a.z * b.z;
Vertex ret = {x, y, z};
return ret;
}
Vertex /operator(Vertex a, Vertex b)
{
float x, y, z;
x = a.x / b.x;
y = a.y / b.y;
z = a.z / b.z;
Vertex ret = {x, y, z};
return ret;
}
struct Triangle
{
Vertex points[3];
};
struct Mesh
{
Triangles* mesh; // Mesh. Each model has many, many meshes
// if they use vertex animation
~Mesh()
{
delete[] mesh;
}
};
struct Animation3D
{
Uint32 startFrame;
Uint32 endFrame;
};
class Model
{
public:
Model(char* location);
void Draw();
~Model();
void Animate(float seconds);
private:
char* name[16];
Mesh* model; // Dynamic array of meshes
Uint32 numAnims; // The number of animations the model contains.
Animation3D* anim; // Aniamtion array
Uint32 frames;
Uint32 currentFrame;
Uint32* numPoints;
Uint32* numTriangles;
bool vertexAnim;
};
Model::Model(char* location)
{
std::ifstream input(location);
Uint32 identifier;// Magic number. Should equal 1,243,853
Uint8 version; // Version of the model
currentFrame = 0;
// If the file loaded correctly
if(input)
{
identifier = binary::Read32(input);
if(identifier != 1243853)
{
std::cout << "Failure loading model: Not valid format.\n";
return;
}
input.read((char*)version, 1);
if(version != VERSION)
{
std::cout << "Failure loading model: Not compatible version.\n";
}
input.read((char*)name, 16);
input.read((char*)vertexAnim, 1);
frames = binary::Read32(input); // Load the number of frames there are
if(frames == 0)
{
std::cout << "Failure loading model: 0 Frames";
input.close();
return;
}
//numPoints = new Uint32[frames];
numTriangles = new Uint32[frames]; // We allocate an array of the number of triangles accordingly
for(unsigned int f = 0; f < frames; f++)
{
//numPoints[f] = binary::Read32(input);
numTriangles[f] = binary::Read32(input); // Load the number of triangles for each frame
}
// Allocate an array of meshes
model = new Mesh[frames];
// Loop through the number of frames, allocate enough memory for the models.
for(Uint32 m = 0; m < frames; m++)
{
model[m]->mesh = new Triangle[numTriangles[f]]; // First allocate triangle memory for the
// each individual mesh
/* -Remember, a mesh is nothing more than a bunch of triangles- */
// Read the positions of the triangle points
// in each individual mesh.
for(Uint32 t = 0; t < numTriangles[f]; t++)
{
// Get each point of the mesh
model[m]->mesh[t]->x = binary::Read32(input); // Read the x coord
model[m]->mesh[t]->y = binary::Read32(input); // Read the y coord
model[m]->mesh[t]->z = binary::Read32(input); // Read the z coord
}
}
// Read the number, then allocate memory for animations
Uint32 animations = binary::Read32(input);
anim = new Animation3D[animations];
// Loop through and read the start and and frames for
// all values
for(Uint32 a = 0; a < animations; a++)
{
// Load values
anim[a].startFrame = binary::Read32(input);
anim[a].endFrame = binary::Read32(input);
}
std::cout << "Model successfully loaded.\n";
}
}
Model::~Model()
{
delete[] Frames;
delete[] anim;
delete[] numPoints;
delete[] model;
};
|