2 Deep Runner - 6dof shooter game for the SGI O2.
3 Copyright (C) 2023 John Tsiombikas <nuclear@mutantstargoat.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #if defined(WIN32) || defined(__WIN32)
26 #include <OpenGL/gl.h>
27 #include <OpenGL/glu.h>
34 static const float *vertex_ptr, *normal_ptr, *texcoord_ptr, *color_ptr;
35 static int vertex_nelem, texcoord_nelem, color_nelem;
36 static int vertex_stride, normal_stride, texcoord_stride, color_stride;
38 static char *glextstr;
39 static int have_edgeclamp = -1;
42 void gaw_viewport(int x, int y, int w, int h)
44 glViewport(x, y, w, h);
47 void gaw_matrix_mode(int mode)
53 void gaw_load_identity(void)
58 void gaw_load_matrix(const float *m)
63 void gaw_mult_matrix(const float *m)
68 void gaw_push_matrix(void)
73 void gaw_pop_matrix(void)
78 void gaw_get_modelview(float *m)
80 glGetFloatv(GL_MODELVIEW_MATRIX, m);
83 void gaw_get_projection(float *m)
85 glGetFloatv(GL_PROJECTION_MATRIX, m);
88 void gaw_translate(float x, float y, float z)
90 glTranslatef(x, y, z);
93 void gaw_rotate(float angle, float x, float y, float z)
95 glRotatef(angle, x, y, z);
98 void gaw_scale(float sx, float sy, float sz)
100 glScalef(sx, sy, sz);
103 void gaw_ortho(float l, float r, float b, float t, float n, float f)
105 glOrtho(l, r, b, t, n, f);
108 void gaw_frustum(float l, float r, float b, float t, float n, float f)
110 glFrustum(l, r, b, t, n, f);
113 void gaw_perspective(float vfov, float aspect, float znear, float zfar)
115 gluPerspective(vfov, aspect, znear, zfar);
120 glPushAttrib(GL_ENABLE_BIT);
123 void gaw_restore(void)
128 void gaw_enable(int st)
132 glEnable(GL_CULL_FACE);
135 glEnable(GL_DEPTH_TEST);
138 glEnable(GL_ALPHA_TEST);
150 glEnable(GL_LIGHTING);
165 glEnable(GL_TEXTURE_1D);
168 glEnable(GL_TEXTURE_2D);
175 void gaw_disable(int st)
179 glDisable(GL_CULL_FACE);
182 glDisable(GL_DEPTH_TEST);
185 glDisable(GL_ALPHA_TEST);
194 glDisable(GL_DITHER);
197 glDisable(GL_LIGHTING);
200 glDisable(GL_LIGHT0);
203 glDisable(GL_LIGHT1);
206 glDisable(GL_LIGHT2);
209 glDisable(GL_LIGHT3);
212 glDisable(GL_TEXTURE_1D);
215 glDisable(GL_TEXTURE_2D);
222 void gaw_depth_func(int func)
224 glDepthFunc(func + GL_NEVER);
227 void gaw_blend_func(int src, int dest)
229 static const int glbf[] = {GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA};
230 glBlendFunc(glbf[src], glbf[dest]);
233 void gaw_alpha_func(int func, float ref)
235 glAlphaFunc(func + GL_NEVER, ref);
238 void gaw_clear_color(float r, float g, float b, float a)
240 glClearColor(r, g, b, a);
243 void gaw_clear(unsigned int flags)
245 unsigned int glflags = 0;
246 if(flags & GAW_COLORBUF) {
247 glflags |= GL_COLOR_BUFFER_BIT;
249 if(flags & GAW_DEPTHBUF) {
250 glflags |= GL_DEPTH_BUFFER_BIT;
252 if(flags & GAW_STENCILBUF) {
253 glflags |= GL_STENCIL_BUFFER_BIT;
258 void gaw_depth_mask(int mask)
263 void gaw_vertex_array(int nelem, int stride, const void *ptr)
265 vertex_nelem = nelem;
266 vertex_stride = stride;
270 void gaw_normal_array(int stride, const void *ptr)
272 normal_stride = stride;
276 void gaw_texcoord_array(int nelem, int stride, const void *ptr)
278 texcoord_nelem = nelem;
279 texcoord_stride = stride;
283 void gaw_color_array(int nelem, int stride, const void *ptr)
286 color_stride = stride;
290 static int glprim[] = {GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS, GL_QUAD_STRIP};
292 void gaw_draw(int prim, int nverts)
294 glEnableClientState(GL_VERTEX_ARRAY);
295 glVertexPointer(vertex_nelem, GL_FLOAT, vertex_stride, vertex_ptr);
297 glEnableClientState(GL_NORMAL_ARRAY);
298 glNormalPointer(GL_FLOAT, normal_stride, normal_ptr);
301 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
302 glTexCoordPointer(texcoord_nelem, GL_FLOAT, texcoord_stride, texcoord_ptr);
305 glEnableClientState(GL_COLOR_ARRAY);
306 glTexCoordPointer(color_nelem, GL_FLOAT, color_stride, color_ptr);
309 glDrawArrays(glprim[prim], 0, nverts);
311 glDisableClientState(GL_VERTEX_ARRAY);
312 glDisableClientState(GL_NORMAL_ARRAY);
313 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
314 glDisableClientState(GL_COLOR_ARRAY);
317 void gaw_draw_indexed(int prim, const unsigned int *idxarr, int nidx)
319 glEnableClientState(GL_VERTEX_ARRAY);
320 glVertexPointer(3, GL_FLOAT, 0, vertex_ptr);
322 glEnableClientState(GL_NORMAL_ARRAY);
323 glNormalPointer(GL_FLOAT, 0, normal_ptr);
326 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
327 glTexCoordPointer(2, GL_FLOAT, 0, texcoord_ptr);
330 glEnableClientState(GL_COLOR_ARRAY);
331 glTexCoordPointer(color_nelem, GL_FLOAT, color_stride, color_ptr);
334 glDrawElements(glprim[prim], nidx, GL_UNSIGNED_INT, idxarr);
336 glDisableClientState(GL_VERTEX_ARRAY);
337 glDisableClientState(GL_NORMAL_ARRAY);
338 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
339 glDisableClientState(GL_COLOR_ARRAY);
342 void gaw_begin(int prim)
344 glBegin(glprim[prim]);
352 void gaw_color3f(float r, float g, float b)
357 void gaw_color4f(float r, float g, float b, float a)
359 glColor4f(r, g, b, a);
362 void gaw_color3ub(int r, int g, int b)
367 void gaw_normal(float x, float y, float z)
372 void gaw_texcoord1f(float u)
377 void gaw_texcoord2f(float u, float v)
382 void gaw_vertex2f(float x, float y)
387 void gaw_vertex3f(float x, float y, float z)
392 void gaw_rect(float x1, float y1, float x2, float y2)
394 glRectf(x1, y1, x2, y2);
397 void gaw_pointsize(float sz)
402 void gaw_linewidth(float w)
407 int gaw_compile_begin(void)
409 int dlist = glGenLists(1);
410 glNewList(dlist, GL_COMPILE);
414 void gaw_compile_end(void)
419 void gaw_draw_compiled(int id)
424 void gaw_free_compiled(int id)
426 glDeleteLists(id, 1);
429 void gaw_mtl_diffuse(float r, float g, float b, float a)
436 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, v);
439 void gaw_mtl_specular(float r, float g, float b, float shin)
446 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, v);
447 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
450 void gaw_mtl_emission(float r, float g, float b)
457 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, v);
460 void gaw_texenv_sphmap(int enable)
463 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
464 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
465 glEnable(GL_TEXTURE_GEN_S);
466 glEnable(GL_TEXTURE_GEN_T);
468 glDisable(GL_TEXTURE_GEN_S);
469 glDisable(GL_TEXTURE_GEN_T);
473 unsigned int gaw_create_tex1d(int texfilter)
476 glGenTextures(1, &tex);
477 glBindTexture(GL_TEXTURE_1D, tex);
478 gaw_texfilter1d(texfilter);
482 unsigned int gaw_create_tex2d(int texfilter)
485 glGenTextures(1, &tex);
486 glBindTexture(GL_TEXTURE_2D, tex);
487 gaw_texfilter2d(texfilter);
491 void gaw_destroy_tex(unsigned int tex)
493 glDeleteTextures(1, &tex);
496 void gaw_bind_tex1d(int tex)
498 glBindTexture(GL_TEXTURE_1D, tex);
501 void gaw_bind_tex2d(int tex)
503 glBindTexture(GL_TEXTURE_2D, tex);
506 void gaw_texfilter1d(int texfilter)
510 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
511 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
515 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
516 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
520 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
521 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
529 void gaw_texfilter2d(int texfilter)
533 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
534 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
538 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
539 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
543 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
544 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
552 static int glwrap(int wrap)
554 if(have_edgeclamp == -1) {
556 glextstr = strdup_nf((char*)glGetString(GL_EXTENSIONS));
558 have_edgeclamp = strstr(glextstr, "SGIS_texture_edge_clamp") != 0;
564 return GL_CLAMP_TO_EDGE;
577 void gaw_texwrap1d(int wrap)
579 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, glwrap(wrap));
582 void gaw_texwrap2d(int uwrap, int vwrap)
584 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, glwrap(uwrap));
585 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, glwrap(vwrap));
589 static const int glfmt[] = {GL_LUMINANCE, GL_RGB, GL_RGBA};
591 void gaw_tex1d(int ifmt, int xsz, int fmt, void *pix)
593 gluBuild1DMipmaps(GL_TEXTURE_1D, glfmt[ifmt], xsz, glfmt[fmt], GL_UNSIGNED_BYTE, pix);
596 void gaw_tex2d(int ifmt, int xsz, int ysz, int fmt, void *pix)
598 gluBuild2DMipmaps(GL_TEXTURE_2D, glfmt[ifmt], xsz, ysz, glfmt[fmt], GL_UNSIGNED_BYTE, pix);
601 void gaw_subtex2d(int lvl, int x, int y, int xsz, int ysz, int fmt, void *pix)
603 glTexSubImage2D(GL_TEXTURE_2D, lvl, x, y, xsz, ysz, glfmt[fmt], GL_UNSIGNED_BYTE, pix);
606 void gaw_set_tex1d(unsigned int texid)
609 glEnable(GL_TEXTURE_1D);
610 glBindTexture(GL_TEXTURE_1D, texid);
612 glDisable(GL_TEXTURE_1D);
616 void gaw_set_tex2d(unsigned int texid)
619 glEnable(GL_TEXTURE_2D);
620 glBindTexture(GL_TEXTURE_2D, texid);
622 glDisable(GL_TEXTURE_2D);
626 void gaw_ambient(float r, float g, float b)
633 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
636 void gaw_light_dir(int idx, float x, float y, float z)
643 glLightfv(GL_LIGHT0 + idx, GL_POSITION, pos);
647 void gaw_light_color(int idx, float r, float g, float b, float s)
654 glLightfv(GL_LIGHT0 + idx, GL_DIFFUSE, color);
655 glLightfv(GL_LIGHT0 + idx, GL_SPECULAR, color);
658 void gaw_lighting_fast(void)
660 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
661 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
665 void gaw_fog_color(float r, float g, float b)
672 glFogfv(GL_FOG_COLOR, col);
675 void gaw_fog_linear(float z0, float z1)
677 glFogi(GL_FOG_MODE, GL_LINEAR);
678 glFogf(GL_FOG_START, z0);
679 glFogf(GL_FOG_END, z1);
682 void gaw_fog_fast(void)
684 glHint(GL_FOG_HINT, GL_FASTEST);
687 void gaw_poly_wire(void)
689 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
692 void gaw_poly_flat(void)
694 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
695 glShadeModel(GL_FLAT);
698 void gaw_poly_gouraud(void)
700 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
701 glShadeModel(GL_SMOOTH);