-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasset_manager.cpp
More file actions
173 lines (137 loc) · 3.87 KB
/
Copy pathasset_manager.cpp
File metadata and controls
173 lines (137 loc) · 3.87 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
/* asset_manager.cpp :
* all methods of all classes and all global functions which deal with managing assets:
* Importing, procedural construction, maybe a bit of pre-processing
*/
#include <fstream>
#include <sstream>
#include <string>
#include"mesh.h"
#include"custom_classes.h"
#include"texture.h"
void preloadAllAssets(){
// quick hack: change me!
std::string assetPath = "C:/corsi/game_engines_2016/kamikazeLab2016/assets/";
CpuMesh tmp;
tmp.import(assetPath + "dark_fighter_6.obj");
//tmp.buildTorus(10,30,5.0,15.0); // or, procedural creation
CpuTexture tmpText;
tmpText.import(assetPath + "dark_fighter_6_color.pbm");
//tmpText.createRandom(256);
GpuTexture gpuText = tmpText.uploadToGPU();
MeshComponent comp;
comp.mesh = tmp.uploadToGPU();
comp.texture = gpuText;
// let set a tranform manually to adapt the asset to our needs
comp.t.setIde();
comp.t.scale = 0.05f;
comp.t.ori = quat( -sqrt(2.0f)/2.0f,0,0,sqrt(2.0f)/2.0f );
// TODO: differntiate models / textures
scene.ships[0].meshComponent = comp;
scene.ships[1].meshComponent = comp;
}
void CpuMesh::addQuad(int i, int j, int k, int h){
tris.push_back( Tri(i,j,k) );
tris.push_back( Tri(k,h,i) );
}
void CpuMesh::buildTorus(int ni, int nj, float innerRadius, float outerRadius){
verts.reserve( ni*nj );
tris.reserve( ni*nj*2 );
for (int j=0; j<nj; j++)
for (int i=0; i<ni; i++) {
// construct geometry
double alpha = 2*3.1415 * i / ni;
float x = (float)cos(alpha) * innerRadius;
float y = (float)sin(alpha) * innerRadius;
x += outerRadius;
double beta = 2*3.1415 * j / nj;
Vertex v;
v.pos.x = (float)cos(beta) * x;
v.pos.y = y;
v.pos.z = (float)sin(beta) * x;
verts.push_back( v );
// construct connectivity
addQuad( j * ni + i ,
j * ni + (i+1)%ni ,
(j+1)%nj * ni + (i+1)%ni ,
(j+1)%nj * ni + i );
}
}
bool CpuMesh::import(const std::string& filename){
std::ifstream infile(filename);
if (!infile.is_open()) return false;
std::string line;
std::vector< vec3 > tmpV;
std::vector< vec3 > tmpN;
while ( std::getline(infile, line) ){
std::istringstream iss(line);
std::string code;
iss >> code;
if (code=="v") {
float x,y,z;
iss >> x >> y >> z;
tmpV.push_back( vec3(x,z,y) );
} else if (code=="vn") {
float x,y,z;
iss >> x >> y >> z;
tmpN.push_back( vec3(x,z,y) );
} else if (code=="vt") {
float x,y;
iss >> x >> y;
Vertex nv;
nv.uv = vec2(x,1.0f-y); // NB: flipping the y, different conventions about UV space
verts.push_back( nv );
// v.pos e v.norm: ci penso dopo, quando leggero' le facce
} else if (code=="f") {
std::string st_i, st_j, st_k;
iss >> st_i >> st_j >> st_k;
int i0,i1,i2;
int j0,j1,j2;
int k0,k1,k2;
sscanf( st_i.c_str() , "%d/%d/%d" , &i0,&i1,&i2 );
sscanf( st_j.c_str() , "%d/%d/%d" , &j0,&j1,&j2 );
sscanf( st_k.c_str() , "%d/%d/%d" , &k0,&k1,&k2 );
// Obj indices start from 1 not 0!
i0--;j0--;k0--;
i1--;j1--;k1--;
i2--;j2--;k2--;
Tri nt( i1, j1, k1 );
tris.push_back( nt );
verts[ i1 ].pos = tmpV[ i0 ];
verts[ i1 ].norm = tmpN[ i2 ];
verts[ j1 ].pos = tmpV[ j0 ];
verts[ j1 ].norm = tmpN[ j2 ];
verts[ k1 ].pos = tmpV[ k0 ];
verts[ k1 ].norm = tmpN[ k2 ];
}
}
return true;
}
bool CpuTexture::import(std::string filename){
std::ifstream infile(filename,std::ios::binary);
if (!infile.is_open()) return false;
int depth;
std::string token;
infile >> token >> sizeX >> sizeY >> depth;
// TODO: some checking please!
data.reserve( sizeX*sizeY );
for (int i=0; i<sizeX*sizeY; i++) {
char rgb[3];
infile.read(rgb, 3 );
Texel t;
t.r = rgb[0];
t.g = rgb[1];
t.b = rgb[2];
data.push_back( t );
}
return true;
}
void CpuTexture::createRandom(int size){
sizeX = sizeY = size;
data.resize(sizeX * sizeY);
for (Texel &t : data) {
t.r = rand()%256;
t.g = rand()%256;
t.b = rand()%256;
t.a = 255;
}
}