/* d3d_shape_demo: 多种基本体旋转展示 + 光照 */
#include "eduwin.h"

#define NUM_OBJS 4
static dx_geometry *g_geo[NUM_OBJS+1];
static dx_mesh *g_objs[NUM_OBJS+1];
static float g_rots[NUM_OBJS];
static dx_camera g_cam;

static void on_frame(void)
{
    int i;
    for (i = 0; i < NUM_OBJS; i++) {
        g_rots[i] += (i == 0) ? 0.016f : (i == 1) ? 0.024f : (i == 2) ? 0.02f : 0.032f;
        g_objs[i+1]->rotation[1] = g_rots[i];
    }

    dx_camera_update(&g_cam);
    dx_begin(0.15f, 0.15f, 0.2f, 1);

    for (i = 0; i <= NUM_OBJS; i++)
        dx_mesh_draw(g_objs[i]);

    dx_end();
}

int main(void)
{
    int i;
    if (init_app() != 0) { log_error(L"init失败"); return 1; }
    HWND win = create_window(L"3D 基本体展示", 800, 600);
    if (!win) return 1;
    show_window(win);
    if (!dx_init(win, 800, 600)) return 1;

    dx_camera_init(&g_cam, 0, 3, -6, 800.0f / 600.0f);

    /* 光照 */
    dx_set_ambient_light(0.5f, 0.5f, 0.5f, 1);
    dx_set_directional_light(0.3f, -1, 0.5f, 1, 1, 1, 2.0f);
    dx_set_point_light(1.5f, 3, 2, 1, 0.8f, 0.6f, 1.5f, 6);

    /* 地面 + 四个基本体 */
    g_geo[0] = dx_create_plane(6, 6, 0.3f, 0.3f, 0.3f, 1);
    g_geo[1] = dx_create_box(1.0f, 1.0f, 1.0f, 0.9f, 0.2f, 0.2f, 1);
    g_geo[2] = dx_create_sphere(0.6f, 0.2f, 0.6f, 0.2f, 1);
    g_geo[3] = dx_create_cylinder(0.5f, 1.0f, 0.2f, 0.2f, 0.8f, 1);
    g_geo[4] = dx_create_pyramid(0.8f, 0.8f, 0.8f, 0.8f, 0.6f, 0.2f, 1);

    for (i = 0; i <= NUM_OBJS; i++)
        g_objs[i] = dx_create_mesh(g_geo[i], NULL);

    dx_mesh_set_position(g_objs[0], 0, -1.5f, 0);
    dx_mesh_set_position(g_objs[1], -2.0f, 0.5f, 0);
    dx_mesh_set_position(g_objs[2],  2.0f, 0.5f, 0);
    dx_mesh_set_position(g_objs[3],  0, 2.0f, 0);
    dx_mesh_set_position(g_objs[4],  0, -1.0f, 0);

    run_app_frame(on_frame);

    for (i = 0; i <= NUM_OBJS; i++) {
        dx_free_geometry(g_geo[i]);
        dx_free_mesh(g_objs[i]);
    }
    dx_shutdown();
    return 0;
}
