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.
25 #include "solid_draw.h"
27 #include "game_draw.h"
29 /*---------------------------------------------------------------------------*/
31 static void game_draw_balls(const struct s_vary *vary,
32 const float *bill_M, float t)
34 float c[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
39 m_basis(ball_M, vary->uv[0].e[0], vary->uv[0].e[1], vary->uv[0].e[2]);
40 m_basis(pend_M, vary->uv[0].E[0], vary->uv[0].E[1], vary->uv[0].E[2]);
42 glPushAttrib(GL_LIGHTING_BIT);
45 glTranslatef(vary->uv[0].p[0],
46 vary->uv[0].p[1] + BALL_FUDGE,
48 glScalef(vary->uv[0].r,
53 ball_draw(ball_M, pend_M, bill_M, t);
59 static void game_draw_items(const struct s_vary *vary, float t)
64 glPushAttrib(GL_LIGHTING_BIT);
68 for (hi = 0; hi < vary->hc; hi++)
70 if (vary->hv[hi].t == ITEM_COIN && vary->hv[hi].n > 0)
74 glTranslatef(vary->hv[hi].p[0],
77 glRotatef(r, 0.0f, 1.0f, 0.0f);
78 item_draw(&vary->hv[hi], r);
85 item_push(ITEM_SHRINK);
87 for (hi = 0; hi < vary->hc; hi++)
89 if (vary->hv[hi].t == ITEM_SHRINK)
93 glTranslatef(vary->hv[hi].p[0],
96 glRotatef(r, 0.0f, 1.0f, 0.0f);
97 item_draw(&vary->hv[hi], r);
104 item_push(ITEM_GROW);
106 for (hi = 0; hi < vary->hc; hi++)
108 if (vary->hv[hi].t == ITEM_GROW)
112 glTranslatef(vary->hv[hi].p[0],
115 glRotatef(r, 0.0f, 1.0f, 0.0f);
116 item_draw(&vary->hv[hi], r);
126 static void game_draw_goals(const struct game_draw *gd,
127 const float *M, float t)
129 const struct s_base *base = gd->vary.base;
135 /* Draw the goal particles. */
137 for (zi = 0; zi < base->zc; zi++)
141 glTranslatef(base->zv[zi].p[0],
145 part_draw_goal(M, base->zv[zi].r, gd->goal_k, t);
150 /* Draw the goal column. */
152 for (zi = 0; zi < base->zc; zi++)
156 glTranslatef(base->zv[zi].p[0],
160 glScalef(base->zv[zi].r,
171 static void game_draw_jumps(const struct game_draw *gd,
172 const float *M, float t)
174 const struct s_base *base = gd->vary.base;
178 glEnable(GL_TEXTURE_2D);
180 for (ji = 0; ji < base->jc; ji++)
184 glTranslatef(base->jv[ji].p[0],
188 part_draw_jump(M, base->jv[ji].r, 1.0f, t);
193 glDisable(GL_TEXTURE_2D);
195 for (ji = 0; ji < base->jc; ji++)
199 glTranslatef(base->jv[ji].p[0],
202 glScalef(base->jv[ji].r,
206 jump_draw(!gd->jump_e);
212 static void game_draw_swchs(const struct s_vary *vary)
216 for (xi = 0; xi < vary->xc; xi++)
218 struct v_swch *xp = vary->xv + xi;
225 glTranslatef(xp->base->p[0],
228 glScalef(xp->base->r,
232 swch_draw(xp->f, xp->e);
238 /*---------------------------------------------------------------------------*/
240 static void game_draw_tilt(const struct game_draw *gd, int d)
242 const struct game_tilt *tilt = &gd->tilt;
243 const float *ball_p = gd->vary.uv[0].p;
245 /* Rotate the environment about the position of the ball. */
247 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
248 glRotatef(-tilt->rz * d, tilt->z[0], tilt->z[1], tilt->z[2]);
249 glRotatef(-tilt->rx * d, tilt->x[0], tilt->x[1], tilt->x[2]);
250 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
253 static void game_refl_all(const struct game_draw *gd)
257 game_draw_tilt(gd, 1);
259 /* Draw the floor. */
266 /*---------------------------------------------------------------------------*/
268 static void game_draw_light(void)
270 const float light_p[2][4] = {
271 { -8.0f, +32.0f, -8.0f, 0.0f },
272 { +8.0f, +32.0f, +8.0f, 0.0f },
274 const float light_c[2][4] = {
275 { 1.0f, 0.8f, 0.8f, 1.0f },
276 { 0.8f, 1.0f, 0.8f, 1.0f },
279 /* Configure the lighting. */
282 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
283 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
284 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
287 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
288 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
289 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
292 static void game_draw_back(const struct game_draw *gd, int pose, int d, float t)
294 if (pose == POSE_BALL)
299 const struct game_view *view = &gd->view;
303 const struct game_tilt *tilt = &gd->tilt;
305 glRotatef(tilt->rz * 2, tilt->z[0], tilt->z[1], tilt->z[2]);
306 glRotatef(tilt->rx * 2, tilt->x[0], tilt->x[1], tilt->x[2]);
309 glTranslatef(view->p[0], view->p[1] * d, view->p[2]);
311 if (config_get_d(CONFIG_BACKGROUND))
313 /* Draw all background layers back to front. */
315 sol_back(&gd->back.draw, BACK_DIST, FAR_DIST, t);
317 sol_back(&gd->back.draw, 0, BACK_DIST, t);
324 static void game_clip_refl(int d)
326 /* Fudge to eliminate the floor from reflection. */
328 GLdouble e[4], k = -0.00001;
335 glClipPlane(GL_CLIP_PLANE0, e);
338 static void game_clip_ball(const struct game_draw *gd, int d, const float *p)
340 GLdouble r, c[3], pz[4], nz[4];
342 /* Compute the plane giving the front of the ball, as seen from view.p. */
348 pz[0] = gd->view.p[0] - c[0];
349 pz[1] = gd->view.p[1] - c[1];
350 pz[2] = gd->view.p[2] - c[2];
352 r = sqrt(pz[0] * pz[0] + pz[1] * pz[1] + pz[2] * pz[2]);
357 pz[3] = -(pz[0] * c[0] +
361 /* Find the plane giving the back of the ball, as seen from view.p. */
368 /* Reflect these planes as necessary, and store them in the GL state. */
373 glClipPlane(GL_CLIP_PLANE1, nz);
374 glClipPlane(GL_CLIP_PLANE2, pz);
377 static void game_draw_fore(const struct game_draw *gd,
378 int pose, const float *M,
381 const float *ball_p = gd->vary.uv[0].p;
383 const struct s_draw *draw = &gd->draw;
387 /* Rotate the environment about the position of the ball. */
389 game_draw_tilt(gd, d);
391 /* Compute clipping planes for reflection and ball facing. */
394 game_clip_ball(gd, d, ball_p);
397 glEnable(GL_CLIP_PLANE0);
402 sol_draw(draw, 0, 1);
406 /* Draw the coins. */
408 game_draw_items(draw->vary, t);
410 /* Draw the floor. */
412 sol_draw(draw, 0, 1);
418 /* Draw the ball shadow. */
420 if (d > 0 && config_get_d(CONFIG_SHADOW))
429 game_draw_balls(draw->vary, M, t);
434 /* Draw the particles and light columns. */
436 glEnable(GL_COLOR_MATERIAL);
437 glDisable(GL_LIGHTING);
438 glDepthMask(GL_FALSE);
440 glColor3f(1.0f, 1.0f, 1.0f);
442 sol_bill(draw, M, t);
443 part_draw_coin(M, t);
445 game_draw_goals(gd, M, t);
447 glDisable(GL_TEXTURE_2D);
449 game_draw_jumps(gd, M, t);
450 game_draw_swchs(draw->vary);
452 glEnable(GL_TEXTURE_2D);
454 glColor3f(1.0f, 1.0f, 1.0f);
456 glDepthMask(GL_TRUE);
457 glEnable(GL_LIGHTING);
458 glDisable(GL_COLOR_MATERIAL);
461 glDisable(GL_CLIP_PLANE0);
466 /*---------------------------------------------------------------------------*/
468 void game_draw(const struct game_draw *gd, int pose, float t)
470 float fov = (float) config_get_d(CONFIG_VIEW_FOV);
472 if (gd->jump_b) fov *= 2.f * fabsf(gd->jump_dt - 0.5);
476 const struct game_view *view = &gd->view;
478 video_push_persp(fov, 0.1f, FAR_DIST);
481 float T[16], U[16], M[16], v[3];
483 /* Compute direct and reflected view bases. */
489 m_view(T, view->c, view->p, view->e[1]);
490 m_view(U, view->c, v, view->e[1]);
494 /* Apply the current view. */
496 v_sub(v, view->c, view->p);
498 glTranslatef(0.f, 0.f, -v_len(v));
500 glTranslatef(-view->c[0], -view->c[1], -view->c[2]);
502 if (gd->draw.reflective && config_get_d(CONFIG_REFLECTION))
504 glEnable(GL_STENCIL_TEST);
506 /* Draw the mirrors only into the stencil buffer. */
508 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
509 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
510 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
511 glDepthMask(GL_FALSE);
515 glDepthMask(GL_TRUE);
516 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
517 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
518 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
520 /* Draw the scene reflected into color and depth buffers. */
525 glScalef(+1.0f, -1.0f, +1.0f);
528 game_draw_back(gd, pose, -1, t);
529 game_draw_fore(gd, pose, U, -1, t);
534 glDisable(GL_STENCIL_TEST);
537 /* Draw the scene normally. */
541 if (gd->draw.reflective)
543 if (config_get_d(CONFIG_REFLECTION))
545 /* Draw background while preserving reflections. */
547 glEnable(GL_STENCIL_TEST);
549 glStencilFunc(GL_NOTEQUAL, 1, 0xFFFFFFFF);
550 game_draw_back(gd, pose, +1, t);
552 glDisable(GL_STENCIL_TEST);
560 /* Draw background. */
562 game_draw_back(gd, pose, +1, t);
565 * Draw mirrors, first fully opaque with a custom
566 * material color, then blending normally with the
567 * opaque surfaces using their original material
568 * properties. (Keeps background from showing
572 glEnable(GL_COLOR_MATERIAL);
574 glColor4f(0.0, 0.0, 0.05, 1.0);
576 glColor4f(1.0, 1.0, 1.0, 1.0);
578 glDisable(GL_COLOR_MATERIAL);
585 game_draw_back(gd, pose, +1, t);
589 game_draw_fore(gd, pose, T, +1, t);
594 /* Draw the fade overlay. */
596 fade_draw(gd->fade_k);
600 /*---------------------------------------------------------------------------*/
605 void game_lerp_init(struct game_lerp *gl, struct game_draw *gd)
609 sol_load_lerp(&gl->lerp, &gd->vary);
611 gl->tilt[PREV] = gl->tilt[CURR] = gd->tilt;
612 gl->view[PREV] = gl->view[CURR] = gd->view;
614 gl->goal_k[PREV] = gl->goal_k[CURR] = gd->goal_k;
615 gl->jump_dt[PREV] = gl->jump_dt[CURR] = gd->jump_dt;
618 void game_lerp_free(struct game_lerp *gl)
620 sol_free_lerp(&gl->lerp);
623 void game_lerp_copy(struct game_lerp *gl)
625 sol_lerp_copy(&gl->lerp);
627 gl->tilt[PREV] = gl->tilt[CURR];
628 gl->view[PREV] = gl->view[CURR];
630 gl->goal_k[PREV] = gl->goal_k[CURR];
631 gl->jump_dt[PREV] = gl->jump_dt[CURR];
634 void game_lerp_apply(struct game_lerp *gl, struct game_draw *gd)
640 sol_lerp_apply(&gl->lerp, a);
648 v_lerp(gd->tilt.x, gl->tilt[PREV].x, gl->tilt[CURR].x, a);
649 v_lerp(gd->tilt.z, gl->tilt[PREV].z, gl->tilt[CURR].z, a);
651 gd->tilt.rx = (gl->tilt[PREV].rx * (1.0f - a) + gl->tilt[CURR].rx * a);
652 gd->tilt.rz = (gl->tilt[PREV].rz * (1.0f - a) + gl->tilt[CURR].rz * a);
656 v_lerp(gd->view.c, gl->view[PREV].c, gl->view[CURR].c, a);
657 v_lerp(gd->view.p, gl->view[PREV].p, gl->view[CURR].p, a);
658 e_lerp(gd->view.e, gl->view[PREV].e, gl->view[CURR].e, a);
662 gd->goal_k = (gl->goal_k[PREV] * (1.0f - a) + gl->goal_k[CURR] * a);
663 gd->jump_dt = (gl->jump_dt[PREV] * (1.0f - a) + gl->jump_dt[CURR] * a);
666 /*---------------------------------------------------------------------------*/