tavli

annotate src/scenery.cc @ 16:d6209903454b

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