rev |
line source |
nuclear@1
|
1 #include <math.h>
|
nuclear@0
|
2 #include "level.h"
|
nuclear@0
|
3 #include "logger.h"
|
nuclear@1
|
4 #include "texman.h"
|
nuclear@1
|
5 #include "sdrman.h"
|
nuclear@0
|
6
|
nuclear@0
|
7 static void gen_mesh(Mesh *mesh);
|
nuclear@0
|
8
|
nuclear@1
|
9 static ShaderProg *level_sdr;
|
nuclear@1
|
10
|
nuclear@0
|
11 Level::Level()
|
nuclear@0
|
12 {
|
nuclear@1
|
13 dlimit_near = 0;
|
nuclear@1
|
14 dlimit_far = 25;
|
nuclear@1
|
15
|
nuclear@1
|
16 fly_speed = 3.0;
|
nuclear@0
|
17
|
nuclear@0
|
18 gen_mesh(&tile_mesh);
|
nuclear@0
|
19
|
nuclear@0
|
20 tile_obj = new Object;
|
nuclear@0
|
21 tile_obj->set_mesh(&tile_mesh);
|
nuclear@1
|
22 tile_obj->material.tex[0] = texset.get("data/purple_grid.png");
|
nuclear@1
|
23
|
nuclear@1
|
24 level_sdr = get_sdrprog("sdr/default.v.glsl", "sdr/level.p.glsl");
|
nuclear@0
|
25 }
|
nuclear@0
|
26
|
nuclear@0
|
27 Level::~Level()
|
nuclear@0
|
28 {
|
nuclear@0
|
29 delete tile_obj;
|
nuclear@0
|
30 }
|
nuclear@0
|
31
|
nuclear@0
|
32 void Level::set_draw_limits(float dnear, float dfar)
|
nuclear@0
|
33 {
|
nuclear@0
|
34 dlimit_near = dnear;
|
nuclear@0
|
35 dlimit_far = dfar;
|
nuclear@0
|
36 }
|
nuclear@0
|
37
|
nuclear@1
|
38 void Level::draw(long msec) const
|
nuclear@0
|
39 {
|
nuclear@0
|
40 AABox bbox = tile_obj->get_aabbox();
|
nuclear@0
|
41 float zsize = bbox.max.z - bbox.min.z;
|
nuclear@0
|
42 float zoffs = -zsize / 2.0;
|
nuclear@0
|
43
|
nuclear@1
|
44 float tm = (float)msec / 1000.0;
|
nuclear@1
|
45
|
nuclear@1
|
46 level_sdr->bind();
|
nuclear@1
|
47
|
nuclear@0
|
48 float z = dlimit_near;
|
nuclear@0
|
49 while(z < dlimit_far) {
|
nuclear@1
|
50 tile_obj->set_position(Vector3(0, 0, -(z - zoffs) + fmod(tm * fly_speed, zsize)));
|
nuclear@1
|
51
|
nuclear@1
|
52 // draw mirrored
|
nuclear@1
|
53 glFrontFace(GL_CW);
|
nuclear@1
|
54 tile_obj->set_scaling(Vector3(-1, 1, 1));
|
nuclear@0
|
55 tile_obj->draw();
|
nuclear@1
|
56 glFrontFace(GL_CCW);
|
nuclear@1
|
57 tile_obj->set_scaling(Vector3(1, 1, 1));
|
nuclear@1
|
58 tile_obj->draw();
|
nuclear@1
|
59
|
nuclear@0
|
60 z += zsize;
|
nuclear@0
|
61 }
|
nuclear@0
|
62 }
|
nuclear@0
|
63
|
nuclear@1
|
64 #define DEPTH 2.0
|
nuclear@1
|
65 #define POSTZ (0.8 * DEPTH)
|
nuclear@1
|
66
|
nuclear@0
|
67 static void gen_mesh(Mesh *mesh)
|
nuclear@0
|
68 {
|
nuclear@1
|
69 // vertical side
|
nuclear@0
|
70 mesh->normal(1, 0, 0);
|
nuclear@1
|
71 mesh->texcoord(0, 0); mesh->vertex(-2, 0, DEPTH);
|
nuclear@1
|
72 mesh->texcoord(3, 0); mesh->vertex(-2, 0, -DEPTH);
|
nuclear@1
|
73 mesh->texcoord(3, 2); mesh->vertex(-2, 1, -DEPTH);
|
nuclear@1
|
74 mesh->texcoord(0, 2); mesh->vertex(-2, 1, DEPTH);
|
nuclear@0
|
75
|
nuclear@1
|
76 // left diagonal
|
nuclear@0
|
77 Vector3 dnorm = Vector3(0.5, 1, 0).normalized();
|
nuclear@0
|
78 mesh->normal(dnorm.x, dnorm.y, dnorm.z);
|
nuclear@1
|
79 mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, DEPTH);
|
nuclear@1
|
80 mesh->texcoord(3, 0); mesh->vertex(-1, -0.5, -DEPTH);
|
nuclear@1
|
81 mesh->texcoord(3, 2); mesh->vertex(-2, 0, -DEPTH);
|
nuclear@1
|
82 mesh->texcoord(0, 2); mesh->vertex(-2, 0, DEPTH);
|
nuclear@1
|
83
|
nuclear@1
|
84 // vertical post front
|
nuclear@1
|
85 mesh->normal(0, 0, 1);
|
nuclear@1
|
86 mesh->texcoord(0, 0); mesh->vertex(-2, -0.5, -POSTZ);
|
nuclear@1
|
87 mesh->texcoord(1, 0); mesh->vertex(-1.7, -0.5, -POSTZ);
|
nuclear@1
|
88 mesh->texcoord(1, 3); mesh->vertex(-1.7, 1, -POSTZ);
|
nuclear@1
|
89 mesh->texcoord(0, 3); mesh->vertex(-2, 1, -POSTZ);
|
nuclear@1
|
90
|
nuclear@1
|
91 // vertical post side
|
nuclear@1
|
92 mesh->normal(1, 0, 0);
|
nuclear@1
|
93 mesh->texcoord(0, 0); mesh->vertex(-1.7, -0.5, -POSTZ);
|
nuclear@1
|
94 mesh->texcoord(1, 0); mesh->vertex(-1.7, -0.5, -DEPTH);
|
nuclear@1
|
95 mesh->texcoord(1, 3); mesh->vertex(-1.7, 1, -DEPTH);
|
nuclear@1
|
96 mesh->texcoord(0, 3); mesh->vertex(-1.7, 1, -POSTZ);
|
nuclear@1
|
97
|
nuclear@1
|
98 // vertical post top
|
nuclear@1
|
99 mesh->normal(0, 1, 0);
|
nuclear@1
|
100 mesh->texcoord(0, 0); mesh->vertex(-2, 1, -POSTZ);
|
nuclear@1
|
101 mesh->texcoord(1, 0); mesh->vertex(-1.7, 1, -POSTZ);
|
nuclear@1
|
102 mesh->texcoord(1, 1); mesh->vertex(-1.7, 1, -DEPTH);
|
nuclear@1
|
103 mesh->texcoord(0, 1); mesh->vertex(-2, 1, -DEPTH);
|
nuclear@0
|
104
|
nuclear@0
|
105 /* floor */
|
nuclear@0
|
106 mesh->normal(0, 1, 0);
|
nuclear@1
|
107 mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, DEPTH);
|
nuclear@1
|
108 mesh->texcoord(1, 0); mesh->vertex(0, -0.5, DEPTH);
|
nuclear@1
|
109 mesh->texcoord(1, 3); mesh->vertex(0, -0.5, -DEPTH);
|
nuclear@1
|
110 mesh->texcoord(0, 3); mesh->vertex(-1, -0.5, -DEPTH);
|
nuclear@0
|
111
|
nuclear@0
|
112 // quad face index buffer
|
nuclear@0
|
113 int nverts = mesh->get_attrib_count(MESH_ATTR_VERTEX);
|
nuclear@0
|
114 for(int i=0; i<nverts; i+=4) {
|
nuclear@0
|
115 mesh->face(i, i + 1, i + 2);
|
nuclear@0
|
116 mesh->face(i, i + 2, i + 3);
|
nuclear@0
|
117 }
|
nuclear@0
|
118
|
nuclear@0
|
119 info_log("generated level trough mesh: %d polygons\n", mesh->get_poly_count());
|
nuclear@0
|
120 }
|