fix: .gitignore + add 44 language JSON files

This commit is contained in:
shuiyu1123 committed 2026-07-14 01:17:33 +08:00
1 parent e8fd68e27b
commit 8de545dd2c
227 files changed
+22603

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
#include "camera_controller.h"
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846f
#endif
CameraController camera_controller_create(Camera* camera) {
CameraController ctrl = {0};
ctrl.camera = camera;
ctrl.mode = CAMERA_MODE_FLY;
ctrl.move_speed = 10.0f;
ctrl.mouse_sensitivity = 0.003f;
ctrl.height_offset = 1.75f;
ctrl.pitch_min = -1.45f;
ctrl.pitch_max = 1.45f;
ctrl.yaw = 0.0f;
ctrl.pitch = 0.0f;
ctrl.ground_height_fn = NULL;
ctrl.ground_user = NULL;
return ctrl;
}
void camera_controller_set_ground_callback(CameraController* ctrl,
float (*get_height)(float x, float z, void* user),
void* user) {
ctrl->ground_height_fn = get_height;
ctrl->ground_user = user;
}
void camera_controller_set_mode(CameraController* ctrl, CameraMode mode) {
ctrl->mode = mode;
}
void camera_controller_look(CameraController* ctrl, float delta_x, float delta_y) {
ctrl->yaw -= delta_x * ctrl->mouse_sensitivity;
ctrl->pitch -= delta_y * ctrl->mouse_sensitivity;
if (ctrl->pitch < ctrl->pitch_min) ctrl->pitch = ctrl->pitch_min;
if (ctrl->pitch > ctrl->pitch_max) ctrl->pitch = ctrl->pitch_max;
// 重建 target
Vec3 forward;
forward.x = cosf(ctrl->pitch) * sinf(ctrl->yaw);
forward.y = sinf(ctrl->pitch);
forward.z = cosf(ctrl->pitch) * cosf(ctrl->yaw);
vec3_normalize(&forward, &forward);
vec3_add(&ctrl->camera->position, &forward, &ctrl->camera->target);
ctrl->camera->dirty = 1;
}
void camera_controller_move(CameraController* ctrl, Vec3 dir, float dt) {
Camera* cam = ctrl->camera;
Vec3 forward, right, up;
vec3_sub(&cam->target, &cam->position, &forward);
forward.y = 0.0f;
vec3_normalize(&forward, &forward);
vec3_cross(&cam->up, &forward, &right);
vec3_normalize(&right, &right);
up = cam->up;
Vec3 move = {0};
Vec3 tmp;
vec3_scale(&right, dir.x, &tmp);
vec3_add(&move, &tmp, &move);
vec3_scale(&up, dir.y, &tmp);
vec3_add(&move, &tmp, &move);
vec3_scale(&forward, dir.z, &tmp);
vec3_add(&move, &tmp, &move);
float len = vec3_length(&move);
if (len > 0.0001f) {
vec3_scale(&move, ctrl->move_speed * dt / len, &move);
vec3_add(&cam->position, &move, &cam->position);
vec3_add(&cam->target, &move, &cam->target);
cam->dirty = 1;
}
}
void camera_controller_update(CameraController* ctrl, float dt) {
(void)dt;
if (ctrl->mode != CAMERA_MODE_WALK) return;
if (!ctrl->ground_height_fn) return;
float ground = ctrl->ground_height_fn(ctrl->camera->position.x, ctrl->camera->position.z, ctrl->ground_user);
float desired_y = ground + ctrl->height_offset;
float delta = desired_y - ctrl->camera->position.y;
if (fabsf(delta) > 0.01f) {
Vec3 move = {0, delta, 0};
vec3_add(&ctrl->camera->position, &move, &ctrl->camera->position);
vec3_add(&ctrl->camera->target, &move, &ctrl->camera->target);
ctrl->camera->dirty = 1;
}
}
+44
View File
@@ -0,0 +1,44 @@
#pragma once
// ============================================================================
// camera_controller.h - 增强相机控制器(飞行 / 步行模式)
// ============================================================================
#include "camera.h"
#include "../ASM/math_c_fallback.h"
typedef enum {
CAMERA_MODE_FLY,
CAMERA_MODE_WALK,
} CameraMode;
typedef struct {
Camera* camera;
CameraMode mode;
float move_speed;
float mouse_sensitivity;
float height_offset; // 步行模式眼睛离地面高度
float pitch_min;
float pitch_max;
float yaw;
float pitch;
float (*ground_height_fn)(float x, float z, void* user);
void* ground_user;
} CameraController;
CameraController camera_controller_create(Camera* camera);
// 更新 yaw/pitch,鼠标输入以 delta 像素为单位
void camera_controller_look(CameraController* ctrl, float delta_x, float delta_y);
// 移动:dir 为本地空间方向 (x=右, y=上, z=前)
void camera_controller_move(CameraController* ctrl, Vec3 dir, float dt);
// 设置地形高度采样回调
void camera_controller_set_ground_callback(CameraController* ctrl, float (*get_height)(float x, float z, void* user), void* user);
// 切换模式
void camera_controller_set_mode(CameraController* ctrl, CameraMode mode);
// 每帧应用约束(如步行贴地)
void camera_controller_update(CameraController* ctrl, float dt);
+60
View File
@@ -0,0 +1,60 @@
#include "frustum.h"
#include <math.h>
void frustum_from_matrix(const Mat4* vp, Vec4 planes[6]) {
// 行主序存储:clip = vec4(pos,1) * vp
// 因此 clip 分量 = dot(pos, vp 的列)
// left = col3 + col0
// right = col3 - col0
// bottom = col3 + col1
// top = col3 - col1
// near = col3 + col2
// far = col3 - col2
planes[0] = (Vec4){ vp->m[0][3] + vp->m[0][0],
vp->m[1][3] + vp->m[1][0],
vp->m[2][3] + vp->m[2][0],
vp->m[3][3] + vp->m[3][0] };
planes[1] = (Vec4){ vp->m[0][3] - vp->m[0][0],
vp->m[1][3] - vp->m[1][0],
vp->m[2][3] - vp->m[2][0],
vp->m[3][3] - vp->m[3][0] };
planes[2] = (Vec4){ vp->m[0][3] + vp->m[0][1],
vp->m[1][3] + vp->m[1][1],
vp->m[2][3] + vp->m[2][1],
vp->m[3][3] + vp->m[3][1] };
planes[3] = (Vec4){ vp->m[0][3] - vp->m[0][1],
vp->m[1][3] - vp->m[1][1],
vp->m[2][3] - vp->m[2][1],
vp->m[3][3] - vp->m[3][1] };
planes[4] = (Vec4){ vp->m[0][3] + vp->m[0][2],
vp->m[1][3] + vp->m[1][2],
vp->m[2][3] + vp->m[2][2],
vp->m[3][3] + vp->m[3][2] };
planes[5] = (Vec4){ vp->m[0][3] - vp->m[0][2],
vp->m[1][3] - vp->m[1][2],
vp->m[2][3] - vp->m[2][2],
vp->m[3][3] - vp->m[3][2] };
for (int i = 0; i < 6; i++) {
float len = sqrtf(planes[i].x * planes[i].x +
planes[i].y * planes[i].y +
planes[i].z * planes[i].z);
if (len > 0.0001f) {
planes[i].x /= len;
planes[i].y /= len;
planes[i].z /= len;
planes[i].w /= len;
}
}
}
int frustum_intersects_sphere(const Vec4 planes[6], Vec3 center, float radius) {
for (int i = 0; i < 6; i++) {
float dist = planes[i].x * center.x +
planes[i].y * center.y +
planes[i].z * center.z +
planes[i].w;
if (dist < -radius) return 0;
}
return 1;
}
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include "../ASM/math_c_fallback.h"
// 从视图*投影矩阵提取 6 个视锥平面(行主序,planes[0..5] = left, right, bottom, top, near, far)
void frustum_from_matrix(const Mat4* vp, Vec4 planes[6]);
// 判断球体是否与视锥相交(1=可见/相交,0=完全在外)
int frustum_intersects_sphere(const Vec4 planes[6], Vec3 center, float radius);
+26
View File
@@ -0,0 +1,26 @@
#include "lighting.h"
#include <stdlib.h>
#include <string.h>
Light* light_create_dir(float r, float g, float b, float intensity, float dx, float dy, float dz) {
Light* l = (Light*)calloc(1, sizeof(Light));
l->type = LIGHT_DIR;
l->color[0] = r; l->color[1] = g; l->color[2] = b;
l->intensity = intensity;
l->direction[0] = dx; l->direction[1] = dy; l->direction[2] = dz;
float len = sqrtf(dx*dx + dy*dy + dz*dz);
if (len > 0.0001f) { l->direction[0] /= len; l->direction[1] /= len; l->direction[2] /= len; }
return l;
}
Light* light_create_point(float r, float g, float b, float intensity, float x, float y, float z, float range) {
Light* l = (Light*)calloc(1, sizeof(Light));
l->type = LIGHT_POINT;
l->color[0] = r; l->color[1] = g; l->color[2] = b;
l->intensity = intensity;
l->position[0] = x; l->position[1] = y; l->position[2] = z;
l->range = range;
return l;
}
void light_destroy(Light* light) { free(light); }
+17
View File
@@ -0,0 +1,17 @@
// lighting.h
#pragma once
typedef enum { LIGHT_DIR=0, LIGHT_POINT } LightType;
typedef struct {
LightType type;
float color[3];
float intensity;
float direction[3];
float position[3];
float range;
} Light;
Light* light_create_dir(float r, float g, float b, float intensity, float dx, float dy, float dz);
Light* light_create_point(float r, float g, float b, float intensity, float x, float y, float z, float range);
void light_destroy(Light* light);
+15
View File
@@ -0,0 +1,15 @@
#include "material.h"
#include <stdlib.h>
Material* material_create(Shader* shader, Texture* texture) {
Material* m = (Material*)calloc(1, sizeof(Material));
m->shader = shader;
m->texture = texture;
m->color = (Vec4){1.0f, 1.0f, 1.0f, 1.0f};
m->use_texture = (texture != NULL) ? 1 : 0;
return m;
}
void material_destroy(Material* m) {
free(m);
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include "shader.h"
#include "texture.h"
#include "../ASM/math_c_fallback.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
Shader* shader;
Texture* texture;
Vec4 color;
int use_texture;
} Material;
Material* material_create(Shader* shader, Texture* texture);
void material_destroy(Material* m);
#ifdef __cplusplus
}
#endif