/* d3d_light_demo: 地面 + 旋转立方体 + 点光源环绕 */
#include "eduwin.h"

static dx_mesh *g_box, *g_ground;
static dx_camera g_cam;
static float g_light_angle = 0;

static void on_frame(void)
{
    float px, py, pz;

    /* 点光源在立方体一半高度绕转，立方体自身不动 */
    g_light_angle += 0.02f;
    px = 1.3f * cosf(g_light_angle);
    pz = 1.3f * sinf(g_light_angle);
    py = 0.0f;  /* 立方体高1.2，中点高度 */

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

    dx_set_ambient_light(0.06f, 0.06f, 0.08f, 1); /* 极低环境光，突出点光源 */
    dx_set_point_light(px, py, pz, 1.0f, 0.8f, 0.2f, 2.5f, 2.5f);

    dx_mesh_draw(g_ground);
    dx_mesh_draw(g_box);

    dx_end();
}

int main(void)
{
    if (init_app() != 0) { log_error(L"init失败"); return 1; }
    HWND win = create_window(L"D3D Light Demo", 800, 600);
    if (!win) return 1;
    show_window(win);

    if (!dx_init(win, 800, 600)) return 1;

    g_box    = dx_create_mesh(dx_create_box(1.2f, 1.2f, 1.2f, 0.85f, 0.15f, 0.15f, 1), NULL);
    g_ground = dx_create_mesh(dx_create_plane(8, 8, 0.5f, 0.5f, 0.5f, 1), NULL);
    dx_mesh_set_position(g_box,    0, 0, 0);
    dx_mesh_set_position(g_ground, 0, -0.8f, 0);

    dx_camera_init(&g_cam, 0, 1, -3.5f, 800.0f / 600.0f);

    run_app_frame(on_frame);

    dx_free_geometry(g_box->geometry);    dx_free_mesh(g_box);
    dx_free_geometry(g_ground->geometry); dx_free_mesh(g_ground);
    dx_shutdown();
    return 0;
}
