Jo Engine 2026.11.1
Jo Sega Saturn Engine
Loading...
Searching...
No Matches
voxel.h
Go to the documentation of this file.
1/*
2** Jo Sega Saturn Engine
3** Copyright (c) 2012-2026, 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*/
34
35#ifndef __JO_VOXEL_H__
36# define __JO_VOXEL_H__
37
38#ifdef JO_COMPILE_WITH_SOFTWARE_RENDERER_SUPPORT
39
53
66
80
81/*
82 ██████╗ ██████╗ ██████╗ ███████╗
83██╔════╝██╔═══██╗██╔══██╗██╔════╝
84██║ ██║ ██║██████╔╝█████╗
85██║ ██║ ██║██╔══██╗██╔══╝
86╚██████╗╚██████╔╝██║ ██║███████╗
87 ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
88*/
89
94void jo_voxel_do_angle_computation(jo_voxel * const voxel_data);
95
100void jo_voxel_redraw(const jo_voxel * const voxel_data);
101
106void jo_voxel_run(jo_voxel * const voxel_data);
107
112void __jo_voxel_redraw(const jo_voxel * const voxel_data, const bool clear);
113
118static __jo_force_inline void jo_voxel_redraw_and_flush(const jo_voxel * const voxel_data)
119{
120 // (the pixels that are not drawn are cleared in black: the buffer is not cleared first)
121 __jo_voxel_redraw(voxel_data, true);
122 jo_software_renderer_flush(voxel_data->gfx);
123}
124
125/*
126 ██████╗ █████╗ ███╗ ███╗███████╗██████╗ █████╗
127██╔════╝██╔══██╗████╗ ████║██╔════╝██╔══██╗██╔══██╗
128██║ ███████║██╔████╔██║█████╗ ██████╔╝███████║
129██║ ██╔══██║██║╚██╔╝██║██╔══╝ ██╔══██╗██╔══██║
130╚██████╗██║ ██║██║ ╚═╝ ██║███████╗██║ ██║██║ ██║
131 ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝
132*/
133
138static __jo_force_inline void jo_voxel_set_scale(jo_voxel * const voxel_data, const jo_fixed scale)
139{
140 voxel_data->camera.scale = scale;
141}
142
147static __jo_force_inline void jo_voxel_increase_view_distance(jo_voxel * const voxel_data, const jo_fixed distance)
148{
149 voxel_data->camera.zfar += distance;
150 voxel_data->__cache.inv_zfar = jo_fixed_div(JO_FIXED_1, voxel_data->camera.zfar);
152}
153
158static __jo_force_inline void jo_voxel_decrease_view_distance(jo_voxel * const voxel_data, const jo_fixed distance)
159{
160 voxel_data->camera.zfar -= distance;
161 voxel_data->__cache.inv_zfar = jo_fixed_div(JO_FIXED_1, voxel_data->camera.zfar);
163}
164
169static __jo_force_inline void jo_voxel_horizon_up(jo_voxel * const voxel_data, const jo_fixed distance)
170{
171 voxel_data->camera.horizon += distance;
172}
173
178static __jo_force_inline void jo_voxel_horizon_down(jo_voxel *const voxel_data, const jo_fixed distance)
179{
180 voxel_data->camera.horizon -= distance;
181}
182
187static __jo_force_inline void jo_voxel_move_up(jo_voxel *voxel_data, const jo_fixed distance)
188{
189 voxel_data->camera.height += distance;
190}
191
196static __jo_force_inline void jo_voxel_move_down(jo_voxel * const voxel_data, const jo_fixed distance)
197{
198 voxel_data->camera.height -= distance;
199}
200
205static __jo_force_inline void jo_voxel_move_forward(jo_voxel * const voxel_data, const jo_fixed distance)
206{
207 voxel_data->camera.x += jo_fixed_mult(jo_fixed_cos(voxel_data->camera.angle), distance);
208 voxel_data->camera.y += jo_fixed_mult(jo_fixed_sin(voxel_data->camera.angle), distance);
209}
210
215static __jo_force_inline void jo_voxel_move_backward(jo_voxel * const voxel_data, const jo_fixed distance)
216{
217 voxel_data->camera.x -= jo_fixed_mult(jo_fixed_cos(voxel_data->camera.angle), distance);
218 voxel_data->camera.y -= jo_fixed_mult(jo_fixed_sin(voxel_data->camera.angle), distance);
219}
220
225static __jo_force_inline void jo_voxel_rotate_left(jo_voxel * const voxel_data, const jo_fixed angle_rad)
226{
227 voxel_data->camera.angle -= angle_rad;
229}
230
235static __jo_force_inline void jo_voxel_rotate_right(jo_voxel * const voxel_data, const jo_fixed angle_rad)
236{
237 voxel_data->camera.angle += angle_rad;
239}
240
241#endif // JO_COMPILE_WITH_SOFTWARE_RENDERER_SUPPORT
242
243#endif /* !__JO_VOXEL_H__ */
244
245/*
246** END OF FILE
247*/
static __jo_force_inline jo_fixed jo_fixed_cos(const jo_fixed rad)
Fast cosinus computation using fixed number.
Definition math.h:1061
static __jo_force_inline jo_fixed jo_fixed_sin(const jo_fixed rad)
Fast sinus computation using fixed number.
Definition math.h:980
static __jo_force_inline jo_fixed jo_fixed_mult(const jo_fixed x, const jo_fixed y)
Multiply to fixed number.
Definition math.h:546
#define JO_FIXED_1
Fixed floating point value for 1.
Definition math.h:405
static __jo_force_inline jo_fixed jo_fixed_div(const jo_fixed dividend, const jo_fixed divisor)
Divide fixed-point numbers (expresses dividend / divisor).
Definition math.h:605
void jo_software_renderer_flush(jo_software_renderer_gfx *const gfx)
Copy the buffer into VDP1/2 VRAM.
This is the main struct that handle everything for drawing.
Definition software_renderer.h:93
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39
int jo_fixed
Fixed point Q16.16 number.
Definition types.h:48
8 bits image struct
Definition types.h:239
Palette contents struct.
Definition types.h:255
jo_voxel_camera camera
Definition voxel.h:76
void jo_voxel_redraw(const jo_voxel *const voxel_data)
Draw voxels to gfx.
static __jo_force_inline void jo_voxel_horizon_up(jo_voxel *const voxel_data, const jo_fixed distance)
Increase horizon height (camera.horizon).
Definition voxel.h:169
jo_img_8bits terrain_img
Definition voxel.h:74
jo_fixed inv_clipping_size_width
Definition voxel.h:45
jo_fixed deltax_incr
Definition voxel.h:48
jo_palette height_pal
Definition voxel.h:73
jo_fixed deltay_incr
Definition voxel.h:49
static __jo_force_inline void jo_voxel_horizon_down(jo_voxel *const voxel_data, const jo_fixed distance)
Decrease horizon height (camera.horizon).
Definition voxel.h:178
jo_fixed scale
Definition voxel.h:58
static __jo_force_inline void jo_voxel_rotate_left(jo_voxel *const voxel_data, const jo_fixed angle_rad)
Rotate camera on the left (camera.angle).
Definition voxel.h:225
jo_fixed x
Definition voxel.h:59
static __jo_force_inline void jo_voxel_decrease_view_distance(jo_voxel *const voxel_data, const jo_fixed distance)
Decrease view distance (camera.zfar).
Definition voxel.h:158
static __jo_force_inline void jo_voxel_redraw_and_flush(const jo_voxel *const voxel_data)
Draw voxels and flush the buffer.
Definition voxel.h:118
void jo_voxel_run(jo_voxel *const voxel_data)
Draw voxels to gfx.
void jo_voxel_do_angle_computation(jo_voxel *const voxel_data)
Do computation linked to camera angle.
jo_software_renderer_gfx * gfx
Definition voxel.h:71
static __jo_force_inline void jo_voxel_move_up(jo_voxel *voxel_data, const jo_fixed distance)
Increase camera height (camera.height).
Definition voxel.h:187
__jo_voxel_computation_cache __cache
Definition voxel.h:78
jo_fixed angle
Definition voxel.h:64
jo_fixed zfar
Definition voxel.h:63
jo_fixed y
Definition voxel.h:60
bool use_zbuffer
Definition voxel.h:77
static __jo_force_inline void jo_voxel_increase_view_distance(jo_voxel *const voxel_data, const jo_fixed distance)
Increase view distance (camera.zfar).
Definition voxel.h:147
jo_fixed horizon
Definition voxel.h:62
jo_fixed height
Definition voxel.h:61
static __jo_force_inline void jo_voxel_move_down(jo_voxel *const voxel_data, const jo_fixed distance)
Decrease camera height (camera.height).
Definition voxel.h:196
static __jo_force_inline void jo_voxel_move_backward(jo_voxel *const voxel_data, const jo_fixed distance)
Move camera backward (camera.x & camera.y).
Definition voxel.h:215
static __jo_force_inline void jo_voxel_move_forward(jo_voxel *const voxel_data, const jo_fixed distance)
Move camera forward (camera.x & camera.y).
Definition voxel.h:205
jo_fixed deltay_0
Definition voxel.h:51
jo_palette terrain_pal
Definition voxel.h:72
void __jo_voxel_redraw(const jo_voxel *const voxel_data, const bool clear)
Draw voxels to gfx (internal engine usage, see jo_voxel_redraw() and jo_voxel_redraw_and_flush()).
jo_fixed deltax_0
Definition voxel.h:50
jo_img_8bits height_img
Definition voxel.h:75
static __jo_force_inline void jo_voxel_set_scale(jo_voxel *const voxel_data, const jo_fixed scale)
Set scale (camera.scale).
Definition voxel.h:138
static __jo_force_inline void jo_voxel_rotate_right(jo_voxel *const voxel_data, const jo_fixed angle_rad)
Rotate camera on the right (camera.angle).
Definition voxel.h:235
jo_fixed inv_zfar
Definition voxel.h:46
int map_width_minus_1
Definition voxel.h:47
Voxel computation cache (internal engine usage).
Definition voxel.h:44
Voxel main definition.
Definition voxel.h:70
Voxel camera definition.
Definition voxel.h:57