rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@1
|
2 #include "opengl.h"
|
nuclear@4
|
3 #include <assimp/assimp.h>
|
nuclear@4
|
4 #include <assimp/aiScene.h>
|
nuclear@4
|
5 #include <assimp/aiPostProcess.h>
|
nuclear@1
|
6 #include "tile.h"
|
nuclear@1
|
7
|
nuclear@1
|
8 bool Tile::load(const char *fname)
|
nuclear@1
|
9 {
|
nuclear@3
|
10 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
|
nuclear@3
|
11 aiProcess_PreTransformVertices | aiProcess_Triangulate |
|
nuclear@3
|
12 aiProcess_GenNormals | aiProcess_SortByPType | aiProcess_FlipUVs;
|
nuclear@4
|
13
|
nuclear@3
|
14 const aiScene *scn = aiImportFile(fname, proc_flags);
|
nuclear@3
|
15 if(!scn) {
|
nuclear@3
|
16 fprintf(stderr, "failed to load tile: %s\n", fname);
|
nuclear@3
|
17 return -1;
|
nuclear@3
|
18 }
|
nuclear@3
|
19
|
nuclear@3
|
20 load_lights(scn);
|
nuclear@3
|
21
|
nuclear@3
|
22 load_meshes(scn);
|
nuclear@3
|
23
|
nuclear@3
|
24 printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights);
|
nuclear@1
|
25 return true;
|
nuclear@1
|
26 }
|
nuclear@1
|
27
|
nuclear@3
|
28 void Tile::draw(unsigned int drawmask) const
|
nuclear@1
|
29 {
|
nuclear@4
|
30 for(size_t i=0; i<meshes.size(); i++) {
|
nuclear@4
|
31 if(mesh_side[i] & draw_mask) {
|
nuclear@4
|
32 meshes[i].draw();
|
nuclear@4
|
33 }
|
nuclear@4
|
34 }
|
nuclear@4
|
35 }
|
nuclear@1
|
36
|
nuclear@4
|
37 int Tile::load_lights(const aiScene *scn)
|
nuclear@4
|
38 {
|
nuclear@4
|
39 int count = 0;
|
nuclear@4
|
40
|
nuclear@4
|
41 for(int i=0; i<(int)scn->mNumLights; i++) {
|
nuclear@4
|
42 Light *lt;
|
nuclear@4
|
43 aiLight *ailt = scn->mLights[i];
|
nuclear@4
|
44
|
nuclear@4
|
45 switch(ailt->mType) {
|
nuclear@4
|
46 case aiLightSource_POINT:
|
nuclear@4
|
47 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
|
nuclear@4
|
48 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
|
nuclear@4
|
49 ailt->mAttenuationQuadratic);
|
nuclear@4
|
50 break;
|
nuclear@4
|
51
|
nuclear@4
|
52 case aiLightSource_DIRECTIONAL:
|
nuclear@4
|
53 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
|
nuclear@4
|
54 break;
|
nuclear@4
|
55
|
nuclear@4
|
56 default:
|
nuclear@4
|
57 continue;
|
nuclear@4
|
58 }
|
nuclear@4
|
59
|
nuclear@4
|
60 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
|
nuclear@4
|
61
|
nuclear@4
|
62 lights.push_back(lt);
|
nuclear@4
|
63 count++;
|
nuclear@4
|
64 }
|
nuclear@4
|
65
|
nuclear@4
|
66 return count;
|
nuclear@1
|
67 }
|
nuclear@4
|
68
|
nuclear@4
|
69 int Tile::load_meshes(const aiScene *scn)
|
nuclear@4
|
70 {
|
nuclear@4
|
71 int count = 0;
|
nuclear@4
|
72
|
nuclear@4
|
73 for(int i=0; i<(int)scn->mNumMeshes; i++) {
|
nuclear@4
|
74 Mesh *mesh = new Mesh;
|
nuclear@4
|
75 if(!mesh->create(scn, scn->mMeshes[i])) {
|
nuclear@4
|
76 delete mesh;
|
nuclear@4
|
77 continue;
|
nuclear@4
|
78 }
|
nuclear@4
|
79
|
nuclear@4
|
80 meshes.push_back(mesh);
|
nuclear@4
|
81
|
nuclear@4
|
82 // find which side is this mesh on
|
nuclear@4
|
83 const char *name = mesh->get_name();
|
nuclear@4
|
84 unsigned int side;
|
nuclear@4
|
85
|
nuclear@4
|
86 if(strstr(name, "NORTH") == name) {
|
nuclear@4
|
87 side = TILE_NORTH;
|
nuclear@4
|
88 } else if(strstr(name, "SOUTH") == name) {
|
nuclear@4
|
89 side = TILE_SOUTH;
|
nuclear@4
|
90 } else if(strstr(name, "EAST") == name) {
|
nuclear@4
|
91 side = TILE_EAST;
|
nuclear@4
|
92 } else if(strstr(name, "WEST") == name) {
|
nuclear@4
|
93 side = TILE_WEST;
|
nuclear@4
|
94 } else {
|
nuclear@4
|
95 side = TILE_ALL;
|
nuclear@4
|
96 }
|
nuclear@4
|
97 mesh_side.push_back(side);
|
nuclear@4
|
98
|
nuclear@4
|
99 count++;
|
nuclear@4
|
100 }
|
nuclear@4
|
101 return count;
|
nuclear@4
|
102 }
|