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 for (ji = 0; ji < base->jc; ji++)
182 glTranslatef(base->jv[ji].p[0],
186 part_draw_jump(M, base->jv[ji].r, 1.0f, t);
191 for (ji = 0; ji < base->jc; ji++)
195 glTranslatef(base->jv[ji].p[0],
198 glScalef(base->jv[ji].r,
202 jump_draw(!gd->jump_e);
208 static void game_draw_swchs(const struct s_vary *vary)
212 for (xi = 0; xi < vary->xc; xi++)
214 struct v_swch *xp = vary->xv + xi;
221 glTranslatef(xp->base->p[0],
224 glScalef(xp->base->r,
228 swch_draw(xp->f, xp->e);
234 /*---------------------------------------------------------------------------*/
236 static void game_draw_tilt(const struct game_draw *gd, int d)
238 const struct game_tilt *tilt = &gd->tilt;
239 const float *ball_p = gd->vary.uv[0].p;
241 /* Rotate the environment about the position of the ball. */
243 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
244 glRotatef(-tilt->rz * d, tilt->z[0], tilt->z[1], tilt->z[2]);
245 glRotatef(-tilt->rx * d, tilt->x[0], tilt->x[1], tilt->x[2]);
246 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
249 static void game_refl_all(const struct game_draw *gd)
253 game_draw_tilt(gd, 1);
255 /* Draw the floor. */
262 /*---------------------------------------------------------------------------*/
264 static void game_draw_light(void)
266 const float light_p[2][4] = {
267 { -8.0f, +32.0f, -8.0f, 0.0f },
268 { +8.0f, +32.0f, +8.0f, 0.0f },
270 const float light_c[2][4] = {
271 { 1.0f, 0.8f, 0.8f, 1.0f },
272 { 0.8f, 1.0f, 0.8f, 1.0f },
275 /* Configure the lighting. */
278 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
279 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
280 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
283 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
284 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
285 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
288 static void game_draw_back(const struct game_draw *gd, int pose, int d, float t)
290 if (pose == POSE_BALL)
295 const struct game_view *view = &gd->view;
299 const struct game_tilt *tilt = &gd->tilt;
301 glRotatef(tilt->rz * 2, tilt->z[0], tilt->z[1], tilt->z[2]);
302 glRotatef(tilt->rx * 2, tilt->x[0], tilt->x[1], tilt->x[2]);
305 glTranslatef(view->p[0], view->p[1] * d, view->p[2]);
307 if (config_get_d(CONFIG_BACKGROUND))
309 /* Draw all background layers back to front. */
311 sol_back(&gd->back.draw, BACK_DIST, FAR_DIST, t);
313 sol_back(&gd->back.draw, 0, BACK_DIST, t);
320 static void game_clip_refl(int d)
322 /* Fudge to eliminate the floor from reflection. */
324 GLdouble e[4], k = -0.00001;
331 glClipPlane(GL_CLIP_PLANE0, e);
334 static void game_clip_ball(const struct game_draw *gd, int d, const float *p)
336 GLdouble r, c[3], pz[4], nz[4];
338 /* Compute the plane giving the front of the ball, as seen from view.p. */
344 pz[0] = gd->view.p[0] - c[0];
345 pz[1] = gd->view.p[1] - c[1];
346 pz[2] = gd->view.p[2] - c[2];
348 r = sqrt(pz[0] * pz[0] + pz[1] * pz[1] + pz[2] * pz[2]);
353 pz[3] = -(pz[0] * c[0] +
357 /* Find the plane giving the back of the ball, as seen from view.p. */
364 /* Reflect these planes as necessary, and store them in the GL state. */
369 glClipPlane(GL_CLIP_PLANE1, nz);
370 glClipPlane(GL_CLIP_PLANE2, pz);
373 static void game_draw_fore(const struct game_draw *gd,
374 int pose, const float *M,
377 const float *ball_p = gd->vary.uv[0].p;
379 const struct s_draw *draw = &gd->draw;
383 /* Rotate the environment about the position of the ball. */
385 game_draw_tilt(gd, d);
387 /* Compute clipping planes for reflection and ball facing. */
390 game_clip_ball(gd, d, ball_p);
393 glEnable(GL_CLIP_PLANE0);
398 sol_draw(draw, 0, 1);
402 /* Draw the coins. */
404 game_draw_items(draw->vary, t);
406 /* Draw the floor. */
408 sol_draw(draw, 0, 1);
414 /* Draw the ball shadow. */
416 if (d > 0 && config_get_d(CONFIG_SHADOW))
425 game_draw_balls(draw->vary, M, t);
430 /* Draw the particles and light columns. */
432 glEnable(GL_COLOR_MATERIAL);
433 glDisable(GL_LIGHTING);
434 glDepthMask(GL_FALSE);
436 sol_bill(draw, M, t);
437 part_draw_coin(M, t);
439 game_draw_goals(gd, M, t);
440 game_draw_jumps(gd, M, t);
441 game_draw_swchs(draw->vary);
443 glDepthMask(GL_TRUE);
444 glEnable(GL_LIGHTING);
445 glDisable(GL_COLOR_MATERIAL);
448 glDisable(GL_CLIP_PLANE0);
453 /*---------------------------------------------------------------------------*/
455 void game_draw(const struct game_draw *gd, int pose, float t)
457 float fov = (float) config_get_d(CONFIG_VIEW_FOV);
459 if (gd->jump_b) fov *= 2.f * fabsf(gd->jump_dt - 0.5);
463 const struct game_view *view = &gd->view;
465 video_push_persp(fov, 0.1f, FAR_DIST);
468 float T[16], U[16], M[16], v[3];
470 /* Compute direct and reflected view bases. */
476 m_view(T, view->c, view->p, view->e[1]);
477 m_view(U, view->c, v, view->e[1]);
481 /* Apply the current view. */
483 v_sub(v, view->c, view->p);
485 glTranslatef(0.f, 0.f, -v_len(v));
487 glTranslatef(-view->c[0], -view->c[1], -view->c[2]);
489 if (gd->draw.reflective && config_get_d(CONFIG_REFLECTION))
491 glEnable(GL_STENCIL_TEST);
493 /* Draw the mirrors only into the stencil buffer. */
495 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
496 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
497 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
498 glDepthMask(GL_FALSE);
502 glDepthMask(GL_TRUE);
503 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
504 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
505 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
507 /* Draw the scene reflected into color and depth buffers. */
512 glScalef(+1.0f, -1.0f, +1.0f);
515 game_draw_back(gd, pose, -1, t);
516 game_draw_fore(gd, pose, U, -1, t);
521 glDisable(GL_STENCIL_TEST);
524 /* Draw the scene normally. */
528 if (gd->draw.reflective)
530 if (config_get_d(CONFIG_REFLECTION))
532 /* Draw background while preserving reflections. */
534 glEnable(GL_STENCIL_TEST);
536 glStencilFunc(GL_NOTEQUAL, 1, 0xFFFFFFFF);
537 game_draw_back(gd, pose, +1, t);
539 glDisable(GL_STENCIL_TEST);
547 /* Draw background. */
549 game_draw_back(gd, pose, +1, t);
552 * Draw mirrors, first fully opaque with a custom
553 * material color, then blending normally with the
554 * opaque surfaces using their original material
555 * properties. (Keeps background from showing
559 glEnable(GL_COLOR_MATERIAL);
561 glColor4f(0.0, 0.0, 0.05, 1.0);
563 glColor4f(1.0, 1.0, 1.0, 1.0);
565 glDisable(GL_COLOR_MATERIAL);
572 game_draw_back(gd, pose, +1, t);
576 game_draw_fore(gd, pose, T, +1, t);
581 /* Draw the fade overlay. */
583 fade_draw(gd->fade_k);
587 /*---------------------------------------------------------------------------*/
592 void game_lerp_init(struct game_lerp *gl, struct game_draw *gd)
596 sol_load_lerp(&gl->lerp, &gd->vary);
598 gl->tilt[PREV] = gl->tilt[CURR] = gd->tilt;
599 gl->view[PREV] = gl->view[CURR] = gd->view;
601 gl->goal_k[PREV] = gl->goal_k[CURR] = gd->goal_k;
602 gl->jump_dt[PREV] = gl->jump_dt[CURR] = gd->jump_dt;
605 void game_lerp_free(struct game_lerp *gl)
607 sol_free_lerp(&gl->lerp);
610 void game_lerp_copy(struct game_lerp *gl)
612 sol_lerp_copy(&gl->lerp);
614 gl->tilt[PREV] = gl->tilt[CURR];
615 gl->view[PREV] = gl->view[CURR];
617 gl->goal_k[PREV] = gl->goal_k[CURR];
618 gl->jump_dt[PREV] = gl->jump_dt[CURR];
621 void game_lerp_apply(struct game_lerp *gl, struct game_draw *gd)
627 sol_lerp_apply(&gl->lerp, a);
635 v_lerp(gd->tilt.x, gl->tilt[PREV].x, gl->tilt[CURR].x, a);
636 v_lerp(gd->tilt.z, gl->tilt[PREV].z, gl->tilt[CURR].z, a);
638 gd->tilt.rx = (gl->tilt[PREV].rx * (1.0f - a) + gl->tilt[CURR].rx * a);
639 gd->tilt.rz = (gl->tilt[PREV].rz * (1.0f - a) + gl->tilt[CURR].rz * a);
643 v_lerp(gd->view.c, gl->view[PREV].c, gl->view[CURR].c, a);
644 v_lerp(gd->view.p, gl->view[PREV].p, gl->view[CURR].p, a);
645 e_lerp(gd->view.e, gl->view[PREV].e, gl->view[CURR].e, a);
649 gd->goal_k = (gl->goal_k[PREV] * (1.0f - a) + gl->goal_k[CURR] * a);
650 gd->jump_dt = (gl->jump_dt[PREV] * (1.0f - a) + gl->jump_dt[CURR] * a);
653 /*---------------------------------------------------------------------------*/