Loading MD3 and now we need to place models properly

This commit is contained in:
Vladislav Khorev 2025-02-23 21:24:15 +03:00
parent e130c2ba7f
commit 329f1b1663
9 changed files with 1298 additions and 403 deletions

43
AnimatedModel.h Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include "Renderer.h"
#include "TextureManager.h"
namespace ZL
{
struct MeshGroup
{
std::vector<std::shared_ptr<Texture>> textures;
std::vector<VertexDataStruct> meshes;
std::vector<VertexRenderStruct> renderMeshes;
};
struct AnimatedModel
{
std::vector<MeshGroup> parts;
void RefreshRenderMeshes()
{
for (int i = 0; i < parts.size(); i++)
{
parts[i].renderMeshes.resize(parts[i].meshes.size());
for (int j = 0; j < parts[i].meshes.size(); j++)
{
parts[i].renderMeshes[j].AssignFrom(parts[i].meshes[j]);
parts[i].renderMeshes[j].RefreshVBO();
}
}
}
};
}

View File

@ -1,4 +1,5 @@
#include "Game.h"
#include "AnimatedModel.h"
namespace ZL
{
@ -26,7 +27,7 @@ namespace ZL
std::shared_ptr<Texture> pipeTexturePtr;
std::shared_ptr<Texture> gameOverTexturePtr;
std::shared_ptr<Texture> testObjTexturePtr;
std::shared_ptr<Texture> md3TexturePtr;
//std::shared_ptr<Texture> md3TexturePtr;
VertexRenderStruct birdMesh;
@ -42,8 +43,8 @@ namespace ZL
VertexDataStruct testObjMesh;
VertexRenderStruct testObjMeshMutable;
std::vector<VertexDataStruct> testmd3;
std::vector<VertexRenderStruct> testmd3mutable;
ZL::AnimatedModel testmd3;
//std::vector<VertexRenderStruct> testmd3mutable;
}

9
Game.h
View File

@ -4,6 +4,7 @@
#include "Physics.h"
#include "TextureManager.h"
#include "Renderer.h"
#include "AnimatedModel.h"
#include <memory>
namespace ZL
@ -98,7 +99,7 @@ namespace ZL
extern std::shared_ptr<Texture> pipeTexturePtr;
extern std::shared_ptr<Texture> gameOverTexturePtr;
extern std::shared_ptr<Texture> testObjTexturePtr;
extern std::shared_ptr<Texture> md3TexturePtr;
//extern std::shared_ptr<Texture> md3TexturePtr;
extern VertexRenderStruct birdMesh;
@ -113,7 +114,9 @@ namespace ZL
extern VertexDataStruct testObjMesh;
extern VertexRenderStruct testObjMeshMutable;
extern std::vector<VertexDataStruct> testmd3;
extern std::vector<VertexRenderStruct> testmd3mutable;
extern ZL::AnimatedModel testmd3;
//extern std::vector<VertexDataStruct> testmd3;
//extern std::vector<VertexRenderStruct> testmd3mutable;
}
}

View File

@ -158,6 +158,7 @@
<ClCompile Include="Utils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AnimatedModel.h" />
<ClInclude Include="Game.h" />
<ClInclude Include="Math.h" />
<ClInclude Include="md3test.h" />

View File

