-
Notifications
You must be signed in to change notification settings - Fork 3
/
OGFLoader.cpp
160 lines (130 loc) · 4.4 KB
/
OGFLoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "stdafx.h"
#include "OGFLoader.h"
#include "nv_dds.h"
using namespace nv_dds;
void ReadChunk1284(FILE* inputFile, int pos,OGFModel *model,const std::string &pathToTextures)
{
fseek(inputFile, pos,SEEK_SET);
Chunk1284 *c1284 = new Chunk1284;
fread(c1284,sizeof(Chunk1284),1,inputFile);
if(c1284->header[0] != 1 && c1284->header[1] != 44 && (c1284->header[2] != 1284 || c1284->header[2] != 1028))
{
delete c1284;
return;
}
delete c1284;
TextData *td = new TextData;
fread(&td->type,sizeof(int),1,inputFile);
fread(&td->size,sizeof(int),1,inputFile);
memset(td->data,0,1024);
fread((char*)td->data,td->size,1,inputFile);
model->m_MaterialCount++;
char *materialName = strcat(td->data,".dds");
std::string fullName(pathToTextures);
fullName += std::string(materialName);
CDDSImage image;
GLuint texobj;
if(image.load(fullName.c_str()))
{
glGenTextures(1, &texobj);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texobj);
glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, image.get_format(),
image.get_width(), image.get_height(), 0, image.get_size(),
image);
for (int i = 0; i < image.get_num_mipmaps(); i++)
{
glCompressedTexImage2DARB(GL_TEXTURE_2D, i+1, image.get_format(),
image.get_mipmap(i).get_width(), image.get_mipmap(i).get_height(), 0,
image.get_mipmap(i).get_size(), image.get_mipmap(i));
}
model->m_Material[model->m_MaterialCount-1].m_MaterialID = texobj;
}else
{
model->m_Material[model->m_MaterialCount-1].m_MaterialID = 0;
}
int num,size,ident,numverts;
fread(&num,sizeof(int),1,inputFile);
fread(&size,sizeof(int),1,inputFile);
fread(&ident,sizeof(int),1,inputFile);
// ident = 302455168; Ïðîâåðÿë íà 2õ ôàéëàõ
fread(&numverts,sizeof(int),1,inputFile);
if(ident == 302455168)
{
OGFVertex *v = new OGFVertex[numverts];
fread(v,numverts*sizeof(OGFVertex),1,inputFile);
model->m_Material[model->m_MaterialCount-1].m_VerticesCount = numverts;
model->m_Material[model->m_MaterialCount-1].m_Vertices = new Vertex[numverts];
OGFVertex vert;
Vertex mVertex;
for(int i=0; i< numverts;i++)
{
vert = v[i];
mVertex.x = vert.x/1.5f; mVertex.y = vert.y; mVertex.z = vert.z; mVertex.tx = vert.s[1]; mVertex.ty = -vert.d[0];
model->m_Material[model->m_MaterialCount-1].m_Vertices[i] = mVertex;
}
int type,datasize,count;
fread(&type,sizeof(int),1,inputFile);
fread(&datasize,sizeof(int),1,inputFile);
fread(&count,sizeof(int),1,inputFile);
Indices indices;
indices.count = count;
indices.idx = new unsigned short[count];
fread(indices.idx,sizeof(unsigned short)*count,1,inputFile);
model->m_Material[model->m_MaterialCount-1].m_Indices = new unsigned int[count];
model->m_Material[model->m_MaterialCount-1].m_IndicesCount = count;
for (int i=0; i<count;i++)
{
model->m_Material[model->m_MaterialCount-1].m_Indices[i] = indices.idx[i];
}
delete indices.idx;
delete v;
}
}
OGFModel *Load(char *fileName,const std::string &pathToTextures)
{
FILE *inputFile;
//MeshBuilder meshBuilder;
GLeeInit();
inputFile = fopen(fileName,"rb");
if(!inputFile)
{
// Log::Get().Write("<*> OGF : File not found. %s", fileName);
return NULL;
}
OGFModel *model = new OGFModel();
fseek(inputFile,0,SEEK_SET);
Chunk2564 *c2564 = new Chunk2564;
fread(c2564,sizeof(Chunk2564),1,inputFile);
delete c2564;
OGFHeader header;
fread( &header.count, sizeof(int),1,inputFile);
fread( &header.size, sizeof(int),1,inputFile);
memset(header.modelname,0,1024);
fread( (char*)header.modelname, header.size,1,inputFile );
fread( &header.hz1, sizeof(int),1,inputFile);
header.pos1 = ftell(inputFile);
fread( &header.hz2, sizeof(int),1,inputFile);
header.pos2 = ftell(inputFile);
fread( &header.hz3, sizeof(int),1,inputFile);
header.pos3 = ftell(inputFile);
fread( &header.hz4, sizeof(int),1,inputFile);
header.pos4 = ftell(inputFile);
ReadChunk1284(inputFile,header.pos1 + header.hz1 + 3,model,pathToTextures);
fseek(inputFile,header.pos2 + header.hz2,SEEK_SET);
structF *ds = new structF;
fread(ds,sizeof(structF),1,inputFile);
Bone *bone = new Bone;
fread( &bone->id, sizeof(int),1,inputFile );
fread( bone->bone, ds->f[1]-sizeof(int),1,inputFile);
fread(ds,sizeof(structF),1,inputFile);
delete bone;
delete ds;
if(header.hz2 != header.hz4+ 8)
{
ReadChunk1284(inputFile,header.pos4 + header.hz4 + 8,model,pathToTextures);
}
//Log::Get().Write("<!> OGF : Loaded %s successfull", fileName);
//inputFile.Close();
return model;
}