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.
24 #include "solid_draw.h"
26 #include "game_draw.h"
28 /*---------------------------------------------------------------------------*/
30 static const struct d_mtrl *game_draw_balls(const struct d_mtrl *mq,
31 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]);
44 glTranslatef(vary->uv[0].p[0],
45 vary->uv[0].p[1] + BALL_FUDGE,
47 glScalef(vary->uv[0].r,
52 mq = ball_draw(mq, ball_M, pend_M, bill_M, t);
59 static const struct d_mtrl *game_draw_items(const struct d_mtrl *mq,
60 const struct s_vary *vary,
61 const float *bill_M, float t)
65 glPushAttrib(GL_LIGHTING_BIT);
69 for (hi = 0; hi < vary->hc; hi++)
71 if (vary->hv[hi].t == ITEM_COIN && vary->hv[hi].n > 0)
75 glTranslatef(vary->hv[hi].p[0],
78 mq = item_draw(mq, &vary->hv[hi], bill_M, t);
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 mq = item_draw(mq, &vary->hv[hi], bill_M, t);
103 item_push(ITEM_GROW);
105 for (hi = 0; hi < vary->hc; hi++)
107 if (vary->hv[hi].t == ITEM_GROW)
111 glTranslatef(vary->hv[hi].p[0],
114 mq = item_draw(mq, &vary->hv[hi], bill_M, t);
126 static const struct d_mtrl *game_draw_goals(const struct d_mtrl *mq,
127 const struct game_draw *gd,
128 const float *M, float t)
130 const struct s_base *base = gd->vary.base;
136 /* Draw the goal column. */
138 for (zi = 0; zi < base->zc; zi++)
142 glTranslatef(base->zv[zi].p[0],
146 glScalef(base->zv[zi].r,
150 mq = goal_draw(mq, t);
158 static const struct d_mtrl *game_draw_jumps(const struct d_mtrl *mq,
159 const struct game_draw *gd,
160 const float *M, float t)
162 const struct s_base *base = gd->vary.base;
166 for (ji = 0; ji < base->jc; ji++)
170 glTranslatef(base->jv[ji].p[0],
173 glScalef(base->jv[ji].r,
177 mq = jump_draw(mq, t, !gd->jump_e);
184 static const struct d_mtrl *game_draw_swchs(const struct d_mtrl *mq,
185 const struct s_vary *vary)
189 for (xi = 0; xi < vary->xc; xi++)
191 struct v_swch *xp = vary->xv + xi;
198 glTranslatef(xp->base->p[0],
201 glScalef(xp->base->r,
205 mq = swch_draw(mq, xp->f, xp->e);
212 /*---------------------------------------------------------------------------*/
214 static void game_draw_tilt(const struct game_draw *gd, int d)
216 const struct game_tilt *tilt = &gd->tilt;
217 const float *ball_p = gd->vary.uv[0].p;
219 /* Rotate the environment about the position of the ball. */
221 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
222 glRotatef(-tilt->rz * d, tilt->z[0], tilt->z[1], tilt->z[2]);
223 glRotatef(-tilt->rx * d, tilt->x[0], tilt->x[1], tilt->x[2]);
224 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
227 static const struct d_mtrl *game_refl_all(const struct d_mtrl *mq,
228 const struct game_draw *gd)
232 game_draw_tilt(gd, 1);
234 /* Draw the floor. */
236 mq = sol_refl(&gd->draw, mq);
243 /*---------------------------------------------------------------------------*/
245 static void game_draw_light(void)
247 const float light_p[2][4] = {
248 { -8.0f, +32.0f, -8.0f, 0.0f },
249 { +8.0f, +32.0f, +8.0f, 0.0f },
251 const float light_c[2][4] = {
252 { 1.0f, 0.8f, 0.8f, 1.0f },
253 { 0.8f, 1.0f, 0.8f, 1.0f },
256 /* Configure the lighting. */
259 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
260 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
261 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
264 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
265 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
266 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
269 static const struct d_mtrl *game_draw_back(const struct d_mtrl *mq,
270 const struct game_draw *gd,
271 int pose, int d, float t)
273 if (pose == POSE_BALL)
278 const struct game_view *view = &gd->view;
282 const struct game_tilt *tilt = &gd->tilt;
284 glRotatef(tilt->rz * 2, tilt->z[0], tilt->z[1], tilt->z[2]);
285 glRotatef(tilt->rx * 2, tilt->x[0], tilt->x[1], tilt->x[2]);
288 glTranslatef(view->p[0], view->p[1] * d, view->p[2]);
290 if (config_get_d(CONFIG_BACKGROUND))
292 /* Draw all background layers back to front. */
294 mq = sol_back(&gd->back.draw, mq, BACK_DIST, FAR_DIST, t);
295 mq = back_draw(mq, 0);
296 mq = sol_back(&gd->back.draw, mq, 0, BACK_DIST, t);
298 else back_draw(mq, t);
305 static void game_clip_refl(int d)
307 /* Fudge to eliminate the floor from reflection. */
309 GLdouble e[4], k = -0.00001;
316 glClipPlane(GL_CLIP_PLANE0, e);
319 static void game_clip_ball(const struct game_draw *gd, int d, const float *p)
321 GLdouble r, c[3], pz[4], nz[4];
323 /* Compute the plane giving the front of the ball, as seen from view.p. */
329 pz[0] = gd->view.p[0] - c[0];
330 pz[1] = gd->view.p[1] - c[1];
331 pz[2] = gd->view.p[2] - c[2];
333 r = sqrt(pz[0] * pz[0] + pz[1] * pz[1] + pz[2] * pz[2]);
338 pz[3] = -(pz[0] * c[0] +
342 /* Find the plane giving the back of the ball, as seen from view.p. */
349 /* Reflect these planes as necessary, and store them in the GL state. */
354 glClipPlane(GL_CLIP_PLANE1, nz);
355 glClipPlane(GL_CLIP_PLANE2, pz);
358 static const struct d_mtrl *game_draw_fore(const struct d_mtrl *mq,
359 const struct game_draw *gd,
360 int pose, const float *M,
363 const float *ball_p = gd->vary.uv[0].p;
365 const struct s_draw *draw = &gd->draw;
369 /* Rotate the environment about the position of the ball. */
371 game_draw_tilt(gd, d);
373 /* Compute clipping planes for reflection and ball facing. */
376 game_clip_ball(gd, d, ball_p);
379 glEnable(GL_CLIP_PLANE0);
384 mq = sol_draw(draw, mq, 0, 1);
388 /* Draw the floor. */
390 mq = sol_draw(draw, mq, 0, 1);
392 /* Draw the coins. */
394 mq = game_draw_items(mq, draw->vary, M, t);
402 mq = game_draw_balls(mq, draw->vary, M, t);
407 /* Draw the billboards, entities, and particles. */
409 glEnable(GL_COLOR_MATERIAL);
410 glDisable(GL_LIGHTING);
411 glDepthMask(GL_FALSE);
413 mq = sol_bill(draw, mq, M, t);
415 mq = game_draw_goals(mq, gd, M, t);
416 mq = game_draw_jumps(mq, gd, M, t);
417 mq = game_draw_swchs(mq, draw->vary);
419 mq = part_draw_coin(mq);
421 glDepthMask(GL_TRUE);
422 glEnable(GL_LIGHTING);
423 glDisable(GL_COLOR_MATERIAL);
426 glDisable(GL_CLIP_PLANE0);
433 /*---------------------------------------------------------------------------*/
435 void game_draw(const struct game_draw *gd, int pose, float t)
437 float fov = (float) config_get_d(CONFIG_VIEW_FOV);
439 if (gd->jump_b) fov *= 2.f * fabsf(gd->jump_dt - 0.5);
443 const struct game_view *view = &gd->view;
444 const struct d_mtrl *mq = sol_draw_enable();
446 video_push_persp(fov, 0.1f, FAR_DIST);
449 float T[16], U[16], M[16], v[3];
451 /* Compute direct and reflected view bases. */
457 m_view(T, view->c, view->p, view->e[1]);
458 m_view(U, view->c, v, view->e[1]);
462 /* Apply the current view. */
464 v_sub(v, view->c, view->p);
466 glTranslatef(0.f, 0.f, -v_len(v));
468 glTranslatef(-view->c[0], -view->c[1], -view->c[2]);
470 if (gd->draw.reflective && config_get_d(CONFIG_REFLECTION))
472 glEnable(GL_STENCIL_TEST);
474 /* Draw the mirrors only into the stencil buffer. */
476 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
477 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
478 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
479 glDepthMask(GL_FALSE);
481 mq = game_refl_all(mq, gd);
483 glDepthMask(GL_TRUE);
484 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
485 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
486 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
488 /* Draw the scene reflected into color and depth buffers. */
493 glScalef(+1.0f, -1.0f, +1.0f);
497 mq = game_draw_back(mq, gd, pose, -1, t);
498 mq = game_draw_fore(mq, gd, pose, U, -1, t);
503 glDisable(GL_STENCIL_TEST);
506 /* Draw the scene normally. */
510 if (gd->draw.reflective)
512 if (config_get_d(CONFIG_REFLECTION))
514 /* Draw background while preserving reflections. */
516 glEnable(GL_STENCIL_TEST);
518 glStencilFunc(GL_NOTEQUAL, 1, 0xFFFFFFFF);
519 mq = game_draw_back(mq, gd, pose, +1, t);
521 glDisable(GL_STENCIL_TEST);
525 mq = game_refl_all(mq, gd);
529 /* Draw background. */
531 mq = game_draw_back(mq, gd, pose, +1, t);
534 * Draw mirrors, first fully opaque with a custom
535 * material color, then blending normally with the
536 * opaque surfaces using their original material
537 * properties. (Keeps background from showing
541 glEnable(GL_COLOR_MATERIAL);
543 glColor4f(0.0, 0.0, 0.05, 1.0);
544 mq = game_refl_all(mq, gd);
545 glColor4f(1.0, 1.0, 1.0, 1.0);
547 glDisable(GL_COLOR_MATERIAL);
549 mq = game_refl_all(mq, gd);
554 mq = game_draw_back(mq, gd, pose, +1, t);
555 mq = game_refl_all(mq, gd);
558 mq = game_draw_fore(mq, gd, pose, T, +1, t);
563 sol_draw_disable(mq);
565 /* Draw the fade overlay. */
567 sol_fade(&gd->draw, gd->fade_k);
571 /*---------------------------------------------------------------------------*/
576 void game_lerp_init(struct game_lerp *gl, struct game_draw *gd)
580 sol_load_lerp(&gl->lerp, &gd->vary);
582 gl->tilt[PREV] = gl->tilt[CURR] = gd->tilt;
583 gl->view[PREV] = gl->view[CURR] = gd->view;
585 gl->goal_k[PREV] = gl->goal_k[CURR] = gd->goal_k;
586 gl->jump_dt[PREV] = gl->jump_dt[CURR] = gd->jump_dt;
589 void game_lerp_free(struct game_lerp *gl)
591 sol_free_lerp(&gl->lerp);
594 void game_lerp_copy(struct game_lerp *gl)
596 sol_lerp_copy(&gl->lerp);
598 gl->tilt[PREV] = gl->tilt[CURR];
599 gl->view[PREV] = gl->view[CURR];
601 gl->goal_k[PREV] = gl->goal_k[CURR];
602 gl->jump_dt[PREV] = gl->jump_dt[CURR];
605 void game_lerp_apply(struct game_lerp *gl, struct game_draw *gd)
611 sol_lerp_apply(&gl->lerp, a);
619 v_lerp(gd->tilt.x, gl->tilt[PREV].x, gl->tilt[CURR].x, a);
620 v_lerp(gd->tilt.z, gl->tilt[PREV].z, gl->tilt[CURR].z, a);
622 gd->tilt.rx = (gl->tilt[PREV].rx * (1.0f - a) + gl->tilt[CURR].rx * a);
623 gd->tilt.rz = (gl->tilt[PREV].rz * (1.0f - a) + gl->tilt[CURR].rz * a);
627 v_lerp(gd->view.c, gl->view[PREV].c, gl->view[CURR].c, a);
628 v_lerp(gd->view.p, gl->view[PREV].p, gl->view[CURR].p, a);
629 e_lerp(gd->view.e, gl->view[PREV].e, gl->view[CURR].e, a);
633 gd->goal_k = (gl->goal_k[PREV] * (1.0f - a) + gl->goal_k[CURR] * a);
634 gd->jump_dt = (gl->jump_dt[PREV] * (1.0f - a) + gl->jump_dt[CURR] * a);
637 /*---------------------------------------------------------------------------*/