johannes-fetz / joengine

Jo Engine is an open source 2D and 3D game engine for the Sega Saturn written in C under MIT license
http://jo-engine.org/
MIT License
205 stars 32 forks source link

jo_sphere #79

Open celsowm opened 2 weeks ago

celsowm commented 2 weeks ago

Hi Johannes !

I am trying to create a sphere programmaticaly:

#include <jo/jo.h>

jo_camera cam;
jo_3d_mesh *sphere_mesh;

#define SPHERE_STACKS 10
#define SPHERE_SLICES 10
#define SPHERE_RADIUS 50

void create_sphere_mesh(int stacks, int slices, int radius)
{
    int i, j;
    float theta, phi;
    jo_fixed x, y, z;
    int vertex_index = 0;

    sphere_mesh = jo_3d_create_mesh(stacks * slices);

    for (i = 0; i < stacks; ++i)
    {
        theta = (float)i / (stacks - 1) * JO_PI;
        for (j = 0; j < slices; ++j)
        {
            phi = (float)j / (slices - 1) * JO_PI * 2;
            x = jo_float2fixed(radius * jo_cos(theta) * jo_sin(phi));
            y = jo_float2fixed(radius * jo_sin(theta) * jo_sin(phi));
            z = jo_float2fixed(radius * jo_cos(phi));

            jo_3d_set_mesh_vertice(sphere_mesh, x, y, z, vertex_index++);
        }
    }

    for (i = 0; i < stacks * slices; ++i)
    {
        jo_3d_set_mesh_polygon_color(sphere_mesh, JO_COLOR_Red, i);
    }
}

void my_draw(void)
{
    static short rx, ry, rz;

    jo_printf(12, 1, "*Sphere 3D demo*");

    jo_3d_camera_look_at(&cam);
    jo_3d_push_matrix();
    {
        jo_3d_rotate_matrix(rx, ry, rz);
        rx += 1;
        ry += 2;
        rz += 1;
        jo_3d_mesh_draw(sphere_mesh);
    }
    jo_3d_pop_matrix();

    jo_printf(0, 28, "Polygon count: %d  ", jo_3d_get_polygon_count());
}

void jo_main(void)
{
    jo_core_init(JO_COLOR_Black);
    jo_3d_camera_init(&cam);

    create_sphere_mesh(SPHERE_STACKS, SPHERE_SLICES, SPHERE_RADIUS);

    jo_core_add_callback(my_draw);
    jo_core_run();
}

but the result is empty :(

image

1- What I did wrong? 2- What do you think about a new 3d.c function called "create_sphere" ?

Thanks in advance !

slinga-homebrew commented 2 weeks ago

I am seeing some output on Yabause v0.9.14. Red lines are moving across the screen.

image

slinga-homebrew commented 2 weeks ago

Still note quite right, but I see a set of dots circling a center dot: image

1) in my_draw() move "jo_3d_camera_look_at(&cam);" to jo_main(), right after initing the camera. I don't believe you should be setting the camera every frame? 2) There's definitely an issue with how you are generating x, y, z. I tried something like this (not correct, but seeing output):

void create_sphere_mesh(int stacks, int slices, int radius)
{
    int i, j;
    float theta, phi;
    jo_fixed x, y, z;
    int vertex_index = 0;

    sphere_mesh = jo_3d_create_mesh(stacks * slices);

    for (i = 0; i < stacks; ++i)
    {
        theta = (float)(i / (stacks - 1) * JO_PI);
        jo_fixed theta_fixed = jo_float2fixed(theta);
        for (j = 0; j < slices; ++j)
        {
            phi = (float)j / (slices - 1) * JO_PI * 2;
            jo_fixed phi_fixed = jo_float2fixed(phi);

        x = jo_float2fixed(radius * jo_cosf(theta_fixed >> 16) * jo_sinf(phi_fixed >> 16));
            y = jo_float2fixed(radius * jo_sinf(theta_fixed >> 16) * jo_sinf(phi_fixed >> 16));
            z = jo_float2fixed(radius * jo_cosf(phi_fixed >> 16));

            jo_3d_set_mesh_vertice(sphere_mesh, x, y, z, vertex_index++);
        }
    }

    for (i = 0; i < stacks * slices; ++i)
    {
        jo_3d_set_mesh_polygon_color(sphere_mesh, JO_COLOR_Red, i);
    }
}

Long story short you are mixing floats, ints, and jo_fixed. The bug is there somewhere.

celsowm commented 2 weeks ago

thanks @slinga-homebrew , I hope @johannes-fetz give me some light

johannes-fetz commented 1 day ago

Hi @celsowm I'll look this weekend