2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
28 #define PI 3.1415926535897932
30 /*---------------------------------------------------------------------------*/
32 static GLuint ball_list;
33 static GLuint ball_text[2];
38 int i, slices = b ? 32 : 16;
39 int j, stacks = b ? 16 : 8;
41 config_get_s(CONFIG_BALL, name, MAXSTR);
42 ball_text[0] = make_image_from_file(NULL, NULL, NULL, NULL, name);
44 config_get_s(CONFIG_BALL_BONUS, name, MAXSTR);
45 ball_text[1] = make_image_from_file(NULL, NULL, NULL, NULL, name);
47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
50 ball_list = glGenLists(1);
52 glNewList(ball_list, GL_COMPILE);
54 for (i = 0; i < stacks; i++)
56 float k0 = (float) i / stacks;
57 float k1 = (float) (i + 1) / stacks;
59 float s0 = fsinf(V_PI * (k0 - 0.5));
60 float c0 = fcosf(V_PI * (k0 - 0.5));
61 float s1 = fsinf(V_PI * (k1 - 0.5));
62 float c1 = fcosf(V_PI * (k1 - 0.5));
64 glBegin(GL_QUAD_STRIP);
66 for (j = 0; j <= slices; j++)
68 float k = (float) j / slices;
69 float s = fsinf(V_PI * k * 2.0);
70 float c = fcosf(V_PI * k * 2.0);
73 glNormal3f(s * c0, c * c0, s0);
74 glVertex3f(s * c0, c * c0, s0);
77 glNormal3f(s * c1, c * c1, s1);
78 glVertex3f(s * c1, c * c1, s1);
89 if (glIsList(ball_list))
90 glDeleteLists(ball_list, 1);
92 if (glIsTexture(ball_text[0]))
93 glDeleteTextures(1, &ball_text[0]);
95 if (glIsTexture(ball_text[1]))
96 glDeleteTextures(1, &ball_text[1]);
103 void ball_draw(int i)
105 glPushAttrib(GL_POLYGON_BIT |
107 GL_DEPTH_BUFFER_BIT);
109 static const float s[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
110 static const float e[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
111 static const float h[1] = { 64.0f };
113 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, s);
114 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, e);
115 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, h);
117 glEnable(GL_COLOR_MATERIAL);
119 glBindTexture(GL_TEXTURE_2D, ball_text[i]);
121 /* Render the ball back to front in case it is translucent. */
123 glDepthMask(GL_FALSE);
125 glCullFace(GL_FRONT);
126 glCallList(ball_list);
128 glCallList(ball_list);
130 /* Render the ball into the depth buffer. */
132 glDepthMask(GL_TRUE);
133 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
135 glCallList(ball_list);
137 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
139 /* Ensure the ball is visible even when obscured by geometry. */
141 glDisable(GL_DEPTH_TEST);
143 glColor4f(1.0f, 1.0f, 1.0f, 0.1f);
144 glCallList(ball_list);
149 /*---------------------------------------------------------------------------*/
151 static GLuint mark_list;
153 void mark_init(int b)
155 int i, slices = b ? 32 : 16;
157 mark_list = glGenLists(1);
159 glNewList(mark_list, GL_COMPILE);
161 glBegin(GL_TRIANGLE_FAN);
163 glNormal3f(0.f, 1.f, 0.f);
165 for (i = 0; i < slices; i++)
167 float x = fcosf(-2.f * PI * i / slices);
168 float y = fsinf(-2.f * PI * i / slices);
180 glPushAttrib(GL_TEXTURE_BIT);
181 glPushAttrib(GL_LIGHTING_BIT);
182 glPushAttrib(GL_DEPTH_BUFFER_BIT);
184 glEnable(GL_COLOR_MATERIAL);
185 glDisable(GL_TEXTURE_2D);
186 glDepthMask(GL_FALSE);
188 glCallList(mark_list);
197 if (glIsList(mark_list))
198 glDeleteLists(mark_list, 1);
203 /*---------------------------------------------------------------------------*/
205 static GLuint coin_text;
206 static GLuint coin_list;
208 static void coin_head(int n, float radius, float thick)
212 glBegin(GL_TRIANGLE_FAN);
214 glNormal3f(0.f, 0.f, +1.f);
216 for (i = 0; i < n; i++)
218 float x = fcosf(+2.f * PI * i / n);
219 float y = fsinf(+2.f * PI * i / n);
221 glTexCoord2f(-x * 0.5f + 0.5f, +y * 0.5f + 0.5f);
222 glVertex3f(radius * x, radius * y, +thick);
228 static void coin_tail(int n, float radius, float thick)
232 glBegin(GL_TRIANGLE_FAN);
234 glNormal3f(0.f, 0.f, -1.f);
236 for (i = 0; i < n; i++)
238 float x = fcosf(-2.f * PI * i / n);
239 float y = fsinf(-2.f * PI * i / n);
241 glTexCoord2f(+x * 0.5f + 0.5f, +y * 0.5f + 0.5f);
242 glVertex3f(radius * x, radius * y, -thick);
248 static void coin_edge(int n, float radius, float thick)
252 glBegin(GL_QUAD_STRIP);
254 for (i = 0; i <= n; i++)
256 float x = fcosf(2.f * PI * i / n);
257 float y = fsinf(2.f * PI * i / n);
259 glNormal3f(x, y, 0.f);
260 glVertex3f(radius * x, radius * y, +thick);
261 glVertex3f(radius * x, radius * y, -thick);
267 void coin_color(float *c, int n)
289 void coin_init(int b)
293 coin_text = make_image_from_file(NULL, NULL, NULL, NULL, IMG_COIN);
294 coin_list = glGenLists(1);
296 glNewList(coin_list, GL_COMPILE);
298 coin_edge(n, COIN_RADIUS, COIN_THICK);
299 coin_head(n, COIN_RADIUS, COIN_THICK);
300 coin_tail(n, COIN_RADIUS, COIN_THICK);
307 if (glIsList(coin_list))
308 glDeleteLists(coin_list, 1);
310 if (glIsTexture(coin_text))
311 glDeleteTextures(1, &coin_text);
319 static const float a[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
320 static const float s[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
321 static const float e[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
322 static const float h[1] = { 32.0f };
324 glPushAttrib(GL_LIGHTING_BIT);
326 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, a);
327 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, s);
328 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, e);
329 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, h);
331 glEnable(GL_COLOR_MATERIAL);
332 glBindTexture(GL_TEXTURE_2D, coin_text);
335 void coin_draw(int n, float r)
342 glCallList(coin_list);
350 /*---------------------------------------------------------------------------*/
352 static GLuint goal_list;
354 void goal_init(int b)
356 int i, n = b ? 32 : 8;
358 goal_list = glGenLists(1);
360 glNewList(goal_list, GL_COMPILE);
362 glPushAttrib(GL_TEXTURE_BIT |
364 GL_DEPTH_BUFFER_BIT);
366 glEnable(GL_COLOR_MATERIAL);
367 glDisable(GL_LIGHTING);
368 glDisable(GL_TEXTURE_2D);
369 glDepthMask(GL_FALSE);
371 glBegin(GL_QUAD_STRIP);
373 for (i = 0; i <= n; i++)
375 float x = fcosf(2.f * PI * i / n);
376 float y = fsinf(2.f * PI * i / n);
378 glColor4f(1.0f, 1.0f, 0.0f, 0.5f);
379 glVertex3f(x, 0.0f, y);
381 glColor4f(1.0f, 1.0f, 0.0f, 0.0f);
382 glVertex3f(x, GOAL_HEIGHT, y);
394 if (glIsList(goal_list))
395 glDeleteLists(goal_list, 1);
402 glCallList(goal_list);
405 /*---------------------------------------------------------------------------*/
407 static GLuint jump_list;
409 void jump_init(int b)
411 int k, i, n = b ? 32 : 8;
413 jump_list = glGenLists(2);
415 for (k = 0; k < 12; k++)
417 glNewList(jump_list + k, GL_COMPILE);
419 glPushAttrib(GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_DEPTH_BUFFER_BIT);
421 glEnable(GL_COLOR_MATERIAL);
422 glDisable(GL_LIGHTING);
423 glDisable(GL_TEXTURE_2D);
424 glDepthMask(GL_FALSE);
426 glBegin(GL_QUAD_STRIP);
428 for (i = 0; i <= n; i++)
430 float x = fcosf(2.f * PI * i / n);
431 float y = fsinf(2.f * PI * i / n);
433 glColor4f(1.0f, 1.0f, 1.0f, (k == 0 ? 0.5f : 0.8f));
434 glVertex3f(x, 0.0f, y);
436 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
437 glVertex3f(x, JUMP_HEIGHT, y);
450 if (glIsList(jump_list))
451 glDeleteLists(jump_list, 1);
456 void jump_draw(int highlight)
458 glCallList(jump_list + highlight);
461 /*---------------------------------------------------------------------------*/
463 static GLuint swch_list;
465 static GLfloat swch_colors[8][4] = {
466 {1.0f, 0.0f, 0.0f, 0.5f}, /* red out */
467 {1.0f, 0.0f, 0.0f, 0.0f},
468 {1.0f, 0.0f, 0.0f, 0.8f}, /* red in */
469 {1.0f, 0.0f, 0.0f, 0.0f},
470 {0.0f, 1.0f, 0.0f, 0.5f}, /* green out */
471 {0.0f, 1.0f, 0.0f, 0.0f},
472 {0.0f, 1.0f, 0.0f, 0.8f}, /* green in */
473 {0.0f, 1.0f, 0.0f, 0.0f}};
475 void swch_init(int b)
477 int k, i, n = b ? 32 : 8;
479 swch_list = glGenLists(4);
481 /* Create the display lists. */
483 for (k = 0; k < 4; k++)
485 glNewList(swch_list + k, GL_COMPILE);
487 glPushAttrib(GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_DEPTH_BUFFER_BIT);
489 glEnable(GL_COLOR_MATERIAL);
490 glDisable(GL_LIGHTING);
491 glDisable(GL_TEXTURE_2D);
492 glDepthMask(GL_FALSE);
494 glBegin(GL_QUAD_STRIP);
496 for (i = 0; i <= n; i++)
498 float x = fcosf(2.f * PI * i / n);
499 float y = fsinf(2.f * PI * i / n);
501 glColor4fv(swch_colors[2 * k]);
502 glVertex3f(x, 0.0f, y);
504 glColor4fv(swch_colors[2 * k + 1]);
505 glVertex3f(x, SWCH_HEIGHT, y);
518 if (glIsList(swch_list))
519 glDeleteLists(swch_list, 2);
524 void swch_draw(int b, int e)
526 glCallList(swch_list + b * 2 + e);
529 /*---------------------------------------------------------------------------*/
531 static GLuint flag_list;
533 void flag_init(int b)
535 int i, n = b ? 8 : 4;
537 flag_list = glGenLists(1);
539 glNewList(flag_list, GL_COMPILE);
541 glPushAttrib(GL_TEXTURE_BIT | GL_LIGHTING_BIT);
543 glEnable(GL_COLOR_MATERIAL);
544 glDisable(GL_LIGHTING);
545 glDisable(GL_TEXTURE_2D);
547 glBegin(GL_QUAD_STRIP);
549 for (i = 0; i <= n; i++)
551 float x = fcosf(2.f * PI * i / n) * 0.01f;
552 float y = fsinf(2.f * PI * i / n) * 0.01f;
554 glColor3f(1.0f, 1.0f, 1.0f);
555 glVertex3f(x, 0.0f, y);
556 glVertex3f(x, GOAL_HEIGHT, y);
561 glBegin(GL_TRIANGLES);
563 glColor3f(1.0f, 0.0f, 0.0f);
565 glVertex3f( 0.0f, GOAL_HEIGHT, 0.0f);
566 glVertex3f(GOAL_HEIGHT * 0.2f, GOAL_HEIGHT * 0.9f, 0.0f);
567 glVertex3f( 0.0f, GOAL_HEIGHT * 0.8f, 0.0f);
569 glVertex3f( 0.0f, GOAL_HEIGHT, 0.0f);
570 glVertex3f( 0.0f, GOAL_HEIGHT * 0.8f, 0.0f);
571 glVertex3f(GOAL_HEIGHT * 0.2f, GOAL_HEIGHT * 0.9f, 0.0f);
582 if (glIsList(flag_list))
583 glDeleteLists(flag_list, 1);
590 glCallList(flag_list);
593 /*---------------------------------------------------------------------------*/
595 * A note about lighting and shadow: technically speaking, it's wrong.
596 * The light position and shadow projection behave as if the
597 * light-source rotates with the floor. However, the skybox does not
598 * rotate, thus the light should also remain stationary.
600 * The correct behavior would eliminate a significant 3D cue: the
601 * shadow of the ball indicates the ball's position relative to the
602 * floor even when the ball is in the air. This was the motivating
603 * idea behind the shadow in the first place, so correct shadow
604 * projection would only magnify the problem.
607 static GLuint shad_text;
611 shad_text = make_image_from_file(NULL, NULL, NULL, NULL, IMG_SHAD);
613 if (config_get_d(CONFIG_SHADOW) == 2)
615 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
616 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
622 if (glIsTexture(shad_text))
623 glDeleteTextures(1, &shad_text);
626 void shad_draw_set(const float *p, float r)
628 glMatrixMode(GL_TEXTURE);
632 glBindTexture(GL_TEXTURE_2D, shad_text);
635 glTranslatef(0.5f - k * p[0],
636 0.5f - k * p[2], 0.f);
637 glScalef(k, k, 1.0f);
639 glMatrixMode(GL_MODELVIEW);
642 void shad_draw_clr(void)
644 glMatrixMode(GL_TEXTURE);
648 glMatrixMode(GL_MODELVIEW);
651 /*---------------------------------------------------------------------------*/
653 void fade_draw(float k)
655 int w = config_get_d(CONFIG_WIDTH);
656 int h = config_get_d(CONFIG_HEIGHT);
661 glPushAttrib(GL_TEXTURE_BIT |
663 GL_COLOR_BUFFER_BIT |
664 GL_DEPTH_BUFFER_BIT);
666 glEnable(GL_COLOR_MATERIAL);
667 glDisable(GL_LIGHTING);
668 glDisable(GL_DEPTH_TEST);
669 glDisable(GL_TEXTURE_2D);
671 glColor4f(0.0f, 0.0f, 0.0f, k);
687 /*---------------------------------------------------------------------------*/