vrshoot

view src/level.cc @ 3:c179c72369be

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