952f92c4068648244de0647bc08ad3c1cd56199e
[retroray] / src / gaw / gawswtnl.h
1 /*
2 Deep Runner - 6dof shooter game for the SGI O2.
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
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.
9
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.
14
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/>.
17 */
18 #ifndef GAWSWTNL_H_
19 #define GAWSWTNL_H_
20
21 #include "polyfill.h"
22
23 enum {
24         GAW_SPHEREMAP   = 24
25 };
26
27 /* modelview, projection, texture */
28 #define NUM_MATRICES    3
29 #define STACK_SIZE      8
30 typedef float gaw_matrix[16];
31
32 #define MAX_LIGHTS              4
33 #define MAX_TEXTURES    256
34 #define MAX_COMPILED    256
35
36 #define IMM_VBUF_SIZE   256
37
38 enum {LT_POS, LT_DIR};
39 struct light {
40         int type;
41         float x, y, z;
42         float r, g, b;
43 };
44
45 struct material {
46         float kd[4];
47         float ks[3];
48         float ke[3];
49         float shin;
50 };
51
52 struct comp_geom {
53         int prim;
54         float *varr, *narr, *uvarr, *carr;      /* darr */
55 };
56
57
58 struct gaw_state {
59         uint32_t opt;
60         uint32_t savopt[STACK_SIZE];
61         int savopt_top;
62
63         int frontface;
64         int polymode;
65
66         const float *varr, *narr, *uvarr;
67
68         gaw_matrix mat[NUM_MATRICES][STACK_SIZE];
69         int mtop[NUM_MATRICES];
70         int mmode;
71
72         gaw_matrix norm_mat;
73
74         float ambient[3];
75         struct light lt[MAX_LIGHTS];
76         struct material mtl;
77
78         int bsrc, bdst;
79
80         int width, height;
81         gaw_pixel *pixels;
82
83         int vport[4];
84
85         uint32_t clear_color;
86         uint32_t clear_depth;
87
88         const float *vertex_ptr, *normal_ptr, *texcoord_ptr, *color_ptr;
89         int vertex_nelem, texcoord_nelem, color_nelem;
90         int vertex_stride, normal_stride, texcoord_stride, color_stride;
91
92         /* immediate mode */
93         int imm_prim;
94         int imm_numv, imm_pcount;
95         struct vertex imm_curv;
96         float imm_curcol[4];
97         struct vertex imm_vbuf[IMM_VBUF_SIZE];
98         float imm_cbuf[IMM_VBUF_SIZE * 4];
99
100         /* textures */
101         int cur_tex;
102         int textypes[MAX_TEXTURES];
103
104         /* compiled geometries */
105         int cur_comp;
106         struct comp_geom comp[MAX_COMPILED];
107 };
108
109 extern struct gaw_state *gaw_state;
110 #define ST      gaw_state
111
112
113 void gaw_swtnl_reset(void);
114 void gaw_swtnl_init(void);
115 void gaw_swtnl_destroy(void);
116
117 void gaw_swtnl_enable(int what);
118 void gaw_swtnl_disable(int what);
119
120 void gaw_swtnl_color_mask(int rmask, int gmask, int bmask, int amask);
121 void gaw_swtnl_depth_mask(int mask);
122
123 void gaw_swtnl_tex1d(int ifmt, int xsz, int fmt, void *pix);
124 void gaw_swtnl_tex2d(int ifmt, int xsz, int ysz, int fmt, void *pix);
125 void gaw_swtnl_subtex2d(int lvl, int x, int y, int xsz, int ysz, int fmt, void *pix);
126
127 void gaw_swtnl_drawprim(int prim, struct vertex *v, int vnum);
128
129
130 #if defined(__i386__) || defined(__386__) || defined(MSDOS)
131 /* fast conversion of double -> 32bit int
132  * for details see:
133  *  - http://chrishecker.com/images/f/fb/Gdmfp.pdf
134  *  - http://stereopsis.com/FPU.html#convert
135  */
136 static INLINE int32_t cround64(double val)
137 {
138         val += 6755399441055744.0;
139         return *(int32_t*)&val;
140 }
141 #else
142 #define cround64(x)     ((int32_t)(x))
143 #endif
144
145
146 #endif  /* GAWSWTNL_H_ */