2 * Copyright (C) 2003 Robert Kooima
4 * NEVERPUTT 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.
31 /*---------------------------------------------------------------------------*/
33 static struct s_file file;
36 static float view_a; /* Ideal view rotation about Y axis */
38 static float view_ry; /* Angular velocity about Y axis */
39 static float view_dy; /* Ideal view distance above ball */
40 static float view_dz; /* Ideal view distance behind ball */
42 static float view_c[3]; /* Current view center */
43 static float view_v[3]; /* Current view vector */
44 static float view_p[3]; /* Current view position */
45 static float view_e[3][3]; /* Current view orientation */
47 static float jump_e = 1; /* Jumping enabled flag */
48 static float jump_b = 0; /* Jump-in-progress flag */
49 static float jump_dt; /* Jump duration */
50 static float jump_p[3]; /* Jump destination */
52 /*---------------------------------------------------------------------------*/
54 static void view_init(void)
81 void game_init(const char *s)
87 sol_load_gl(&file, config_data(s), config_get_d(CONFIG_TEXTURES),
88 config_get_d(CONFIG_SHADOW));
96 /*---------------------------------------------------------------------------*/
98 static void game_draw_vect_prim(const struct s_file *fp, GLenum mode)
105 v_cpy(p, fp->uv[ball].p);
113 glColor4f(1.0f, 1.0f, 0.5f, 0.5f);
114 glVertex3f(p[0] - x[0] * r,
118 glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
119 glVertex3f(p[0] + z[0] * view_m,
120 p[1] + z[1] * view_m,
121 p[2] + z[2] * view_m);
123 glColor4f(1.0f, 1.0f, 0.0f, 0.5f);
124 glVertex3f(p[0] + x[0] * r,
131 static void game_draw_vect(const struct s_file *fp)
135 glPushAttrib(GL_TEXTURE_BIT);
136 glPushAttrib(GL_POLYGON_BIT);
137 glPushAttrib(GL_LIGHTING_BIT);
138 glPushAttrib(GL_DEPTH_BUFFER_BIT);
140 glEnable(GL_COLOR_MATERIAL);
141 glDisable(GL_LIGHTING);
142 glDisable(GL_TEXTURE_2D);
143 glDepthMask(GL_FALSE);
145 glEnable(GL_DEPTH_TEST);
146 game_draw_vect_prim(fp, GL_TRIANGLES);
148 glDisable(GL_DEPTH_TEST);
149 game_draw_vect_prim(fp, GL_LINE_STRIP);
158 static void game_draw_balls(const struct s_file *fp,
159 const float *bill_M, float t)
161 static const GLfloat color[5][4] = {
162 { 1.0f, 1.0f, 1.0f, 0.7f },
163 { 1.0f, 0.0f, 0.0f, 1.0f },
164 { 0.0f, 1.0f, 0.0f, 1.0f },
165 { 0.0f, 0.0f, 1.0f, 1.0f },
166 { 1.0f, 1.0f, 0.0f, 1.0f },
171 for (ui = curr_party(); ui > 0; ui--)
178 m_basis(ball_M, fp->uv[ui].e[0], fp->uv[ui].e[1], fp->uv[ui].e[2]);
179 m_basis(pend_M, fp->uv[ui].E[0], fp->uv[ui].E[1], fp->uv[ui].E[2]);
183 glTranslatef(fp->uv[ui].p[0],
184 fp->uv[ui].p[1] + BALL_FUDGE,
186 glScalef(fp->uv[ui].r,
190 glColor4fv(color[ui]);
191 ball_draw(ball_M, pend_M, bill_M, t);
199 glTranslatef(fp->uv[ui].p[0],
200 fp->uv[ui].p[1] - fp->uv[ui].r + BALL_FUDGE,
202 glScalef(fp->uv[ui].r,
206 glColor4f(color[ui][0],
215 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
218 static void game_draw_goals(const struct s_file *fp)
222 for (zi = 0; zi < fp->zc; zi++)
226 glTranslatef(fp->zv[zi].p[0],
235 static void game_draw_jumps(const struct s_file *fp)
239 for (ji = 0; ji < fp->jc; ji++)
243 glTranslatef(fp->jv[ji].p[0],
247 glScalef(fp->jv[ji].r, 1.f, fp->jv[ji].r);
254 static void game_draw_swchs(const struct s_file *fp)
258 for (xi = 0; xi < fp->xc; xi++)
262 glTranslatef(fp->xv[xi].p[0],
266 glScalef(fp->xv[xi].r, 1.f, fp->xv[xi].r);
267 swch_draw(fp->xv[xi].f, fp->xv[xi].e);
273 /*---------------------------------------------------------------------------*/
275 void game_draw(int pose, float t)
277 static const float a[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
278 static const float s[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
279 static const float e[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
280 static const float h[1] = { 0.0f };
282 const float light_p[4] = { 8.f, 32.f, 8.f, 1.f };
284 const struct s_file *fp = &file;
288 if (jump_b) fov *= 2.0f * fabsf(jump_dt - 0.5f);
290 config_push_persp(fov, 0.1f, FAR_DIST);
291 glPushAttrib(GL_LIGHTING_BIT);
294 float T[16], M[16], v[3], rx, ry;
296 m_view(T, view_c, view_p, view_e[1]);
299 v_sub(v, view_c, view_p);
301 rx = V_DEG(fatan2f(-v[1], fsqrtf(v[0] * v[0] + v[2] * v[2])));
302 ry = V_DEG(fatan2f(+v[0], -v[2]));
304 glTranslatef(0.f, 0.f, -v_len(v));
306 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
308 /* Center the skybox about the position of the camera. */
312 glTranslatef(view_p[0], view_p[1], view_p[2]);
318 glLightfv(GL_LIGHT0, GL_POSITION, light_p);
320 /* Draw the floor. */
324 if (config_get_d(CONFIG_SHADOW) && !pose)
326 shad_draw_set(fp->uv[ball].p, fp->uv[ball].r);
331 /* Draw the game elements. */
334 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
338 game_draw_balls(fp, T, t);
342 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, a);
343 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, s);
344 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, e);
345 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, h);
349 glEnable(GL_COLOR_MATERIAL);
350 glDisable(GL_LIGHTING);
351 glDisable(GL_TEXTURE_2D);
352 glDepthMask(GL_FALSE);
357 glDepthMask(GL_TRUE);
358 glEnable(GL_TEXTURE_2D);
359 glEnable(GL_LIGHTING);
360 glDisable(GL_COLOR_MATERIAL);
367 /*---------------------------------------------------------------------------*/
369 void game_update_view(float dt)
371 const float y[3] = { 0.f, 1.f, 0.f };
380 /* Center the view about the ball. */
382 v_cpy(view_c, file.uv[ball].p);
383 v_inv(view_v, file.uv[ball].v);
385 switch (config_get_d(CONFIG_CAMERA))
388 /* Camera 2: View vector is given by view angle. */
390 view_e[2][0] = fsinf(V_RAD(view_a));
392 view_e[2][2] = fcosf(V_RAD(view_a));
398 /* View vector approaches the ball velocity vector. */
400 v_mad(e, view_v, y, v_dot(view_v, y));
403 k = v_dot(view_v, view_v);
405 v_sub(view_e[2], view_p, view_c);
406 v_mad(view_e[2], view_e[2], view_v, k * dt * 0.1f);
409 /* Orthonormalize the basis of the view in its new position. */
411 v_crs(view_e[0], view_e[1], view_e[2]);
412 v_crs(view_e[2], view_e[0], view_e[1]);
413 v_nrm(view_e[0], view_e[0]);
414 v_nrm(view_e[2], view_e[2]);
416 /* The current view (dy, dz) approaches the ideal (view_dy, view_dz). */
418 v_sub(d, view_p, view_c);
420 dy = v_dot(view_e[1], d);
421 dz = v_dot(view_e[2], d);
423 dy += (view_dy - dy) * s;
424 dz += (view_dz - dz) * s;
426 /* Compute the new view position. */
428 view_p[0] = view_p[1] = view_p[2] = 0.f;
430 v_mad(view_p, view_c, view_e[1], dy);
431 v_mad(view_p, view_p, view_e[2], dz);
433 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
436 static int game_update_state(float dt)
438 static float t = 0.f;
440 struct s_file *fp = &file;
448 /* Test for a switch. */
450 if (sol_swch_test(fp, ball))
451 audio_play(AUD_SWITCH, 1.f);
453 /* Test for a jump. */
455 if (jump_e == 1 && jump_b == 0 && sol_jump_test(fp, jump_p, ball) == 1)
461 audio_play(AUD_JUMP, 1.f);
463 if (jump_e == 0 && jump_b == 0 && sol_jump_test(fp, jump_p, ball) == 0)
466 /* Test for fall-out. */
468 if (fp->uv[ball].p[1] < -10.f)
471 /* Test for a goal or stop. */
477 if (sol_goal_test(fp, p, ball))
487 * On most hardware, rendering requires much more computing power than
488 * physics. Since physics takes less time than graphics, it make sense to
489 * detach the physics update time step from the graphics frame rate. By
490 * performing multiple physics updates for each graphics update, we get away
491 * with higher quality physics with little impact on overall performance.
493 * Toward this end, we establish a baseline maximum physics time step. If
494 * the measured frame time exceeds this maximum, we cut the time step in
495 * half, and do two updates. If THIS time step exceeds the maximum, we do
496 * four updates. And so on. In this way, the physics system is allowed to
497 * seek an optimal update rate independent of, yet in integral sync with, the
498 * graphics frame rate.
501 int game_step(const float g[3], float dt)
503 struct s_file *fp = &file;
505 static float s = 0.f;
506 static float t = 0.f;
513 s = (7.f * s + dt) / 8.f;
524 fp->uv[ball].p[0] = jump_p[0];
525 fp->uv[ball].p[1] = jump_p[1];
526 fp->uv[ball].p[2] = jump_p[2];
535 while (t > MAX_DT && n < MAX_DN)
541 for (i = 0; i < n; i++)
543 d = sol_step(fp, g, t, ball, &m);
551 /* Mix the sound of a ball bounce. */
554 audio_play(AUD_BUMP, (float) (b - 0.5) * 2.0f);
557 game_update_view(dt);
558 return game_update_state(st);
564 * HACK: The BALL_FUDGE here guarantees that a putt doesn't drive
565 * the ball too directly down toward a lump, triggering rolling
566 * friction too early and stopping the ball prematurely.
569 file.uv[ball].v[0] = -4.f * view_e[2][0] * view_m;
570 file.uv[ball].v[1] = -4.f * view_e[2][1] * view_m + BALL_FUDGE;
571 file.uv[ball].v[2] = -4.f * view_e[2][2] * view_m;
576 /*---------------------------------------------------------------------------*/
578 void game_set_rot(int d)
580 view_a += (float) (30.f * d) / config_get_d(CONFIG_MOUSE_SENSE);
583 void game_clr_mag(void)
588 void game_set_mag(int d)
590 view_m -= (float) (1.f * d) / config_get_d(CONFIG_MOUSE_SENSE);
596 void game_set_fly(float k)
598 struct s_file *fp = &file;
600 float x[3] = { 1.f, 0.f, 0.f };
601 float y[3] = { 0.f, 1.f, 0.f };
602 float z[3] = { 0.f, 0.f, 1.f };
603 float c0[3] = { 0.f, 0.f, 0.f };
604 float p0[3] = { 0.f, 0.f, 0.f };
605 float c1[3] = { 0.f, 0.f, 0.f };
606 float p1[3] = { 0.f, 0.f, 0.f };
611 v_sub(view_e[2], fp->uv[ball].p, fp->zv[0].p);
613 if (fabs(v_dot(view_e[1], view_e[2])) > 0.999)
616 v_crs(view_e[0], view_e[1], view_e[2]);
617 v_crs(view_e[2], view_e[0], view_e[1]);
619 v_nrm(view_e[0], view_e[0]);
620 v_nrm(view_e[2], view_e[2]);
622 /* k = 0.0 view is at the ball. */
626 v_cpy(c0, fp->uv[ball].p);
627 v_cpy(p0, fp->uv[ball].p);
630 v_mad(p0, p0, view_e[1], view_dy);
631 v_mad(p0, p0, view_e[2], view_dz);
633 /* k = +1.0 view is s_view 0 */
635 if (k >= 0 && fp->wc > 0)
637 v_cpy(p1, fp->wv[0].p);
638 v_cpy(c1, fp->wv[0].q);
641 /* k = -1.0 view is s_view 1 */
643 if (k <= 0 && fp->wc > 1)
645 v_cpy(p1, fp->wv[1].p);
646 v_cpy(c1, fp->wv[1].q);
649 /* Interpolate the views. */
652 v_mad(view_p, p0, v, k * k);
655 v_mad(view_c, c0, v, k * k);
657 /* Orthonormalize the view basis. */
659 v_sub(view_e[2], view_p, view_c);
660 v_crs(view_e[0], view_e[1], view_e[2]);
661 v_crs(view_e[2], view_e[0], view_e[1]);
662 v_nrm(view_e[0], view_e[0]);
663 v_nrm(view_e[2], view_e[2]);
665 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
668 void game_ball(int i)
677 for (ui = 0; ui < file.uc; ui++)
679 file.uv[ui].v[0] = 0.f;
680 file.uv[ui].v[1] = 0.f;
681 file.uv[ui].v[2] = 0.f;
683 file.uv[ui].w[0] = 0.f;
684 file.uv[ui].w[1] = 0.f;
685 file.uv[ui].w[2] = 0.f;
689 void game_get_pos(float p[3], float e[3][3])
691 v_cpy(p, file.uv[ball].p);
692 v_cpy(e[0], file.uv[ball].e[0]);
693 v_cpy(e[1], file.uv[ball].e[1]);
694 v_cpy(e[2], file.uv[ball].e[2]);
697 void game_set_pos(float p[3], float e[3][3])
699 v_cpy(file.uv[ball].p, p);
700 v_cpy(file.uv[ball].e[0], e[0]);
701 v_cpy(file.uv[ball].e[1], e[1]);
702 v_cpy(file.uv[ball].e[2], e[2]);
705 /*---------------------------------------------------------------------------*/