完整源代码:http://git.oschina.net/lyricc/LolView
源码与演示:源码出处
#ifndef _lol_h_
#define _lol_h_
namespace Lol
{
//00 type
typedef int int32;
typedef unsigned int uint32;
typedef short int16;
typedef unsigned short uint16;
typedef char int8;
typedef unsigned char uint8;
typedef float float32;
// 00 struct
struct LolVertex
{
float32 position[3]; // xyz position of vertex
int8 boneldx[4]; // Array of 4 bone number ID's
float32 weights[4]; // Corresponding bone weights
float32 normals[3]; // vertex normals
float32 texcoords[2]; // vertex UV coordinates
};
struct LolSknHeader
{
int32 magic;
int16 numObjects;
int16 matHeader;
//---------if matHeader = 1----------------
int32 numMaterials; //if matHeader = 1
//-------------------------
//-----block
// if matHeader == 1 && numMaerials
int8 nameMat[64]; // name of material
int32 statVertex; // First vertex belonging to this material
int32 numVertices; // Number of vertices in this material
int32 startIndex; // First index belonging to this material
int32 numIndices; // Number of indicies belonging to this material
//if matHeader == 0 the above block will be absent from the file.
//----count block -------------------------
// the values obtained here should match the sums of the numIndices and numVertices for the material blocks if present
int32 numOfIndices;
int32 numOfVertices;
int16 *indices; // Array of vertex indices
LolVertex*verteces; // Array of vertex values
};
// skl
// Bone ID's are implicit to their order in the file starting at 0.
// So the first bone has an ID of 0, second bone an ID of 1, etc.
struct LolSklBone
{
int8 name[32]; // name of bone
int32 parent; // parent bone id. -1 implies no parent
float32 scale; // scale of bone ? of armature?
float32 matrix[12]; // 3*4 affine bone matrix in row major format(first four values
// belong to first row ,2nd four to 2nd row ....
};
// ---header
// ---bone array
struct LolSklHeader
{
int8 version[8]; // file format version ==== extra 1 for '\0'
int32 numObjects; // num of objects
int32 skeletonHash; // unique id
int32 numElements; // num of bones
LolSklBone* bonesArray;//
};
// not finished - version 4
struct LolAnmHeader
{
int8 name[8];
int32 version;
int32 sizeTillEnd;
int32 magic3;
int32 magic4;
int32 magic5;
int32 numofbones;
int32 numofframes;
float32 playbackFPS;
int32 offset[9];
int32 datasize[3];
};
//----------------------------------------------------------------------------------------------------------
// interface
LolSknHeader* loadSkn(char* file);
void freeSkn(LolSknHeader*);
void drawSkn(LolSknHeader*sknHeader, unsigned int texid);
LolSklHeader*loadSkl(char*file);
void freeSkl(LolSklHeader*);
LolAnmHeader *loadAnm(char*file);
} // end namespace
#endif
#include "Lol.h"
#include "gl/glut.h"
#include "gl/GL.h"
#include
#include
using namespace std;
namespace Lol
{
LolSknHeader* loadSkn(char* file)
{
ifstream in(file,ios::binary);
if(!in.is_open()) return NULL;
LolSknHeader *header = (LolSknHeader*) malloc(sizeof(LolSknHeader));
memset(header,0,sizeof(LolSknHeader));
in.read((char*)&header->magic,4);
in.read((char*)&header->numObjects,2);
in.read((char*)&header->matHeader,2);
assert(header->matHeader == 1 && "matHeader == else not implimented!");
in.read((char*)&header->numMaterials,4);
in.read(header->nameMat,64);
in.read((char*)&header->statVertex,4);
in.read((char*)&header->numVertices,4);
in.read((char*)&header->startIndex,4);
in.read((char*)&header->numIndices,4);
in.read((char*)&header->numOfIndices,4);
in.read((char*)&header->numOfVertices,4);
header->indices = (int16*)malloc(sizeof(int16) * header->numOfIndices);
in.read((char*)header->indices,sizeof(int16)* header->numOfIndices);
header->verteces = (LolVertex*)malloc(sizeof(LolVertex)* header->numOfVertices);
in.read((char*)header->verteces,sizeof(LolVertex)*header->numOfVertices);
return header;
}
void freeSkn(LolSknHeader* headerPtr)
{
if(headerPtr == 0) return ;
if(headerPtr->indices) free(headerPtr->indices);
if(headerPtr->verteces) free(headerPtr->verteces);
free(headerPtr);
}
void drawSkn(LolSknHeader*sknHeader, unsigned int texid)
{
if(texid != 0)
{
glBindTexture(GL_TEXTURE_2D,texid);
}
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY); // 1.
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glNormalPointer(GL_FLOAT,sizeof(LolVertex),&(sknHeader->verteces[0].normals[0]));
glVertexPointer(3,GL_FLOAT,sizeof(LolVertex),sknHeader->verteces); // 2.
glTexCoordPointer(2,GL_FLOAT,sizeof(LolVertex),&sknHeader->verteces[0].texcoords[0]);
glDrawElements(GL_TRIANGLES,sknHeader->numOfIndices,GL_UNSIGNED_SHORT,sknHeader->indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
if(texid != 0)
{
glBindTexture(GL_TEXTURE_2D,0);
}
}
LolSklHeader*loadSkl(char*file)
{
ifstream in(file,ios::binary);
if(!in.is_open()) return NULL;
LolSklHeader*sklPtr = (LolSklHeader*)malloc(sizeof(LolSklHeader));
memset(sklPtr,0,sizeof(LolSklHeader));
in.read(sklPtr->version,8);
in.read((char*)&sklPtr->numObjects,4);
in.read((char*)&sklPtr->skeletonHash,4);
in.read((char*)&sklPtr->numElements,4);
sklPtr->bonesArray = (LolSklBone*)malloc(sizeof(LolSklBone)*sklPtr->numElements);
in.read((char*)sklPtr->bonesArray,sizeof(LolSklBone)*sklPtr->numElements);
return sklPtr;
}
void freeSkl(LolSklHeader*sklHeadePtr)
{
if(!sklHeadePtr)return ;
if(sklHeadePtr->bonesArray)
free(sklHeadePtr->bonesArray);
free(sklHeadePtr);
}
LolAnmHeader *loadAnm(char*file)
{
ifstream in(file,ios::binary);
if(!in.is_open()) return NULL;
LolAnmHeader*anmPtr = (LolAnmHeader*)malloc(sizeof(LolAnmHeader));
memset(anmPtr,0,sizeof(LolAnmHeader));
in.read((char*)anmPtr,sizeof(LolAnmHeader) - sizeof(anmPtr->datasize));
/*
data.data_size4=data.start_offset5-data.start_offset4-- 12720 bytes
data.data_size5=data.start_offset6-data.start_offset5-- 29296 bytes
data.data_size6=file_size-data.start_offset6-- 36900 bytes
*/
anmPtr->datasize[0] = anmPtr->offset[4]- anmPtr->offset[3];
anmPtr->datasize[1] = anmPtr->offset[5]- anmPtr->offset[4];
anmPtr->datasize[2] = anmPtr->sizeTillEnd - anmPtr->offset[5];
int size = sizeof (LolAnmHeader);
return anmPtr;
}
}