Jo Engine  2023.08.26
Jo Sega Saturn Engine
software_renderer.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2020, Johannes Fetz (johannesfetz@gmail.com)
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are met:
8 ** * Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** * Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** * Neither the name of the Johannes Fetz nor the
14 ** names of its contributors may be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 ** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
36 #ifndef __JO_SOFTWARE_RENDERER_H__
37 # define __JO_SOFTWARE_RENDERER_H__
38 
39 #ifdef JO_COMPILE_WITH_SOFTWARE_RENDERER_SUPPORT
40 
41 /*
42  ██████╗ ██████╗ ██████╗ ███████╗
43 ██╔════╝██╔═══██╗██╔══██╗██╔════╝
44 ██║ ██║ ██║██████╔╝█████╗
45 ██║ ██║ ██║██╔══██╗██╔══╝
46 ╚██████╗╚██████╔╝██║ ██║███████╗
47  ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
48 */
49 
50 /*
51  Jo Engine Software Rendering XYZ axes
52 
53  Z-
54  /
55  /
56  0------> X+ (width)
57  /|
58  / |
59  Z+ V
60  Y+ (height)
61 */
62 
64 typedef enum
65 {
66  JO_SR_DRAW_TEXTURED = (1 << 0),
67  JO_SR_DRAW_FLAT = (1 << 1),
68  JO_SR_DRAW_WIREFRAME = (1 << 2)
69 } jo_software_renderer_draw_mode;
70 
72 typedef enum
73 {
74  JO_SR_BACK_FACE_CULLING = (1 << 0),
75  JO_SR_FRONT_FACE_CULLING = (1 << 1),
76  JO_SR_NO_FACE_CULLING = (1 << 2)
77 } jo_software_renderer_face_culling_mode;
78 
80 typedef enum
81 {
82  JO_SR_DEPTH_IGNORE = (1 << 0),
83  JO_SR_DEPTH_LESS = (1 << 1),
84  JO_SR_DEPTH_LESS_OR_EQUAL = (1 << 2),
85  JO_SR_DEPTH_GREATER_OR_EQUAL = (1 << 3),
86  JO_SR_DEPTH_GREATER = (1 << 4),
87  JO_SR_DEPTH_NOT_EQUAL = (1 << 5),
88  JO_SR_DEPTH_EQUAL = (1 << 6)
89 } jo_software_renderer_depth_mode;
90 
92 typedef struct
93 {
94  jo_size clipping_size;
95  int sprite_id;
96  jo_fixed *depth_buffer;
97  unsigned int depth_buffer_dword_size;
98  jo_color *color_buffer;
99  unsigned int color_buffer_size;
100  void *vram;
101  jo_size vram_size;
102  jo_software_renderer_depth_mode depth_mode_testing;
103  jo_software_renderer_draw_mode draw_mode;
104  jo_software_renderer_face_culling_mode face_culling_mode;
105 } jo_software_renderer_gfx;
106 
108 typedef struct
109 {
110  jo_vector4_fixed pos;
111  jo_vector2_fixed uv_texture_mapping;
112  jo_color color;
113 } jo_software_renderer_vertex;
114 
121 jo_software_renderer_gfx *jo_software_renderer_create(unsigned short width, unsigned short height, const jo_scroll_screen screen);
122 
127 void jo_software_renderer_free(jo_software_renderer_gfx * const gfx);
128 
132 void jo_software_renderer_flush(jo_software_renderer_gfx * const gfx);
133 
134 /*
135 ██████╗ █████╗ ███████╗██╗ ██████╗ ██████╗ ██████╗ █████╗ ██╗ ██╗██╗███╗ ██╗ ██████╗
136 ██╔══██╗██╔══██╗██╔════╝██║██╔════╝ ██╔══██╗██╔══██╗██╔══██╗██║ ██║██║████╗ ██║██╔════╝
137 ██████╔╝███████║███████╗██║██║ ██║ ██║██████╔╝███████║██║ █╗ ██║██║██╔██╗ ██║██║ ███╗
138 ██╔══██╗██╔══██║╚════██║██║██║ ██║ ██║██╔══██╗██╔══██║██║███╗██║██║██║╚██╗██║██║ ██║
139 ██████╔╝██║ ██║███████║██║╚██████╗ ██████╔╝██║ ██║██║ ██║╚███╔███╔╝██║██║ ╚████║╚██████╔╝
140 ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══╝╚══╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝
141 */
142 
147 void jo_software_renderer_clear(const jo_software_renderer_gfx * const gfx, const jo_color color);
148 
155 void jo_software_renderer_draw_pixel2D(const jo_software_renderer_gfx * const gfx, const jo_fixed x, const jo_fixed y, const jo_color color);
156 
164 void jo_software_renderer_draw_pixel3D(const jo_software_renderer_gfx * const gfx, const jo_fixed x, const jo_fixed y, const jo_fixed z, const jo_color color);
165 
177 void jo_software_renderer_draw_line3D(const jo_software_renderer_gfx * const gfx,
178  jo_fixed x0, jo_fixed y0, jo_fixed z0,
179  jo_fixed x1, jo_fixed y1, jo_fixed z1,
180  const jo_color color0, const jo_color color1);
181 
182 /*
183 ████████╗██████╗ ██╗ █████╗ ███╗ ██╗ ██████╗ ██╗ ███████╗
184 ╚══██╔══╝██╔══██╗██║██╔══██╗████╗ ██║██╔════╝ ██║ ██╔════╝
185  ██║ ██████╔╝██║███████║██╔██╗ ██║██║ ███╗██║ █████╗
186  ██║ ██╔══██╗██║██╔══██║██║╚██╗██║██║ ██║██║ ██╔══╝
187  ██║ ██║ ██║██║██║ ██║██║ ╚████║╚██████╔╝███████╗███████╗
188  ╚═╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚══════╝╚══════╝
189 */
190 
192 typedef struct
193 {
194  jo_software_renderer_vertex v0;
195  jo_software_renderer_vertex v1;
196  jo_software_renderer_vertex v2;
197  int sprite_id;
198 } jo_software_renderer_triangle;
199 
200 void jo_software_renderer_draw_triangle(const jo_software_renderer_gfx * const gfx,
201  const jo_software_renderer_triangle * const triangle,
202  const jo_matrix * const transform_matrix);
203 
208 static __jo_force_inline void jo_software_renderer_draw_triangle_wireframe(const jo_software_renderer_gfx * const gfx, const jo_software_renderer_triangle * const triangle)
209 {
210  jo_software_renderer_draw_line3D(gfx,
211  triangle->v0.pos.x, triangle->v0.pos.y, triangle->v0.pos.z,
212  triangle->v1.pos.x, triangle->v1.pos.y, triangle->v1.pos.z,
213  triangle->v0.color, triangle->v1.color);
214  jo_software_renderer_draw_line3D(gfx,
215  triangle->v1.pos.x, triangle->v1.pos.y, triangle->v1.pos.z,
216  triangle->v2.pos.x, triangle->v2.pos.y, triangle->v2.pos.z,
217  triangle->v1.color, triangle->v2.color);
218  jo_software_renderer_draw_line3D(gfx,
219  triangle->v2.pos.x, triangle->v2.pos.y, triangle->v2.pos.z,
220  triangle->v0.pos.x, triangle->v0.pos.y, triangle->v0.pos.z,
221  triangle->v2.color, triangle->v0.color);
222 }
223 
224 #endif // JO_COMPILE_WITH_SOFTWARE_RENDERER_SUPPORT
225 
226 #endif /* !__JO_SOFTWARE_RENDERER_H__ */
227 
228 /*
229 ** END OF FILE
230 */
__jo_size
Size struct.
Definition: types.h:200
jo_vector4_fixed
Vector4 for 3D computation using fixed number.
Definition: types.h:165
jo_vector2_fixed
Vector for 2D computation using fixed number.
Definition: types.h:156
jo_color
unsigned short jo_color
15 bits color type
Definition: types.h:42
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: types.h:39
jo_fixed
int jo_fixed
Fixed point Q16.16 number.
Definition: types.h:49
jo_matrix
4x4 MATRIX for 3D computation using fixed number
Definition: types.h:186
jo_scroll_screen
jo_scroll_screen
Sega Saturn Scroll Screen Ids.
Definition: types.h:316