tavli
diff src/scenery.cc @ 14:283eb6e9f0a3
scenery
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 28 Jun 2015 07:44:23 +0300 |
parents | |
children | b1a195c3ee16 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/scenery.cc Sun Jun 28 07:44:23 2015 +0300 1.3 @@ -0,0 +1,131 @@ 1.4 +#include <vector> 1.5 +#include "opengl.h" 1.6 +#include "scenery.h" 1.7 +#include "game.h" 1.8 +#include "mesh.h" 1.9 +#include "meshgen.h" 1.10 +#include "object.h" 1.11 +#include "revol.h" 1.12 +#include "image.h" 1.13 + 1.14 +static bool gen_textures(); 1.15 + 1.16 +static std::vector<Object*> obj; 1.17 +static Image img_marble; 1.18 + 1.19 +static const vec2_t table_cp[] = { 1.20 + {0, 0}, 1.21 + {3, 0}, // mid 0 1.22 + {5.8, 0}, 1.23 + {5.99, 0}, // mid 1 1.24 + {6, -0.15}, 1.25 + {6.15, -0.15}, // mid 2 1.26 + {6.2, -0.4} 1.27 +}; 1.28 +static const BezCurve table_curve = { 1.29 + sizeof table_cp / sizeof *table_cp, 1.30 + (vec2_t*)table_cp, 1.31 + 1.0 / 6.2 1.32 +}; 1.33 + 1.34 + 1.35 + 1.36 +bool init_scenery() 1.37 +{ 1.38 + if(!gen_textures()) { 1.39 + return false; 1.40 + } 1.41 + 1.42 + Matrix4x4 xform; 1.43 + 1.44 + Mesh *table = new Mesh; 1.45 + gen_revol(table, 48, 12, bezier_revol, bezier_revol_normal, (void*)&table_curve); 1.46 + table->texcoord_gen_plane(Vector3(0, 1, 0), Vector3(1, 0, 0)); 1.47 + xform.set_scaling(Vector3(0.5, 0.5, 0.5)); 1.48 + xform.translate(Vector3(1, 1, 0)); 1.49 + table->texcoord_apply_xform(xform); 1.50 + 1.51 + static const float table_scale = 1.8; 1.52 + xform.set_scaling(Vector3(table_scale, table_scale, table_scale)); 1.53 + table->apply_xform(xform); 1.54 + 1.55 + Object *otable = new Object; 1.56 + otable->set_mesh(table); 1.57 + otable->mtl.diffuse = Vector3(0.6, 0.6, 0.6); 1.58 + otable->mtl.specular = Vector3(0.8, 0.8, 0.8); 1.59 + otable->xform().set_translation(Vector3(0, -0.025, 0)); 1.60 + otable->set_texture(img_marble.texture()); 1.61 + obj.push_back(otable); 1.62 + 1.63 + 1.64 + // meshgen stats 1.65 + printf("Generated scenery:\n %u meshes\n", (unsigned int)obj.size()); 1.66 + unsigned int polycount = 0; 1.67 + for(size_t i=0; i<obj.size(); i++) { 1.68 + const Mesh *m = obj[i]->get_mesh(); 1.69 + polycount += m->get_poly_count(); 1.70 + } 1.71 + printf(" %u polygons\n", polycount); 1.72 + 1.73 + return true; 1.74 +} 1.75 + 1.76 +void destroy_scenery() 1.77 +{ 1.78 + for(size_t i=0; i<obj.size(); i++) { 1.79 + delete obj[i]; 1.80 + } 1.81 + obj.clear(); 1.82 + 1.83 + img_marble.destroy(); 1.84 +} 1.85 + 1.86 +void draw_scenery() 1.87 +{ 1.88 + for(size_t i=0; i<obj.size(); i++) { 1.89 + if(wireframe) { 1.90 + obj[i]->draw_wire(); 1.91 + obj[i]->draw_normals(0.075); 1.92 + } else { 1.93 + obj[i]->draw(); 1.94 + } 1.95 + } 1.96 +} 1.97 + 1.98 +static float marble(float x, float y) 1.99 +{ 1.100 + float theta = x * M_PI * 2.0 * cos(y * 1.5); 1.101 + theta += turbulence2(x * 10.0, y * 10.0, 4) * 2; 1.102 + float val = 0.5 + sin(theta * 5.0) + sin(theta * 8.0) / 2.0 + sin(theta * 19.0) / 4.0; 1.103 + return val * 0.5 + 0.5; 1.104 +} 1.105 + 1.106 +static bool gen_textures() 1.107 +{ 1.108 + static const Vector3 marble_col1 = Vector3(0.78, 0.85, 0.85); 1.109 + static const Vector3 marble_col2 = Vector3(0.56, 0.68, 0.7); 1.110 + 1.111 + img_marble.create(512, 512); 1.112 + unsigned char *pptr = img_marble.pixels; 1.113 + for(int i=0; i<img_marble.height; i++) { 1.114 + float v = (float)i / (float)img_marble.height; 1.115 + for(int j=0; j<img_marble.width; j++) { 1.116 + float u = (float)j / (float)img_marble.width; 1.117 + 1.118 + float marble_val = marble(u, v); 1.119 + Vector3 color = lerp(marble_col2, marble_col1, marble_val); 1.120 + 1.121 + int r = (int)(color.x * 255.0); 1.122 + int g = (int)(color.y * 255.0); 1.123 + int b = (int)(color.z * 255.0); 1.124 + 1.125 + pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r); 1.126 + pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g); 1.127 + pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b); 1.128 + pptr += 3; 1.129 + } 1.130 + } 1.131 + img_marble.texture(); 1.132 + 1.133 + return true; 1.134 +}