Jo Engine  2024.04.28
Jo Sega Saturn Engine
software_renderer.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2024, 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)
70 
72 typedef enum
73 {
76  JO_SR_NO_FACE_CULLING = (1 << 2)
78 
80 typedef enum
81 {
82  JO_SR_DEPTH_IGNORE = (1 << 0),
83  JO_SR_DEPTH_LESS = (1 << 1),
86  JO_SR_DEPTH_GREATER = (1 << 4),
88  JO_SR_DEPTH_EQUAL = (1 << 6)
90 
92 typedef struct
93 {
95  int sprite_id;
99  unsigned int color_buffer_size;
100  void *vram;
106 
108 typedef struct
109 {
114 
122 jo_software_renderer_gfx *jo_software_renderer_create_ex(unsigned short width, unsigned short height, const jo_scroll_screen screen, const bool enable_zbuffer);
123 
130 static __jo_force_inline jo_software_renderer_gfx *jo_software_renderer_create(unsigned short width, unsigned short height, const jo_scroll_screen screen)
131 {
132  return (jo_software_renderer_create_ex(width, height, screen, true));
133 }
134 
140 
145 
146 /*
147 ██████╗ █████╗ ███████╗██╗ ██████╗ ██████╗ ██████╗ █████╗ ██╗ ██╗██╗███╗ ██╗ ██████╗
148 ██╔══██╗██╔══██╗██╔════╝██║██╔════╝ ██╔══██╗██╔══██╗██╔══██╗██║ ██║██║████╗ ██║██╔════╝
149 ██████╔╝███████║███████╗██║██║ ██║ ██║██████╔╝███████║██║ █╗ ██║██║██╔██╗ ██║██║ ███╗
150 ██╔══██╗██╔══██║╚════██║██║██║ ██║ ██║██╔══██╗██╔══██║██║███╗██║██║██║╚██╗██║██║ ██║
151 ██████╔╝██║ ██║███████║██║╚██████╗ ██████╔╝██║ ██║██║ ██║╚███╔███╔╝██║██║ ╚████║╚██████╔╝
152 ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══╝╚══╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝
153 */
154 
159 void jo_software_renderer_clear(const jo_software_renderer_gfx * const gfx, const jo_color color);
160 
167 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);
168 
176 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);
177 
190  jo_fixed x0, jo_fixed y0, jo_fixed z0,
191  jo_fixed x1, jo_fixed y1, jo_fixed z1,
192  const jo_color color0, const jo_color color1);
193 
194 /*
195 ████████╗██████╗ ██╗ █████╗ ███╗ ██╗ ██████╗ ██╗ ███████╗
196 ╚══██╔══╝██╔══██╗██║██╔══██╗████╗ ██║██╔════╝ ██║ ██╔════╝
197  ██║ ██████╔╝██║███████║██╔██╗ ██║██║ ███╗██║ █████╗
198  ██║ ██╔══██╗██║██╔══██║██║╚██╗██║██║ ██║██║ ██╔══╝
199  ██║ ██║ ██║██║██║ ██║██║ ╚████║╚██████╔╝███████╗███████╗
200  ╚═╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚══════╝╚══════╝
201 */
202 
204 typedef struct
205 {
211 
213  const jo_software_renderer_triangle * const triangle,
214  const jo_matrix * const transform_matrix);
215 
221 {
223  triangle->v0.pos.x, triangle->v0.pos.y, triangle->v0.pos.z,
224  triangle->v1.pos.x, triangle->v1.pos.y, triangle->v1.pos.z,
225  triangle->v0.color, triangle->v1.color);
227  triangle->v1.pos.x, triangle->v1.pos.y, triangle->v1.pos.z,
228  triangle->v2.pos.x, triangle->v2.pos.y, triangle->v2.pos.z,
229  triangle->v1.color, triangle->v2.color);
231  triangle->v2.pos.x, triangle->v2.pos.y, triangle->v2.pos.z,
232  triangle->v0.pos.x, triangle->v0.pos.y, triangle->v0.pos.z,
233  triangle->v2.color, triangle->v0.color);
234 }
235 
236 #endif // JO_COMPILE_WITH_SOFTWARE_RENDERER_SUPPORT
237 
238 #endif /* !__JO_SOFTWARE_RENDERER_H__ */
239 
240 /*
241 ** END OF FILE
242 */
jo_software_renderer_clear
void jo_software_renderer_clear(const jo_software_renderer_gfx *const gfx, const jo_color color)
Clear the buffer with the given color.
jo_software_renderer_free
void jo_software_renderer_free(jo_software_renderer_gfx *const gfx)
Free the rendering surface.
jo_software_renderer_gfx::depth_buffer
jo_fixed * depth_buffer
Definition: software_renderer.h:96
JO_SR_NO_FACE_CULLING
@ JO_SR_NO_FACE_CULLING
Definition: software_renderer.h:76
jo_software_renderer_triangle::v2
jo_software_renderer_vertex v2
Definition: software_renderer.h:208
__jo_size
Size struct.
Definition: types.h:200
jo_software_renderer_gfx::vram_size
jo_size vram_size
Definition: software_renderer.h:101
jo_software_renderer_gfx::color_buffer
jo_color * color_buffer
Definition: software_renderer.h:98
JO_SR_FRONT_FACE_CULLING
@ JO_SR_FRONT_FACE_CULLING
Definition: software_renderer.h:75
jo_software_renderer_gfx::vram
void * vram
Definition: software_renderer.h:100
JO_SR_DRAW_FLAT
@ JO_SR_DRAW_FLAT
Definition: software_renderer.h:67
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_software_renderer_gfx::color_buffer_size
unsigned int color_buffer_size
Definition: software_renderer.h:99
jo_software_renderer_gfx::sprite_id
int sprite_id
Definition: software_renderer.h:95
jo_software_renderer_draw_triangle
void jo_software_renderer_draw_triangle(const jo_software_renderer_gfx *const gfx, const jo_software_renderer_triangle *const triangle, const jo_matrix *const transform_matrix)
jo_software_renderer_vertex::pos
jo_vector4_fixed pos
Definition: software_renderer.h:110
JO_SR_DEPTH_GREATER
@ JO_SR_DEPTH_GREATER
Definition: software_renderer.h:86
JO_SR_DEPTH_IGNORE
@ JO_SR_DEPTH_IGNORE
Definition: software_renderer.h:82
jo_software_renderer_create
static __jo_force_inline jo_software_renderer_gfx * jo_software_renderer_create(unsigned short width, unsigned short height, const jo_scroll_screen screen)
Create a full-color rendering surface.
Definition: software_renderer.h:130
jo_software_renderer_gfx::draw_mode
jo_software_renderer_draw_mode draw_mode
Definition: software_renderer.h:103
jo_software_renderer_gfx::depth_buffer_dword_size
unsigned int depth_buffer_dword_size
Definition: software_renderer.h:97
jo_software_renderer_triangle::sprite_id
int sprite_id
Definition: software_renderer.h:209
JO_SR_DEPTH_LESS
@ JO_SR_DEPTH_LESS
Definition: software_renderer.h:83
jo_color
unsigned short jo_color
15 bits color type
Definition: types.h:42
jo_software_renderer_create_ex
jo_software_renderer_gfx * jo_software_renderer_create_ex(unsigned short width, unsigned short height, const jo_scroll_screen screen, const bool enable_zbuffer)
Create a full-color rendering surface.
jo_software_renderer_gfx::face_culling_mode
jo_software_renderer_face_culling_mode face_culling_mode
Definition: software_renderer.h:104
jo_software_renderer_vertex::uv_texture_mapping
jo_vector2_fixed uv_texture_mapping
Definition: software_renderer.h:111
__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_software_renderer_gfx
This is the main struct that handle everything for drawing.
Definition: software_renderer.h:93
jo_matrix
4x4 MATRIX for 3D computation using fixed number
Definition: types.h:186
jo_software_renderer_face_culling_mode
jo_software_renderer_face_culling_mode
Face culling mode.
Definition: software_renderer.h:73
JO_SR_DRAW_TEXTURED
@ JO_SR_DRAW_TEXTURED
Definition: software_renderer.h:66
JO_SR_DEPTH_NOT_EQUAL
@ JO_SR_DEPTH_NOT_EQUAL
Definition: software_renderer.h:87
JO_SR_DRAW_WIREFRAME
@ JO_SR_DRAW_WIREFRAME
Definition: software_renderer.h:68
jo_software_renderer_vertex::color
jo_color color
Definition: software_renderer.h:112
jo_software_renderer_depth_mode
jo_software_renderer_depth_mode
Z-BUFFER Computation mode.
Definition: software_renderer.h:81
JO_SR_BACK_FACE_CULLING
@ JO_SR_BACK_FACE_CULLING
Definition: software_renderer.h:74
jo_software_renderer_triangle
Triangle definition.
Definition: software_renderer.h:205
jo_software_renderer_triangle::v1
jo_software_renderer_vertex v1
Definition: software_renderer.h:207
jo_software_renderer_draw_pixel2D
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)
Draw a pixel without handling the Z-Buffer.
JO_SR_DEPTH_LESS_OR_EQUAL
@ JO_SR_DEPTH_LESS_OR_EQUAL
Definition: software_renderer.h:84
jo_software_renderer_gfx::depth_mode_testing
jo_software_renderer_depth_mode depth_mode_testing
Definition: software_renderer.h:102
JO_SR_DEPTH_GREATER_OR_EQUAL
@ JO_SR_DEPTH_GREATER_OR_EQUAL
Definition: software_renderer.h:85
jo_software_renderer_triangle::v0
jo_software_renderer_vertex v0
Definition: software_renderer.h:206
jo_software_renderer_vertex
Vertex.
Definition: software_renderer.h:109
jo_software_renderer_draw_triangle_wireframe
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)
Draw a triangle in wireframe.
Definition: software_renderer.h:220
jo_software_renderer_gfx::clipping_size
jo_size clipping_size
Definition: software_renderer.h:94
jo_software_renderer_draw_pixel3D
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)
Draw a pixel.
JO_SR_DEPTH_EQUAL
@ JO_SR_DEPTH_EQUAL
Definition: software_renderer.h:88
jo_software_renderer_draw_mode
jo_software_renderer_draw_mode
Draw mode.
Definition: software_renderer.h:65
jo_software_renderer_draw_line3D
void jo_software_renderer_draw_line3D(const jo_software_renderer_gfx *const gfx, jo_fixed x0, jo_fixed y0, jo_fixed z0, jo_fixed x1, jo_fixed y1, jo_fixed z1, const jo_color color0, const jo_color color1)
Draw a line.
jo_software_renderer_flush
void jo_software_renderer_flush(jo_software_renderer_gfx *const gfx)
Copy the buffer into VDP1/2 VRAM.
jo_scroll_screen
jo_scroll_screen
Sega Saturn Scroll Screen Ids.
Definition: types.h:316