tavli

view 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 source
1 #include <vector>
2 #include "opengl.h"
3 #include "scenery.h"
4 #include "game.h"
5 #include "mesh.h"
6 #include "meshgen.h"
7 #include "object.h"
8 #include "revol.h"
9 #include "image.h"
11 static bool gen_textures();
13 static std::vector<Object*> obj;
14 static Image img_marble;
16 static const vec2_t table_cp[] = {
17 {0, 0},
18 {3, 0}, // mid 0
19 {5.8, 0},
20 {5.99, 0}, // mid 1
21 {6, -0.15},
22 {6.15, -0.15}, // mid 2
23 {6.2, -0.4}
24 };
25 static const BezCurve table_curve = {
26 sizeof table_cp / sizeof *table_cp,
27 (vec2_t*)table_cp,
28 1.0 / 6.2
29 };
33 bool init_scenery()
34 {
35 if(!gen_textures()) {
36 return false;
37 }
39 Matrix4x4 xform;
41 Mesh *table = new Mesh;
42 gen_revol(table, 48, 12, bezier_revol, bezier_revol_normal, (void*)&table_curve);
43 table->texcoord_gen_plane(Vector3(0, 1, 0), Vector3(1, 0, 0));
44 xform.set_scaling(Vector3(0.5, 0.5, 0.5));
45 xform.translate(Vector3(1, 1, 0));
46 table->texcoord_apply_xform(xform);
48 static const float table_scale = 1.8;
49 xform.set_scaling(Vector3(table_scale, table_scale, table_scale));
50 table->apply_xform(xform);
52 Object *otable = new Object;
53 otable->set_mesh(table);
54 otable->mtl.diffuse = Vector3(0.6, 0.6, 0.6);
55 otable->mtl.specular = Vector3(0.8, 0.8, 0.8);
56 otable->xform().set_translation(Vector3(0, -0.025, 0));
57 otable->set_texture(img_marble.texture());
58 obj.push_back(otable);
61 // meshgen stats
62 printf("Generated scenery:\n %u meshes\n", (unsigned int)obj.size());
63 unsigned int polycount = 0;
64 for(size_t i=0; i<obj.size(); i++) {
65 const Mesh *m = obj[i]->get_mesh();
66 polycount += m->get_poly_count();
67 }
68 printf(" %u polygons\n", polycount);
70 return true;
71 }
73 void destroy_scenery()
74 {
75 for(size_t i=0; i<obj.size(); i++) {
76 delete obj[i];
77 }
78 obj.clear();
80 img_marble.destroy();
81 }
83 void draw_scenery()
84 {
85 for(size_t i=0; i<obj.size(); i++) {
86 if(wireframe) {
87 obj[i]->draw_wire();
88 obj[i]->draw_normals(0.075);
89 } else {
90 obj[i]->draw();
91 }
92 }
93 }
95 static float marble(float x, float y)
96 {
97 float theta = x * M_PI * 2.0 * cos(y * 1.5);
98 theta += turbulence2(x * 10.0, y * 10.0, 4) * 2;
99 float val = 0.5 + sin(theta * 5.0) + sin(theta * 8.0) / 2.0 + sin(theta * 19.0) / 4.0;
100 return val * 0.5 + 0.5;
101 }
103 static bool gen_textures()
104 {
105 static const Vector3 marble_col1 = Vector3(0.78, 0.85, 0.85);
106 static const Vector3 marble_col2 = Vector3(0.56, 0.68, 0.7);
108 img_marble.create(512, 512);
109 unsigned char *pptr = img_marble.pixels;
110 for(int i=0; i<img_marble.height; i++) {
111 float v = (float)i / (float)img_marble.height;
112 for(int j=0; j<img_marble.width; j++) {
113 float u = (float)j / (float)img_marble.width;
115 float marble_val = marble(u, v);
116 Vector3 color = lerp(marble_col2, marble_col1, marble_val);
118 int r = (int)(color.x * 255.0);
119 int g = (int)(color.y * 255.0);
120 int b = (int)(color.z * 255.0);
122 pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
123 pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
124 pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
125 pptr += 3;
126 }
127 }
128 img_marble.texture();
130 return true;
131 }