vrshoot
diff src/level.cc @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children | e7ca128b8713 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/level.cc Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,89 @@ 1.4 +#include "level.h" 1.5 +#include "logger.h" 1.6 + 1.7 +static void gen_mesh(Mesh *mesh); 1.8 + 1.9 +Level::Level() 1.10 +{ 1.11 + dlimit_near = -2; 1.12 + dlimit_far = 8; 1.13 + 1.14 + gen_mesh(&tile_mesh); 1.15 + 1.16 + tile_obj = new Object; 1.17 + tile_obj->set_mesh(&tile_mesh); 1.18 +} 1.19 + 1.20 +Level::~Level() 1.21 +{ 1.22 + delete tile_obj; 1.23 +} 1.24 + 1.25 +void Level::set_draw_limits(float dnear, float dfar) 1.26 +{ 1.27 + dlimit_near = dnear; 1.28 + dlimit_far = dfar; 1.29 +} 1.30 + 1.31 +void Level::draw() const 1.32 +{ 1.33 + AABox bbox = tile_obj->get_aabbox(); 1.34 + float zsize = bbox.max.z - bbox.min.z; 1.35 + float zoffs = -zsize / 2.0; 1.36 + 1.37 + float z = dlimit_near; 1.38 + while(z < dlimit_far) { 1.39 + tile_obj->set_position(Vector3(0, 0, -(z - zoffs))); 1.40 + tile_obj->draw(); 1.41 + z += zsize; 1.42 + } 1.43 +} 1.44 + 1.45 +static void gen_mesh(Mesh *mesh) 1.46 +{ 1.47 + /* left vertical side */ 1.48 + mesh->normal(1, 0, 0); 1.49 + mesh->texcoord(0, 0); mesh->vertex(-2, 0, 1); 1.50 + mesh->texcoord(1, 0); mesh->vertex(-2, 0, -1); 1.51 + mesh->texcoord(1, 1); mesh->vertex(-2, 1, -1); 1.52 + mesh->texcoord(0, 1); mesh->vertex(-2, 1, 1); 1.53 + 1.54 + /* left diagonal */ 1.55 + Vector3 dnorm = Vector3(0.5, 1, 0).normalized(); 1.56 + mesh->normal(dnorm.x, dnorm.y, dnorm.z); 1.57 + mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, 1); 1.58 + mesh->texcoord(1, 0); mesh->vertex(-1, -0.5, -1); 1.59 + mesh->texcoord(1, 1); mesh->vertex(-2, 0, -1); 1.60 + mesh->texcoord(0, 1); mesh->vertex(-2, 0, 1); 1.61 + 1.62 + /* floor */ 1.63 + mesh->normal(0, 1, 0); 1.64 + mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, 1); 1.65 + mesh->texcoord(1, 0); mesh->vertex(1, -0.5, 1); 1.66 + mesh->texcoord(1, 1); mesh->vertex(1, -0.5, -1); 1.67 + mesh->texcoord(0, 1); mesh->vertex(-1, -0.5, -1); 1.68 + 1.69 + /* right vertical side */ 1.70 + mesh->normal(-1, 0, 0); 1.71 + mesh->texcoord(0, 0); mesh->vertex(2, 0, -1); 1.72 + mesh->texcoord(1, 0); mesh->vertex(2, 0, 1); 1.73 + mesh->texcoord(1, 1); mesh->vertex(2, 1, 1); 1.74 + mesh->texcoord(0, 1); mesh->vertex(2, 1, -1); 1.75 + 1.76 + /* right diagonal */ 1.77 + dnorm = Vector3(-0.5, 1, 0).normalized(); 1.78 + mesh->normal(dnorm.x, dnorm.y, dnorm.z); 1.79 + mesh->texcoord(0, 0); mesh->vertex(1, -0.5, -1); 1.80 + mesh->texcoord(1, 0); mesh->vertex(1, -0.5, 1); 1.81 + mesh->texcoord(1, 1); mesh->vertex(2, 0, 1); 1.82 + mesh->texcoord(0, 1); mesh->vertex(2, 0, -1); 1.83 + 1.84 + // quad face index buffer 1.85 + int nverts = mesh->get_attrib_count(MESH_ATTR_VERTEX); 1.86 + for(int i=0; i<nverts; i+=4) { 1.87 + mesh->face(i, i + 1, i + 2); 1.88 + mesh->face(i, i + 2, i + 3); 1.89 + } 1.90 + 1.91 + info_log("generated level trough mesh: %d polygons\n", mesh->get_poly_count()); 1.92 +}