Added my seq file with an animation for the lighthouse
[faros-demo] / src / geom.cc
1 #include <math.h>
2 #include <GL/glew.h>
3 #include <GL/freeglut.h>
4 #include "geom.h"
5 #include "sdr.h"
6
7 static unsigned int sdr_curve_top;
8 static unsigned int tex_xcircle;
9
10 static const unsigned char tex_xcircle_pixels[] = {
11         0, 64, 255, 255, 255, 255, 64, 0,
12         0, 64, 255, 255, 255, 255, 64, 0,
13         0, 64, 255, 255, 255, 255, 64, 0,
14         0, 64, 255, 255, 255, 255, 64, 0,
15         0, 64, 255, 255, 255, 255, 64, 0,
16         0, 64, 255, 255, 255, 255, 64, 0,
17         0, 64, 255, 255, 255, 255, 64, 0,
18         0, 64, 255, 255, 255, 255, 64, 0,
19
20         0, 64, 255, 255, 255, 255, 64, 0,
21         0, 0, 255, 255, 255, 255, 0, 0,
22         0, 0, 128, 255, 255, 128, 0, 0,
23         0, 0, 0, 64, 64, 0, 0, 0,
24         0, 0, 0, 0, 0, 0, 0, 0,
25         0, 0, 0, 0, 0, 0, 0, 0,
26         0, 0, 0, 0, 0, 0, 0, 0,
27         0, 0, 0, 0, 0, 0, 0, 0
28 };
29
30
31 bool init_geom()
32 {
33         if(!(sdr_curve_top = create_program_load("sdr/curve_top.v.glsl", "sdr/curve_top.f.glsl"))) {
34                 return false;
35         }
36
37         glGenTextures(1, &tex_xcircle);
38         glBindTexture(GL_TEXTURE_2D, tex_xcircle);
39         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
40         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
41         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
42         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 8, 16, 0,
43                         GL_LUMINANCE, GL_UNSIGNED_BYTE, tex_xcircle_pixels);
44         return true;
45 }
46
47 void destroy_geom()
48 {
49         glDeleteTextures(1, &tex_xcircle);
50         free_program(sdr_curve_top);
51 }
52
53 void faros()
54 {
55         // kormos
56         glPushMatrix();
57         glScalef(1.1, 3, 1.1);
58         glTranslatef(0, 0.5, 0);
59         glutSolidCube(1.0);
60         glPopMatrix();
61
62         glShadeModel(GL_FLAT);
63
64         // base
65         glPushMatrix();
66         glRotatef(90, 1, 0, 0);
67         glTranslatef(0, -0.15, 0);
68         glutSolidCylinder(2, 0.3, 16, 1);
69         glPopMatrix();
70
71         // middle cylinder
72         glPushMatrix();
73         glTranslatef(0, 3, 0);
74         glRotatef(22.5, 0, 1, 0);
75         glRotatef(-90, 1, 0, 0);
76         glutSolidCylinder(0.5, 1.0, 8, 1);
77         glPopMatrix();
78
79         // trim middle cylinder (mporntoura)
80         glPushMatrix();
81         glTranslatef(0, 3.9, 0);
82         glRotatef(22.5, 0, 1, 0);
83         glRotatef(-90, 1, 0, 0);
84         glutSolidCylinder(0.55, 0.02, 8, 1);
85         glPopMatrix();
86
87         // top smaller cylinder
88         glPushMatrix();
89         glTranslatef(0, 4, 0);
90         glRotatef(22.5, 0, 1, 0);
91         glRotatef(-90, 1, 0, 0);
92         glutSolidCylinder(0.28, 0.5, 8, 1);
93         glPopMatrix();
94
95         // top wire even smaller cylinder
96         glPushMatrix();
97         glTranslatef(0, 4.5, 0);
98         glRotatef(22.5, 0, 1, 0);
99         glRotatef(-90, 1, 0, 0);
100         glutWireCylinder(0.18, 0.3, 9, 3);
101         glPopMatrix();
102
103         glShadeModel(GL_SMOOTH);
104
105         // top troulos
106         glPushMatrix();
107         glTranslatef(0, 4.8, 0);
108         glRotatef(22.5, 0, 1, 0);
109         glRotatef(-90, 1, 0, 0);
110         glutSolidCone(0.18, 0.2, 9, 1);
111         glPopMatrix();
112
113         // tsamploukano
114         glPushMatrix();
115         glTranslatef(-0.28, 4, 0);
116         glScalef(1, 13, 1);
117         glutSolidSphere(0.1, 16, 16);
118         glPopMatrix();
119
120         //pyramid on top of kormos
121         bind_program(sdr_curve_top);
122
123         glPushMatrix();
124         glTranslatef(0, 3, 0);
125         glRotatef(45, 0, 1, 0);
126         glRotatef(-90, 1, 0, 0);
127         glScalef(1, 1, 0.45);
128         glutSolidCylinder(1, 1, 4, 16);
129         glPopMatrix();
130
131         bind_program(0);
132 }
133
134
135 void ground()
136 {
137         glPushMatrix();
138
139         glTranslatef(0, -1.25, 0);
140         glScalef(1, 0.1, 1);
141
142         glutSolidSphere(10, 32, 32);
143
144         glPopMatrix();
145 }
146
147 void xlogo(float sz, const float *col_ink, const float *col_paper, float alpha, float xcircle)
148 {
149         static const float xlogo_varr[] = {
150                 -0.500, 0.407, -0.113, -0.109, 0.059, -0.006, -0.251, 0.407,
151                 -0.113, -0.109, -0.499, -0.593, -0.410, -0.593, 0.059, -0.006,
152                 -0.058, -0.182, 0.251, -0.593, 0.500, -0.593, 0.114, -0.079,
153                 -0.058, -0.182, 0.114, -0.079, 0.500, 0.407, 0.411, 0.407
154         };
155
156         /* billboarding */
157         float mv[16];
158         glGetFloatv(GL_MODELVIEW_MATRIX, mv);
159         mv[0] = mv[5] = mv[10] = sz;
160         mv[1] = mv[2] = mv[4] = mv[6] = mv[8] = mv[9] = 0.0f;
161
162         glPushMatrix();
163         glLoadMatrixf(mv);
164         glTranslatef(0, 0.15, 0);
165
166         glPushAttrib(GL_ENABLE_BIT);
167         glEnable(GL_BLEND);
168         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
169
170         glBegin(GL_QUADS);
171         glColor4f(col_ink[0], col_ink[1], col_ink[2], alpha);
172         const float *vptr = xlogo_varr;
173         for(int i=0; i<(int)(sizeof xlogo_varr / sizeof *xlogo_varr) / 2; i++) {
174                 glVertex2fv(vptr);
175                 vptr += 2;
176         }
177         glEnd();
178         glTranslatef(0, -0.15, 0);
179         glDisable(GL_BLEND);
180
181         glEnable(GL_TEXTURE_2D);
182         glBindTexture(GL_TEXTURE_2D, tex_xcircle);
183
184         glDisable(GL_CULL_FACE);
185         glEnable(GL_ALPHA_TEST);
186         float aref = 1.0f - xcircle;
187         glAlphaFunc(GL_GREATER, aref > 0.0f ? aref : 0.0f);
188
189         glScalef(1.4, 1, 1);
190
191 #define XLOGO_CIRCLE_SEG        64
192         // circle thingy
193         glBegin(GL_QUAD_STRIP);
194         for(int i=0; i<XLOGO_CIRCLE_SEG; i++) {
195                 float t = (float)i / (float)(XLOGO_CIRCLE_SEG - 1);
196                 float tcol = fmod(t + 0.075f, 1.0f);
197
198                 float theta = t * M_PI * 2.0f;
199                 float rad = 0.4f;
200                 float width = 0.05f * tcol;
201                 float z = -cos(theta) * 0.1;
202
203                 glColor4f(col_paper[0], col_paper[1], col_paper[2], tcol);
204                 glTexCoord2f(0, 1.0f - tcol);
205                 glVertex3f(sin(theta) * (rad + width), cos(theta) * (rad + width), z);
206                 glTexCoord2f(1, 1.0f - tcol);
207                 glVertex3f(sin(theta) * (rad - width), cos(theta) * (rad - width), z);
208         }
209         glEnd();
210
211         glPopAttrib();
212         glPopMatrix();
213 }