added summerhack
[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                 add_texture(tex[i]);
99                 mat->set_texture(tex[i], TEXTYPE_DIFFUSE);
100                 face[i]->set_texture_addressing(TEXADDR_CLAMP);
101                 scene->add_object(face[i]);
102         }
103 }
104
105 static void make_particles(Scene *scene) {
106         MotionController mc(CTRL_LIN, TIME_FREE);
107         mc.set_slope(1);
108         mc.set_control_axis(CTRL_Y);
109         
110         ParticleSystem *galaxy = new ParticleSystem("data/galaxy.psys");
111         galaxy->add_controller(mc, CTRL_ROTATION);
112         scene->add_particle_sys(galaxy);
113
114         galaxy = new ParticleSystem("data/galaxy.psys");
115         mc.set_origin(half_pi);
116         galaxy->add_controller(mc, CTRL_ROTATION);
117         scene->add_particle_sys(galaxy);
118
119         ParticleSystem *stars = new ParticleSystem("data/stars.psys");
120         scene->add_particle_sys(stars);
121 }
122
123 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
124
125 static void draw_design(float t) {
126         static const float des_fill = 3.0;
127         static const float yoffs = des_sz * 0.8;// + des_sz / 8.0;
128         static const Vector2 szvec(des_sz / 2, des_sz / 2);
129         static const float phase_offs = half_pi;
130         static const float x = des_sz;
131         static const float bar_len = 0.95;
132
133         static const float leave_start = 19.0;
134         static const float leave_dur = 4.0;
135         
136         float fill_t;
137         if(t < leave_start) {
138                 fill_t = MIN(1.0, t / des_fill);
139         } else {
140                 fill_t = 1.0 - (t - leave_start) / leave_dur;
141         }
142
143         set_zbuffering(false);
144         set_lighting(false);
145         set_alpha_blending(true);
146         set_blend_func(BLEND_ONE_MINUS_DST_COLOR, BLEND_ZERO);
147
148         for(int i=0; i<des_count; i++) {
149                 if((float)i / (float)des_count > fill_t) break;
150                 Vector2 pos(x + des_sz * (float)i, yoffs);
151         
152                 glMatrixMode(GL_MODELVIEW);
153                 glLoadIdentity();
154                 glTranslatef(pos.x, pos.y, 0);
155                 glRotatef(100.0 * cos(t * 3.0 + (float)i * phase_offs), 0, 0, 1);
156                 glTranslatef(-pos.x, -pos.y, 0);
157                 
158                 float sz = sin(t * 4.0 + (float)i * phase_offs) * 0.25 + 0.5;
159                 draw_scr_quad(pos - szvec * sz, pos + szvec * sz, 1.0, false);
160                 draw_scr_quad(pos - szvec * sz * 0.7, pos + szvec * sz * 0.7, 1.0, false);
161
162                 pos = Vector2(1.0, 1.0) - pos;
163                 glMatrixMode(GL_MODELVIEW);
164                 glLoadIdentity();
165                 glTranslatef(pos.x, pos.y, 0);
166                 glRotatef(100.0 * cos(t * 3.0 + (float)i * phase_offs), 0, 0, 1);
167                 glTranslatef(-pos.x, -pos.y, 0);
168                 
169                 draw_scr_quad(pos - szvec * sz, pos + szvec * sz, 1.0, false);
170                 draw_scr_quad(pos - szvec * sz * 0.7, pos + szvec * sz * 0.7, 1.0, false);
171         }
172
173         float bar_rest = 1.0 - bar_len;
174         float bar_start = bar_rest / 2.0;
175         float bar_end = bar_start + bar_len * fill_t;
176         draw_scr_quad(Vector2(bar_start, yoffs - 0.002), Vector2(bar_end, yoffs + 0.002));
177         draw_scr_quad(Vector2(1.0 - bar_start, 1.0 - yoffs + 0.002), Vector2(1.0 - bar_end, 1.0 - yoffs - 0.002));
178         
179         set_alpha_blending(false);
180         set_lighting(true);
181         set_zbuffering(true);
182 }