@ -80,5 +80,8 @@
<ClInclude Include="md3test.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="AnimatedModel.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -13,8 +13,9 @@
#include <fstream>
#include "Game.h"
#include "AnimatedModel.h"
std::vector<ZL::VertexDataStruct> testLoadModel();
ZL::AnimatedModel testLoadModel();
namespace ZL
{
@ -140,16 +141,28 @@ namespace ZL
renderer.EnableVertexAttribArray(vPositionName);
renderer.EnableVertexAttribArray(vTexCoordName);
renderer.PushPerspectiveProjectionMatrix(1.0 / 6.0, static_cast<float>(Env::width) / static_cast<float>(Env::height), 1, 1000);
renderer.PushPerspectiveProjectionMatrix(1.0 / 6.0, static_cast<float>(Env::width) / static_cast<float>(Env::height), 100, 100000);
renderer.PushMatrix();
renderer.LoadIdentity();
renderer.TranslateMatrix({ 0,0, -300 });
renderer.TranslateMatrix({ 0,0, -30000 });
renderer.RotateMatrix(QuatFromRotateAroundX(-M_PI / 3.0));
for (int i = 0; i < GameObjects::testmd3.parts.size(); i++)
{
for (int j = 0; j < GameObjects::testmd3.parts[i].renderMeshes.size(); j++)
{
glBindTexture(GL_TEXTURE_2D, GameObjects::testmd3.parts[i].textures[0]->getTexID());
renderer.DrawVertexRenderStruct(GameObjects::testmd3.parts[i].renderMeshes[j]);
}
}
/*
GameObjects::testObjMeshMutable.AssignFrom(GameObjects::testObjMesh);
GameObjects::testObjMeshMutable.data.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundZ(gs.rotateTimer * M_PI / 3.0)));
@ -162,10 +175,11 @@ namespace ZL
//glBindTexture(GL_TEXTURE_2D, GameObjects::testObjTexturePtr->getTexID());
//renderer.DrawVertexRenderStruct(GameObjects::testObjMeshMutable);
/*
glBindTexture(GL_TEXTURE_2D, GameObjects::md3TexturePtr->getTexID());
renderer.DrawVertexRenderStruct(GameObjects::testmd3mutable[0]);
renderer.DrawVertexRenderStruct(GameObjects::testmd3mutable[1]);
*/
renderer.PopMatrix();
renderer.PopProjectionMatrix();
@ -260,6 +274,7 @@ namespace ZL
GameObjects::testmd3 = testLoadModel();
/*
GameObjects::testmd3mutable.resize(GameObjects::testmd3.size());
for (int i = 0; i < GameObjects::testmd3.size(); i++)
@ -268,7 +283,7 @@ namespace ZL
GameObjects::testmd3mutable[i].data = GameObjects::testmd3[i];
GameObjects::testmd3mutable[i].RefreshVBO();
}
}*/
//Load shaders:
renderer.shaderManager.AddShaderFromFiles("default", "./default.vertex", "./default.fragment");
@ -281,7 +296,7 @@ namespace ZL
GameObjects::gameOverTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp32("./game_over.bmp32"));
GameObjects::testObjTexturePtr = std::make_shared<Texture>(CreateTextureDataFromPng("./chair_01_Base_Color.png"));
GameObjects::md3TexturePtr = std::make_shared<Texture>(CreateTextureDataFromPng("./model/sarge/band.png"));
//GameObjects::md3TexturePtr = std::make_shared<Texture>(CreateTextureDataFromPng("./model/sarge/band.png"));
CheckGlError();

File diff suppressed because it is too large Load Diff

195
md3test.h
View File

