added starfield
[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 = 100.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;
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         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;
39
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);
42         }
43         return 0;
44 }
45
46 void draw_starfield(void)
47 {
48         int i;
49         cgm_vec3 pos;
50         float x, y, z, t;
51         float x0, y0, x1, y1;
52         float theta, sz;
53
54         glPushAttrib(GL_ENABLE_BIT);
55         glDisable(GL_DEPTH_TEST);
56         glDisable(GL_LIGHTING);
57         glDisable(GL_CULL_FACE);
58         glEnable(GL_BLEND);
59         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
60
61         glEnable(GL_TEXTURE_2D);
62         glBindTexture(GL_TEXTURE_2D, tex_bolt);
63
64         glBegin(GL_QUADS);
65         for(i=0; i<STAR_COUNT; i++) {
66                 pos = star[i];
67                 z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
68                 t = z / star_depth;
69                 pos.z = z - star_depth + STAR_ZOFFS;
70
71                 theta = atan2(pos.y / star_lenxy[i], pos.x / star_lenxy[i]);
72
73                 y = -star_size;
74                 x = 0;
75
76                 x0 = x * cos(theta) - y * sin(theta);
77                 y0 = x * sin(theta) + y * cos(theta);
78
79                 y = star_size;
80
81                 x1 = x * cos(theta) - y * sin(theta);
82                 y1 = x * sin(theta) + y * cos(theta);
83
84                 x0 += pos.x;
85                 x1 += pos.x;
86                 y0 += pos.y;
87                 y1 += pos.y;
88
89                 glColor4f(1, 1, 1, t);
90                 glTexCoord2f(0, 1);
91                 glVertex3f(x0, y0, pos.z);
92                 glTexCoord2f(1, 1);
93                 glVertex3f(x1, y1, pos.z);
94                 glTexCoord2f(1, 0);
95                 glVertex3f(x1, y1, pos.z - star_size * 16.0);
96                 glTexCoord2f(0, 0);
97                 glVertex3f(x0, y0, pos.z - star_size * 16.0);
98         }
99         glEnd();
100
101         glBindTexture(GL_TEXTURE_2D, tex_star);
102         sz = star_size * 4.0f;
103         glBegin(GL_QUADS);
104         for(i=0; i<STAR_COUNT; i++) {
105                 pos = star[i];
106                 z = fmod(pos.z + time_msec * star_speed / 1000.0f, star_depth);
107                 t = z / star_depth;
108                 pos.z = z - star_depth + STAR_ZOFFS;
109
110                 glColor4f(1, 1, 1, t);
111                 glTexCoord2f(0, 0);
112                 glVertex3f(pos.x - sz, pos.y - sz, pos.z);
113                 glTexCoord2f(1, 0);
114                 glVertex3f(pos.x + sz, pos.y - sz, pos.z);
115                 glTexCoord2f(1, 1);
116                 glVertex3f(pos.x + sz, pos.y + sz, pos.z);
117                 glTexCoord2f(0, 1);
118                 glVertex3f(pos.x - sz, pos.y + sz, pos.z);
119         }
120         glEnd();
121
122         glPopAttrib();
123 }