4 #include <cgmath/cgmath.h>
10 static unsigned int tex_bolt, tex_star;
11 static float star_speed = 50.0f;
12 static float star_depth = 1000.0f;
13 static float star_size = 0.35f;
15 #define STAR_ZOFFS 100.0f
16 #define STAR_COUNT 4096
17 static cgm_vec3 star[STAR_COUNT];
18 static float star_lenxy[STAR_COUNT];
20 int init_starfield(void)
23 float width, excl_rad;
25 if(!(tex_star = img_gltexture_load("data/pimg.png"))) {
26 error_log("failed to load star texture\n");
29 if(!(tex_bolt = img_gltexture_load("data/bolt.png"))) {
30 error_log("failed to load star tail texture\n");
34 width = star_depth / 4.0f;
35 excl_rad = 10.0f / width;
36 for(i=0; i<STAR_COUNT; i++) {
40 x = (2.0f * rand() / RAND_MAX) - 1.0;
41 y = (2.0f * rand() / RAND_MAX) - 1.0;
42 } while(x * x + y * y < excl_rad * excl_rad);
43 z = (float)rand() / RAND_MAX;
45 cgm_vcons(star + i, x * width, y * width, z * star_depth);
46 star_lenxy[i] = sqrt(star[i].x * star[i].x + star[i].y * star[i].y);
51 void draw_starfield(void)
59 glPushAttrib(GL_ENABLE_BIT);
60 glDisable(GL_DEPTH_TEST);
61 glDisable(GL_LIGHTING);
62 glDisable(GL_CULL_FACE);
64 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
66 glEnable(GL_TEXTURE_2D);
67 glBindTexture(GL_TEXTURE_2D, tex_bolt);
70 for(i=0; i<STAR_COUNT; i++) {
72 z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
74 pos.z = z - star_depth + STAR_ZOFFS;
76 theta = atan2(pos.y / star_lenxy[i], pos.x / star_lenxy[i]);
81 x0 = x * cos(theta) - y * sin(theta);
82 y0 = x * sin(theta) + y * cos(theta);
86 x1 = x * cos(theta) - y * sin(theta);
87 y1 = x * sin(theta) + y * cos(theta);
94 glColor4f(1, 1, 1, t);
96 glVertex3f(x0, y0, pos.z);
98 glVertex3f(x1, y1, pos.z);
100 glVertex3f(x1, y1, pos.z - star_size * 16.0);
102 glVertex3f(x0, y0, pos.z - star_size * 16.0);
106 glBindTexture(GL_TEXTURE_2D, tex_star);
107 sz = star_size * 4.0f;
109 for(i=0; i<STAR_COUNT; i++) {
111 z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
113 pos.z = z - star_depth + STAR_ZOFFS;
115 glColor4f(1, 1, 1, t);
117 glVertex3f(pos.x - sz, pos.y - sz, pos.z);
119 glVertex3f(pos.x + sz, pos.y - sz, pos.z);
121 glVertex3f(pos.x + sz, pos.y + sz, pos.z);
123 glVertex3f(pos.x - sz, pos.y + sz, pos.z);