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,
51 glColor4f(c[0], c[1], c[2], c[3]);
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 glEnable(GL_COLOR_MATERIAL);
67 for (hi = 0; hi < vary->hc; hi++)
68 if (vary->hv[hi].t == ITEM_COIN && vary->hv[hi].n > 0)
72 glTranslatef(vary->hv[hi].p[0],
75 mq = item_draw(mq, &vary->hv[hi], bill_M, t);
80 for (hi = 0; hi < vary->hc; hi++)
81 if (vary->hv[hi].t == ITEM_SHRINK)
85 glTranslatef(vary->hv[hi].p[0],
88 mq = item_draw(mq, &vary->hv[hi], bill_M, t);
93 for (hi = 0; hi < vary->hc; hi++)
94 if (vary->hv[hi].t == ITEM_GROW)
98 glTranslatef(vary->hv[hi].p[0],
101 mq = item_draw(mq, &vary->hv[hi], bill_M, t);
107 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
108 glDisable(GL_COLOR_MATERIAL);
113 static const struct d_mtrl *game_draw_goals(const struct d_mtrl *mq,
114 const struct game_draw *gd,
115 const float *M, float t)
117 const struct s_base *base = gd->vary.base;
123 /* Draw the goal column. */
125 for (zi = 0; zi < base->zc; zi++)
129 glTranslatef(base->zv[zi].p[0],
133 glScalef(base->zv[zi].r,
137 mq = goal_draw(mq, t);
145 static const struct d_mtrl *game_draw_jumps(const struct d_mtrl *mq,
146 const struct game_draw *gd,
147 const float *M, float t)
149 const struct s_base *base = gd->vary.base;
153 for (ji = 0; ji < base->jc; ji++)
157 glTranslatef(base->jv[ji].p[0],
160 glScalef(base->jv[ji].r,
164 mq = jump_draw(mq, t, !gd->jump_e);
171 static const struct d_mtrl *game_draw_swchs(const struct d_mtrl *mq,
172 const struct s_vary *vary)
176 for (xi = 0; xi < vary->xc; xi++)
178 struct v_swch *xp = vary->xv + xi;
185 glTranslatef(xp->base->p[0],
188 glScalef(xp->base->r,
192 mq = swch_draw(mq, xp->f, xp->e);
199 /*---------------------------------------------------------------------------*/
201 static void game_draw_tilt(const struct game_draw *gd, int d)
203 const struct game_tilt *tilt = &gd->tilt;
204 const float *ball_p = gd->vary.uv[0].p;
206 /* Rotate the environment about the position of the ball. */
208 glTranslatef(+ball_p[0], +ball_p[1] * d, +ball_p[2]);
209 glRotatef(-tilt->rz * d, tilt->z[0], tilt->z[1], tilt->z[2]);
210 glRotatef(-tilt->rx * d, tilt->x[0], tilt->x[1], tilt->x[2]);
211 glTranslatef(-ball_p[0], -ball_p[1] * d, -ball_p[2]);
214 static const struct d_mtrl *game_refl_all(const struct d_mtrl *mq,
215 const struct game_draw *gd)
219 game_draw_tilt(gd, 1);
221 /* Draw the floor. */
223 mq = sol_refl(&gd->draw, mq);
230 /*---------------------------------------------------------------------------*/
232 static void game_draw_light(void)
234 const float light_p[2][4] = {
235 { -8.0f, +32.0f, -8.0f, 0.0f },
236 { +8.0f, +32.0f, +8.0f, 0.0f },
238 const float light_c[2][4] = {
239 { 1.0f, 0.8f, 0.8f, 1.0f },
240 { 0.8f, 1.0f, 0.8f, 1.0f },
243 /* Configure the lighting. */
246 glLightfv(GL_LIGHT0, GL_POSITION, light_p[0]);
247 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_c[0]);
248 glLightfv(GL_LIGHT0, GL_SPECULAR, light_c[0]);
251 glLightfv(GL_LIGHT1, GL_POSITION, light_p[1]);
252 glLightfv(GL_LIGHT1, GL_DIFFUSE, light_c[1]);
253 glLightfv(GL_LIGHT1, GL_SPECULAR, light_c[1]);
256 static const struct d_mtrl *game_draw_back(const struct d_mtrl *mq,
257 const struct game_draw *gd,
258 int pose, int d, float t)
260 if (pose == POSE_BALL)
265 const struct game_view *view = &gd->view;
269 const struct game_tilt *tilt = &gd->tilt;
271 glRotatef(tilt->rz * 2, tilt->z[0], tilt->z[1], tilt->z[2]);
272 glRotatef(tilt->rx * 2, tilt->x[0], tilt->x[1], tilt->x[2]);
275 glTranslatef(view->p[0], view->p[1] * d, view->p[2]);
277 if (config_get_d(CONFIG_BACKGROUND))
279 mq = back_draw(mq, 0);
280 mq = sol_back(&gd->back.draw, mq, 0, FAR_DIST, t);
282 else back_draw(mq, 0);
289 static void game_clip_refl(int d)
291 /* Fudge to eliminate the floor from reflection. */
293 glClipPlane4f(GL_CLIP_PLANE0, 0, 1, 0, -0.00001);
296 static void game_clip_ball(const struct game_draw *gd, int d, const float *p)
298 GLfloat r, c[3], pz[4], nz[4];
300 /* Compute the plane giving the front of the ball, as seen from view.p. */
306 pz[0] = gd->view.p[0] - c[0];
307 pz[1] = gd->view.p[1] - c[1];
308 pz[2] = gd->view.p[2] - c[2];
310 r = sqrt(pz[0] * pz[0] + pz[1] * pz[1] + pz[2] * pz[2]);
315 pz[3] = -(pz[0] * c[0] +
319 /* Find the plane giving the back of the ball, as seen from view.p. */
326 /* Reflect these planes as necessary, and store them in the GL state. */
331 glClipPlane4f(GL_CLIP_PLANE1, nz[0], nz[1], nz[2], nz[3]);
332 glClipPlane4f(GL_CLIP_PLANE2, pz[0], pz[1], pz[2], pz[3]);
335 static const struct d_mtrl *game_draw_fore(const struct d_mtrl *mq,
336 const struct game_draw *gd,
337 int pose, const float *M,
340 const float *ball_p = gd->vary.uv[0].p;
342 const struct s_draw *draw = &gd->draw;
346 /* Rotate the environment about the position of the ball. */
348 game_draw_tilt(gd, d);
350 /* Compute clipping planes for reflection and ball facing. */
353 game_clip_ball(gd, d, ball_p);
356 glEnable(GL_CLIP_PLANE0);
361 mq = sol_draw(draw, mq, 0, 1);
365 /* Draw the floor. */
367 mq = sol_draw(draw, mq, 0, 1);
369 /* Draw the coins. */
371 mq = game_draw_items(mq, draw->vary, M, t);
379 mq = game_draw_balls(mq, draw->vary, M, t);
384 /* Draw the billboards, entities, and particles. */
386 glEnable(GL_COLOR_MATERIAL);
387 glDisable(GL_LIGHTING);
388 glDepthMask(GL_FALSE);
390 mq = sol_bill(draw, mq, M, t);
392 mq = game_draw_goals(mq, gd, M, t);
393 mq = game_draw_jumps(mq, gd, M, t);
394 mq = game_draw_swchs(mq, draw->vary);
396 mq = part_draw_coin(mq);
398 glDepthMask(GL_TRUE);
399 glEnable(GL_LIGHTING);
400 glDisable(GL_COLOR_MATERIAL);
403 glDisable(GL_CLIP_PLANE0);
410 /*---------------------------------------------------------------------------*/
412 void game_draw(const struct game_draw *gd, int pose, float t)
414 float fov = (float) config_get_d(CONFIG_VIEW_FOV);
416 if (gd->jump_b) fov *= 2.f * fabsf(gd->jump_dt - 0.5);
420 const struct game_view *view = &gd->view;
421 const struct d_mtrl *mq = sol_draw_enable();
423 video_push_persp(fov, 0.1f, FAR_DIST);
426 float T[16], U[16], M[16], v[3];
428 /* Compute direct and reflected view bases. */
434 m_view(T, view->c, view->p, view->e[1]);
435 m_view(U, view->c, v, view->e[1]);
439 /* Apply the current view. */
441 v_sub(v, view->c, view->p);
443 glTranslatef(0.f, 0.f, -v_len(v));
445 glTranslatef(-view->c[0], -view->c[1], -view->c[2]);
447 /* Draw the background. */
449 mq = game_draw_back(mq, gd, pose, +1, t);
451 /* Draw the reflection. */
453 if (gd->draw.reflective && config_get_d(CONFIG_REFLECTION))
455 glEnable(GL_STENCIL_TEST);
457 /* Draw the mirrors only into the stencil buffer. */
459 glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
460 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
461 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
462 glDepthMask(GL_FALSE);
464 mq = game_refl_all(mq, gd);
466 glDepthMask(GL_TRUE);
467 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
468 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
469 glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
471 /* Draw the scene reflected into color and depth buffers. */
476 glScalef(+1.0f, -1.0f, +1.0f);
480 mq = game_draw_back(mq, gd, pose, -1, t);
481 mq = game_draw_fore(mq, gd, pose, U, -1, t);
486 glStencilFunc(GL_ALWAYS, 0, 0xFFFFFFF);
488 glDisable(GL_STENCIL_TEST);
491 /* Ready the lights for foreground rendering. */
495 /* When reflection is disabled, mirrors must be rendered opaque */
496 /* to prevent the background from showing. */
498 if (gd->draw.reflective && !config_get_d(CONFIG_REFLECTION))
500 glEnable(GL_COLOR_MATERIAL);
502 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
503 mq = game_refl_all(mq, gd);
504 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
506 glDisable(GL_COLOR_MATERIAL);
509 /* Draw the mirrors and the rest of the foreground. */
511 mq = game_refl_all (mq, gd);
512 mq = game_draw_fore(mq, gd, pose, T, +1, t);
517 /* Draw the fade overlay. */
519 sol_fade(&gd->draw, gd->fade_k);
520 sol_draw_disable(mq);
524 /*---------------------------------------------------------------------------*/
529 void game_lerp_init(struct game_lerp *gl, struct game_draw *gd)
533 sol_load_lerp(&gl->lerp, &gd->vary);
535 gl->tilt[PREV] = gl->tilt[CURR] = gd->tilt;
536 gl->view[PREV] = gl->view[CURR] = gd->view;
538 gl->goal_k[PREV] = gl->goal_k[CURR] = gd->goal_k;
539 gl->jump_dt[PREV] = gl->jump_dt[CURR] = gd->jump_dt;
542 void game_lerp_free(struct game_lerp *gl)
544 sol_free_lerp(&gl->lerp);
547 void game_lerp_copy(struct game_lerp *gl)
549 sol_lerp_copy(&gl->lerp);
551 gl->tilt[PREV] = gl->tilt[CURR];
552 gl->view[PREV] = gl->view[CURR];
554 gl->goal_k[PREV] = gl->goal_k[CURR];
555 gl->jump_dt[PREV] = gl->jump_dt[CURR];
558 void game_lerp_apply(struct game_lerp *gl, struct game_draw *gd)
564 sol_lerp_apply(&gl->lerp, a);
572 v_lerp(gd->tilt.x, gl->tilt[PREV].x, gl->tilt[CURR].x, a);
573 v_lerp(gd->tilt.z, gl->tilt[PREV].z, gl->tilt[CURR].z, a);
575 gd->tilt.rx = (gl->tilt[PREV].rx * (1.0f - a) + gl->tilt[CURR].rx * a);
576 gd->tilt.rz = (gl->tilt[PREV].rz * (1.0f - a) + gl->tilt[CURR].rz * a);
580 v_lerp(gd->view.c, gl->view[PREV].c, gl->view[CURR].c, a);
581 v_lerp(gd->view.p, gl->view[PREV].p, gl->view[CURR].p, a);
582 e_lerp(gd->view.e, gl->view[PREV].e, gl->view[CURR].e, a);
586 gd->goal_k = (gl->goal_k[PREV] * (1.0f - a) + gl->goal_k[CURR] * a);
587 gd->jump_dt = (gl->jump_dt[PREV] * (1.0f - a) + gl->jump_dt[CURR] * a);
590 /*---------------------------------------------------------------------------*/