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.
16 #include <SDL_rwops.h>
26 #include "base_image.h"
28 #include "base_config.h"
30 /*---------------------------------------------------------------------------*/
32 static int sol_enum_mtrl(const struct s_file *fp,
33 const struct s_body *bp, int mi)
37 /* Count all lump geoms with this material. */
39 for (li = 0; li < bp->lc; li++)
41 int g0 = fp->lv[bp->l0 + li].g0;
42 int gc = fp->lv[bp->l0 + li].gc;
44 for (gi = 0; gi < gc; gi++)
45 if (fp->gv[fp->iv[g0 + gi]].mi == mi)
49 /* Count all body geoms with this material. */
51 for (gi = 0; gi < bp->gc; gi++)
52 if (fp->gv[fp->iv[bp->g0 + gi]].mi == mi)
58 static int sol_enum_body(const struct s_file *fp,
59 const struct s_body *bp, int fl)
63 /* Count all geoms with this flag. */
65 for (mi = 0; mi < fp->mc; mi++)
66 if (fp->mv[mi].fl & fl)
67 c = c + sol_enum_mtrl(fp, bp, mi);
72 /*---------------------------------------------------------------------------*/
74 #define color_cmp(a, b) ((a)[0] == (b)[0] && \
79 static struct s_mtrl default_mtrl =
81 { 0.2f, 0.2f, 0.2f, 1.0f },
82 { 0.8f, 0.8f, 0.8f, 1.0f },
83 { 0.0f, 0.0f, 0.0f, 1.0f },
84 { 0.0f, 0.0f, 0.0f, 1.0f },
85 { 0.0f, }, 0.0f, M_OPAQUE, 0, ""
88 static const struct s_mtrl *sol_draw_mtrl(const struct s_file *fp,
89 const struct s_mtrl *mp,
90 const struct s_mtrl *mq)
92 /* Change material properties only as needed. */
94 if (!color_cmp(mp->a, mq->a))
95 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mp->a);
96 if (!color_cmp(mp->d, mq->d))
97 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mp->d);
98 if (!color_cmp(mp->s, mq->s))
99 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mp->s);
100 if (!color_cmp(mp->e, mq->e))
101 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mp->e);
102 if (mp->h[0] != mq->h[0])
103 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mp->h);
105 /* Bind the texture. */
108 glBindTexture(GL_TEXTURE_2D, mp->o);
110 /* Enable environment mapping. */
112 if ((mp->fl & M_ENVIRONMENT) && !(mq->fl & M_ENVIRONMENT))
114 glEnable(GL_TEXTURE_GEN_S);
115 glEnable(GL_TEXTURE_GEN_T);
117 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
118 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
121 /* Disable environment mapping. */
123 if ((mq->fl & M_ENVIRONMENT) && !(mp->fl & M_ENVIRONMENT))
125 glDisable(GL_TEXTURE_GEN_S);
126 glDisable(GL_TEXTURE_GEN_T);
128 glBindTexture(GL_TEXTURE_2D, mp->o);
131 /* Enable additive blending. */
133 if ((mp->fl & M_ADDITIVE) && !(mq->fl & M_ADDITIVE))
134 glBlendFunc(GL_ONE, GL_ONE);
136 /* Enable standard blending. */
138 if ((mq->fl & M_ADDITIVE) && !(mp->fl & M_ADDITIVE))
139 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
141 /* Enable decal offset. */
143 if ((mp->fl & M_DECAL) && !(mq->fl & M_DECAL))
145 glEnable(GL_POLYGON_OFFSET_FILL);
146 glPolygonOffset(-1.0f, -2.0f);
149 /* Disable decal offset. */
151 if ((mq->fl & M_DECAL) && !(mp->fl & M_DECAL))
152 glDisable(GL_POLYGON_OFFSET_FILL);
157 static const struct s_mtrl *sol_draw_bill(const struct s_file *fp,
158 const struct s_bill *rp,
159 const struct s_mtrl *mp, float t)
161 float T = fmodf(t, rp->t) - rp->t / 2;
163 float w = rp->w[0] + rp->w[1] * T + rp->w[2] * T * T;
164 float h = rp->h[0] + rp->h[1] * T + rp->h[2] * T * T;
168 float rx = rp->rx[0] + rp->rx[1] * T + rp->rx[2] * T * T;
169 float ry = rp->ry[0] + rp->ry[1] * T + rp->ry[2] * T * T;
170 float rz = rp->rz[0] + rp->rz[1] * T + rp->rz[2] * T * T;
174 float y0 = (rp->fl & B_EDGE) ? 0 : -h / 2;
175 float y1 = (rp->fl & B_EDGE) ? h : +h / 2;
177 glRotatef(ry, 0.0f, 1.0f, 0.0f);
178 glRotatef(rx, 1.0f, 0.0f, 0.0f);
179 glTranslatef(0.0f, 0.0f, -rp->d);
183 glRotatef(-rx - 90.0f, 1.0f, 0.0f, 0.0f);
184 glRotatef(-ry, 0.0f, 0.0f, 1.0f);
187 glRotatef(-rx, 1.0f, 0.0f, 0.0f);
189 glRotatef(rz, 0.0f, 0.0f, 1.0f);
191 mp = sol_draw_mtrl(fp, fp->mv + rp->mi, mp);
195 glTexCoord2f(0.0f, 1.0f); glVertex2f(-w / 2, y0);
196 glTexCoord2f(1.0f, 1.0f); glVertex2f(+w / 2, y0);
197 glTexCoord2f(1.0f, 0.0f); glVertex2f(+w / 2, y1);
198 glTexCoord2f(0.0f, 0.0f); glVertex2f(-w / 2, y1);
208 void sol_back(const struct s_file *fp, float n, float f, float t)
210 const struct s_mtrl *mp = &default_mtrl;
214 /* Render all billboards in the given range. */
216 glDisable(GL_LIGHTING);
217 glDepthMask(GL_FALSE);
219 for (ri = 0; ri < fp->rc; ri++)
220 if (n <= fp->rv[ri].d && fp->rv[ri].d < f)
221 mp = sol_draw_bill(fp, fp->rv + ri, mp, t);
223 mp = sol_draw_mtrl(fp, &default_mtrl, mp);
225 glDepthMask(GL_TRUE);
226 glEnable(GL_LIGHTING);
229 /*---------------------------------------------------------------------------*/
231 * The following code renders a body in a ludicrously inefficient
232 * manner. It iterates the materials and scans the data structure for
233 * geometry using each. This has the effect of absolutely minimizing
234 * material changes, texture bindings, and Begin/End pairs, but
235 * maximizing trips through the data.
237 * However, this is only done once for each level. The results are
238 * stored in display lists. Thus, it is well worth it.
241 static void sol_draw_geom(const struct s_file *fp,
242 const struct s_geom *gp, int mi)
246 const float *ui = fp->tv[gp->ti].u;
247 const float *uj = fp->tv[gp->tj].u;
248 const float *uk = fp->tv[gp->tk].u;
250 const float *ni = fp->sv[gp->si].n;
251 const float *nj = fp->sv[gp->sj].n;
252 const float *nk = fp->sv[gp->sk].n;
254 const float *vi = fp->vv[gp->vi].p;
255 const float *vj = fp->vv[gp->vj].p;
256 const float *vk = fp->vv[gp->vk].p;
272 static void sol_draw_lump(const struct s_file *fp,
273 const struct s_lump *lp, int mi)
277 for (i = 0; i < lp->gc; i++)
278 sol_draw_geom(fp, fp->gv + fp->iv[lp->g0 + i], mi);
281 static const struct s_mtrl *sol_draw_body(const struct s_file *fp,
282 const struct s_body *bp,
283 const struct s_mtrl *mp,
288 /* Iterate all materials of the correct opacity. */
290 for (mi = 0; mi < fp->mc; mi++)
291 if ((fp->mv[mi].fl & fl) && (fp->mv[mi].fl & M_DECAL) == decal)
293 if (sol_enum_mtrl(fp, bp, mi))
295 /* Set the material state. */
297 mp = sol_draw_mtrl(fp, fp->mv + mi, mp);
299 /* Render all geometry of that material. */
301 glBegin(GL_TRIANGLES);
303 for (li = 0; li < bp->lc; li++)
304 sol_draw_lump(fp, fp->lv + bp->l0 + li, mi);
305 for (gi = 0; gi < bp->gc; gi++)
306 sol_draw_geom(fp, fp->gv + fp->iv[bp->g0 + gi], mi);
315 static void sol_draw_list(const struct s_file *fp,
316 const struct s_body *bp, GLuint list)
320 sol_body_p(p, fp, bp);
324 /* Translate a moving body. */
326 glTranslatef(p[0], p[1], p[2]);
335 void sol_draw(const struct s_file *fp)
339 /* Render all opaque geometry into the color and depth buffers. */
341 for (bi = 0; bi < fp->bc; bi++)
343 sol_draw_list(fp, fp->bv + bi, fp->bv[bi].ol);
345 /* Render all translucent geometry into only the color buffer. */
347 glDepthMask(GL_FALSE);
349 for (bi = 0; bi < fp->bc; bi++)
351 sol_draw_list(fp, fp->bv + bi, fp->bv[bi].tl);
353 glDepthMask(GL_TRUE);
356 void sol_refl(const struct s_file *fp)
360 /* Render all reflective geometry into the color and depth buffers. */
362 for (bi = 0; bi < fp->bc; bi++)
364 sol_draw_list(fp, fp->bv + bi, fp->bv[bi].rl);
367 /*---------------------------------------------------------------------------*/
369 static void sol_shad_geom(const struct s_file *fp,
370 const struct s_geom *gp, int mi)
374 const float *vi = fp->vv[gp->vi].p;
375 const float *vj = fp->vv[gp->vj].p;
376 const float *vk = fp->vv[gp->vk].p;
378 glTexCoord2f(vi[0], vi[2]);
381 glTexCoord2f(vj[0], vj[2]);
384 glTexCoord2f(vk[0], vk[2]);
389 static void sol_shad_lump(const struct s_file *fp,
390 const struct s_lump *lp, int mi)
394 for (i = 0; i < lp->gc; i++)
395 sol_shad_geom(fp, fp->gv + fp->iv[lp->g0 + i], mi);
398 static void sol_shad_body(const struct s_file *fp,
399 const struct s_body *bp,
406 glEnable(GL_POLYGON_OFFSET_FILL);
407 glPolygonOffset(-1.0f, -2.0f);
410 glBegin(GL_TRIANGLES);
412 for (mi = 0; mi < fp->mc; mi++)
413 if ((fp->mv[mi].fl & fl) && (fp->mv[mi].fl & M_DECAL) == decal)
415 for (li = 0; li < bp->lc; li++)
416 sol_shad_lump(fp, fp->lv + bp->l0 + li, mi);
417 for (gi = 0; gi < bp->gc; gi++)
418 sol_shad_geom(fp, fp->gv + fp->iv[bp->g0 + gi], mi);
424 glDisable(GL_POLYGON_OFFSET_FILL);
427 static void sol_shad_list(const struct s_file *fp,
428 const struct s_body *bp, GLuint list)
432 sol_body_p(p, fp, bp);
436 /* Translate a moving body. */
438 glTranslatef(p[0], p[1], p[2]);
440 /* Translate the shadow on a moving body. */
442 glMatrixMode(GL_TEXTURE);
445 glTranslatef(p[0], p[2], 0.0f);
447 glMatrixMode(GL_MODELVIEW);
453 /* Pop the shadow translation. */
455 glMatrixMode(GL_TEXTURE);
459 glMatrixMode(GL_MODELVIEW);
464 void sol_shad(const struct s_file *fp)
468 /* Render all shadowed geometry. */
470 glDepthMask(GL_FALSE);
472 for (bi = 0; bi < fp->bc; bi++)
474 sol_shad_list(fp, fp->bv + bi, fp->bv[bi].sl);
476 glDepthMask(GL_TRUE);
479 /*---------------------------------------------------------------------------*/
481 static void sol_load_objects(struct s_file *fp, int s)
485 /* Here we sort geometry into display lists by material type. */
487 for (i = 0; i < fp->bc; i++)
489 struct s_body *bp = fp->bv + i;
491 int on = sol_enum_body(fp, bp, M_OPAQUE);
492 int tn = sol_enum_body(fp, bp, M_TRANSPARENT);
493 int rn = sol_enum_body(fp, bp, M_REFLECTIVE);
494 int dn = sol_enum_body(fp, bp, M_DECAL);
496 /* Draw all opaque geometry, decals last. */
500 fp->bv[i].ol = glGenLists(1);
502 glNewList(fp->bv[i].ol, GL_COMPILE);
504 const struct s_mtrl *mp = &default_mtrl;
506 mp = sol_draw_body(fp, fp->bv + i, mp, M_OPAQUE, 0);
507 mp = sol_draw_body(fp, fp->bv + i, mp, M_OPAQUE, M_DECAL);
508 mp = sol_draw_mtrl(fp, &default_mtrl, mp);
512 else fp->bv[i].ol = 0;
514 /* Draw all translucent geometry, decals first. */
518 fp->bv[i].tl = glGenLists(1);
520 glNewList(fp->bv[i].tl, GL_COMPILE);
522 const struct s_mtrl *mp = &default_mtrl;
524 mp = sol_draw_body(fp, fp->bv + i, mp, M_TRANSPARENT, M_DECAL);
525 mp = sol_draw_body(fp, fp->bv + i, mp, M_TRANSPARENT, 0);
526 mp = sol_draw_mtrl(fp, &default_mtrl, mp);
530 else fp->bv[i].tl = 0;
532 /* Draw all reflective geometry. */
536 fp->bv[i].rl = glGenLists(1);
538 glNewList(fp->bv[i].rl, GL_COMPILE);
540 const struct s_mtrl *mp = &default_mtrl;
542 mp = sol_draw_body(fp, fp->bv + i, mp, M_REFLECTIVE, 0);
543 mp = sol_draw_mtrl(fp, &default_mtrl, mp);
547 else fp->bv[i].rl = 0;
549 /* Draw all shadowed geometry. */
553 fp->bv[i].sl = glGenLists(1);
555 glNewList(fp->bv[i].sl, GL_COMPILE);
557 if (on) sol_shad_body(fp, fp->bv + i, M_OPAQUE, 0);
558 if (rn) sol_shad_body(fp, fp->bv + i, M_REFLECTIVE, 0);
559 if (dn) sol_shad_body(fp, fp->bv + i, M_OPAQUE, M_DECAL);
563 else fp->bv[i].sl = 0;
567 static GLuint sol_find_texture(const char *name)
574 /* Prefer a lossless copy of the texture over a lossy compression. */
576 strncpy(png, name, PATHMAX); strcat(png, ".png");
577 strncpy(jpg, name, PATHMAX); strcat(jpg, ".jpg");
579 /* Check for a PNG. */
581 if ((o = make_image_from_file(png)))
584 /* Check for a JPG. */
586 if ((o = make_image_from_file(jpg)))
592 static void sol_load_textures(struct s_file *fp, int k)
596 /* Load the image referenced by each material. */
598 for (i = 0; i < fp->mc; i++)
599 if ((fp->mv[i].o = sol_find_texture(fp->mv[i].f)))
601 /* Set the texture to clamp or repeat based on material type. */
603 if (fp->mv[i].fl & M_CLAMPED)
605 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
606 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
610 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
611 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
616 /*---------------------------------------------------------------------------*/
618 int sol_load_gl(struct s_file *fp, const char *filename, int k, int s)
620 if (sol_load_only_file(fp, filename))
622 sol_load_textures(fp, k);
623 sol_load_objects (fp, s);
629 /*---------------------------------------------------------------------------*/
631 void sol_free_gl(struct s_file *fp)
635 for (i = 0; i < fp->mc; i++)
637 if (glIsTexture(fp->mv[i].o))
638 glDeleteTextures(1, &fp->mv[i].o);
641 for (i = 0; i < fp->bc; i++)
643 if (glIsList(fp->bv[i].ol))
644 glDeleteLists(fp->bv[i].ol, 1);
645 if (glIsList(fp->bv[i].tl))
646 glDeleteLists(fp->bv[i].tl, 1);
647 if (glIsList(fp->bv[i].rl))
648 glDeleteLists(fp->bv[i].rl, 1);
654 /*---------------------------------------------------------------------------*/