fixed multiple insertions of the same texture
[summerhack] / src / parts / space.cpp
1 #include "space.hpp"
2
3 static void make_skycube(Scene *scene);
4 static void make_particles(Scene *scene);
5 static void draw_design(float t);
6
7 static const float des_sz = 0.1;
8 static const int des_count = (int)(1.0 / des_sz);
9 static const float des_offs = des_sz;
10
11 #define ANIM
12
13 SpacePart::SpacePart() : ScenePart("space", new Scene) {
14 #ifdef ANIM
15         TargetCamera *cam = new TargetCamera(Vector3(0, 15, -5));
16 #else
17         TargetCamera *cam = new TargetCamera(Vector3(0, 30, -250));
18 #endif
19         cam->set_fov(DEG_TO_RAD(50));
20         scene->add_camera(cam);
21
22         PointLight *lt = new PointLight(Vector3(10, 100, -100));
23         scene->add_light(lt);
24
25         scene->set_ambient_light(0.1);
26
27         make_skycube(scene);
28         make_particles(scene);
29
30         MotionController cam_x, cam_y, cam_z;
31
32         cam_x = MotionController(CTRL_COS, TIME_FREE);
33         cam_x.set_sin_func(0.7, 250);
34         cam_x.set_control_axis(CTRL_X);
35
36         cam_z = MotionController(CTRL_SIN, TIME_FREE);
37         cam_z.set_sin_func(0.9, 120);
38         cam_z.set_control_axis(CTRL_Z);
39
40         cam_y = MotionController(CTRL_SIN, TIME_FREE);
41         cam_y.set_sin_func(0.5, 40);
42         cam_y.set_control_axis(CTRL_Y);
43
44 #ifdef ANIM
45         cam->add_controller(cam_x, CTRL_TRANSLATION);
46         cam->add_controller(cam_z, CTRL_TRANSLATION);
47         cam->add_controller(cam_y, CTRL_TRANSLATION);
48 #endif
49
50         scene->set_background(0.5);
51
52         //scene->render_sequence(0, 15000, 25);
53         //exit(0);
54 }
55
56 SpacePart::~SpacePart() {
57 }
58
59 void SpacePart::draw_part() {
60         float t = (float)time / 1000.0;
61
62         ScenePart::draw_part();
63         draw_design(t);
64 }
65
66 static void make_skycube(Scene *scene) {
67         const float size = 1000;
68         Object *face[6];
69         Texture *tex[6];
70
71         face[0] = new ObjPlane(Vector3(0, -1, 0), Vector2(size, size), 0);
72         face[0]->translate(Vector3(0, size / 2, 0));
73         tex[0] = get_texture("data/img/nebula_py.jpg");
74         
75         face[1] = new ObjPlane(Vector3(0, 1, 0), Vector2(size, size), 0);
76         face[1]->translate(Vector3(0, -size / 2, 0));
77         tex[1] = get_texture("data/img/nebula_ny.jpg");
78
79         face[2] = new ObjPlane(Vector3(0, 0, -1), Vector2(size, size), 0);
80         face[2]->translate(Vector3(0, 0, size / 2));
81         tex[2] = get_texture("data/img/nebula_pz.jpg");
82         
83         face[3] = new ObjPlane(Vector3(0, 0, 1), Vector2(size, size), 0);
84         face[3]->translate(Vector3(0, 0, -size / 2));
85         tex[3] = get_texture("data/img/nebula_nz.jpg");
86
87         face[4] = new ObjPlane(Vector3(-1, 0, 0), Vector2(size, size), 0);
88         face[4]->translate(Vector3(size / 2, 0, 0));
89         tex[4] = get_texture("data/img/nebula_px.jpg");
90         
91         face[5] = new ObjPlane(Vector3(1, 0, 0), Vector2(size, size), 0);
92         face[5]->translate(Vector3(-size / 2, 0, 0));
93         tex[5] = get_texture("data/img/nebula_nx.jpg");
94
95         for(int i=0; i<6; i++) {
96                 Material *mat = face[i]->get_material_ptr();
97                 mat->emissive_color = 1.0;
98                 mat->set_texture(tex[i], TEXTYPE_DIFFUSE);
99                 face[i]->set_texture_addressing(TEXADDR_CLAMP);
100                 scene->add_object(face[i]);
101         }
102 }
103
104 static void make_particles(Scene *scene) {
105         MotionController mc(CTRL_LIN, TIME_FREE);
106         mc.set_slope(1);
107         mc.set_control_axis(CTRL_Y);
108         
109         ParticleSystem *galaxy = new ParticleSystem("data/galaxy.psys");
110         galaxy->add_controller(mc, CTRL_ROTATION);
111         scene->add_particle_sys(galaxy);
112
113         galaxy = new ParticleSystem("data/galaxy.psys");
114         mc.set_origin(half_pi);
115         galaxy->add_controller(mc, CTRL_ROTATION);
116         scene->add_particle_sys(galaxy);
117
118         ParticleSystem *stars = new ParticleSystem("data/stars.psys");
119         scene->add_particle_sys(stars);
120 }
121
122 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
123
124 static void draw_design(float t) {
125         static const float des_fill = 3.0;
126         static const float yoffs = des_sz * 0.8;// + des_sz / 8.0;
127         static const Vector2 szvec(des_sz / 2, des_sz / 2);
128         static const float phase_offs = half_pi;
129         static const float x = des_sz;
130         static const float bar_len = 0.95;
131
132         static const float leave_start = 19.0;
133         static const float leave_dur = 4.0;
134         
135         float fill_t;
136         if(t < leave_start) {
137                 fill_t = MIN(1.0, t / des_fill);
138         } else {
139                 fill_t = 1.0 - (t - leave_start) / leave_dur;
140         }
141
142         set_zbuffering(false);
143         set_lighting(false);
144         set_alpha_blending(true);
145         set_blend_func(BLEND_ONE_MINUS_DST_COLOR, BLEND_ZERO);
146
147         for(int i=0; i<des_count; i++) {
148                 if((float)i / (float)des_count > fill_t) break;
149                 Vector2 pos(x + des_sz * (float)i, yoffs);
150         
151                 glMatrixMode(GL_MODELVIEW);
152                 glLoadIdentity();
153                 glTranslatef(pos.x, pos.y, 0);
154                 glRotatef(100.0 * cos(t * 3.0 + (float)i * phase_offs), 0, 0, 1);
155                 glTranslatef(-pos.x, -pos.y, 0);
156                 
157                 float sz = sin(t * 4.0 + (float)i * phase_offs) * 0.25 + 0.5;
158                 draw_scr_quad(pos - szvec * sz, pos + szvec * sz, 1.0, false);
159                 draw_scr_quad(pos - szvec * sz * 0.7, pos + szvec * sz * 0.7, 1.0, false);
160
161                 pos = Vector2(1.0, 1.0) - pos;
162                 glMatrixMode(GL_MODELVIEW);
163                 glLoadIdentity();
164                 glTranslatef(pos.x, pos.y, 0);
165                 glRotatef(100.0 * cos(t * 3.0 + (float)i * phase_offs), 0, 0, 1);
166                 glTranslatef(-pos.x, -pos.y, 0);
167                 
168                 draw_scr_quad(pos - szvec * sz, pos + szvec * sz, 1.0, false);
169                 draw_scr_quad(pos - szvec * sz * 0.7, pos + szvec * sz * 0.7, 1.0, false);
170         }
171
172         float bar_rest = 1.0 - bar_len;
173         float bar_start = bar_rest / 2.0;
174         float bar_end = bar_start + bar_len * fill_t;
175         draw_scr_quad(Vector2(bar_start, yoffs - 0.002), Vector2(bar_end, yoffs + 0.002));
176         draw_scr_quad(Vector2(1.0 - bar_start, 1.0 - yoffs + 0.002), Vector2(1.0 - bar_end, 1.0 - yoffs - 0.002));
177         
178         set_alpha_blending(false);
179         set_lighting(true);
180         set_zbuffering(true);
181 }