tavli

view src/scenery.cc @ 23:3e6430028d54

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