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;
39 static float view_a; /* Ideal view rotation about Y axis */
41 static float view_ry; /* Angular velocity about Y axis */
42 static float view_dy; /* Ideal view distance above ball */
43 static float view_dz; /* Ideal view distance behind ball */
45 static float view_c[3]; /* Current view center */
46 static float view_v[3]; /* Current view vector */
47 static float view_p[3]; /* Current view position */
48 static float view_e[3][3]; /* Current view orientation */
50 static float jump_e = 1; /* Jumping enabled flag */
51 static float jump_b = 0; /* Jump-in-progress flag */
52 static float jump_dt; /* Jump duration */
53 static float jump_p[3]; /* Jump destination */
55 static float idle_t; /* Idling timeout */
57 /*---------------------------------------------------------------------------*/
59 static void view_init(void)
86 void game_init(const char *s)
96 sol_load_full(&file, s, config_get_d(CONFIG_SHADOW));
97 sol_init_sim(&file.vary);
99 for (i = 0; i < file.base.dc; i++)
101 const char *k = file.base.av + file.base.dv[i].ai;
102 const char *v = file.base.av + file.base.dv[i].aj;
104 if (strcmp(k, "idle") == 0)
106 sscanf(v, "%f", &idle_t);
117 sol_free_full(&file);
120 /*---------------------------------------------------------------------------*/
122 static void game_draw_vect_prim(const struct s_vary *fp, GLenum mode)
129 v_cpy(p, fp->uv[ball].p);
137 glColor4f(1.0f, 1.0f, 0.5f, 0.5f);
138 glVertex3f(p[0] - x[0] * r,
142 glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
143 glVertex3f(p[0] + z[0] * view_m,
144 p[1] + z[1] * view_m,
145 p[2] + z[2] * view_m);
147 glColor4f(1.0f, 1.0f, 0.0f, 0.5f);
148 glVertex3f(p[0] + x[0] * r,
155 static const struct d_mtrl *game_draw_vect(const struct d_mtrl *mq,
156 const struct s_vary *fp)
160 glPushAttrib(GL_TEXTURE_BIT);
161 glPushAttrib(GL_POLYGON_BIT);
162 glPushAttrib(GL_LIGHTING_BIT);
163 glPushAttrib(GL_DEPTH_BUFFER_BIT);
165 glEnable(GL_COLOR_MATERIAL);
166 glDisable(GL_LIGHTING);
167 glDisable(GL_TEXTURE_2D);
168 glDepthMask(GL_FALSE);
170 glEnable(GL_DEPTH_TEST);
171 game_draw_vect_prim(fp, GL_TRIANGLES);
173 glDisable(GL_DEPTH_TEST);
174 game_draw_vect_prim(fp, GL_LINE_STRIP);
184 static const struct d_mtrl *game_draw_balls(const struct d_mtrl *mq,
185 const struct s_vary *fp,
186 const float *bill_M, float t)
188 static const GLfloat color[5][4] = {
189 { 1.0f, 1.0f, 1.0f, 0.7f },
190 { 1.0f, 0.0f, 0.0f, 1.0f },
191 { 0.0f, 1.0f, 0.0f, 1.0f },
192 { 0.0f, 0.0f, 1.0f, 1.0f },
193 { 1.0f, 1.0f, 0.0f, 1.0f },
198 glEnable(GL_COLOR_MATERIAL);
200 for (ui = curr_party(); ui > 0; ui--)
207 m_basis(ball_M, fp->uv[ui].e[0], fp->uv[ui].e[1], fp->uv[ui].e[2]);
208 m_basis(pend_M, fp->uv[ui].E[0], fp->uv[ui].E[1], fp->uv[ui].E[2]);
212 glTranslatef(fp->uv[ui].p[0],
213 fp->uv[ui].p[1] + BALL_FUDGE,
215 glScalef(fp->uv[ui].r,
219 glColor4fv(color[ui]);
220 mq = ball_draw(mq, ball_M, pend_M, bill_M, t);
228 glTranslatef(fp->uv[ui].p[0],
229 fp->uv[ui].p[1] - fp->uv[ui].r + BALL_FUDGE,
231 glScalef(fp->uv[ui].r,
235 glColor4f(color[ui][0],
245 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
246 glDisable(GL_COLOR_MATERIAL);
251 static const struct d_mtrl *game_draw_goals(const struct d_mtrl *mq,
252 const struct s_base *fp)
256 for (zi = 0; zi < fp->zc; zi++)
260 glTranslatef(fp->zv[zi].p[0],
270 static const struct d_mtrl *game_draw_jumps(const struct d_mtrl *mq,
271 const struct s_base *fp)
273 float t = 0.001f * SDL_GetTicks();
276 for (ji = 0; ji < fp->jc; ji++)
280 glTranslatef(fp->jv[ji].p[0],
284 glScalef(fp->jv[ji].r, 1.f, fp->jv[ji].r);
285 mq = jump_draw(mq, t, !jump_e);
292 static const struct d_mtrl *game_draw_swchs(const struct d_mtrl *mq,
293 const struct s_vary *fp)
297 for (xi = 0; xi < fp->xc; xi++)
299 struct v_swch *xp = fp->xv + xi;
306 glTranslatef(xp->base->p[0],
310 glScalef(xp->base->r, 1.f, xp->base->r);
311 mq = swch_draw(mq, xp->f, xp->e);
318 /*---------------------------------------------------------------------------*/
320 void game_draw(int pose, float t)
322 const float light_p[4] = { 8.f, 32.f, 8.f, 0.f };
324 const struct s_draw *fp = &file.draw;
325 const struct d_mtrl *mq = sol_draw_enable();
329 if (jump_b) fov *= 2.0f * fabsf(jump_dt - 0.5f);
331 video_push_persp(fov, 0.1f, FAR_DIST);
334 float T[16], M[16], v[3], rx, ry;
336 m_view(T, view_c, view_p, view_e[1]);
339 v_sub(v, view_c, view_p);
341 rx = V_DEG(fatan2f(-v[1], fsqrtf(v[0] * v[0] + v[2] * v[2])));
342 ry = V_DEG(fatan2f(+v[0], -v[2]));
344 glTranslatef(0.f, 0.f, -v_len(v));
346 glTranslatef(-view_c[0], -view_c[1], -view_c[2]);
348 /* Center the skybox about the position of the camera. */
352 glTranslatef(view_p[0], view_p[1], view_p[2]);
353 mq = back_draw(mq, 0);
358 glLightfv(GL_LIGHT0, GL_POSITION, light_p);
360 /* Draw the floor. */
362 mq = sol_draw(fp, mq, 0, 1);
364 /* Draw the game elements. */
367 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
371 mq = game_draw_balls(mq, fp->vary, T, t);
372 mq = game_draw_vect(mq, fp->vary);
375 glEnable(GL_COLOR_MATERIAL);
376 glDisable(GL_LIGHTING);
377 glDepthMask(GL_FALSE);
379 mq = game_draw_goals(mq, fp->base);
380 mq = game_draw_jumps(mq, fp->base);
381 mq = game_draw_swchs(mq, fp->vary);
383 glDepthMask(GL_TRUE);
384 glEnable(GL_LIGHTING);
385 glDisable(GL_COLOR_MATERIAL);
390 sol_draw_disable(mq);
393 /*---------------------------------------------------------------------------*/
395 void game_update_view(float dt)
397 const float y[3] = { 0.f, 1.f, 0.f };
406 /* Center the view about the ball. */
408 v_cpy(view_c, file.vary.uv[ball].p);
409 v_inv(view_v, file.vary.uv[ball].v);
411 switch (config_get_d(CONFIG_CAMERA))
414 /* Camera 2: View vector is given by view angle. */
416 view_e[2][0] = fsinf(V_RAD(view_a));
418 view_e[2][2] = fcosf(V_RAD(view_a));
424 /* View vector approaches the ball velocity vector. */
426 v_mad(e, view_v, y, v_dot(view_v, y));
429 k = v_dot(view_v, view_v);
431 v_sub(view_e[2], view_p, view_c);
432 v_mad(view_e[2], view_e[2], view_v, k * dt * 0.1f);
435 /* Orthonormalize the basis of the view in its new position. */
437 v_crs(view_e[0], view_e[1], view_e[2]);
438 v_crs(view_e[2], view_e[0], view_e[1]);
439 v_nrm(view_e[0], view_e[0]);
440 v_nrm(view_e[2], view_e[2]);
442 /* The current view (dy, dz) approaches the ideal (view_dy, view_dz). */
444 v_sub(d, view_p, view_c);
446 dy = v_dot(view_e[1], d);
447 dz = v_dot(view_e[2], d);
449 dy += (view_dy - dy) * s;
450 dz += (view_dz - dz) * s;
452 /* Compute the new view position. */
454 view_p[0] = view_p[1] = view_p[2] = 0.f;
456 v_mad(view_p, view_c, view_e[1], dy);
457 v_mad(view_p, view_p, view_e[2], dz);
459 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
462 static int game_update_state(float dt)
464 static float t = 0.f;
466 struct s_vary *fp = &file.vary;
474 /* Test for a switch. */
476 if (sol_swch_test(fp, ball) == SWCH_TRIGGER)
477 audio_play(AUD_SWITCH, 1.f);
479 /* Test for a jump. */
481 if (jump_e == 1 && jump_b == 0 && (sol_jump_test(fp, jump_p, ball) ==
488 audio_play(AUD_JUMP, 1.f);
490 if (jump_e == 0 && jump_b == 0 && (sol_jump_test(fp, jump_p, ball) ==
496 /* Test for fall-out. */
498 if (fp->uv[ball].p[1] < -10.f)
501 /* Test for a goal or stop. */
503 if (t > 1.f && sol_goal_test(fp, p, ball))
519 * On most hardware, rendering requires much more computing power than
520 * physics. Since physics takes less time than graphics, it make sense to
521 * detach the physics update time step from the graphics frame rate. By
522 * performing multiple physics updates for each graphics update, we get away
523 * with higher quality physics with little impact on overall performance.
525 * Toward this end, we establish a baseline maximum physics time step. If
526 * the measured frame time exceeds this maximum, we cut the time step in
527 * half, and do two updates. If THIS time step exceeds the maximum, we do
528 * four updates. And so on. In this way, the physics system is allowed to
529 * seek an optimal update rate independent of, yet in integral sync with, the
530 * graphics frame rate.
533 int game_step(const float g[3], float dt)
535 struct s_vary *fp = &file.vary;
537 static float s = 0.f;
538 static float t = 0.f;
545 s = (7.f * s + dt) / 8.f;
556 fp->uv[ball].p[0] = jump_p[0];
557 fp->uv[ball].p[1] = jump_p[1];
558 fp->uv[ball].p[2] = jump_p[2];
567 while (t > MAX_DT && n < MAX_DN)
573 for (i = 0; i < n; i++)
575 d = sol_step(fp, g, t, ball, &m);
583 /* Mix the sound of a ball bounce. */
586 audio_play(AUD_BUMP, (float) (b - 0.5) * 2.0f);
589 game_update_view(dt);
590 return game_update_state(st);
596 * HACK: The BALL_FUDGE here guarantees that a putt doesn't drive
597 * the ball too directly down toward a lump, triggering rolling
598 * friction too early and stopping the ball prematurely.
601 file.vary.uv[ball].v[0] = -4.f * view_e[2][0] * view_m;
602 file.vary.uv[ball].v[1] = -4.f * view_e[2][1] * view_m + BALL_FUDGE;
603 file.vary.uv[ball].v[2] = -4.f * view_e[2][2] * view_m;
608 /*---------------------------------------------------------------------------*/
610 void game_set_rot(int d)
612 view_a += (float) (30.f * d) / config_get_d(CONFIG_MOUSE_SENSE);
615 void game_clr_mag(void)
620 void game_set_mag(int d)
622 view_m -= (float) (1.f * d) / config_get_d(CONFIG_MOUSE_SENSE);
628 void game_set_fly(float k)
630 struct s_vary *fp = &file.vary;
632 float x[3] = { 1.f, 0.f, 0.f };
633 float y[3] = { 0.f, 1.f, 0.f };
634 float z[3] = { 0.f, 0.f, 1.f };
635 float c0[3] = { 0.f, 0.f, 0.f };
636 float p0[3] = { 0.f, 0.f, 0.f };
637 float c1[3] = { 0.f, 0.f, 0.f };
638 float p1[3] = { 0.f, 0.f, 0.f };
643 v_sub(view_e[2], fp->uv[ball].p, fp->base->zv[0].p);
645 if (fabs(v_dot(view_e[1], view_e[2])) > 0.999)
648 v_crs(view_e[0], view_e[1], view_e[2]);
649 v_crs(view_e[2], view_e[0], view_e[1]);
651 v_nrm(view_e[0], view_e[0]);
652 v_nrm(view_e[2], view_e[2]);
654 /* k = 0.0 view is at the ball. */
658 v_cpy(c0, fp->uv[ball].p);
659 v_cpy(p0, fp->uv[ball].p);
662 v_mad(p0, p0, view_e[1], view_dy);
663 v_mad(p0, p0, view_e[2], view_dz);
665 /* k = +1.0 view is s_view 0 */
667 if (k >= 0 && fp->base->wc > 0)
669 v_cpy(p1, fp->base->wv[0].p);
670 v_cpy(c1, fp->base->wv[0].q);
673 /* k = -1.0 view is s_view 1 */
675 if (k <= 0 && fp->base->wc > 1)
677 v_cpy(p1, fp->base->wv[1].p);
678 v_cpy(c1, fp->base->wv[1].q);
681 /* Interpolate the views. */
684 v_mad(view_p, p0, v, k * k);
687 v_mad(view_c, c0, v, k * k);
689 /* Orthonormalize the view basis. */
691 v_sub(view_e[2], view_p, view_c);
692 v_crs(view_e[0], view_e[1], view_e[2]);
693 v_crs(view_e[2], view_e[0], view_e[1]);
694 v_nrm(view_e[0], view_e[0]);
695 v_nrm(view_e[2], view_e[2]);
697 view_a = V_DEG(fatan2f(view_e[2][0], view_e[2][2]));
700 void game_ball(int i)
709 for (ui = 0; ui < file.vary.uc; ui++)
711 file.vary.uv[ui].v[0] = 0.f;
712 file.vary.uv[ui].v[1] = 0.f;
713 file.vary.uv[ui].v[2] = 0.f;
715 file.vary.uv[ui].w[0] = 0.f;
716 file.vary.uv[ui].w[1] = 0.f;
717 file.vary.uv[ui].w[2] = 0.f;
721 void game_get_pos(float p[3], float e[3][3])
723 v_cpy(p, file.vary.uv[ball].p);
724 v_cpy(e[0], file.vary.uv[ball].e[0]);
725 v_cpy(e[1], file.vary.uv[ball].e[1]);
726 v_cpy(e[2], file.vary.uv[ball].e[2]);
729 void game_set_pos(float p[3], float e[3][3])
731 v_cpy(file.vary.uv[ball].p, p);
732 v_cpy(file.vary.uv[ball].e[0], e[0]);
733 v_cpy(file.vary.uv[ball].e[1], e[1]);
734 v_cpy(file.vary.uv[ball].e[2], e[2]);
737 /*---------------------------------------------------------------------------*/