Hi,
I am trying (again) to learning Jo Engine but I am struggling with a simple thing: put the vdp2 as a floor (or correct the camera perspective):
my current code:
any hints?
I am trying (again) to learning Jo Engine but I am struggling with a simple thing: put the vdp2 as a floor (or correct the camera perspective):
my current code:
Code:
#include <jo/jo.h>
static jo_pos3Df pos, rot;
static jo_palette floor_pal;
static jo_camera cam;
static jo_vertice cube_vertices[] = JO_3D_CUBE_VERTICES(32);
static jo_3d_quad cube_quads[6];
static int box_tex_id = -1;
static jo_pos3Df cube_pos;
static jo_pos3Df cube_prev;
static jo_pos3Df cube2_pos;
static jo_pos3Df cam_posf;
static jo_pos3Df cam_offset = { 0.f, 140.f, 160.f };
static float cam_lerp = 0.12f;
static float cam_max_step = 14.f;
static float cam_lookahead= 1.6f;
static float cam_dead = 2.0f;
static float cam_pitch_down = 40.f;
static jo_palette *my_tga_palette_handling(void){
jo_create_palette(&floor_pal);
return &floor_pal;
}
static void init_vdp2_floor(void){
jo_img_8bits img; img.data = JO_NULL;
jo_core_tv_off();
jo_enable_background_3d_plane(JO_COLOR_Black);
jo_tga_8bits_loader(&img, JO_ROOT_DIR, "WATER.TGA", 0);
jo_background_3d_plane_a_img(&img, floor_pal.id, true, true);
jo_free_img(&img);
jo_core_tv_on();
}
static void create_textured_cube(void){
jo_3d_create_cube(cube_quads, cube_vertices);
for (int i = 0; i < 6; ++i) jo_3d_set_texture(&cube_quads[i], box_tex_id);
}
static void my_input(void){
const float sx = 3.0f, sy = 3.0f, sz = 4.0f;
if (jo_is_pad1_key_pressed(JO_KEY_UP)) cube_pos.z -= sz;
else if (jo_is_pad1_key_pressed(JO_KEY_DOWN)) cube_pos.z += sz;
if (jo_is_pad1_key_pressed(JO_KEY_LEFT)) cube_pos.x += sx;
else if (jo_is_pad1_key_pressed(JO_KEY_RIGHT)) cube_pos.x -= sx;
if (jo_is_pad1_key_pressed(JO_KEY_L)) cube_pos.y += sy;
else if (jo_is_pad1_key_pressed(JO_KEY_R)) cube_pos.y -= sy;
}
static float __absf(float v){ return v < 0.f ? -v : v; }
static float __clampf(float v,float m){ if (v>m) return m; if (v<-m) return -m; return v; }
static void update_camera_follow(void){
const float vx = cube_pos.x - cube_prev.x;
const float vy = cube_pos.y - cube_prev.y;
const float vz = cube_pos.z - cube_prev.z;
const float want_x = cube_pos.x + cam_offset.x;
const float want_y = cube_pos.y + cam_offset.y;
const float want_z = cube_pos.z + cam_offset.z;
float dx = want_x - cam_posf.x;
float dy = want_y - cam_posf.y;
float dz = want_z - cam_posf.z;
if (__absf(dx) > cam_dead) cam_posf.x += __clampf(dx * cam_lerp, cam_max_step);
if (__absf(dy) > cam_dead) cam_posf.y += __clampf(dy * cam_lerp, cam_max_step);
if (__absf(dz) > cam_dead) cam_posf.z += __clampf(dz * cam_lerp, cam_max_step);
const float tgt_x = cube_pos.x + vx * cam_lookahead;
const float tgt_y = cube_pos.y - cam_pitch_down + vy * cam_lookahead;
const float tgt_z = cube_pos.z + vz * cam_lookahead;
jo_3d_camera_set_viewpoint(&cam, (int)cam_posf.x, (int)cam_posf.y, (int)cam_posf.z);
jo_3d_camera_set_target(&cam, (int)tgt_x, (int)tgt_y, (int)tgt_z);
cube_prev = cube_pos;
}
static void draw_scene(void){
jo_printf(0, 1, "UP/DOWN: Z LEFT/RIGHT: X L/R: Y");
jo_printf(0, 27, "Cubo : X=%d Y=%d Z=%d ", (int)cube_pos.x, (int)cube_pos.y, (int)cube_pos.z);
jo_printf(0, 28, "Cam : X=%d Y=%d Z=%d ", (int)cam_posf.x, (int)cam_posf.y, (int)cam_posf.z);
jo_printf(0, 29, "Cubo2: X=%d Y=%d Z=%d ", (int)cube2_pos.x, (int)cube2_pos.y, (int)cube2_pos.z);
update_camera_follow();
jo_3d_camera_look_at(&cam);
jo_3d_push_matrix();
jo_3d_rotate_matrix_rad(rot.x, rot.y, rot.z);
jo_3d_translate_matrixf(pos.x, pos.y, pos.z);
jo_background_3d_plane_a_draw(true);
jo_3d_pop_matrix();
jo_3d_push_matrix();
jo_3d_translate_matrix((int)cube_pos.x, (int)cube_pos.y, (int)cube_pos.z);
jo_3d_draw_array(cube_quads, 6);
jo_3d_pop_matrix();
jo_3d_push_matrix();
jo_3d_translate_matrix((int)cube2_pos.x, (int)cube2_pos.y, (int)cube2_pos.z);
jo_3d_draw_array(cube_quads, 6);
jo_3d_pop_matrix();
}
void jo_main(void){
jo_core_init(JO_COLOR_Black);
jo_set_tga_palette_handling(my_tga_palette_handling);
jo_3d_camera_init(&cam);
init_vdp2_floor();
box_tex_id = jo_sprite_add_tga(JO_ROOT_DIR, "BOX.TGA", JO_COLOR_Transparent);
create_textured_cube();
pos.x = 800.0f; pos.y = -32.0f; pos.z = -35.0f;
rot.x = JO_DEG_TO_RAD(90.0f);
rot.y = JO_DEG_TO_RAD(0.0f);
rot.z = JO_DEG_TO_RAD(45.0f);
cube_pos.x = cube_pos.y = cube_pos.z = 0.0f;
cube_prev = cube_pos;
cube2_pos.x = 120.0f; cube2_pos.y = 0.0f; cube2_pos.z = 0.0f;
cam_posf.x = cube_pos.x + cam_offset.x;
cam_posf.y = cube_pos.y + cam_offset.y;
cam_posf.z = cube_pos.z + cam_offset.z;
jo_core_add_callback(my_input);
jo_core_add_callback(draw_scene);
jo_core_run();
}
any hints?