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)
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 for(i=0; i<STAR_COUNT; i++) {
36 float x = (2.0f * rand() / RAND_MAX) - 1.0;
37 float y = (2.0f * rand() / RAND_MAX) - 1.0;
38 float z = (float)rand() / RAND_MAX;
40 cgm_vcons(star + i, x * width, y * width, z * star_depth);
41 star_lenxy[i] = sqrt(star[i].x * star[i].x + star[i].y * star[i].y);
46 void draw_starfield(void)
54 glPushAttrib(GL_ENABLE_BIT);
55 glDisable(GL_DEPTH_TEST);
56 glDisable(GL_LIGHTING);
57 glDisable(GL_CULL_FACE);
59 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
61 glEnable(GL_TEXTURE_2D);
62 glBindTexture(GL_TEXTURE_2D, tex_bolt);
65 for(i=0; i<STAR_COUNT; i++) {
67 z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
69 pos.z = z - star_depth + STAR_ZOFFS;
71 theta = atan2(pos.y / star_lenxy[i], pos.x / star_lenxy[i]);
76 x0 = x * cos(theta) - y * sin(theta);
77 y0 = x * sin(theta) + y * cos(theta);
81 x1 = x * cos(theta) - y * sin(theta);
82 y1 = x * sin(theta) + y * cos(theta);
89 glColor4f(1, 1, 1, t);
91 glVertex3f(x0, y0, pos.z);
93 glVertex3f(x1, y1, pos.z);
95 glVertex3f(x1, y1, pos.z - star_size * 16.0);
97 glVertex3f(x0, y0, pos.z - star_size * 16.0);
101 glBindTexture(GL_TEXTURE_2D, tex_star);
102 sz = star_size * 4.0f;
104 for(i=0; i<STAR_COUNT; i++) {
106 z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
108 pos.z = z - star_depth + STAR_ZOFFS;
110 glColor4f(1, 1, 1, t);
112 glVertex3f(pos.x - sz, pos.y - sz, pos.z);
114 glVertex3f(pos.x + sz, pos.y - sz, pos.z);
116 glVertex3f(pos.x + sz, pos.y + sz, pos.z);
118 glVertex3f(pos.x - sz, pos.y + sz, pos.z);