Import from seb-0701a.tar.gz
[neverball-archive.git] / src / coin.c
blob4bfbac856bc6c09ec20aafabcbae70a203382e35
1 /*
2 * Copyright (C) 2003 Robert Kooima
4 * SUPER EMPTY BALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * 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.
15 #include <math.h>
17 #include "gl.h"
18 #include "main.h"
19 #include "vec3.h"
20 #include "solid.h"
21 #include "state.h"
22 #include "image.h"
24 #define CR 0.15
25 #define DZ 0.01
26 #define PI 3.1415926
28 /*---------------------------------------------------------------------------*/
30 #define COIN_S "data/png/coin.png"
32 static struct image coin_i, *coin_p = &coin_i;
34 /*---------------------------------------------------------------------------*/
36 GLuint coin_init(int n)
38 GLuint list = glGenLists(1);
40 image_load(coin_p, COIN_S);
42 glNewList(list, GL_COMPILE);
44 double x;
45 double y;
46 int i;
48 image_bind(coin_p);
50 glBegin(GL_TRIANGLE_FAN);
52 glNormal3d(0.0, 0.0, +1.0);
54 for (i = 0; i < n; i++)
56 x = cos(+2.0 * PI * i / n);
57 y = sin(+2.0 * PI * i / n);
59 glTexCoord2d(-0.5 * x - 0.5, 0.5 * y - 0.5);
60 glVertex3d(CR * x, CR * y, +DZ);
63 glEnd();
65 glBegin(GL_TRIANGLE_FAN);
67 glNormal3d(0.0, 0.0, -1.0);
69 for (i = 0; i < n; i++)
71 x = cos(-2.0 * PI * i / n);
72 y = sin(-2.0 * PI * i / n);
74 glTexCoord2d(+0.5 * x - 0.5, 0.5 * y - 0.5);
75 glVertex3d(CR * x, CR * y, +DZ);
78 glEnd();
80 glBegin(GL_QUAD_STRIP);
82 for (i = 0; i <= n; i++)
84 x = cos(+2.0 * PI * i / n);
85 y = sin(+2.0 * PI * i / n);
87 glNormal3d(x, y, 0.0);
88 glVertex3d(CR * x, CR * y, +DZ);
89 glVertex3d(CR * x, CR * y, -DZ);
92 glEnd();
94 glEndList();
96 return list;
99 void coin_draw(GLuint list, const struct s_coin *cv, int cc)
101 static const float c1[4] = { 1.0f, 1.0f, 0.0f, 1.0f };
102 static const float c2[4] = { 1.0f, 0.2f, 0.2f, 1.0f };
103 static const float c3[4] = { 0.2f, 0.2f, 1.0f, 1.0f };
104 static const float s[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
106 double r = 360.0 * fmod(time_state(), 1.0);
107 int i;
109 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, s);
111 for (i = 0; i < cc; i++)
113 if (0 < cv[i].n && cv[i].n < 5)
114 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c1);
116 if (4 < cv[i].n && cv[i].n < 10)
117 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c2);
119 if (9 < cv[i].n)
120 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c3);
122 if (0 < cv[i].n)
124 glPushMatrix();
126 glTranslated(cv[i].p[0],
127 cv[i].p[1],
128 cv[i].p[2]);
129 glRotated(r, 0.0, 1.0, 0.0);
130 glCallList(list);
132 glPopMatrix();
137 int coin_test(const struct s_ball *up, struct s_coin *cv, int cc)
139 int i, n = 0;
141 for (i = 0; i < cc; i++)
143 double r[3];
145 v_sub(r, up->p, cv[i].p);
147 if (v_len(r) < CR + up->r)
149 n += cv[i].n;
150 cv[i].n = 0;
154 return n;
157 /*---------------------------------------------------------------------------*/