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.
30 #include "solid_draw.h"
31 #include "solid_sim.h"
32 #include "solid_all.h"
34 /*---------------------------------------------------------------------------*/
36 static struct s_full file;
41 static float view_a; /* Ideal view rotation about Y axis */
43 static float view_ry; /* Angular velocity about Y axis */
44 static float view_dy; /* Ideal view distance above ball */
45 static float view_dz; /* Ideal view distance behind ball */
47 static float view_c[3]; /* Current view center */
48 static float view_v[3]; /* Current view vector */
49 static float view_p[3]; /* Current view position */
50 static float view_e[3][3]; /* Current view orientation */
52 static float jump_e = 1; /* Jumping enabled flag */
53 static float jump_b = 0; /* Jump-in-progress flag */
54 static float jump_dt; /* Jump duration */
55 static float jump_p[3]; /* Jump destination */
57 static float idle_t; /* Idling timeout */
59 /*---------------------------------------------------------------------------*/
61 static void view_init(void)
88 int game_init(const char *s)
99 if (!(state = sol_load_full(&file, s, config_get_d(CONFIG_SHADOW))))
102 sol_init_sim(&file.vary);
104 for (i = 0; i < file.base.dc; i++)
106 const char *k = file.base.av + file.base.dv[i].ai;
107 const char *v = file.base.av + file.base.dv[i].aj;
109 if (strcmp(k, "idle") == 0)
111 sscanf(v, "%f", &idle_t);
123 sol_free_full(&file);
126 /*---------------------------------------------------------------------------*/
128 static void game_draw_vect(struct s_rend *rend, const struct s_vary *fp)
132 glDisable(GL_LIGHTING);
135 glTranslatef(fp->uv[ball].p[0],
138 glRotatef(view_a, 0.0f, 1.0f, 0.0f);
139 glScalef(fp->uv[ball].r,
140 fp->uv[ball].r * 0.1f, view_m);
145 glEnable(GL_LIGHTING);
149 static void game_draw_balls(struct s_rend *rend,
150 const struct s_vary *fp,
151 const float *bill_M, float t)
153 static const GLfloat color[5][4] = {
154 { 1.0f, 1.0f, 1.0f, 0.7f },
155 { 1.0f, 0.0f, 0.0f, 1.0f },
156 { 0.0f, 1.0f, 0.0f, 1.0f },
157 { 0.0f, 0.0f, 1.0f, 1.0f },
158 { 1.0f, 1.0f, 0.0f, 1.0f },
163 glEnable(GL_COLOR_MATERIAL);
165 for (ui = curr_party(); ui > 0; ui--)
172 m_basis(ball_M, fp->uv[ui].e[0], fp->uv[ui].e[1], fp->uv[ui].e[2]);
173 m_basis(pend_M, fp->uv[ui].E[0], fp->uv[ui].E[1], fp->uv[ui].E[2]);
177 glTranslatef(fp->uv[ui].p[0],
178 fp->uv[ui].p[1] + BALL_FUDGE,
180 glScalef(fp->uv[ui].r,
184 glColor4f(color[ui][0],
188 ball_draw(rend, ball_M, pend_M, bill_M, t);
196 glTranslatef(fp->uv[ui].p[0],
197 fp->uv[ui].p[1] - fp->uv[ui].r + BALL_FUDGE,
199 glScalef(fp->uv[ui].r,
203 glColor4f(color[ui][0],
213 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
214 glDisable(GL_COLOR_MATERIAL);
217 static void game_draw_goals(struct s_rend *rend, const struct s_base *fp)
221 for (zi = 0; zi < fp->zc; zi++)
225 glTranslatef(fp->zv[zi].p[0],
234 static void game_draw_jumps(struct s_rend *rend, const struct s_base *fp)
236 float t = 0.001f * SDL_GetTicks();
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);
248 jump_draw(rend, t, !jump_e);
254 static void game_draw_swchs(struct s_rend *rend, const struct s_vary *fp)
258 for (xi = 0; xi < fp->xc; xi++)
260 struct v_swch *xp = fp->xv + xi;
267 glTranslatef(xp->base->p[0],
271 glScalef(xp->base->r, 1.f, xp->base->r);
272 swch_draw(rend, xp->f, xp->e);
278 /*---------------------------------------------------------------------------*/
280 void game_draw(int pose, float t)
282 const float light_p[4] = { 8.f, 32.f, 8.f, 0.f };
284 struct s_draw *fp = &file.draw;
285 struct s_rend rend = { NULL };
294 sol_draw_enable(&rend);
296 if (jump_b) fov *= 2.0f * fabsf(jump_dt - 0.5f);
298 video_push_persp(fov, 0.1f, FAR_DIST);
301 float T[16], M[16], v[3], rx, ry;
303 m_view(T, view_c, view_p, view_e[1]);
306 v_sub(v, view_c, view_p);
308 rx = V_DEG(fatan2f(-v[1], fsqrtf(v[0] * v[0] + v[2] * v[2])));
309 ry = V_DEG(fatan2f(+v[0], -v[2]));
311 glTranslatef(0.f, 0.f, -v_len(v));
313 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
315 /* Center the skybox about the position of the camera. */
319 glTranslatef(view_p[0], view_p[1], view_p[2]);
325 glLightfv(GL_LIGHT0, GL_POSITION, light_p);
327 /* Draw the floor. */
329 sol_draw(fp, &rend, 0, 1);
331 /* Draw the game elements. */
334 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
338 game_draw_balls(&rend, fp->vary, T, t);
339 game_draw_vect(&rend, fp->vary);
342 glEnable(GL_COLOR_MATERIAL);
343 glDisable(GL_LIGHTING);
344 glDepthMask(GL_FALSE);
346 game_draw_goals(&rend, fp->base);
347 game_draw_jumps(&rend, fp->base);
348 game_draw_swchs(&rend, fp->vary);
350 glDepthMask(GL_TRUE);
351 glEnable(GL_LIGHTING);
352 glDisable(GL_COLOR_MATERIAL);
357 sol_draw_disable(&rend);
360 /*---------------------------------------------------------------------------*/
362 void game_update_view(float dt)
364 const float y[3] = { 0.f, 1.f, 0.f };
376 /* Center the view about the ball. */
378 v_cpy(view_c, file.vary.uv[ball].p);
379 v_inv(view_v, file.vary.uv[ball].v);
381 switch (config_get_d(CONFIG_CAMERA))
384 /* Camera 2: View vector is given by view angle. */
386 view_e[2][0] = fsinf(V_RAD(view_a));
388 view_e[2][2] = fcosf(V_RAD(view_a));
394 /* View vector approaches the ball velocity vector. */
396 v_mad(e, view_v, y, v_dot(view_v, y));
399 k = v_dot(view_v, view_v);
401 v_sub(view_e[2], view_p, view_c);
402 v_mad(view_e[2], view_e[2], view_v, k * dt * 0.1f);
405 /* Orthonormalize the basis of the view in its new position. */
407 v_crs(view_e[0], view_e[1], view_e[2]);
408 v_crs(view_e[2], view_e[0], view_e[1]);
409 v_nrm(view_e[0], view_e[0]);
410 v_nrm(view_e[2], view_e[2]);
412 /* The current view (dy, dz) approaches the ideal (view_dy, view_dz). */
414 v_sub(d, view_p, view_c);
416 dy = v_dot(view_e[1], d);
417 dz = v_dot(view_e[2], d);
419 dy += (view_dy - dy) * s;
420 dz += (view_dz - dz) * s;
422 /* Compute the new view position. */
424 view_p[0] = view_p[1] = view_p[2] = 0.f;
426 v_mad(view_p, view_c, view_e[1], dy);
427 v_mad(view_p, view_p, view_e[2], dz);
429 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
432 static int game_update_state(float dt)
434 static float t = 0.f;
436 struct s_vary *fp = &file.vary;
444 /* Test for a switch. */
446 if (sol_swch_test(fp, ball) == SWCH_TRIGGER)
447 audio_play(AUD_SWITCH, 1.f);
449 /* Test for a jump. */
451 if (jump_e == 1 && jump_b == 0 && (sol_jump_test(fp, jump_p, ball) ==
458 audio_play(AUD_JUMP, 1.f);
460 if (jump_e == 0 && jump_b == 0 && (sol_jump_test(fp, jump_p, ball) ==
466 /* Test for fall-out. */
468 if (fp->uv[ball].p[1] < -10.f)
471 /* Test for a goal or stop. */
473 if (t > 1.f && sol_goal_test(fp, p, ball))
489 * On most hardware, rendering requires much more computing power than
490 * physics. Since physics takes less time than graphics, it make sense to
491 * detach the physics update time step from the graphics frame rate. By
492 * performing multiple physics updates for each graphics update, we get away
493 * with higher quality physics with little impact on overall performance.
495 * Toward this end, we establish a baseline maximum physics time step. If
496 * the measured frame time exceeds this maximum, we cut the time step in
497 * half, and do two updates. If THIS time step exceeds the maximum, we do
498 * four updates. And so on. In this way, the physics system is allowed to
499 * seek an optimal update rate independent of, yet in integral sync with, the
500 * graphics frame rate.
503 int game_step(const float g[3], float dt)
505 struct s_vary *fp = &file.vary;
507 static float s = 0.f;
508 static float t = 0.f;
518 s = (7.f * s + dt) / 8.f;
529 fp->uv[ball].p[0] = jump_p[0];
530 fp->uv[ball].p[1] = jump_p[1];
531 fp->uv[ball].p[2] = jump_p[2];
540 while (t > MAX_DT && n < MAX_DN)
546 for (i = 0; i < n; i++)
548 d = sol_step(fp, g, t, ball, &m);
556 /* Mix the sound of a ball bounce. */
559 audio_play(AUD_BUMP, (float) (b - 0.5) * 2.0f);
562 game_update_view(dt);
563 return game_update_state(st);
569 * HACK: The BALL_FUDGE here guarantees that a putt doesn't drive
570 * the ball too directly down toward a lump, triggering rolling
571 * friction too early and stopping the ball prematurely.
574 file.vary.uv[ball].v[0] = -4.f * view_e[2][0] * view_m;
575 file.vary.uv[ball].v[1] = -4.f * view_e[2][1] * view_m + BALL_FUDGE;
576 file.vary.uv[ball].v[2] = -4.f * view_e[2][2] * view_m;
581 /*---------------------------------------------------------------------------*/
583 void game_set_rot(int d)
585 view_a += (float) (30.f * d) / config_get_d(CONFIG_MOUSE_SENSE);
588 void game_clr_mag(void)
593 void game_set_mag(int d)
595 view_m -= (float) (1.f * d) / config_get_d(CONFIG_MOUSE_SENSE);
601 void game_set_fly(float k)
603 struct s_vary *fp = &file.vary;
605 float x[3] = { 1.f, 0.f, 0.f };
606 float y[3] = { 0.f, 1.f, 0.f };
607 float z[3] = { 0.f, 0.f, 1.f };
608 float c0[3] = { 0.f, 0.f, 0.f };
609 float p0[3] = { 0.f, 0.f, 0.f };
610 float c1[3] = { 0.f, 0.f, 0.f };
611 float p1[3] = { 0.f, 0.f, 0.f };
619 v_sub(view_e[2], fp->uv[ball].p, fp->base->zv[0].p);
621 if (fabs(v_dot(view_e[1], view_e[2])) > 0.999)
624 v_crs(view_e[0], view_e[1], view_e[2]);
625 v_crs(view_e[2], view_e[0], view_e[1]);
627 v_nrm(view_e[0], view_e[0]);
628 v_nrm(view_e[2], view_e[2]);
630 /* k = 0.0 view is at the ball. */
634 v_cpy(c0, fp->uv[ball].p);
635 v_cpy(p0, fp->uv[ball].p);
638 v_mad(p0, p0, view_e[1], view_dy);
639 v_mad(p0, p0, view_e[2], view_dz);
641 /* k = +1.0 view is s_view 0 */
643 if (k >= 0 && fp->base->wc > 0)
645 v_cpy(p1, fp->base->wv[0].p);
646 v_cpy(c1, fp->base->wv[0].q);
649 /* k = -1.0 view is s_view 1 */
651 if (k <= 0 && fp->base->wc > 1)
653 v_cpy(p1, fp->base->wv[1].p);
654 v_cpy(c1, fp->base->wv[1].q);
657 /* Interpolate the views. */
660 v_mad(view_p, p0, v, k * k);
663 v_mad(view_c, c0, v, k * k);
665 /* Orthonormalize the view basis. */
667 v_sub(view_e[2], view_p, view_c);
668 v_crs(view_e[0], view_e[1], view_e[2]);
669 v_crs(view_e[2], view_e[0], view_e[1]);
670 v_nrm(view_e[0], view_e[0]);
671 v_nrm(view_e[2], view_e[2]);
673 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
676 void game_ball(int i)
685 for (ui = 0; ui < file.vary.uc; ui++)
687 file.vary.uv[ui].v[0] = 0.f;
688 file.vary.uv[ui].v[1] = 0.f;
689 file.vary.uv[ui].v[2] = 0.f;
691 file.vary.uv[ui].w[0] = 0.f;
692 file.vary.uv[ui].w[1] = 0.f;
693 file.vary.uv[ui].w[2] = 0.f;
697 void game_get_pos(float p[3], float e[3][3])
699 v_cpy(p, file.vary.uv[ball].p);
700 v_cpy(e[0], file.vary.uv[ball].e[0]);
701 v_cpy(e[1], file.vary.uv[ball].e[1]);
702 v_cpy(e[2], file.vary.uv[ball].e[2]);
705 void game_set_pos(float p[3], float e[3][3])
707 v_cpy(file.vary.uv[ball].p, p);
708 v_cpy(file.vary.uv[ball].e[0], e[0]);
709 v_cpy(file.vary.uv[ball].e[1], e[1]);
710 v_cpy(file.vary.uv[ball].e[2], e[2]);
713 /*---------------------------------------------------------------------------*/