tavli

view src/scenery.cc @ 18:986c0b76513f

shadows, not completed
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Jun 2015 01:29:36 +0300
parents b1a195c3ee16
children 37dead56f01e
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 };
27 static const BezCurve table_curve = {
28 sizeof table_cp / sizeof *table_cp,
29 (vec2_t*)table_cp,
30 1.0 / 6.2
31 };
35 bool init_scenery()
36 {
37 unsigned int sdr = opt.shadows && sdr_shadow ? sdr_shadow : sdr_phong;
38 if(!gen_textures()) {
39 return false;
40 }
42 Matrix4x4 xform;
44 Mesh *table = new Mesh;
45 gen_revol(table, 48, 16, bezier_revol, bezier_revol_normal, (void*)&table_curve);
46 table->texcoord_gen_plane(Vector3(0, 1, 0), Vector3(1, 0, 0));
47 xform.set_scaling(Vector3(0.5, 0.5, 0.5));
48 xform.translate(Vector3(1, 1, 0));
49 table->texcoord_apply_xform(xform);
51 static const float table_scale = 1.8;
52 xform.set_scaling(Vector3(table_scale, table_scale, table_scale));
53 table->apply_xform(xform);
55 Object *otable = new Object;
56 otable->set_mesh(table);
57 otable->mtl.diffuse = Vector3(1, 1, 1);
58 otable->mtl.specular = Vector3(0.7, 0.7, 0.7);
59 otable->xform().set_translation(Vector3(0, -0.025, 0));
60 otable->set_texture(img_marble.texture());
61 otable->set_shader(sdr);
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 for(size_t i=0; i<obj.size(); i++) {
90 if(wireframe) {
91 obj[i]->draw_wire();
92 obj[i]->draw_normals(0.075);
93 } else {
94 obj[i]->draw();
95 }
96 }
97 }
99 static float marble(float x, float y)
100 {
101 float theta = x * M_PI * 2.0 * cos(y * 1.5);
102 theta += turbulence2(x * 10.0, y * 10.0, 4) * 2;
103 float val = 0.5 + sin(theta * 5.0) + sin(theta * 8.0) / 2.0 + sin(theta * 19.0) / 4.0;
104 return val * 0.5 + 0.5;
105 }
107 static bool gen_textures()
108 {
109 static const Vector3 marble_col1 = Vector3(0.78, 0.85, 0.85);
110 static const Vector3 marble_col2 = Vector3(0.56, 0.68, 0.7);
112 img_marble.create(512, 512);
113 unsigned char *pptr = img_marble.pixels;
114 for(int i=0; i<img_marble.height; i++) {
115 float v = (float)i / (float)img_marble.height;
116 for(int j=0; j<img_marble.width; j++) {
117 float u = (float)j / (float)img_marble.width;
119 float marble_val = marble(u, v);
120 Vector3 color = lerp(marble_col2, marble_col1, marble_val) * 0.6;
122 int r = (int)(color.x * 255.0);
123 int g = (int)(color.y * 255.0);
124 int b = (int)(color.z * 255.0);
126 pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
127 pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
128 pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
129 pptr += 3;
130 }
131 }
132 img_marble.texture();
134 return true;
135 }