I'd like to have my models animated and with textures but at the moment I'm just having problems loading the model at all.
Any help would be appreciated.

Code: Select all
#include <GL/gl.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
#ifndef GAMEOBJECT_H_
#define GAMEOBJECT_H_
class GameObject
{
public:
GameObject();
GameObject(const char * filename);
virtual ~GameObject();
virtual void draw();
private:
struct md3_header
{
int ident;
int version;
char name[64];
int flags;
int num_frames;
int num_tags;
int num_surfaces;
int num_skins;
int ofs_frames;
int ofs_tags;
int ofs_surfaces;
int ofs_eof;
};
typedef float vec3[3];
struct md3_frame_header
{
vec3 min_bounds;
vec3 max_bounds;
vec3 local_origin;
float radius;
char name[16];
};
struct md3_tag_header
{
char name[64];
vec3 origin;
vec3 axis[3];
};
struct md3_surface_header
{
int ident;
char name[64];
int flags;
int num_frames;
int num_shaders;
int num_verts;
int num_triangles;
int ofs_triangles;
int ofs_shaders;
int ofs_st;
int ofs_xyznormal;
int ofs_end;
};
struct md3_shader_header
{
char name[64];
int shader_index;
};
struct md3_triangle_header
{
int indexes[3];
};
struct md3_texcoord_header
{
float st[2];
};
struct md3_vertex_header
{
short coord[3];
char normal[2];
};
vec3 * vertices;
int num_frames;
md3_frame_header * frames;
int num_vertices;
md3_vertex_header * verts;
int num_surfaces;
md3_surface_header * surfaces;
int num_triangles;
md3_triangle_header * triangles;
int num_tags;
md3_tag_header * tags;
int num_shaders;
md3_shader_header * shaders;
md3_texcoord_header * coords;
};
#endif
Code: Select all
#include "GameObject.h"
GameObject::GameObject()
{
}
GameObject::GameObject(const char * filename) {
ifstream md3file;
md3file.open(filename, ios::in|ios::binary);
//C Stuff
md3_header * md3header = (struct md3_header*)
malloc(sizeof(struct md3_header));
md3file.read((char *) md3header, sizeof (struct md3_header));
if ((md3header->ident != 860898377) || (md3header->version <15)) {
//Error!
cerr << "Error: bad version or identifier" << endl;
}
this->triangles = (md3_triangle_header *)
calloc(this->surfaces->num_triangles,sizeof(struct md3_surface_header));
md3file.seekg(this->surfaces->ofs_triangles, ios::beg);
md3file.read((char*)triangles,sizeof (struct md3_triangle_header)* surfaces->num_triangles);
this->surfaces= (md3_surface_header *)
calloc(md3header->num_surfaces,sizeof(struct md3_surface_header));
md3file.seekg(md3header->ofs_surfaces,ios::beg);
md3file.read((char *) this->surfaces, sizeof(struct md3_surface_header));
this->verts = (md3_vertex_header *)
calloc(this->surfaces->num_verts,sizeof(struct md3_vertex_header));
md3file.seekg(surfaces->ofs_xyznormal, ios::beg);
md3file.read((char*)verts,sizeof (struct md3_vertex_header)* surfaces->num_verts);
this->surfaces = (struct md3_surface_header *)
calloc (md3header->ofs_surfaces, sizeof(struct md3_surface_header));
md3file.seekg(md3header->ofs_surfaces,ios::beg);
md3file.read((char*)surfaces, sizeof (struct md3_surface_header)* md3header->num_surfaces);
this->frames = (struct md3_frame_header *)
calloc (md3header->num_frames, sizeof(struct md3_frame_header));
md3file.seekg(md3header->ofs_frames, ios::beg);
md3file.read((char *) frames, sizeof (struct md3_frame_header) * md3header->num_frames);
this->tags = (struct md3_tag_header *)
malloc (sizeof(struct md3_tag_header) * md3header->num_tags);
md3file.seekg(md3header->ofs_tags,ios::beg);
md3file.read((char*)tags,sizeof(struct md3_tag_header)* md3header->num_tags);
cout << "Ident: " << md3header->ident <<endl;
cout << "Version: " << md3header->version <<endl;
cout << "Name: " << md3header->name <<endl;
cout << "Flags: " << md3header->flags <<endl;
cout << "Num Frames: " << md3header->num_frames <<endl;
cout << "Num_tags: " << md3header->num_tags <<endl;
cout << "Num Surfaces: " << md3header->num_surfaces <<endl;
cout << "Num Skins: " << md3header->num_skins <<endl;
cout << "Ofs Frames: " << md3header->ofs_frames <<endl;
cout << "Ofs Tags: " << md3header->ofs_tags <<endl;
cout << "Ofs Surfaces: " << md3header->ofs_surfaces <<endl;
cout << "Ofs End: " << md3header->ofs_eof <<endl;
for(int i=0; i<md3header->num_frames; i++)
{
cout << "Min Bounds: " << frames[i].min_bounds << endl;
cout << "Max Bounds: " << frames[i].max_bounds << endl;
cout << "Local Origin: " << frames[i].local_origin << endl;
cout << "Radius: " << frames[i].radius << endl;
cout << "Name: " << frames[i].name << endl;
}
for (int m=0; m<surfaces->num_triangles; m++)
{
cout << "1. " << triangles[m].indexes[0] <<endl;
cout << "2. " << triangles[m].indexes[1] <<endl;
cout << "3. " << triangles[m].indexes[2] <<endl;
}
this->num_triangles = surfaces->num_triangles;
cout << surfaces->num_triangles << endl;
for (int j=0; j<md3header->num_surfaces; j++)
{
cout << "Ident: " << surfaces[j].ident << endl;
cout << "Name: " << surfaces[j].name <<endl;
cout << "Flags: " << surfaces[j].flags <<endl;
cout << "Num Frames: " << surfaces[j].num_frames <<endl;
cout << "Num Shaders: " << surfaces[j].num_shaders <<endl;
cout << "Num Vertices: " << surfaces[j].num_verts <<endl;
cout << "Num Triangles: " << surfaces[j].num_triangles <<endl;
cout << "Offset Triangles: " << surfaces[j].ofs_triangles <<endl;
cout << "Offset Shaders: " << surfaces[j].ofs_shaders <<endl;
cout << "Offset st: " << surfaces[j].ofs_st <<endl;
cout << "Offset xyznormal: " << surfaces[j].ofs_xyznormal << endl;
cout << "Offset End: " << surfaces[j].ofs_end <<endl;
}
}
GameObject::~GameObject()
{
//TODO: Some stuff
}
void GameObject::draw() {
cout << this->num_triangles << endl;
glBegin(GL_TRIANGLES);
for(int i=0; i<this->num_triangles;i++) {
glVertex3f(this->triangles[i].indexes[0] /100000000,this->triangles[i].indexes[1] /100000000,this->triangles[i].indexes[2] /1000000000);
}
glEnd();
}
Code: Select all
this->triangles = (md3_triangle_header *)
calloc(this->num_triangles,sizeof(struct md3_surface_header));
md3file.seekg(this->surfaces->ofs_triangles, ios::beg);
md3file.read((char *)triangles,sizeof (struct md3_triangle_header)* surfaces->num_triangles);
This is always the wrong answer when it comes to programming. :)Selphie142 wrote:I'm not too sure.