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;
38 int i, slices = b ? 32 : 16;
39 int j, stacks = b ? 16 : 8;
41 config_get_s(CONFIG_BALL, name, MAXSTR);
43 ball_text = make_image_from_file(name);
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
48 ball_list = glGenLists(1);
50 glNewList(ball_list, GL_COMPILE);
52 for (i = 0; i < stacks; i++)
54 float k0 = (float) i / stacks;
55 float k1 = (float) (i + 1) / stacks;
57 float s0 = fsinf(V_PI * (k0 - 0.5));
58 float c0 = fcosf(V_PI * (k0 - 0.5));
59 float s1 = fsinf(V_PI * (k1 - 0.5));
60 float c1 = fcosf(V_PI * (k1 - 0.5));
62 glBegin(GL_QUAD_STRIP);
64 for (j = 0; j <= slices; j++)
66 float k = (float) j / slices;
67 float s = fsinf(V_PI * k * 2.0);
68 float c = fcosf(V_PI * k * 2.0);
71 glNormal3f(s * c0, c * c0, s0);
72 glVertex3f(s * c0, c * c0, s0);
75 glNormal3f(s * c1, c * c1, s1);
76 glVertex3f(s * c1, c * c1, s1);
87 if (glIsList(ball_list))
88 glDeleteLists(ball_list, 1);
90 if (glIsTexture(ball_text))
91 glDeleteTextures(1, &ball_text);
99 static const float a[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
100 static const float s[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
101 static const float e[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
102 static const float h[1] = { 64.0f };
104 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, a);
105 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, s);
106 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, e);
107 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, h);
109 glEnable(GL_COLOR_MATERIAL);
111 glBindTexture(GL_TEXTURE_2D, ball_text);
113 /* Render the ball back to front in case it is translucent. */
115 glDepthMask(GL_FALSE);
117 glCullFace(GL_FRONT);
118 glCallList(ball_list);
120 glCallList(ball_list);
122 glDepthMask(GL_TRUE);
124 /* Render the ball into the depth buffer. */
126 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
128 glCallList(ball_list);
130 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
132 /* Ensure the ball is visible even when obscured by geometry. */
134 glDisable(GL_DEPTH_TEST);
136 glColor4f(1.0f, 1.0f, 1.0f, 0.1f);
137 glCallList(ball_list);
139 glEnable(GL_DEPTH_TEST);
141 glDisable(GL_COLOR_MATERIAL);
144 /*---------------------------------------------------------------------------*/
146 static GLuint mark_list;
148 void mark_init(int b)
150 int i, slices = b ? 32 : 16;
152 mark_list = glGenLists(1);
154 glNewList(mark_list, GL_COMPILE);
156 glBegin(GL_TRIANGLE_FAN);
158 glNormal3f(0.f, 1.f, 0.f);
160 for (i = 0; i < slices; i++)
162 float x = fcosf(-2.f * PI * i / slices);
163 float y = fsinf(-2.f * PI * i / slices);
175 glEnable(GL_COLOR_MATERIAL);
176 glDisable(GL_TEXTURE_2D);
177 glDepthMask(GL_FALSE);
180 glCallList(mark_list);
182 glDepthMask(GL_TRUE);
183 glEnable(GL_TEXTURE_2D);
184 glDisable(GL_COLOR_MATERIAL);
189 if (glIsList(mark_list))
190 glDeleteLists(mark_list, 1);
195 /*---------------------------------------------------------------------------*/
197 static void coin_head(int n, float radius, float thick)
201 glBegin(GL_TRIANGLE_FAN);
203 glNormal3f(0.f, 0.f, +1.f);
205 for (i = 0; i < n; i++)
207 float x = fcosf(+2.f * PI * i / n);
208 float y = fsinf(+2.f * PI * i / n);
210 glTexCoord2f(-x * 0.5f + 0.5f, +y * 0.5f + 0.5f);
211 glVertex3f(radius * x, radius * y, +thick);
217 static void coin_tail(int n, float radius, float thick)
221 glBegin(GL_TRIANGLE_FAN);
223 glNormal3f(0.f, 0.f, -1.f);
225 for (i = 0; i < n; i++)
227 float x = fcosf(-2.f * PI * i / n);
228 float y = fsinf(-2.f * PI * i / n);
230 glTexCoord2f(+x * 0.5f + 0.5f, +y * 0.5f + 0.5f);
231 glVertex3f(radius * x, radius * y, -thick);
237 static void coin_edge(int n, float radius, float thick)
241 glBegin(GL_QUAD_STRIP);
243 for (i = 0; i <= n; i++)
245 float x = fcosf(2.f * PI * i / n);
246 float y = fsinf(2.f * PI * i / n);
248 glNormal3f(x, y, 0.f);
249 glVertex3f(radius * x, radius * y, +thick);
250 glVertex3f(radius * x, radius * y, -thick);
256 /*---------------------------------------------------------------------------*/
258 static GLuint item_coin_text;
259 static GLuint item_grow_text;
260 static GLuint item_shrink_text;
261 static GLuint item_list;
263 void item_color(const struct s_item *hp, float *c)
303 void item_init(int b)
307 item_coin_text = make_image_from_file(IMG_ITEM_COIN);
308 item_grow_text = make_image_from_file(IMG_ITEM_GROW);
309 item_shrink_text = make_image_from_file(IMG_ITEM_SHRINK);
311 item_list = glGenLists(1);
313 glNewList(item_list, GL_COMPILE);
315 coin_edge(n, COIN_RADIUS, COIN_THICK);
316 coin_head(n, COIN_RADIUS, COIN_THICK);
317 coin_tail(n, COIN_RADIUS, COIN_THICK);
324 if (glIsList(item_list))
325 glDeleteLists(item_list, 1);
327 if (glIsTexture(item_coin_text))
328 glDeleteTextures(1, &item_coin_text);
330 if (glIsTexture(item_grow_text))
331 glDeleteTextures(1, &item_grow_text);
333 if (glIsTexture(item_shrink_text))
334 glDeleteTextures(1, &item_shrink_text);
339 item_shrink_text = 0;
342 void item_push(int type)
344 static const float a[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
345 static const float s[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
346 static const float e[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
347 static const float h[1] = { 32.0f };
349 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, a);
350 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, s);
351 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, e);
352 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, h);
354 glEnable(GL_COLOR_MATERIAL);
359 glBindTexture(GL_TEXTURE_2D, item_coin_text);
363 glBindTexture(GL_TEXTURE_2D, item_grow_text);
367 glBindTexture(GL_TEXTURE_2D, item_shrink_text);
372 void item_draw(const struct s_item *hp, float r)
379 glCallList(item_list);
384 glColor3f(1.0f, 1.0f, 1.0f);
385 glDisable(GL_COLOR_MATERIAL);
388 /*---------------------------------------------------------------------------*/
390 static GLuint goal_list;
392 void goal_init(int b)
394 int i, n = b ? 32 : 8;
396 goal_list = glGenLists(1);
398 glNewList(goal_list, GL_COMPILE);
400 glBegin(GL_QUAD_STRIP);
402 for (i = 0; i <= n; i++)
404 float x = fcosf(2.f * PI * i / n);
405 float y = fsinf(2.f * PI * i / n);
407 glColor4f(1.0f, 1.0f, 0.0f, 0.5f);
408 glVertex3f(x, 0.0f, y);
410 glColor4f(1.0f, 1.0f, 0.0f, 0.0f);
411 glVertex3f(x, GOAL_HEIGHT, y);
421 if (glIsList(goal_list))
422 glDeleteLists(goal_list, 1);
429 glCallList(goal_list);
432 /*---------------------------------------------------------------------------*/
434 static GLuint jump_list;
436 void jump_init(int b)
438 int k, i, n = b ? 32 : 8;
440 jump_list = glGenLists(2);
442 for (k = 0; k < 2; k++)
444 glNewList(jump_list + k, GL_COMPILE);
446 glBegin(GL_QUAD_STRIP);
448 for (i = 0; i <= n; i++)
450 float x = fcosf(2.f * PI * i / n);
451 float y = fsinf(2.f * PI * i / n);
453 glColor4f(1.0f, 1.0f, 1.0f, (k == 0 ? 0.5f : 0.8f));
454 glVertex3f(x, 0.0f, y);
456 glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
457 glVertex3f(x, JUMP_HEIGHT, y);
468 glDeleteLists(jump_list, 2);
472 void jump_draw(int highlight)
474 glCallList(jump_list + highlight);
477 /*---------------------------------------------------------------------------*/
479 static GLuint swch_list;
481 static GLfloat swch_colors[8][4] = {
482 { 1.0f, 0.0f, 0.0f, 0.5f }, /* red out */
483 { 1.0f, 0.0f, 0.0f, 0.0f },
484 { 1.0f, 0.0f, 0.0f, 0.8f }, /* red in */
485 { 1.0f, 0.0f, 0.0f, 0.0f },
486 { 0.0f, 1.0f, 0.0f, 0.5f }, /* green out */
487 { 0.0f, 1.0f, 0.0f, 0.0f },
488 { 0.0f, 1.0f, 0.0f, 0.8f }, /* green in */
489 { 0.0f, 1.0f, 0.0f, 0.0f }};
491 void swch_init(int b)
493 int k, i, n = b ? 32 : 8;
495 swch_list = glGenLists(4);
497 /* Create the display lists. */
499 for (k = 0; k < 4; k++)
501 glNewList(swch_list + k, GL_COMPILE);
503 glBegin(GL_QUAD_STRIP);
505 for (i = 0; i <= n; i++)
507 float x = fcosf(2.f * PI * i / n);
508 float y = fsinf(2.f * PI * i / n);
510 glColor4fv(swch_colors[2 * k + 0]);
511 glVertex3f(x, 0.0f, y);
513 glColor4fv(swch_colors[2 * k + 1]);
514 glVertex3f(x, SWCH_HEIGHT, y);
525 if (glIsList(swch_list))
526 glDeleteLists(swch_list, 2);
531 void swch_draw(int b, int e)
533 glCallList(swch_list + b * 2 + e);
536 /*---------------------------------------------------------------------------*/
538 static GLuint flag_list;
540 void flag_init(int b)
542 int i, n = b ? 8 : 4;
544 flag_list = glGenLists(1);
546 glNewList(flag_list, GL_COMPILE);
548 glEnable(GL_COLOR_MATERIAL);
549 glDisable(GL_LIGHTING);
550 glDisable(GL_TEXTURE_2D);
552 glBegin(GL_QUAD_STRIP);
554 for (i = 0; i <= n; i++)
556 float x = fcosf(2.f * PI * i / n) * 0.01f;
557 float y = fsinf(2.f * PI * i / n) * 0.01f;
559 glColor3f(1.0f, 1.0f, 1.0f);
560 glVertex3f(x, 0.0f, y);
561 glVertex3f(x, GOAL_HEIGHT, y);
566 glBegin(GL_TRIANGLES);
568 glColor3f(1.0f, 0.0f, 0.0f);
570 glVertex3f( 0.0f, GOAL_HEIGHT, 0.0f);
571 glVertex3f(GOAL_HEIGHT * 0.2f, GOAL_HEIGHT * 0.9f, 0.0f);
572 glVertex3f( 0.0f, GOAL_HEIGHT * 0.8f, 0.0f);
574 glVertex3f( 0.0f, GOAL_HEIGHT, 0.0f);
575 glVertex3f( 0.0f, GOAL_HEIGHT * 0.8f, 0.0f);
576 glVertex3f(GOAL_HEIGHT * 0.2f, GOAL_HEIGHT * 0.9f, 0.0f);
580 glEnable(GL_TEXTURE_2D);
581 glEnable(GL_LIGHTING);
582 glDisable(GL_COLOR_MATERIAL);
589 if (glIsList(flag_list))
590 glDeleteLists(flag_list, 1);
597 glCallList(flag_list);
600 /*---------------------------------------------------------------------------*/
602 * A note about lighting and shadow: technically speaking, it's wrong.
603 * The light position and shadow projection behave as if the
604 * light-source rotates with the floor. However, the skybox does not
605 * rotate, thus the light should also remain stationary.
607 * The correct behavior would eliminate a significant 3D cue: the
608 * shadow of the ball indicates the ball's position relative to the
609 * floor even when the ball is in the air. This was the motivating
610 * idea behind the shadow in the first place, so correct shadow
611 * projection would only magnify the problem.
614 static GLuint shad_text;
618 shad_text = make_image_from_file(IMG_SHAD);
620 if (config_get_d(CONFIG_SHADOW) == 2)
622 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
623 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
629 if (glIsTexture(shad_text))
630 glDeleteTextures(1, &shad_text);
633 void shad_draw_set(const float *p, float r)
635 glMatrixMode(GL_TEXTURE);
639 glBindTexture(GL_TEXTURE_2D, shad_text);
642 glTranslatef(0.5f - k * p[0],
643 0.5f - k * p[2], 0.f);
644 glScalef(k, k, 1.0f);
646 glMatrixMode(GL_MODELVIEW);
649 void shad_draw_clr(void)
651 glMatrixMode(GL_TEXTURE);
655 glMatrixMode(GL_MODELVIEW);
658 /*---------------------------------------------------------------------------*/
660 void fade_draw(float k)
664 int w = config_get_d(CONFIG_WIDTH);
665 int h = config_get_d(CONFIG_HEIGHT);
669 glEnable(GL_COLOR_MATERIAL);
670 glDisable(GL_LIGHTING);
671 glDisable(GL_DEPTH_TEST);
672 glDisable(GL_TEXTURE_2D);
674 glColor4f(0.0f, 0.0f, 0.0f, k);
685 glEnable(GL_TEXTURE_2D);
686 glEnable(GL_DEPTH_TEST);
687 glEnable(GL_LIGHTING);
688 glDisable(GL_COLOR_MATERIAL);
694 /*---------------------------------------------------------------------------*/