rev |
line source |
nuclear@0
|
1 #include "level.h"
|
nuclear@0
|
2 #include "logger.h"
|
nuclear@0
|
3
|
nuclear@0
|
4 static void gen_mesh(Mesh *mesh);
|
nuclear@0
|
5
|
nuclear@0
|
6 Level::Level()
|
nuclear@0
|
7 {
|
nuclear@0
|
8 dlimit_near = -2;
|
nuclear@0
|
9 dlimit_far = 8;
|
nuclear@0
|
10
|
nuclear@0
|
11 gen_mesh(&tile_mesh);
|
nuclear@0
|
12
|
nuclear@0
|
13 tile_obj = new Object;
|
nuclear@0
|
14 tile_obj->set_mesh(&tile_mesh);
|
nuclear@0
|
15 }
|
nuclear@0
|
16
|
nuclear@0
|
17 Level::~Level()
|
nuclear@0
|
18 {
|
nuclear@0
|
19 delete tile_obj;
|
nuclear@0
|
20 }
|
nuclear@0
|
21
|
nuclear@0
|
22 void Level::set_draw_limits(float dnear, float dfar)
|
nuclear@0
|
23 {
|
nuclear@0
|
24 dlimit_near = dnear;
|
nuclear@0
|
25 dlimit_far = dfar;
|
nuclear@0
|
26 }
|
nuclear@0
|
27
|
nuclear@0
|
28 void Level::draw() const
|
nuclear@0
|
29 {
|
nuclear@0
|
30 AABox bbox = tile_obj->get_aabbox();
|
nuclear@0
|
31 float zsize = bbox.max.z - bbox.min.z;
|
nuclear@0
|
32 float zoffs = -zsize / 2.0;
|
nuclear@0
|
33
|
nuclear@0
|
34 float z = dlimit_near;
|
nuclear@0
|
35 while(z < dlimit_far) {
|
nuclear@0
|
36 tile_obj->set_position(Vector3(0, 0, -(z - zoffs)));
|
nuclear@0
|
37 tile_obj->draw();
|
nuclear@0
|
38 z += zsize;
|
nuclear@0
|
39 }
|
nuclear@0
|
40 }
|
nuclear@0
|
41
|
nuclear@0
|
42 static void gen_mesh(Mesh *mesh)
|
nuclear@0
|
43 {
|
nuclear@0
|
44 /* left vertical side */
|
nuclear@0
|
45 mesh->normal(1, 0, 0);
|
nuclear@0
|
46 mesh->texcoord(0, 0); mesh->vertex(-2, 0, 1);
|
nuclear@0
|
47 mesh->texcoord(1, 0); mesh->vertex(-2, 0, -1);
|
nuclear@0
|
48 mesh->texcoord(1, 1); mesh->vertex(-2, 1, -1);
|
nuclear@0
|
49 mesh->texcoord(0, 1); mesh->vertex(-2, 1, 1);
|
nuclear@0
|
50
|
nuclear@0
|
51 /* left diagonal */
|
nuclear@0
|
52 Vector3 dnorm = Vector3(0.5, 1, 0).normalized();
|
nuclear@0
|
53 mesh->normal(dnorm.x, dnorm.y, dnorm.z);
|
nuclear@0
|
54 mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, 1);
|
nuclear@0
|
55 mesh->texcoord(1, 0); mesh->vertex(-1, -0.5, -1);
|
nuclear@0
|
56 mesh->texcoord(1, 1); mesh->vertex(-2, 0, -1);
|
nuclear@0
|
57 mesh->texcoord(0, 1); mesh->vertex(-2, 0, 1);
|
nuclear@0
|
58
|
nuclear@0
|
59 /* floor */
|
nuclear@0
|
60 mesh->normal(0, 1, 0);
|
nuclear@0
|
61 mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, 1);
|
nuclear@0
|
62 mesh->texcoord(1, 0); mesh->vertex(1, -0.5, 1);
|
nuclear@0
|
63 mesh->texcoord(1, 1); mesh->vertex(1, -0.5, -1);
|
nuclear@0
|
64 mesh->texcoord(0, 1); mesh->vertex(-1, -0.5, -1);
|
nuclear@0
|
65
|
nuclear@0
|
66 /* right vertical side */
|
nuclear@0
|
67 mesh->normal(-1, 0, 0);
|
nuclear@0
|
68 mesh->texcoord(0, 0); mesh->vertex(2, 0, -1);
|
nuclear@0
|
69 mesh->texcoord(1, 0); mesh->vertex(2, 0, 1);
|
nuclear@0
|
70 mesh->texcoord(1, 1); mesh->vertex(2, 1, 1);
|
nuclear@0
|
71 mesh->texcoord(0, 1); mesh->vertex(2, 1, -1);
|
nuclear@0
|
72
|
nuclear@0
|
73 /* right diagonal */
|
nuclear@0
|
74 dnorm = Vector3(-0.5, 1, 0).normalized();
|
nuclear@0
|
75 mesh->normal(dnorm.x, dnorm.y, dnorm.z);
|
nuclear@0
|
76 mesh->texcoord(0, 0); mesh->vertex(1, -0.5, -1);
|
nuclear@0
|
77 mesh->texcoord(1, 0); mesh->vertex(1, -0.5, 1);
|
nuclear@0
|
78 mesh->texcoord(1, 1); mesh->vertex(2, 0, 1);
|
nuclear@0
|
79 mesh->texcoord(0, 1); mesh->vertex(2, 0, -1);
|
nuclear@0
|
80
|
nuclear@0
|
81 // quad face index buffer
|
nuclear@0
|
82 int nverts = mesh->get_attrib_count(MESH_ATTR_VERTEX);
|
nuclear@0
|
83 for(int i=0; i<nverts; i+=4) {
|
nuclear@0
|
84 mesh->face(i, i + 1, i + 2);
|
nuclear@0
|
85 mesh->face(i, i + 2, i + 3);
|
nuclear@0
|
86 }
|
nuclear@0
|
87
|
nuclear@0
|
88 info_log("generated level trough mesh: %d polygons\n", mesh->get_poly_count());
|
nuclear@0
|
89 }
|