mirror of
https://github.com/simtactics/niotso.git
synced 2025-03-22 02:52:18 +00:00
libvitaboy: Completely working model renderer. No animation as of yet.
This commit is contained in:
parent
860f67d522
commit
048a5ca2b2
3 changed files with 238 additions and 93 deletions
|
@ -22,30 +22,37 @@ bool keys[256] = {0}; // Array Used For The Keyboard Routine
|
||||||
bool active=TRUE; // Window Active Flag Set To TRUE By Default
|
bool active=TRUE; // Window Active Flag Set To TRUE By Default
|
||||||
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
|
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
|
||||||
|
|
||||||
float zoom = -4;
|
float zoom = -10;
|
||||||
struct CharacterPlacement_t {
|
struct CharacterPlacement_t {
|
||||||
Vertex_t Translation;
|
Vertex_t Translation;
|
||||||
Vertex_t Rotation;
|
Vertex_t Rotation;
|
||||||
};
|
};
|
||||||
|
|
||||||
CharacterPlacement_t Character = {{0,0,0}, {0,0,0}};
|
CharacterPlacement_t Character = {{0,-3,0}, {0,0,0}};
|
||||||
|
|
||||||
GLuint texture[2]; // Storage For One Texture ( NEW )
|
|
||||||
enum { Texture_Head, Texture_Body };
|
|
||||||
|
|
||||||
Skeleton_t Skeleton;
|
Skeleton_t Skeleton;
|
||||||
Mesh_t HeadMesh, BodyMesh;
|
|
||||||
|
|
||||||
bool DrawMesh = true;
|
unsigned TextureCount = 3;
|
||||||
|
GLuint texture[3]; // Storage For One Texture ( NEW )
|
||||||
|
enum { Texture_Body, Texture_Head, Texture_Hand };
|
||||||
|
const char* TexturePaths[] = {"body.jpg", "head.jpg", "hand.jpg"};
|
||||||
|
|
||||||
|
unsigned MeshCount = 4;
|
||||||
|
Mesh_t Meshes[4];
|
||||||
|
enum { Mesh_Body, Mesh_Head, Mesh_LHand, Mesh_RHand };
|
||||||
|
unsigned Mesh_UseTexture[] = { Texture_Body, Texture_Head, Texture_Hand, Texture_Hand };
|
||||||
|
const char* MeshActivate[] = {NULL, "HEAD", "L_HAND", "R_HAND"};
|
||||||
|
|
||||||
|
bool ShowMesh = true;
|
||||||
bool ShowSkeleton = true;
|
bool ShowSkeleton = true;
|
||||||
|
|
||||||
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
|
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
|
||||||
|
|
||||||
int LoadGLTextures() // Load Bitmaps And Convert To Textures
|
int LoadGLTextures() // Load Bitmaps And Convert To Textures
|
||||||
{
|
{
|
||||||
for(int i=0; i<2; i++){
|
for(int i=0; i<3; i++){
|
||||||
/* load an image file directly as a new OpenGL texture */
|
/* load an image file directly as a new OpenGL texture */
|
||||||
texture[i] = SOIL_load_OGL_texture((i==0) ? "head.jpg" : "body.jpg", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS);
|
texture[i] = SOIL_load_OGL_texture(TexturePaths[i], SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS);
|
||||||
if(texture[i] == 0)
|
if(texture[i] == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -85,11 +92,9 @@ int InitGL(void) // All Setup For OpenGL Goes Here
|
||||||
return FALSE; // If Texture Didn't Load Return FALSE
|
return FALSE; // If Texture Didn't Load Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
|
|
||||||
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
|
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
|
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
|
||||||
glClearDepth(1.0f); // Depth Buffer Setup
|
glClearDepth(1.0f); // Depth Buffer Setup
|
||||||
glPointSize(5.0);
|
|
||||||
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
|
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
glEnable(GL_RESCALE_NORMAL);
|
glEnable(GL_RESCALE_NORMAL);
|
||||||
|
@ -99,36 +104,105 @@ int InitGL(void) // All Setup For OpenGL Goes Here
|
||||||
return TRUE; // Initialization Went OK
|
return TRUE; // Initialization Went OK
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawBonesMesh(Bone_t& Bone){
|
void TransformVertices(Bone_t& Bone){
|
||||||
glTranslatef(Bone.Translation.x, Bone.Translation.y, Bone.Translation.z);
|
glTranslatef(Bone.Translation.x, Bone.Translation.y, Bone.Translation.z);
|
||||||
float RotationMatrix[16];
|
float Matrix[16];
|
||||||
FindQuaternionMatrix(RotationMatrix, &Bone.Rotation);
|
FindQuaternionMatrix(Matrix, &Bone.Rotation);
|
||||||
glMultMatrixf(RotationMatrix);
|
glMultMatrixf(Matrix);
|
||||||
|
|
||||||
|
unsigned MeshIndex = 0;
|
||||||
|
unsigned BoneIndex;
|
||||||
|
|
||||||
if(!strcmp(Bone.Name, "HEAD")){
|
for(unsigned i=1; i<MeshCount; i++){
|
||||||
glBindTexture(GL_TEXTURE_2D, texture[0]);
|
if(!strcmp(Bone.Name, MeshActivate[i])){
|
||||||
|
MeshIndex = i;
|
||||||
glVertexPointer(3, GL_FLOAT, offsetof(Vertex_t, y)-offsetof(Vertex_t, x)-sizeof(float), HeadMesh.VertexData);
|
break;
|
||||||
glNormalPointer(GL_FLOAT, offsetof(Vertex_t, y)-offsetof(Vertex_t, x)-sizeof(float), HeadMesh.VertexNorms);
|
}
|
||||||
glTexCoordPointer(2, GL_FLOAT, offsetof(TextureVertex_t, v)-offsetof(TextureVertex_t, u)-sizeof(float), HeadMesh.TextureVertexData);
|
}
|
||||||
glDrawElements(GL_TRIANGLES, HeadMesh.FaceCount*3, GL_UNSIGNED_INT, HeadMesh.FaceData);
|
Mesh_t& Mesh = Meshes[MeshIndex];
|
||||||
}else{
|
for(BoneIndex=0; BoneIndex<Mesh.BindingCount; BoneIndex++){
|
||||||
glBindTexture(GL_TEXTURE_2D, texture[1]);
|
if(!strcmp(Bone.Name, Mesh.BoneNames[Mesh.BoneBindings[BoneIndex].BoneIndex]))
|
||||||
|
break;
|
||||||
glVertexPointer(3, GL_FLOAT, offsetof(Vertex_t, y)-offsetof(Vertex_t, x)-sizeof(float), BodyMesh.VertexData);
|
}
|
||||||
glNormalPointer(GL_FLOAT, offsetof(Vertex_t, y)-offsetof(Vertex_t, x)-sizeof(float), BodyMesh.VertexNorms);
|
|
||||||
glTexCoordPointer(2, GL_FLOAT, offsetof(TextureVertex_t, v)-offsetof(TextureVertex_t, u)-sizeof(float), BodyMesh.TextureVertexData);
|
if(BoneIndex < Mesh.BindingCount){
|
||||||
glDrawElements(GL_TRIANGLES, BodyMesh.FaceCount*3, GL_UNSIGNED_INT, BodyMesh.FaceData);
|
for(unsigned i=0; i<Mesh.BoneBindings[BoneIndex].FixedVertexCount; i++){
|
||||||
|
glTranslatef(Mesh.VertexData[Mesh.BoneBindings[BoneIndex].FirstFixedVertex + i].x,
|
||||||
|
Mesh.VertexData[Mesh.BoneBindings[BoneIndex].FirstFixedVertex + i].y,
|
||||||
|
Mesh.VertexData[Mesh.BoneBindings[BoneIndex].FirstFixedVertex + i].z);
|
||||||
|
glGetFloatv(GL_MODELVIEW_MATRIX, Matrix);
|
||||||
|
Mesh.TransformedVertexData[Mesh.BoneBindings[BoneIndex].FirstFixedVertex + i].x = Matrix[12];
|
||||||
|
Mesh.TransformedVertexData[Mesh.BoneBindings[BoneIndex].FirstFixedVertex + i].y = Matrix[13];
|
||||||
|
Mesh.TransformedVertexData[Mesh.BoneBindings[BoneIndex].FirstFixedVertex + i].z = Matrix[14];
|
||||||
|
glTranslatef(-Mesh.VertexData[Mesh.BoneBindings[BoneIndex].FirstFixedVertex + i].x,
|
||||||
|
-Mesh.VertexData[Mesh.BoneBindings[BoneIndex].FirstFixedVertex + i].y,
|
||||||
|
-Mesh.VertexData[Mesh.BoneBindings[BoneIndex].FirstFixedVertex + i].z);
|
||||||
|
}
|
||||||
|
for(unsigned i=0; i<Mesh.BoneBindings[BoneIndex].BlendedVertexCount; i++){
|
||||||
|
glTranslatef(Mesh.VertexData[Mesh.FixedVertexCount + Mesh.BoneBindings[BoneIndex].FirstBlendedVertex + i].x,
|
||||||
|
Mesh.VertexData[Mesh.FixedVertexCount + Mesh.BoneBindings[BoneIndex].FirstBlendedVertex + i].y,
|
||||||
|
Mesh.VertexData[Mesh.FixedVertexCount + Mesh.BoneBindings[BoneIndex].FirstBlendedVertex + i].z);
|
||||||
|
glGetFloatv(GL_MODELVIEW_MATRIX, Matrix);
|
||||||
|
Mesh.TransformedVertexData[Mesh.FixedVertexCount + Mesh.BoneBindings[BoneIndex].FirstBlendedVertex + i].x = Matrix[12];
|
||||||
|
Mesh.TransformedVertexData[Mesh.FixedVertexCount + Mesh.BoneBindings[BoneIndex].FirstBlendedVertex + i].x = Matrix[13];
|
||||||
|
Mesh.TransformedVertexData[Mesh.FixedVertexCount + Mesh.BoneBindings[BoneIndex].FirstBlendedVertex + i].x = Matrix[14];
|
||||||
|
glTranslatef(-Mesh.VertexData[Mesh.FixedVertexCount + Mesh.BoneBindings[BoneIndex].FirstBlendedVertex + i].x,
|
||||||
|
-Mesh.VertexData[Mesh.FixedVertexCount + Mesh.BoneBindings[BoneIndex].FirstBlendedVertex + i].y,
|
||||||
|
-Mesh.VertexData[Mesh.FixedVertexCount + Mesh.BoneBindings[BoneIndex].FirstBlendedVertex + i].z);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(unsigned i=0; i<Bone.ChildrenCount; i++){
|
for(unsigned i=0; i<Bone.ChildrenCount; i++){
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
DrawBonesMesh(*Bone.Children[i]);
|
TransformVertices(*Bone.Children[i]);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BlendVertices(){
|
||||||
|
for(unsigned i=0; i<MeshCount; i++){
|
||||||
|
Mesh_t& Mesh = Meshes[i];
|
||||||
|
for(unsigned i=0; i<Mesh.BlendedVertexCount; i++){
|
||||||
|
Mesh.TransformedVertexData[Mesh.FixedVertexCount + i].x =
|
||||||
|
Mesh.BlendData[i].Weight * Mesh.TransformedVertexData[Mesh.FixedVertexCount + i].x +
|
||||||
|
(1-Mesh.BlendData[i].Weight) * Mesh.TransformedVertexData[Mesh.BlendData[i].OtherVertex].x;
|
||||||
|
Mesh.TransformedVertexData[Mesh.FixedVertexCount + i].y =
|
||||||
|
Mesh.BlendData[i].Weight * Mesh.TransformedVertexData[Mesh.FixedVertexCount + i].y +
|
||||||
|
(1-Mesh.BlendData[i].Weight) * Mesh.TransformedVertexData[Mesh.BlendData[i].OtherVertex].y;
|
||||||
|
Mesh.TransformedVertexData[Mesh.FixedVertexCount + i].z =
|
||||||
|
Mesh.BlendData[i].Weight * Mesh.TransformedVertexData[Mesh.FixedVertexCount + i].z +
|
||||||
|
(1-Mesh.BlendData[i].Weight) * Mesh.TransformedVertexData[Mesh.BlendData[i].OtherVertex].z;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DrawMeshes(){
|
||||||
|
glPointSize(2.0);
|
||||||
|
glColor3f(1.0, 1.0, 1.0);
|
||||||
|
glPushMatrix();
|
||||||
|
glLoadIdentity();
|
||||||
|
TransformVertices(Skeleton.Bones[0]);
|
||||||
|
glPopMatrix();
|
||||||
|
BlendVertices();
|
||||||
|
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
|
||||||
|
for(unsigned i=0; i<MeshCount; i++){
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture[Mesh_UseTexture[i]]);
|
||||||
|
glVertexPointer(3, GL_FLOAT, offsetof(Vertex_t, y)-offsetof(Vertex_t, x)-sizeof(float), Meshes[i].TransformedVertexData);
|
||||||
|
glTexCoordPointer(2, GL_FLOAT, offsetof(TextureVertex_t, v)-offsetof(TextureVertex_t, u)-sizeof(float), Meshes[i].TransformedTextureData);
|
||||||
|
glDrawElements(GL_TRIANGLES, Meshes[i].FaceCount*3, GL_UNSIGNED_INT, Meshes[i].FaceData);
|
||||||
|
}
|
||||||
|
|
||||||
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
glDisable(GL_TEXTURE_2D);
|
||||||
|
}
|
||||||
|
|
||||||
void DrawBonesSkeleton(Bone_t& Bone){
|
void DrawBonesSkeleton(Bone_t& Bone){
|
||||||
|
glPointSize(5.0);
|
||||||
glTranslatef(Bone.Translation.x, Bone.Translation.y, Bone.Translation.z);
|
glTranslatef(Bone.Translation.x, Bone.Translation.y, Bone.Translation.z);
|
||||||
float RotationMatrix[16];
|
float RotationMatrix[16];
|
||||||
FindQuaternionMatrix(RotationMatrix, &Bone.Rotation);
|
FindQuaternionMatrix(RotationMatrix, &Bone.Rotation);
|
||||||
|
@ -140,7 +214,7 @@ void DrawBonesSkeleton(Bone_t& Bone){
|
||||||
glColor3f(1.0, 1.0, 0.0);
|
glColor3f(1.0, 1.0, 0.0);
|
||||||
else
|
else
|
||||||
glColor3f(0.0, 1.0, 0.0);
|
glColor3f(0.0, 1.0, 0.0);
|
||||||
glBegin(GL_POINTS); glVertex2f(0, 0); glEnd();
|
glBegin(GL_POINTS); glVertex3f(0, 0, 0); glEnd();
|
||||||
|
|
||||||
for(unsigned i=0; i<Bone.ChildrenCount; i++){
|
for(unsigned i=0; i<Bone.ChildrenCount; i++){
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
|
@ -159,6 +233,10 @@ int DrawGLScene(void) // Here's Where We Do All The Drawing
|
||||||
if(keys[VK_RIGHT]){ if((Character.Rotation.y+=1.0f) >=360) Character.Rotation.y-=360; }
|
if(keys[VK_RIGHT]){ if((Character.Rotation.y+=1.0f) >=360) Character.Rotation.y-=360; }
|
||||||
if(keys['X']){ if((Character.Rotation.z-=1.0f) <=-360) Character.Rotation.z+=360; }
|
if(keys['X']){ if((Character.Rotation.z-=1.0f) <=-360) Character.Rotation.z+=360; }
|
||||||
if(keys['Z']){ if((Character.Rotation.z+=1.0f) >=360) Character.Rotation.z-=360; }
|
if(keys['Z']){ if((Character.Rotation.z+=1.0f) >=360) Character.Rotation.z-=360; }
|
||||||
|
if(keys['K']){ Character.Translation.y-=0.05f; }
|
||||||
|
if(keys['I']){ Character.Translation.y+=0.05f; }
|
||||||
|
if(keys['J']){ Character.Translation.x-=0.05f; }
|
||||||
|
if(keys['L']){ Character.Translation.x+=0.05f; }
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
|
||||||
|
|
||||||
|
@ -168,18 +246,11 @@ int DrawGLScene(void) // Here's Where We Do All The Drawing
|
||||||
glRotatef(Character.Rotation.y,0.0f,1.0f,0.0f);
|
glRotatef(Character.Rotation.y,0.0f,1.0f,0.0f);
|
||||||
glRotatef(Character.Rotation.z,0.0f,0.0f,1.0f);
|
glRotatef(Character.Rotation.z,0.0f,0.0f,1.0f);
|
||||||
|
|
||||||
if(DrawMesh){
|
if(ShowMesh){
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
glColor3f(1.0, 1.0, 1.0);
|
glColor3f(1.0, 1.0, 1.0);
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
|
||||||
glEnableClientState(GL_NORMAL_ARRAY);
|
|
||||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
||||||
|
|
||||||
DrawBonesMesh(Skeleton.Bones[0]);
|
DrawMeshes();
|
||||||
|
|
||||||
glDisableClientState(GL_VERTEX_ARRAY);
|
|
||||||
glDisableClientState(GL_NORMAL_ARRAY);
|
|
||||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ShowSkeleton){
|
if(ShowSkeleton){
|
||||||
|
@ -238,7 +309,7 @@ void KillGLWindow(void) // Properly Kill The Window
|
||||||
* bits - Number Of Bits To Use For Color (8/16/24/32) *
|
* bits - Number Of Bits To Use For Color (8/16/24/32) *
|
||||||
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
|
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
|
||||||
|
|
||||||
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
|
BOOL CreateGLWindow(const char * title, int width, int height, int bits, bool fullscreenflag)
|
||||||
{
|
{
|
||||||
GLuint PixelFormat; // Holds The Results After Searching For A Match
|
GLuint PixelFormat; // Holds The Results After Searching For A Match
|
||||||
WNDCLASS wc; // Windows Class Structure
|
WNDCLASS wc; // Windows Class Structure
|
||||||
|
@ -463,10 +534,10 @@ LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
|
||||||
return DefWindowProc(hWnd,uMsg,wParam,lParam);
|
return DefWindowProc(hWnd,uMsg,wParam,lParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
int WINAPI WinMain( HINSTANCE hInstance, // Instance
|
int WINAPI WinMain( HINSTANCE, // Instance
|
||||||
HINSTANCE hPrevInstance, // Previous Instance
|
HINSTANCE, // Previous Instance
|
||||||
LPSTR lpCmdLine, // Command Line Parameters
|
LPSTR, // Command Line Parameters
|
||||||
int nCmdShow) // Window Show State
|
int) // Window Show State
|
||||||
{
|
{
|
||||||
MSG msg; // Windows Message Structure
|
MSG msg; // Windows Message Structure
|
||||||
BOOL done=FALSE; // Bool Variable To Exit Loop
|
BOOL done=FALSE; // Bool Variable To Exit Loop
|
||||||
|
@ -506,31 +577,6 @@ int WINAPI WinMain( HINSTANCE hInstance, // Instance
|
||||||
VBFile.set(InData, FileSize);
|
VBFile.set(InData, FileSize);
|
||||||
ReadSkeleton(Skeleton);
|
ReadSkeleton(Skeleton);
|
||||||
free(InData);
|
free(InData);
|
||||||
|
|
||||||
hFile = CreateFile("head.mesh", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
|
||||||
if(hFile == INVALID_HANDLE_VALUE){
|
|
||||||
if(GetLastError() == ERROR_FILE_NOT_FOUND){
|
|
||||||
MessageBox(NULL, "head.mesh does not exist.", "Error", MB_OK);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
MessageBox(NULL, "head.mesh could not be opened for reading.", "Error", MB_OK);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
FileSize = GetFileSize(hFile, NULL);
|
|
||||||
InData = (uint8_t*) malloc(FileSize);
|
|
||||||
if(InData == NULL){
|
|
||||||
MessageBox(NULL, "Memory for head.mesh could not be allocated.", "Error", MB_OK);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if(!ReadFile(hFile, InData, FileSize, &bytestransferred, NULL) || bytestransferred != FileSize){
|
|
||||||
MessageBox(NULL, "The head.mesh could not be read.", "Error", MB_OK);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
CloseHandle(hFile);
|
|
||||||
|
|
||||||
VBFile.set(InData, FileSize);
|
|
||||||
ReadMesh(HeadMesh);
|
|
||||||
free(InData);
|
|
||||||
|
|
||||||
hFile = CreateFile("body.mesh", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
hFile = CreateFile("body.mesh", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
||||||
if(hFile == INVALID_HANDLE_VALUE){
|
if(hFile == INVALID_HANDLE_VALUE){
|
||||||
|
@ -548,13 +594,88 @@ int WINAPI WinMain( HINSTANCE hInstance, // Instance
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(!ReadFile(hFile, InData, FileSize, &bytestransferred, NULL) || bytestransferred != FileSize){
|
if(!ReadFile(hFile, InData, FileSize, &bytestransferred, NULL) || bytestransferred != FileSize){
|
||||||
MessageBox(NULL, "The body.mesh could not be read.", "Error", MB_OK);
|
MessageBox(NULL, "body.mesh could not be read.", "Error", MB_OK);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
CloseHandle(hFile);
|
CloseHandle(hFile);
|
||||||
|
|
||||||
VBFile.set(InData, FileSize);
|
VBFile.set(InData, FileSize);
|
||||||
ReadMesh(BodyMesh);
|
ReadMesh(Meshes[0]);
|
||||||
|
free(InData);
|
||||||
|
|
||||||
|
hFile = CreateFile("head.mesh", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
||||||
|
if(hFile == INVALID_HANDLE_VALUE){
|
||||||
|
if(GetLastError() == ERROR_FILE_NOT_FOUND){
|
||||||
|
MessageBox(NULL, "head.mesh does not exist.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
MessageBox(NULL, "head.mesh could not be opened for reading.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
FileSize = GetFileSize(hFile, NULL);
|
||||||
|
InData = (uint8_t*) malloc(FileSize);
|
||||||
|
if(InData == NULL){
|
||||||
|
MessageBox(NULL, "Memory for head.mesh could not be allocated.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(!ReadFile(hFile, InData, FileSize, &bytestransferred, NULL) || bytestransferred != FileSize){
|
||||||
|
MessageBox(NULL, "head.mesh could not be read.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
CloseHandle(hFile);
|
||||||
|
|
||||||
|
VBFile.set(InData, FileSize);
|
||||||
|
ReadMesh(Meshes[1]);
|
||||||
|
free(InData);
|
||||||
|
|
||||||
|
hFile = CreateFile("lhand.mesh", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
||||||
|
if(hFile == INVALID_HANDLE_VALUE){
|
||||||
|
if(GetLastError() == ERROR_FILE_NOT_FOUND){
|
||||||
|
MessageBox(NULL, "lhand.mesh does not exist.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
MessageBox(NULL, "lhand.mesh could not be opened for reading.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
FileSize = GetFileSize(hFile, NULL);
|
||||||
|
InData = (uint8_t*) malloc(FileSize);
|
||||||
|
if(InData == NULL){
|
||||||
|
MessageBox(NULL, "Memory for lhand.mesh could not be allocated.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(!ReadFile(hFile, InData, FileSize, &bytestransferred, NULL) || bytestransferred != FileSize){
|
||||||
|
MessageBox(NULL, "lhand.mesh could not be read.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
CloseHandle(hFile);
|
||||||
|
|
||||||
|
VBFile.set(InData, FileSize);
|
||||||
|
ReadMesh(Meshes[2]);
|
||||||
|
free(InData);
|
||||||
|
|
||||||
|
hFile = CreateFile("rhand.mesh", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
||||||
|
if(hFile == INVALID_HANDLE_VALUE){
|
||||||
|
if(GetLastError() == ERROR_FILE_NOT_FOUND){
|
||||||
|
MessageBox(NULL, "rhand.mesh does not exist.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
MessageBox(NULL, "rhand.mesh could not be opened for reading.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
FileSize = GetFileSize(hFile, NULL);
|
||||||
|
InData = (uint8_t*) malloc(FileSize);
|
||||||
|
if(InData == NULL){
|
||||||
|
MessageBox(NULL, "Memory for rhand.mesh could not be allocated.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(!ReadFile(hFile, InData, FileSize, &bytestransferred, NULL) || bytestransferred != FileSize){
|
||||||
|
MessageBox(NULL, "rhand.mesh could not be read.", "Error", MB_OK);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
CloseHandle(hFile);
|
||||||
|
|
||||||
|
VBFile.set(InData, FileSize);
|
||||||
|
ReadMesh(Meshes[3]);
|
||||||
free(InData);
|
free(InData);
|
||||||
|
|
||||||
// Create Our OpenGL Window
|
// Create Our OpenGL Window
|
||||||
|
|
|
@ -185,12 +185,17 @@ struct Face_t {
|
||||||
|
|
||||||
struct BoneBinding_t {
|
struct BoneBinding_t {
|
||||||
unsigned BoneIndex;
|
unsigned BoneIndex;
|
||||||
unsigned FirstVertex;
|
unsigned FirstFixedVertex;
|
||||||
unsigned VertexCount;
|
unsigned FixedVertexCount;
|
||||||
unsigned FirstBlendedVertex;
|
unsigned FirstBlendedVertex;
|
||||||
unsigned BlendedVertexCount;
|
unsigned BlendedVertexCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct BlendData_t {
|
||||||
|
float Weight;
|
||||||
|
unsigned OtherVertex;
|
||||||
|
};
|
||||||
|
|
||||||
struct Mesh_t {
|
struct Mesh_t {
|
||||||
uint32_t Version;
|
uint32_t Version;
|
||||||
uint32_t BoneCount;
|
uint32_t BoneCount;
|
||||||
|
@ -199,12 +204,16 @@ struct Mesh_t {
|
||||||
Face_t * FaceData;
|
Face_t * FaceData;
|
||||||
uint32_t BindingCount;
|
uint32_t BindingCount;
|
||||||
BoneBinding_t * BoneBindings;
|
BoneBinding_t * BoneBindings;
|
||||||
uint32_t TextureVertexCount;
|
uint32_t FixedVertexCount;
|
||||||
TextureVertex_t * TextureVertexData;
|
TextureVertex_t * TextureVertexData;
|
||||||
uint32_t BlendDataCount;
|
uint32_t BlendedVertexCount;
|
||||||
uint32_t VertexCount;
|
BlendData_t * BlendData;
|
||||||
|
uint32_t TotalVertexCount;
|
||||||
Vertex_t * VertexData;
|
Vertex_t * VertexData;
|
||||||
Vertex_t * VertexNorms;
|
Vertex_t * VertexNorms;
|
||||||
|
Vertex_t * TransformedVertexData;
|
||||||
|
Vertex_t * TransformedVertexNorms;
|
||||||
|
TextureVertex_t * TransformedTextureData;
|
||||||
};
|
};
|
||||||
|
|
||||||
void ReadMesh(Mesh_t& Mesh);
|
void ReadMesh(Mesh_t& Mesh);
|
||||||
|
|
|
@ -27,37 +27,52 @@ void ReadMesh(Mesh_t& Mesh){
|
||||||
printf("BindingCount: %u\n", Mesh.BindingCount);
|
printf("BindingCount: %u\n", Mesh.BindingCount);
|
||||||
for(unsigned i=0; i<Mesh.BindingCount; i++){
|
for(unsigned i=0; i<Mesh.BindingCount; i++){
|
||||||
Mesh.BoneBindings[i].BoneIndex = VBFile.readint32();
|
Mesh.BoneBindings[i].BoneIndex = VBFile.readint32();
|
||||||
Mesh.BoneBindings[i].FirstVertex = VBFile.readint32();
|
Mesh.BoneBindings[i].FirstFixedVertex = VBFile.readint32();
|
||||||
Mesh.BoneBindings[i].VertexCount = VBFile.readint32();
|
Mesh.BoneBindings[i].FixedVertexCount = VBFile.readint32();
|
||||||
Mesh.BoneBindings[i].FirstBlendedVertex = VBFile.readint32();
|
Mesh.BoneBindings[i].FirstBlendedVertex = VBFile.readint32();
|
||||||
Mesh.BoneBindings[i].BlendedVertexCount = VBFile.readint32();
|
Mesh.BoneBindings[i].BlendedVertexCount = VBFile.readint32();
|
||||||
}
|
}
|
||||||
|
|
||||||
Mesh.TextureVertexCount = VBFile.readint32();
|
Mesh.FixedVertexCount = VBFile.readint32();
|
||||||
printf("TextureVertexCount: %u\n", Mesh.TextureVertexCount);
|
printf("FixedVertexCount: %u\n", Mesh.FixedVertexCount);
|
||||||
Mesh.TextureVertexData = (TextureVertex_t*) malloc(Mesh.TextureVertexCount * sizeof(TextureVertex_t));
|
Mesh.TextureVertexData = (TextureVertex_t*) malloc(Mesh.FixedVertexCount * sizeof(TextureVertex_t));
|
||||||
for(unsigned i=0; i<Mesh.TextureVertexCount; i++){
|
for(unsigned i=0; i<Mesh.FixedVertexCount; i++){
|
||||||
Mesh.TextureVertexData[i].u = VBFile.readfloat();
|
Mesh.TextureVertexData[i].u = VBFile.readfloat();
|
||||||
Mesh.TextureVertexData[i].v = VBFile.readfloat();
|
Mesh.TextureVertexData[i].v = VBFile.readfloat();
|
||||||
}
|
}
|
||||||
|
|
||||||
Mesh.BlendDataCount = VBFile.readint32();
|
Mesh.BlendedVertexCount = VBFile.readint32();
|
||||||
printf("BlendDataCount: %u\n", Mesh.BlendDataCount);
|
printf("BlendedVertexCount: %u\n", Mesh.BlendedVertexCount);
|
||||||
for(unsigned i=0; i<Mesh.BlendDataCount; i++){
|
Mesh.BlendData = (BlendData_t*) malloc(Mesh.BlendedVertexCount * sizeof(BlendData_t));
|
||||||
VBFile.readint32();
|
for(unsigned i=0; i<Mesh.BlendedVertexCount; i++){
|
||||||
VBFile.readint32();
|
Mesh.BlendData[i].Weight = (float)VBFile.readint32()/0x8000;
|
||||||
|
Mesh.BlendData[i].OtherVertex = VBFile.readint32();
|
||||||
}
|
}
|
||||||
|
|
||||||
Mesh.VertexCount = VBFile.readint32();
|
Mesh.TotalVertexCount = VBFile.readint32();
|
||||||
printf("VertexCount: %u\n", Mesh.VertexCount);
|
printf("TotalVertexCount: %u\n", Mesh.TotalVertexCount);
|
||||||
Mesh.VertexData = (Vertex_t*) malloc(Mesh.VertexCount * sizeof(Vertex_t));
|
Mesh.VertexData = (Vertex_t*) malloc(Mesh.TotalVertexCount * sizeof(Vertex_t));
|
||||||
Mesh.VertexNorms = (Vertex_t*) malloc(Mesh.VertexCount * sizeof(Vertex_t));
|
Mesh.VertexNorms = (Vertex_t*) malloc(Mesh.TotalVertexCount * sizeof(Vertex_t));
|
||||||
for(unsigned i=0; i<Mesh.VertexCount; i++){
|
Mesh.TransformedVertexData = (Vertex_t*) malloc(Mesh.TotalVertexCount * sizeof(Vertex_t));
|
||||||
|
Mesh.TransformedVertexNorms = (Vertex_t*) malloc(Mesh.TotalVertexCount * sizeof(Vertex_t));
|
||||||
|
Mesh.TransformedTextureData = (TextureVertex_t*) malloc(Mesh.TotalVertexCount * sizeof(Vertex_t));
|
||||||
|
for(unsigned i=0; i<Mesh.TotalVertexCount; i++){
|
||||||
Mesh.VertexData[i].x = VBFile.readfloat();
|
Mesh.VertexData[i].x = VBFile.readfloat();
|
||||||
Mesh.VertexData[i].y = VBFile.readfloat();
|
Mesh.VertexData[i].y = VBFile.readfloat();
|
||||||
Mesh.VertexData[i].z = VBFile.readfloat();
|
Mesh.VertexData[i].z = VBFile.readfloat();
|
||||||
Mesh.VertexNorms[i].x = VBFile.readfloat();
|
Mesh.VertexNorms[i].x = VBFile.readfloat();
|
||||||
Mesh.VertexNorms[i].y = VBFile.readfloat();
|
Mesh.VertexNorms[i].y = VBFile.readfloat();
|
||||||
Mesh.VertexNorms[i].z = VBFile.readfloat();
|
Mesh.VertexNorms[i].z = VBFile.readfloat();
|
||||||
|
|
||||||
|
if(i<Mesh.FixedVertexCount){
|
||||||
|
//Fixed vertex
|
||||||
|
Mesh.TransformedTextureData[i].u = Mesh.TextureVertexData[i].u;
|
||||||
|
Mesh.TransformedTextureData[i].v = Mesh.TextureVertexData[i].v;
|
||||||
|
}else{
|
||||||
|
//Blended vertex; inherit
|
||||||
|
TextureVertex_t& Parent = Mesh.TextureVertexData[Mesh.BlendData[i-Mesh.FixedVertexCount].OtherVertex];
|
||||||
|
Mesh.TransformedTextureData[i].u = Parent.u;
|
||||||
|
Mesh.TransformedTextureData[i].v = Parent.v;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue