ok now it works nicely in VR
[vrtris] / src / starfield.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <cgmath/cgmath.h>
5 #include <imago2.h>
6 #include "opengl.h"
7 #include "game.h"
8 #include "logger.h"
9
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;
14
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];
19
20 int init_starfield(void)
21 {
22         int i;
23         float width, excl_rad;
24
25         if(!(tex_star = img_gltexture_load("data/pimg.png"))) {
26                 error_log("failed to load star texture\n");
27                 return -1;
28         }
29         if(!(tex_bolt = img_gltexture_load("data/bolt.png"))) {
30                 error_log("failed to load star tail texture\n");
31                 return -1;
32         }
33
34         width = star_depth / 4.0f;
35         excl_rad = 10.0f / width;
36         for(i=0; i<STAR_COUNT; i++) {
37                 float x, y, z;
38
39                 do {
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;
44
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);
47         }
48         return 0;
49 }
50
51 void draw_starfield(void)
52 {
53         int i;
54         cgm_vec3 pos;
55         float x, y, z, t;
56         float x0, y0, x1, y1;
57         float theta, sz;
58
59         glPushAttrib(GL_ENABLE_BIT);
60         glDisable(GL_DEPTH_TEST);
61         glDisable(GL_LIGHTING);
62         glDisable(GL_CULL_FACE);
63         glEnable(GL_BLEND);
64         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
65
66         glEnable(GL_TEXTURE_2D);
67         glBindTexture(GL_TEXTURE_2D, tex_bolt);
68
69         glBegin(GL_QUADS);
70         for(i=0; i<STAR_COUNT; i++) {
71                 pos = star[i];
72                 z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
73                 t = z / star_depth;
74                 pos.z = z - star_depth + STAR_ZOFFS;
75
76                 theta = atan2(pos.y / star_lenxy[i], pos.x / star_lenxy[i]);
77
78                 y = -star_size;
79                 x = 0;
80
81                 x0 = x * cos(theta) - y * sin(theta);
82                 y0 = x * sin(theta) + y * cos(theta);
83
84                 y = star_size;
85
86                 x1 = x * cos(theta) - y * sin(theta);
87                 y1 = x * sin(theta) + y * cos(theta);
88
89                 x0 += pos.x;
90                 x1 += pos.x;
91                 y0 += pos.y;
92                 y1 += pos.y;
93
94                 glColor4f(1, 1, 1, t);
95                 glTexCoord2f(0, 1);
96                 glVertex3f(x0, y0, pos.z);
97                 glTexCoord2f(1, 1);
98                 glVertex3f(x1, y1, pos.z);
99                 glTexCoord2f(1, 0);
100                 glVertex3f(x1, y1, pos.z - star_size * 16.0);
101                 glTexCoord2f(0, 0);
102                 glVertex3f(x0, y0, pos.z - star_size * 16.0);
103         }
104         glEnd();
105
106         glBindTexture(GL_TEXTURE_2D, tex_star);
107         sz = star_size * 4.0f;
108         glBegin(GL_QUADS);
109         for(i=0; i<STAR_COUNT; i++) {
110                 pos = star[i];
111                 z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
112                 t = z / star_depth;
113                 pos.z = z - star_depth + STAR_ZOFFS;
114
115                 glColor4f(1, 1, 1, t);
116                 glTexCoord2f(0, 0);
117                 glVertex3f(pos.x - sz, pos.y - sz, pos.z);
118                 glTexCoord2f(1, 0);
119                 glVertex3f(pos.x + sz, pos.y - sz, pos.z);
120                 glTexCoord2f(1, 1);
121                 glVertex3f(pos.x + sz, pos.y + sz, pos.z);
122                 glTexCoord2f(0, 1);
123                 glVertex3f(pos.x - sz, pos.y + sz, pos.z);
124         }
125         glEnd();
126
127         glPopAttrib();
128 }