tavli

annotate src/scenery.cc @ 21:c3fbf9616dbd

slot bounds, and ray testing
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 02 Jul 2015 00:01:39 +0300
parents 986c0b76513f
children c2a2069a49ec
rev   line source
nuclear@14 1 #include <vector>
nuclear@14 2 #include "opengl.h"
nuclear@14 3 #include "scenery.h"
nuclear@14 4 #include "game.h"
nuclear@14 5 #include "mesh.h"
nuclear@14 6 #include "meshgen.h"
nuclear@14 7 #include "object.h"
nuclear@14 8 #include "revol.h"
nuclear@14 9 #include "image.h"
nuclear@15 10 #include "sdr.h"
nuclear@18 11 #include "opt.h"
nuclear@14 12
nuclear@14 13 static bool gen_textures();
nuclear@14 14
nuclear@14 15 static std::vector<Object*> obj;
nuclear@14 16 static Image img_marble;
nuclear@14 17
nuclear@14 18 static const vec2_t table_cp[] = {
nuclear@14 19 {0, 0},
nuclear@14 20 {3, 0}, // mid 0
nuclear@14 21 {5.8, 0},
nuclear@14 22 {5.99, 0}, // mid 1
nuclear@15 23 {6, -0.1},
nuclear@15 24 {6.1, -0.1}, // mid 2
nuclear@19 25 {6.13, -0.3},
nuclear@19 26 {3, -0.3}, // mid 3
nuclear@19 27 {0, -0.3}
nuclear@14 28 };
nuclear@14 29 static const BezCurve table_curve = {
nuclear@14 30 sizeof table_cp / sizeof *table_cp,
nuclear@14 31 (vec2_t*)table_cp,
nuclear@14 32 1.0 / 6.2
nuclear@14 33 };
nuclear@14 34
nuclear@14 35
nuclear@14 36
nuclear@14 37 bool init_scenery()
nuclear@14 38 {
nuclear@14 39 if(!gen_textures()) {
nuclear@14 40 return false;
nuclear@14 41 }
nuclear@14 42
nuclear@14 43 Matrix4x4 xform;
nuclear@14 44
nuclear@14 45 Mesh *table = new Mesh;
nuclear@15 46 gen_revol(table, 48, 16, bezier_revol, bezier_revol_normal, (void*)&table_curve);
nuclear@14 47 table->texcoord_gen_plane(Vector3(0, 1, 0), Vector3(1, 0, 0));
nuclear@14 48 xform.set_scaling(Vector3(0.5, 0.5, 0.5));
nuclear@14 49 xform.translate(Vector3(1, 1, 0));
nuclear@14 50 table->texcoord_apply_xform(xform);
nuclear@14 51
nuclear@14 52 static const float table_scale = 1.8;
nuclear@14 53 xform.set_scaling(Vector3(table_scale, table_scale, table_scale));
nuclear@14 54 table->apply_xform(xform);
nuclear@14 55
nuclear@14 56 Object *otable = new Object;
nuclear@14 57 otable->set_mesh(table);
nuclear@15 58 otable->mtl.diffuse = Vector3(1, 1, 1);
nuclear@15 59 otable->mtl.specular = Vector3(0.7, 0.7, 0.7);
nuclear@14 60 otable->xform().set_translation(Vector3(0, -0.025, 0));
nuclear@14 61 otable->set_texture(img_marble.texture());
nuclear@14 62 obj.push_back(otable);
nuclear@14 63
nuclear@14 64
nuclear@14 65 // meshgen stats
nuclear@14 66 printf("Generated scenery:\n %u meshes\n", (unsigned int)obj.size());
nuclear@14 67 unsigned int polycount = 0;
nuclear@14 68 for(size_t i=0; i<obj.size(); i++) {
nuclear@14 69 const Mesh *m = obj[i]->get_mesh();
nuclear@14 70 polycount += m->get_poly_count();
nuclear@14 71 }
nuclear@14 72 printf(" %u polygons\n", polycount);
nuclear@14 73
nuclear@14 74 return true;
nuclear@14 75 }
nuclear@14 76
nuclear@14 77 void destroy_scenery()
nuclear@14 78 {
nuclear@14 79 for(size_t i=0; i<obj.size(); i++) {
nuclear@14 80 delete obj[i];
nuclear@14 81 }
nuclear@14 82 obj.clear();
nuclear@14 83
nuclear@14 84 img_marble.destroy();
nuclear@14 85 }
nuclear@14 86
nuclear@14 87 void draw_scenery()
nuclear@14 88 {
nuclear@19 89 unsigned int sdr = opt.shadows && sdr_shadow ? sdr_shadow : sdr_phong;
nuclear@19 90
nuclear@14 91 for(size_t i=0; i<obj.size(); i++) {
nuclear@14 92 if(wireframe) {
nuclear@14 93 obj[i]->draw_wire();
nuclear@14 94 obj[i]->draw_normals(0.075);
nuclear@14 95 } else {
nuclear@19 96 obj[i]->set_shader(sdr);
nuclear@14 97 obj[i]->draw();
nuclear@14 98 }
nuclear@14 99 }
nuclear@14 100 }
nuclear@14 101
nuclear@14 102 static float marble(float x, float y)
nuclear@14 103 {
nuclear@14 104 float theta = x * M_PI * 2.0 * cos(y * 1.5);
nuclear@14 105 theta += turbulence2(x * 10.0, y * 10.0, 4) * 2;
nuclear@14 106 float val = 0.5 + sin(theta * 5.0) + sin(theta * 8.0) / 2.0 + sin(theta * 19.0) / 4.0;
nuclear@14 107 return val * 0.5 + 0.5;
nuclear@14 108 }
nuclear@14 109
nuclear@14 110 static bool gen_textures()
nuclear@14 111 {
nuclear@14 112 static const Vector3 marble_col1 = Vector3(0.78, 0.85, 0.85);
nuclear@14 113 static const Vector3 marble_col2 = Vector3(0.56, 0.68, 0.7);
nuclear@14 114
nuclear@14 115 img_marble.create(512, 512);
nuclear@14 116 unsigned char *pptr = img_marble.pixels;
nuclear@14 117 for(int i=0; i<img_marble.height; i++) {
nuclear@14 118 float v = (float)i / (float)img_marble.height;
nuclear@14 119 for(int j=0; j<img_marble.width; j++) {
nuclear@14 120 float u = (float)j / (float)img_marble.width;
nuclear@14 121
nuclear@14 122 float marble_val = marble(u, v);
nuclear@15 123 Vector3 color = lerp(marble_col2, marble_col1, marble_val) * 0.6;
nuclear@14 124
nuclear@14 125 int r = (int)(color.x * 255.0);
nuclear@14 126 int g = (int)(color.y * 255.0);
nuclear@14 127 int b = (int)(color.z * 255.0);
nuclear@14 128
nuclear@14 129 pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
nuclear@14 130 pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
nuclear@14 131 pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
nuclear@14 132 pptr += 3;
nuclear@14 133 }
nuclear@14 134 }
nuclear@14 135 img_marble.texture();
nuclear@14 136
nuclear@14 137 return true;
nuclear@14 138 }