blue only
This commit is contained in:
1 parent
1300669643
commit
f69b2b60eb
4 files changed
+5
-305
No files matched your search
+3
-272
@@ -13,46 +13,8 @@
|
|||||||
static int sr_width = 1;
|
static int sr_width = 1;
|
||||||
static int sr_height = 1;
|
static int sr_height = 1;
|
||||||
|
|
||||||
// Render configuration supplied by the application (see renderConst.swift).
|
|
||||||
static raybri_config sr_cfg = { 0 };
|
|
||||||
|
|
||||||
static int sr_ready = 0;
|
static int sr_ready = 0;
|
||||||
|
|
||||||
static Model sr_ground_model = { 0 };
|
|
||||||
static Material sr_ground_material = { 0 };
|
|
||||||
static Material sr_ground_shadow_material = { 0 };
|
|
||||||
|
|
||||||
static Shader sr_main_shader = { 0 };
|
|
||||||
static Shader sr_shadow_shader = { 0 };
|
|
||||||
|
|
||||||
static int sr_shadow_fbo = 0;
|
|
||||||
static unsigned int sr_shadow_color = 0;
|
|
||||||
static unsigned int sr_shadow_depth = 0;
|
|
||||||
|
|
||||||
static Matrix sr_light_view = { 0 };
|
|
||||||
static Matrix sr_light_proj = { 0 };
|
|
||||||
static Vector3 sr_light_pos = { 0.0f, 0.0f, 0.0f };
|
|
||||||
|
|
||||||
// Main shader uniform locations
|
|
||||||
static int loc_light_pos = -1;
|
|
||||||
static int loc_light_color = -1;
|
|
||||||
static int loc_ambient = -1;
|
|
||||||
static int loc_light_enabled = -1;
|
|
||||||
static int loc_emissive = -1;
|
|
||||||
static int loc_albedo_color = -1;
|
|
||||||
static int loc_shadow_map = -1;
|
|
||||||
static int loc_shadow_bias = -1;
|
|
||||||
static int loc_shadow_softness = -1;
|
|
||||||
static int loc_normal_map = -1;
|
|
||||||
static int loc_normal_strength = -1;
|
|
||||||
static int loc_light_mvp = -1;
|
|
||||||
static int loc_is_ground = -1;
|
|
||||||
static int loc_grid_color = -1;
|
|
||||||
static int loc_camera_pos = -1;
|
|
||||||
static int loc_fog_color = -1;
|
|
||||||
static int loc_fog_start = -1;
|
|
||||||
static int loc_fog_end = -1;
|
|
||||||
|
|
||||||
// raylib is built as a static/dynamic library with GLFW baked in, but we never
|
// raylib is built as a static/dynamic library with GLFW baked in, but we never
|
||||||
// call InitWindow(). Instead we render into an OpenGL context owned by the
|
// call InitWindow(). Instead we render into an OpenGL context owned by the
|
||||||
// macOS app, so we must supply the OpenGL entry point loader ourselves.
|
// macOS app, so we must supply the OpenGL entry point loader ourselves.
|
||||||
@@ -67,115 +29,12 @@ static void *sr_gl_loader(const char *name)
|
|||||||
return dlsym(handle, name);
|
return dlsym(handle, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sr_load_shaders(const char *main_vs, const char *main_fs,
|
void raybri_init(void) {
|
||||||
const char *shadow_vs, const char *shadow_fs)
|
//SetTraceLogLevel(LOG_WARNING);
|
||||||
{
|
|
||||||
sr_main_shader = LoadShaderFromMemory(main_vs, main_fs);
|
|
||||||
sr_shadow_shader = LoadShaderFromMemory(shadow_vs, shadow_fs);
|
|
||||||
|
|
||||||
loc_light_pos = GetShaderLocation(sr_main_shader, "u_lightPos");
|
|
||||||
loc_light_color = GetShaderLocation(sr_main_shader, "u_lightColor");
|
|
||||||
loc_ambient = GetShaderLocation(sr_main_shader, "u_ambient");
|
|
||||||
loc_light_enabled = GetShaderLocation(sr_main_shader, "u_lightEnabled");
|
|
||||||
loc_emissive = GetShaderLocation(sr_main_shader, "u_emissive");
|
|
||||||
loc_albedo_color = GetShaderLocation(sr_main_shader, "u_albedoColor");
|
|
||||||
loc_shadow_map = GetShaderLocation(sr_main_shader, "u_shadowMap");
|
|
||||||
loc_shadow_bias = GetShaderLocation(sr_main_shader, "u_shadowBias");
|
|
||||||
loc_shadow_softness = GetShaderLocation(sr_main_shader, "u_shadowSoftness");
|
|
||||||
loc_normal_map = GetShaderLocation(sr_main_shader, "u_normalMap");
|
|
||||||
loc_normal_strength = GetShaderLocation(sr_main_shader, "u_normalStrength");
|
|
||||||
loc_light_mvp = GetShaderLocation(sr_main_shader, "u_lightMVP");
|
|
||||||
loc_is_ground = GetShaderLocation(sr_main_shader, "u_isGround");
|
|
||||||
loc_grid_color = GetShaderLocation(sr_main_shader, "u_gridColor");
|
|
||||||
loc_camera_pos = GetShaderLocation(sr_main_shader, "u_cameraPos");
|
|
||||||
loc_fog_color = GetShaderLocation(sr_main_shader, "u_fogColor");
|
|
||||||
loc_fog_start = GetShaderLocation(sr_main_shader, "u_fogStart");
|
|
||||||
loc_fog_end = GetShaderLocation(sr_main_shader, "u_fogEnd");
|
|
||||||
|
|
||||||
// Build a framebuffer whose depth attachment is a sampleable TEXTURE (raylib's
|
|
||||||
// LoadRenderTexture() uses a non-sampleable renderbuffer, so we build it by hand).
|
|
||||||
sr_shadow_fbo = rlLoadFramebuffer();
|
|
||||||
sr_shadow_color = rlLoadTexture(NULL, sr_cfg.shadow_size, sr_cfg.shadow_size, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
|
|
||||||
sr_shadow_depth = rlLoadTextureDepth(sr_cfg.shadow_size, sr_cfg.shadow_size, false);
|
|
||||||
|
|
||||||
rlEnableFramebuffer(sr_shadow_fbo);
|
|
||||||
rlFramebufferAttach(sr_shadow_fbo, sr_shadow_color, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
|
|
||||||
rlFramebufferAttach(sr_shadow_fbo, sr_shadow_depth, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
|
|
||||||
int shadow_ok = rlFramebufferComplete(sr_shadow_fbo);
|
|
||||||
fprintf(stderr, "SR: shadow framebuffer %s\n", shadow_ok ? "complete" : "INCOMPLETE");
|
|
||||||
rlDisableFramebuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void sr_build_ground(void)
|
|
||||||
{
|
|
||||||
Mesh ground_mesh = GenMeshPlane(sr_cfg.ground_size, sr_cfg.ground_size, 1, 1);
|
|
||||||
sr_ground_model = LoadModelFromMesh(ground_mesh);
|
|
||||||
sr_ground_material = LoadMaterialDefault();
|
|
||||||
sr_ground_material.shader = sr_main_shader;
|
|
||||||
sr_ground_shadow_material = LoadMaterialDefault();
|
|
||||||
sr_ground_shadow_material.shader = sr_shadow_shader;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uniforms that never change between frames are uploaded once at startup.
|
|
||||||
// SetShaderValue() is a bare glUniform*(), so the program must be bound.
|
|
||||||
static void sr_config_shaders(void)
|
|
||||||
{
|
|
||||||
sr_light_pos = (Vector3){ 0.0f, 4.2f, 0.0f };
|
|
||||||
|
|
||||||
Vector3 light_color = { 1.15f, 1.08f, 0.98f };
|
|
||||||
Vector3 ambient = { 0.30f, 0.32f, 0.38f };
|
|
||||||
Vector3 grid_color = { 0.45f, 0.62f, 0.95f };
|
|
||||||
Vector3 fog_color = { 24.0f / 255.0f, 26.0f / 255.0f, 34.0f / 255.0f };
|
|
||||||
Vector3 camera_pos = { 0.0f, 2.6f, 5.6f };
|
|
||||||
float albedo[3] = { 1.0f, 1.0f, 1.0f };
|
|
||||||
float light_amount = 1.0f;
|
|
||||||
float shadow_bias = 0.0025f;
|
|
||||||
float shadow_softness = 4.0f;
|
|
||||||
float fog_start = 10.0f;
|
|
||||||
float fog_end = 34.0f;
|
|
||||||
int shadow_unit = 1;
|
|
||||||
int normal_unit = 2;
|
|
||||||
|
|
||||||
rlEnableShader(sr_main_shader.id);
|
|
||||||
|
|
||||||
SetShaderValue(sr_main_shader, loc_light_pos, &sr_light_pos, SHADER_UNIFORM_VEC3);
|
|
||||||
SetShaderValue(sr_main_shader, loc_light_color, &light_color, SHADER_UNIFORM_VEC3);
|
|
||||||
SetShaderValue(sr_main_shader, loc_ambient, &ambient, SHADER_UNIFORM_VEC3);
|
|
||||||
SetShaderValue(sr_main_shader, loc_light_enabled, &light_amount, SHADER_UNIFORM_FLOAT);
|
|
||||||
SetShaderValue(sr_main_shader, loc_shadow_bias, &shadow_bias, SHADER_UNIFORM_FLOAT);
|
|
||||||
SetShaderValue(sr_main_shader, loc_shadow_softness, &shadow_softness, SHADER_UNIFORM_FLOAT);
|
|
||||||
SetShaderValue(sr_main_shader, loc_grid_color, &grid_color, SHADER_UNIFORM_VEC3);
|
|
||||||
SetShaderValue(sr_main_shader, loc_camera_pos, &camera_pos, SHADER_UNIFORM_VEC3);
|
|
||||||
SetShaderValue(sr_main_shader, loc_fog_color, &fog_color, SHADER_UNIFORM_VEC3);
|
|
||||||
SetShaderValue(sr_main_shader, loc_fog_start, &fog_start, SHADER_UNIFORM_FLOAT);
|
|
||||||
SetShaderValue(sr_main_shader, loc_fog_end, &fog_end, SHADER_UNIFORM_FLOAT);
|
|
||||||
SetShaderValue(sr_main_shader, loc_albedo_color, albedo, SHADER_UNIFORM_VEC3);
|
|
||||||
SetShaderValue(sr_main_shader, loc_shadow_map, &shadow_unit, SHADER_UNIFORM_INT);
|
|
||||||
SetShaderValue(sr_main_shader, loc_normal_map, &normal_unit, SHADER_UNIFORM_INT);
|
|
||||||
|
|
||||||
rlDisableShader();
|
|
||||||
}
|
|
||||||
|
|
||||||
void raybri_init(
|
|
||||||
const char *main_vs,
|
|
||||||
const char *main_fs,
|
|
||||||
const char *shadow_vs,
|
|
||||||
const char *shadow_fs,
|
|
||||||
raybri_config config
|
|
||||||
) {
|
|
||||||
sr_cfg = config;
|
|
||||||
|
|
||||||
// raylib's default INFO logging is noisy; keep warnings and errors only.
|
|
||||||
SetTraceLogLevel(LOG_WARNING);
|
|
||||||
|
|
||||||
rlLoadExtensions((void *)sr_gl_loader);
|
rlLoadExtensions((void *)sr_gl_loader);
|
||||||
// Init with default 1x1 size
|
// Init with default 1x1 size
|
||||||
rlglInit(1, 1);
|
rlglInit(1, 1);
|
||||||
|
|
||||||
sr_load_shaders(main_vs, main_fs, shadow_vs, shadow_fs);
|
|
||||||
sr_config_shaders();
|
|
||||||
sr_build_ground();
|
|
||||||
|
|
||||||
sr_ready = 1;
|
sr_ready = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,9 +44,6 @@ void raybri_resize(int width, int height)
|
|||||||
if (height > 0) sr_height = height;
|
if (height > 0) sr_height = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sr_draw_object(Model model, Material material, Matrix model_mat,
|
|
||||||
Texture2D normal, float is_ground, float emissive);
|
|
||||||
|
|
||||||
static void sr_render_main_pass(void)
|
static void sr_render_main_pass(void)
|
||||||
{
|
{
|
||||||
// --- Main pass: camera ---
|
// --- Main pass: camera ---
|
||||||
@@ -195,118 +51,8 @@ static void sr_render_main_pass(void)
|
|||||||
rlSetFramebufferWidth(sr_width);
|
rlSetFramebufferWidth(sr_width);
|
||||||
rlSetFramebufferHeight(sr_height);
|
rlSetFramebufferHeight(sr_height);
|
||||||
|
|
||||||
rlClearColor(24, 26, 34, 255);
|
rlClearColor(0, 0, 255, 255);
|
||||||
rlClearScreenBuffers();
|
rlClearScreenBuffers();
|
||||||
|
|
||||||
rlMatrixMode(RL_PROJECTION);
|
|
||||||
rlPushMatrix();
|
|
||||||
rlLoadIdentity();
|
|
||||||
|
|
||||||
float aspect = (float)sr_width / (float)sr_height;
|
|
||||||
double top = (double)sr_cfg.near_plane * tan((double)sr_cfg.fov_y * 0.5 * DEG2RAD);
|
|
||||||
double right = top * (double)aspect;
|
|
||||||
rlFrustum(-right, right, -top, top, (double)sr_cfg.near_plane, (double)sr_cfg.far_plane);
|
|
||||||
|
|
||||||
rlMatrixMode(RL_MODELVIEW);
|
|
||||||
rlLoadIdentity();
|
|
||||||
|
|
||||||
Vector3 eye = { 0.0f, 2.6f, 5.6f };
|
|
||||||
Vector3 target = { 0.0f, 1.0f, 0.0f };
|
|
||||||
Vector3 up = { 0.0f, 1.0f, 0.0f };
|
|
||||||
|
|
||||||
Matrix view = MatrixLookAt(eye, target, up);
|
|
||||||
rlMultMatrixf(MatrixToFloat(view));
|
|
||||||
|
|
||||||
rlEnableDepthTest();
|
|
||||||
rlEnableBackfaceCulling();
|
|
||||||
rlSetCullFace(RL_CULL_FACE_BACK);
|
|
||||||
|
|
||||||
rlEnableShader(sr_main_shader.id);
|
|
||||||
|
|
||||||
rlActiveTextureSlot(1);
|
|
||||||
rlEnableTexture(sr_shadow_depth);
|
|
||||||
rlActiveTextureSlot(0);
|
|
||||||
|
|
||||||
// --- Ground (procedural grid floor) ---
|
|
||||||
sr_draw_object(sr_ground_model, sr_ground_material, MatrixTranslate(0.0f, 0.0f, 0.0f), (Texture2D){ 0 }, 1.0f, 0.0f);
|
|
||||||
|
|
||||||
// Flush while the projection/modelview matrices are still active, otherwise
|
|
||||||
// rlgl draws the pending batch with the restored (2D) projection.
|
|
||||||
rlDrawRenderBatchActive();
|
|
||||||
|
|
||||||
// --- Restore matrices (equivalent to raylib's EndMode3D) ---
|
|
||||||
rlMatrixMode(RL_PROJECTION);
|
|
||||||
rlPopMatrix();
|
|
||||||
rlMatrixMode(RL_MODELVIEW);
|
|
||||||
rlLoadIdentity();
|
|
||||||
rlDisableDepthTest();
|
|
||||||
rlDisableBackfaceCulling();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void sr_render_shadow_pass(void)
|
|
||||||
{
|
|
||||||
rlViewport(0, 0, sr_cfg.shadow_size, sr_cfg.shadow_size);
|
|
||||||
rlSetFramebufferWidth(sr_cfg.shadow_size);
|
|
||||||
rlSetFramebufferHeight(sr_cfg.shadow_size);
|
|
||||||
|
|
||||||
rlEnableFramebuffer(sr_shadow_fbo);
|
|
||||||
|
|
||||||
rlClearColor(255, 255, 255, 255);
|
|
||||||
rlClearScreenBuffers();
|
|
||||||
|
|
||||||
rlEnableDepthTest();
|
|
||||||
rlEnableBackfaceCulling();
|
|
||||||
rlSetCullFace(RL_CULL_FACE_FRONT);
|
|
||||||
|
|
||||||
rlMatrixMode(RL_PROJECTION);
|
|
||||||
rlPushMatrix();
|
|
||||||
rlLoadIdentity();
|
|
||||||
rlMultMatrixf(MatrixToFloat(sr_light_proj));
|
|
||||||
|
|
||||||
rlMatrixMode(RL_MODELVIEW);
|
|
||||||
rlLoadIdentity();
|
|
||||||
rlMultMatrixf(MatrixToFloat(sr_light_view));
|
|
||||||
|
|
||||||
rlDrawRenderBatchActive();
|
|
||||||
|
|
||||||
rlMatrixMode(RL_PROJECTION);
|
|
||||||
rlPopMatrix();
|
|
||||||
|
|
||||||
rlSetCullFace(RL_CULL_FACE_BACK);
|
|
||||||
rlDisableFramebuffer();
|
|
||||||
|
|
||||||
rlViewport(0, 0, sr_width, sr_height);
|
|
||||||
rlSetFramebufferWidth(sr_width);
|
|
||||||
rlSetFramebufferHeight(sr_height);
|
|
||||||
rlDisableDepthTest();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void sr_set_light_mvp(Matrix model)
|
|
||||||
{
|
|
||||||
Matrix light_mvp = MatrixMultiply(MatrixMultiply(model, sr_light_view), sr_light_proj);
|
|
||||||
SetShaderValueMatrix(sr_main_shader, loc_light_mvp, light_mvp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void sr_bind_normal(Texture2D tex)
|
|
||||||
{
|
|
||||||
rlActiveTextureSlot(2);
|
|
||||||
if (tex.id > 0) rlEnableTexture(tex.id);
|
|
||||||
rlActiveTextureSlot(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void sr_draw_object(Model model, Material material, Matrix model_mat,
|
|
||||||
Texture2D normal, float is_ground, float emissive)
|
|
||||||
{
|
|
||||||
sr_set_light_mvp(model_mat);
|
|
||||||
|
|
||||||
float normal_strength = (normal.id > 0) ? 1.0f : 0.0f;
|
|
||||||
SetShaderValue(sr_main_shader, loc_emissive, &emissive, SHADER_UNIFORM_FLOAT);
|
|
||||||
SetShaderValue(sr_main_shader, loc_is_ground, &is_ground, SHADER_UNIFORM_FLOAT);
|
|
||||||
SetShaderValue(sr_main_shader, loc_normal_strength, &normal_strength, SHADER_UNIFORM_FLOAT);
|
|
||||||
|
|
||||||
sr_bind_normal(normal);
|
|
||||||
|
|
||||||
DrawMesh(model.meshes[0], material, model_mat);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void raybri_render(void)
|
void raybri_render(void)
|
||||||
@@ -315,13 +61,6 @@ void raybri_render(void)
|
|||||||
|
|
||||||
rlDrawRenderBatchActive();
|
rlDrawRenderBatchActive();
|
||||||
|
|
||||||
// --- Light (point light above the scene) ---
|
|
||||||
Vector3 light_target = (Vector3){ 0.0f, -2.0f, 0.0f };
|
|
||||||
Vector3 light_up = (Vector3){ 0.0f, 0.0f, -1.0f };
|
|
||||||
sr_light_view = MatrixLookAt(sr_light_pos, light_target, light_up);
|
|
||||||
sr_light_proj = MatrixPerspective(90.0 * DEG2RAD, 1.0, 1.5, 16.0);
|
|
||||||
|
|
||||||
sr_render_shadow_pass();
|
|
||||||
sr_render_main_pass();
|
sr_render_main_pass();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,14 +68,6 @@ void raybri_shutdown(void)
|
|||||||
{
|
{
|
||||||
if (!sr_ready) return;
|
if (!sr_ready) return;
|
||||||
|
|
||||||
UnloadModel(sr_ground_model);
|
|
||||||
|
|
||||||
if (sr_shadow_color > 0) rlUnloadTexture(sr_shadow_color);
|
|
||||||
if (sr_shadow_depth > 0) rlUnloadTexture(sr_shadow_depth);
|
|
||||||
if (sr_shadow_fbo > 0) rlUnloadFramebuffer(sr_shadow_fbo);
|
|
||||||
if (sr_main_shader.id > 0) UnloadShader(sr_main_shader);
|
|
||||||
if (sr_shadow_shader.id > 0) UnloadShader(sr_shadow_shader);
|
|
||||||
|
|
||||||
rlglClose();
|
rlglClose();
|
||||||
sr_ready = 0;
|
sr_ready = 0;
|
||||||
}
|
}
|
||||||
@@ -5,24 +5,9 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Render configuration, sourced from renderConst.swift.
|
|
||||||
typedef struct raybri_config {
|
|
||||||
float fov_y; // vertical field of view (degrees)
|
|
||||||
float near_plane; // camera near plane
|
|
||||||
float far_plane; // camera far plane
|
|
||||||
int shadow_size; // shadow map resolution (px)
|
|
||||||
float ground_size; // ground plane extent
|
|
||||||
} raybri_config;
|
|
||||||
|
|
||||||
// Initialise the OpenGL-backed raylib scene (no raylib window is created).
|
// Initialise the OpenGL-backed raylib scene (no raylib window is created).
|
||||||
// The four shader sources come from renderConstShader.swift.
|
// The four shader sources come from renderConstShader.swift.
|
||||||
void raybri_init(
|
void raybri_init(void);
|
||||||
const char *main_vs,
|
|
||||||
const char *main_fs,
|
|
||||||
const char *shadow_vs,
|
|
||||||
const char *shadow_fs,
|
|
||||||
raybri_config config
|
|
||||||
);
|
|
||||||
|
|
||||||
// Resize the viewport.
|
// Resize the viewport.
|
||||||
void raybri_resize(int width, int height);
|
void raybri_resize(int width, int height);
|
||||||
|
|||||||
@@ -1,13 +1,4 @@
|
|||||||
import AppKit
|
import AppKit
|
||||||
import RaylibBridge
|
|
||||||
|
|
||||||
let RENDER_CONFIG = raybri_config(
|
|
||||||
fov_y: 45.0,
|
|
||||||
near_plane: 0.1,
|
|
||||||
far_plane: 1000.0,
|
|
||||||
shadow_size: 256,
|
|
||||||
ground_size: 40.0
|
|
||||||
)
|
|
||||||
|
|
||||||
let RENDER_FPS = 60.0
|
let RENDER_FPS = 60.0
|
||||||
|
|
||||||
|
|||||||
@@ -16,14 +16,7 @@ func renderInit(_ vm: RenderVM) {
|
|||||||
var swapInterval: Int32 = 1
|
var swapInterval: Int32 = 1
|
||||||
vm.glView.openGLContext?.setValues(&swapInterval, for: .swapInterval)
|
vm.glView.openGLContext?.setValues(&swapInterval, for: .swapInterval)
|
||||||
|
|
||||||
// Init Raylib
|
raybri_init()
|
||||||
raybri_init(
|
|
||||||
RENDER_SHADER_MAIN_VS,
|
|
||||||
RENDER_SHADER_MAIN_FS,
|
|
||||||
RENDER_SHADER_SHADOW_VS,
|
|
||||||
RENDER_SHADER_SHADOW_FS,
|
|
||||||
RENDER_CONFIG
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderStartTimer(_ vm: RenderVM) {
|
func renderStartTimer(_ vm: RenderVM) {
|
||||||
|
|||||||
Reference in new issue
Block a user