d01629189b71b04f532b5e99553a24eab4f31a6e
[retroray] / src / gaw / gawswtnl.h
1 /*
2 RetroRay - integrated standalone vintage modeller/renderer
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         float zoffs;
93
94         /* immediate mode */
95         int imm_prim;
96         int imm_numv, imm_pcount;
97         struct vertex imm_curv;
98         float imm_curcol[4];
99         struct vertex imm_vbuf[IMM_VBUF_SIZE];
100         float imm_cbuf[IMM_VBUF_SIZE * 4];
101
102         /* textures */
103         int cur_tex;
104         int textypes[MAX_TEXTURES];
105
106         /* compiled geometries */
107         int cur_comp;
108         struct comp_geom comp[MAX_COMPILED];
109 };
110
111 extern struct gaw_state *gaw_state;
112 #define ST      gaw_state
113
114
115 void gaw_swtnl_reset(void);
116 void gaw_swtnl_init(void);
117 void gaw_swtnl_destroy(void);
118
119 void gaw_swtnl_enable(int what);
120 void gaw_swtnl_disable(int what);
121
122 void gaw_swtnl_color_mask(int rmask, int gmask, int bmask, int amask);
123 void gaw_swtnl_depth_mask(int mask);
124
125 void gaw_swtnl_tex1d(int ifmt, int xsz, int fmt, void *pix);
126 void gaw_swtnl_tex2d(int ifmt, int xsz, int ysz, int fmt, void *pix);
127 void gaw_swtnl_subtex2d(int lvl, int x, int y, int xsz, int ysz, int fmt, void *pix);
128
129 void gaw_swtnl_drawprim(int prim, struct vertex *v, int vnum);
130
131
132 #if defined(__i386__) || defined(__386__) || defined(MSDOS)
133
134 #ifndef INLINE
135 #if (__STDC_VERSION__ >= 199901) || defined(__GNUC__)
136 #define INLINE inline
137 #else
138 #define INLINE __inline
139 #endif
140 #endif
141
142 /* fast conversion of double -> 32bit int
143  * for details see:
144  *  - http://chrishecker.com/images/f/fb/Gdmfp.pdf
145  *  - http://stereopsis.com/FPU.html#convert
146  */
147 static INLINE int32_t cround64(double val)
148 {
149         val += 6755399441055744.0;
150         return *(int32_t*)&val;
151 }
152 #else
153 #define cround64(x)     ((int32_t)(x))
154 #endif
155
156
157 #endif  /* GAWSWTNL_H_ */