tavli

view src/scenery.cc @ 15:b1a195c3ee16

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