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 glEnable(GL_TEXTURE_2D);
139 for (zi = 0; zi < base->zc; zi++)
143 glTranslatef(base->zv[zi].p[0],
147 part_draw_goal(M, base->zv[zi].r, gd->goal_k, t);
152 glDisable(GL_TEXTURE_2D);
154 /* Draw the goal column. */
156 for (zi = 0; zi < base->zc; zi++)
160 glTranslatef(base->zv[zi].p[0],
164 glScalef(base->zv[zi].r,
175 static void game_draw_jumps(const struct game_draw *gd,
176 const float *M, float t)
178 const struct s_base *base = gd->vary.base;
182 glEnable(GL_TEXTURE_2D);
184 for (ji = 0; ji < base->jc; ji++)
188 glTranslatef(base->jv[ji].p[0],
192 part_draw_jump(M, base->jv[ji].r, 1.0f, t);
197 glDisable(GL_TEXTURE_2D);
199 for (ji = 0; ji < base->jc; ji++)
203 glTranslatef(base->jv[ji].p[0],
206 glScalef(base->jv[ji].r,
210 jump_draw(!gd->jump_e);
216 static void game_draw_swchs(const struct s_vary *vary)
220 for (xi = 0; xi < vary->xc; xi++)
222 struct v_swch *xp = vary->xv + xi;
229 glTranslatef(xp->base->p[0],
232 glScalef(xp->base->r,
236 swch_draw(xp->f, xp->e);
242 /*---------------------------------------------------------------------------*/
244 static void game_draw_tilt(const struct game_draw *gd, int d)
246 const struct game_tilt *tilt = &gd->tilt;
247 const float *ball_p = gd->vary.uv[0].p;
249 /* Rotate the environment about the position of the ball. */
251 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
252 glRotatef(-tilt->rz * d, tilt->z[0], tilt->z[1], tilt->z[2]);
253 glRotatef(-tilt->rx * d, tilt->x[0], tilt->x[1], tilt->x[2]);
254 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
257 static void game_refl_all(const struct game_draw *gd)
261 game_draw_tilt(gd, 1);
263 /* Draw the floor. */
270 /*---------------------------------------------------------------------------*/
272 static void game_draw_light(void)
274 const float light_p[2][4] = {
275 { -8.0f, +32.0f, -8.0f, 0.0f },
276 { +8.0f, +32.0f, +8.0f, 0.0f },
278 const float light_c[2][4] = {
279 { 1.0f, 0.8f, 0.8f, 1.0f },
280 { 0.8f, 1.0f, 0.8f, 1.0f },
283 /* Configure the lighting. */
286 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
287 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
288 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
291 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
292 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
293 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
296 static void game_draw_back(const struct game_draw *gd, int pose, int d, float t)
298 if (pose == POSE_BALL)
303 const struct game_view *view = &gd->view;
307 const struct game_tilt *tilt = &gd->tilt;
309 glRotatef(tilt->rz * 2, tilt->z[0], tilt->z[1], tilt->z[2]);
310 glRotatef(tilt->rx * 2, tilt->x[0], tilt->x[1], tilt->x[2]);
313 glTranslatef(view->p[0], view->p[1] * d, view->p[2]);
315 if (config_get_d(CONFIG_BACKGROUND))
317 /* Draw all background layers back to front. */
319 sol_back(&gd->back.draw, BACK_DIST, FAR_DIST, t);
321 sol_back(&gd->back.draw, 0, BACK_DIST, t);
328 static void game_clip_refl(int d)
330 /* Fudge to eliminate the floor from reflection. */
332 GLdouble e[4], k = -0.00001;
339 glClipPlane(GL_CLIP_PLANE0, e);
342 static void game_clip_ball(const struct game_draw *gd, int d, const float *p)
344 GLdouble r, c[3], pz[4], nz[4];
346 /* Compute the plane giving the front of the ball, as seen from view.p. */
352 pz[0] = gd->view.p[0] - c[0];
353 pz[1] = gd->view.p[1] - c[1];
354 pz[2] = gd->view.p[2] - c[2];
356 r = sqrt(pz[0] * pz[0] + pz[1] * pz[1] + pz[2] * pz[2]);
361 pz[3] = -(pz[0] * c[0] +
365 /* Find the plane giving the back of the ball, as seen from view.p. */
372 /* Reflect these planes as necessary, and store them in the GL state. */
377 glClipPlane(GL_CLIP_PLANE1, nz);
378 glClipPlane(GL_CLIP_PLANE2, pz);
381 static void game_draw_fore(const struct game_draw *gd,
382 int pose, const float *M,
385 const float *ball_p = gd->vary.uv[0].p;
387 const struct s_draw *draw = &gd->draw;
391 /* Rotate the environment about the position of the ball. */
393 game_draw_tilt(gd, d);
395 /* Compute clipping planes for reflection and ball facing. */
398 game_clip_ball(gd, d, ball_p);
401 glEnable(GL_CLIP_PLANE0);
406 sol_draw(draw, 0, 1);
410 /* Draw the coins. */
412 game_draw_items(draw->vary, t);
414 /* Draw the floor. */
416 sol_draw(draw, 0, 1);
422 /* Draw the ball shadow. */
424 if (d > 0 && config_get_d(CONFIG_SHADOW))
433 game_draw_balls(draw->vary, M, t);
438 /* Draw the particles and light columns. */
440 glEnable(GL_COLOR_MATERIAL);
441 glDisable(GL_LIGHTING);
442 glDepthMask(GL_FALSE);
444 glColor3f(1.0f, 1.0f, 1.0f);
446 sol_bill(draw, M, t);
447 part_draw_coin(M, t);
449 glDisable(GL_TEXTURE_2D);
451 game_draw_goals(gd, M, t);
452 game_draw_jumps(gd, M, t);
453 game_draw_swchs(draw->vary);
455 glEnable(GL_TEXTURE_2D);
457 glColor3f(1.0f, 1.0f, 1.0f);
459 glDepthMask(GL_TRUE);
460 glEnable(GL_LIGHTING);
461 glDisable(GL_COLOR_MATERIAL);
464 glDisable(GL_CLIP_PLANE0);
469 /*---------------------------------------------------------------------------*/
471 void game_draw(const struct game_draw *gd, int pose, float t)
473 float fov = (float) config_get_d(CONFIG_VIEW_FOV);
475 if (gd->jump_b) fov *= 2.f * fabsf(gd->jump_dt - 0.5);
479 const struct game_view *view = &gd->view;
481 video_push_persp(fov, 0.1f, FAR_DIST);
484 float T[16], U[16], M[16], v[3];
486 /* Compute direct and reflected view bases. */
492 m_view(T, view->c, view->p, view->e[1]);
493 m_view(U, view->c, v, view->e[1]);
497 /* Apply the current view. */
499 v_sub(v, view->c, view->p);
501 glTranslatef(0.f, 0.f, -v_len(v));
503 glTranslatef(-view->c[0], -view->c[1], -view->c[2]);
505 if (gd->draw.reflective && config_get_d(CONFIG_REFLECTION))
507 glEnable(GL_STENCIL_TEST);
509 /* Draw the mirrors only into the stencil buffer. */
511 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
512 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
513 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
514 glDepthMask(GL_FALSE);
518 glDepthMask(GL_TRUE);
519 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
520 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
521 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
523 /* Draw the scene reflected into color and depth buffers. */
528 glScalef(+1.0f, -1.0f, +1.0f);
531 game_draw_back(gd, pose, -1, t);
532 game_draw_fore(gd, pose, U, -1, t);
537 glDisable(GL_STENCIL_TEST);
540 /* Draw the scene normally. */
544 if (gd->draw.reflective)
546 if (config_get_d(CONFIG_REFLECTION))
548 /* Draw background while preserving reflections. */
550 glEnable(GL_STENCIL_TEST);
552 glStencilFunc(GL_NOTEQUAL, 1, 0xFFFFFFFF);
553 game_draw_back(gd, pose, +1, t);
555 glDisable(GL_STENCIL_TEST);
563 /* Draw background. */
565 game_draw_back(gd, pose, +1, t);
568 * Draw mirrors, first fully opaque with a custom
569 * material color, then blending normally with the
570 * opaque surfaces using their original material
571 * properties. (Keeps background from showing
575 glEnable(GL_COLOR_MATERIAL);
577 glColor4f(0.0, 0.0, 0.05, 1.0);
579 glColor4f(1.0, 1.0, 1.0, 1.0);
581 glDisable(GL_COLOR_MATERIAL);
588 game_draw_back(gd, pose, +1, t);
592 game_draw_fore(gd, pose, T, +1, t);
597 /* Draw the fade overlay. */
599 fade_draw(gd->fade_k);
603 /*---------------------------------------------------------------------------*/
608 void game_lerp_init(struct game_lerp *gl, struct game_draw *gd)
612 sol_load_lerp(&gl->lerp, &gd->vary);
614 gl->tilt[PREV] = gl->tilt[CURR] = gd->tilt;
615 gl->view[PREV] = gl->view[CURR] = gd->view;
617 gl->goal_k[PREV] = gl->goal_k[CURR] = gd->goal_k;
618 gl->jump_dt[PREV] = gl->jump_dt[CURR] = gd->jump_dt;
621 void game_lerp_free(struct game_lerp *gl)
623 sol_free_lerp(&gl->lerp);
626 void game_lerp_copy(struct game_lerp *gl)
628 sol_lerp_copy(&gl->lerp);
630 gl->tilt[PREV] = gl->tilt[CURR];
631 gl->view[PREV] = gl->view[CURR];
633 gl->goal_k[PREV] = gl->goal_k[CURR];
634 gl->jump_dt[PREV] = gl->jump_dt[CURR];
637 void game_lerp_apply(struct game_lerp *gl, struct game_draw *gd)
643 sol_lerp_apply(&gl->lerp, a);
651 v_lerp(gd->tilt.x, gl->tilt[PREV].x, gl->tilt[CURR].x, a);
652 v_lerp(gd->tilt.z, gl->tilt[PREV].z, gl->tilt[CURR].z, a);
654 gd->tilt.rx = (gl->tilt[PREV].rx * (1.0f - a) + gl->tilt[CURR].rx * a);
655 gd->tilt.rz = (gl->tilt[PREV].rz * (1.0f - a) + gl->tilt[CURR].rz * a);
659 v_lerp(gd->view.c, gl->view[PREV].c, gl->view[CURR].c, a);
660 v_lerp(gd->view.p, gl->view[PREV].p, gl->view[CURR].p, a);
661 e_lerp(gd->view.e, gl->view[PREV].e, gl->view[CURR].e, a);
665 gd->goal_k = (gl->goal_k[PREV] * (1.0f - a) + gl->goal_k[CURR] * a);
666 gd->jump_dt = (gl->jump_dt[PREV] * (1.0f - a) + gl->jump_dt[CURR] * a);
669 /*---------------------------------------------------------------------------*/