766fef6659dd6533c6a6d3370fd030f6cc240dbd
[tar-legacy.git] / MCDV / vvd.hpp
1 #pragma once
2 #include <vector>
3 #include <string>
4 #include <fstream>
5 #include <iostream>
6
7 #include <glm\glm.hpp>
8 #include <glm\gtc\matrix_transform.hpp>
9 #include <glm\gtc\type_ptr.hpp>
10
11 #include "util.h"
12
13 //StudioMDL constants
14 #define MAX_NUM_LODS 8
15 #define MAX_NUM_BONES_PER_VERT 3
16
17 namespace VVD
18 {
19 #pragma pack(push, 1)
20
21 struct boneWeight
22 {
23 float weight[MAX_NUM_BONES_PER_VERT];
24 char bone[MAX_NUM_BONES_PER_VERT];
25 char numbones;
26 };
27
28 struct Vertex
29 {
30 boneWeight m_boneweights;
31 glm::vec3 m_vecPosition;
32 glm::vec3 m_vecNormal;
33 glm::vec2 m_vecTexCoord;
34 };
35
36 struct Header
37 {
38 int id;
39 int version;
40 long checksum;
41 int numLods;
42 int numLodVertexes[MAX_NUM_LODS];
43 int numFixups;
44 int fixupTableStart;
45 int vertexDataStart;
46 int tangentDataStart;
47 };
48
49 #pragma pack(pop)
50 }
51
52 class vvd_data : public util::verboseControl
53 {
54 private:
55 public:
56 VVD::Header header;
57
58 std::vector<VVD::Vertex> verticesLOD0;
59
60 vvd_data(std::string filepath, bool verbose = false)
61 {
62 this->use_verbose = verbose;
63
64 //Create file handle
65 std::ifstream reader(filepath, std::ios::in | std::ios::binary);
66
67 if (!reader) {
68 throw std::exception("VVD::OPEN FAILED"); return;
69 }
70
71 reader.read((char*)&this->header, sizeof(this->header));
72 this->debug("VVD Version:", this->header.version);
73
74 //Read vertex data
75 reader.seekg(header.vertexDataStart);
76
77 //Read LOD0
78 for (int i = 0; i < header.numLodVertexes[0]; i++)
79 {
80 VVD::Vertex vert;
81 reader.read((char*)&vert, sizeof(vert));
82
83 // THIS HURTS
84 // REAL BAD
85 // forgot to copy this... spent 9 hours trying to learning studiomdl.
86 // note to self: dont dupe your code.
87 // Do the sorce->opengl flipperoo
88 glm::vec3 temp = vert.m_vecPosition;
89 vert.m_vecPosition = glm::vec3(-temp.x, temp.z, temp.y);
90
91 this->verticesLOD0.push_back(vert);
92 }
93
94 reader.close();
95 }
96
97 vvd_data(std::ifstream* stream, unsigned int offset, bool verbost = false) {
98 this->use_verbose = verbost;
99
100 stream->seekg(offset);
101 stream->read((char*)&this->header, sizeof(this->header));
102 this->debug("VVD Version:", this->header.version);
103
104 //Read vertex data
105 stream->seekg(offset + header.vertexDataStart);
106
107 //Read LOD0
108 for (int i = 0; i < header.numLodVertexes[0]; i++)
109 {
110 VVD::Vertex vert;
111 stream->read((char*)&vert, sizeof(vert));
112
113 // Do the sorce->opengl flipperoo
114 glm::vec3 temp = vert.m_vecPosition;
115 vert.m_vecPosition = glm::vec3(-temp.x, temp.z, temp.y);
116
117 this->verticesLOD0.push_back(vert);
118 }
119
120 this->debug("Data length: ", this->verticesLOD0.size());
121 }
122 ~vvd_data() {};
123 };