@ -1,113 +1,198 @@
#pragma once
#define SZ_MD3_FILE "model/sarge/upper.md3"
#define SZ_MD3_LOWER_FILE "model\\sarge\\lower.md3"
#define SZ_MD3_LOWER_SKIN_FILE "model\\sarge\\lower_default.skin"
#define SZ_MD3_UPPER_FILE "model\\sarge\\upper.md3"
#define SZ_MD3_UPPER_SKIN_FILE "model\\sarge\\upper_default.skin"
#define SZ_MD3_HEAD_FILE "model\\sarge\\head.md3"
#define SZ_MD3_HEAD_SKIN_FILE "model\\sarge\\head_default.skin"
#define SZ_MD3_TEXTURE_PATH "model\\sarge\\"
#define SZ_MD3_ANIM_FILE "model\\sarge\\animation.cfg"
#define MAX_FILENAME_LENGTH 256
#define MAX_TEXTURES 20
//---------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h> //sprintf(...)
#include <stdio.h> //sprintf(...)
#include <string.h>
//---------------------------------------------------------------------------
typedef unsigned int uint32;
typedef int int32;
typedef unsigned short int uint16;
typedef short int int16;
typedef float float32;
typedef unsigned int uint32;
typedef int int32;
typedef unsigned short int uint16;
typedef short int int16;
typedef float float32;
struct stMD3Header
{
char ID[4]; // ID of the file is always "IDP3"
int32 Version; // Version number, usually 15
char Filename[68]; // Filename, sometimes left blank
int32 numBoneFrames; // Number of BoneFrames
int32 numTags; // Number of 'tags' per BoneFrame
int32 numMeshes; // Number of Meshes/Skins in MaxSkin
int32 numMaxSkins; // Maximum number of unique skins
int32 ofsFrames; // Always equal to the length this header
int32 ofsTagStart; // Starting position of tag structures
int32 ofMeshSurfaces; // Ending position of tag structure
int32 ofEndOfFile; // Size of file
char ID[4]; // ID of the file is always "IDP3"
int32 Version; // Version number, usually 15
char Filename[68]; // Filename, sometimes left blank
int32 numBoneFrames; // Number of BoneFrames
int32 numTags; // Number of 'tags' per BoneFrame
int32 numMeshes; // Number of Meshes/Skins in MaxSkin
int32 numMaxSkins; // Maximum number of unique skins
int32 ofsFrames; // Always equal to the length this header
int32 ofsTagStart; // Starting position of tag structures
int32 ofMeshSurfaces; // Ending position of tag structure
int32 ofEndOfFile; // Size of file
};
struct stBoneFrame
{
float32 mins[3];
float32 maxs[3];
float32 Position[3];
float32 Scale;
char Creator[16];
float32 mins[3];
float32 maxs[3];
float32 Position[3];
float32 Scale;
char Creator[16];
};
struct stAnim
{
int32 FirstFrame;
int32 numFrames;
int32 LoopingFrames;
int32 FPS;
int32 FirstFrame;
int32 numFrames;
int32 LoopingFrames;
int32 FPS;
};
struct stSkin
{
char Name[64];
int32 index;
char Name[64];
int32 index;
};
struct stTag
{
char Name[64];
float32 Position[3];
float32 Rotation[3][3];
char Name[64];
float32 Position[3];
float32 Rotation[3][3];
};
struct stTriangle
{
int32 Vertex[3];
int32 Vertex[3];
};
struct stTexCoord
{
float32 Coord[2];
float32 Coord[2];
};
struct stVertex // = Record
{
int16 Vertex[3];
unsigned char Normal[2];
int16 Vertex[3];
unsigned char Normal[2];
};
struct stMeshHeader
{
char ID[4];
char Name[64];
int32 flags;
int32 numMeshFrames;
int32 numSkins;
int32 numVertexes;
int32 numTriangles;
int32 ofsTriangles;
int32 ofsSkins;
int32 ofsTexVector;
int32 ofsVertex;
int32 ofsEndMeshSize;
char ID[4];
char Name[64];
int32 flags;
int32 numMeshFrames;
int32 numSkins;
int32 numVertexes;
int32 numTriangles;
int32 ofsTriangles;
int32 ofsSkins;
int32 ofsTexVector;
int32 ofsVertex;
int32 ofsEndMeshSize;
};
struct stMesh
{
stMeshHeader MeshHeader;
stSkin* pSkins;
stTriangle* pTriangle;
stTexCoord* pTexCoord;
stVertex* pVertex;
stMeshHeader MeshHeader;
stSkin* pSkins;
stTriangle* pTriangle;
stTexCoord* pTexCoord;
stVertex* pVertex;
int texID;
};
struct stM3DModel
{
char m_md3FileName[MAX_FILENAME_LENGTH];
stMD3Header m_md3Header;
stBoneFrame* m_pBoneFrame;
stTag* m_pTags;
stMesh* m_pMeshes;
int m_FPS;
int m_startFrame;
int m_endFrame;
int m_nextFrame;
int m_anim;
float m_poll;
//int m_animLower;
//int m_animUpper;
stM3DModel* m_pLinks[10];
int m_currentframe;
};
//IDirect3DTexture8* pTextures[MAX_TEXTURES];
int g_NumLoadedTextures = 0;
//---------------------------------------------------------------------------
long filesize(FILE* stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}
//---------------------------------------------------------------------------
enum
{
BOTH_DEATH1 = 0,
BOTH_DEAD1 = 1,
BOTH_DEATH2 = 2,
BOTH_DEAD2 = 3,
BOTH_DEATH3 = 4,
BOTH_DEAD3 = 5,
TORSO_GESTURE = 6,
TORSO_ATTACK = 7,
TORSO_ATTACK2 = 8,
TORSO_DROP = 9,
TORSO_RAISE = 10,
TORSO_STAND = 11,
TORSO_STAND2 = 12,
LEGS_WALKCR = 13,
LEGS_WALK = 14,
LEGS_RUN = 15,
LEGS_BACK = 16,
LEGS_SWIM = 17,
LEGS_JUMP = 18,
LEGS_LAND = 19,
LEGS_JUMPB = 20,
LEGS_LANDB = 21,
LEGS_IDLE = 22,
LEGS_IDLECR = 23,
LEGS_TURN = 24,
MAX_ANIMATIONS
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 921 B

After

Width:  |  Height:  |  Size: 662 B