This commit is contained in:
Михаил Капелько committed 2026-10-22 15:37:33 +03:00
1 parent 7a0a09c2a1
commit 0af2d75754
18 files changed
+509 -37

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
#ifndef OTHER_BRIDGING_HEADER_H
#define OTHER_BRIDGING_HEADER_H
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#endif
+16 -1
View File
@@ -4,12 +4,15 @@
#include "rlgl.h"
#include "raymath.h"
#include <dlfcn.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#if !defined(PLATFORM_IOS)
#include <dlfcn.h>
#endif
static int sr_width = 1;
static int sr_height = 1;
@@ -65,6 +68,7 @@ static int loc_fog_color = -1;
static int loc_fog_start = -1;
static int loc_fog_end = -1;
#if !defined(PLATFORM_IOS)
// 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
// macOS app, so we must supply the OpenGL entry point loader ourselves.
@@ -78,6 +82,7 @@ static void *sr_gl_loader(const char *name)
if (handle == NULL) return NULL;
return dlsym(handle, name);
}
#endif
// Compose a model transform exactly like raylib's DrawModel() does, so the
// matrices we feed to DrawMesh() match what rlgl computes internally.
@@ -190,9 +195,14 @@ void raybri_init(
// raylib's default INFO logging is noisy; keep warnings and errors only.
SetTraceLogLevel(LOG_WARNING);
#if !defined(PLATFORM_IOS)
rlLoadExtensions((void *)sr_gl_loader);
// Init with default 1x1 size
rlglInit(1, 1);
#else
// On iOS the prebuilt raylib (ghera/raylib-iOS) already calls
// rlLoadExtensions() and rlglInit() inside InitWindow(); skip here.
#endif
sr_load_shaders(main_vs, main_fs, shadow_vs, shadow_fs);
sr_config_shaders();
@@ -216,6 +226,9 @@ void raybri_load_model(const char *gltf_path)
if (sr_has_model)
{
fprintf(stderr, "SR: model loaded: %s (%d meshes, %d materials)\n",
gltf_path, sr_model.meshCount, sr_model.materialCount);
int cube_mat_index = (sr_model.materialCount > 0) ? sr_model.meshMaterial[0] : 0;
sr_cube_material = sr_model.materials[cube_mat_index];
sr_cube_material.shader = sr_main_shader;
@@ -435,6 +448,8 @@ void raybri_shutdown(void)
if (sr_main_shader.id > 0) UnloadShader(sr_main_shader);
if (sr_shadow_shader.id > 0) UnloadShader(sr_shadow_shader);
#if !defined(PLATFORM_IOS)
rlglClose();
#endif
sr_ready = 0;
}
+55
View File
@@ -0,0 +1,55 @@
import SwiftUI
import UIKit
// The prebuilt raylib iOS framework owns the app entry point (main()/
// AppDelegate/SceneDelegate in rcore_ios.c), so our SwiftUI @main shell is
// not used on iOS. Instead the C bridge calls rcios_present_overlay() from
// ios_ready(), and we stack a transparent window hosting ChkgfxView above
// raylib's window.
//
// ChkgfxComponent/ChkgfxVM live here because the component lifecycle is
// otherwise driven by the (iOS-unavailable) SwiftUI app shell.
enum RenderIOS {
static let chkgfx = ChkgfxComponent()
static let chkgfxVM = ChkgfxVM()
// Inert on iOS (the raylib framework drives the scene), but the generated
// dialect refers to them, so keep them alive.
static let render = RenderComponent()
static let renderVM = RenderVM()
}
@_cdecl("rcios_present_overlay")
public func rciosPresentOverlay() {
otherSetupConsoleLogging(RenderIOS.chkgfx.ctrl, "Chkgfx")
RenderIOS.chkgfx.setup()
DispatchQueue.main.async {
guard let scene = UIApplication.shared.connectedScenes
.compactMap({ $0 as? UIWindowScene })
.first(where: { $0.activationState == .foregroundActive })
?? UIApplication.shared.connectedScenes
.compactMap({ $0 as? UIWindowScene }).first
else { return }
let root = ChkgfxView(RenderIOS.chkgfxVM)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
let hosting = UIHostingController(rootView: root)
hosting.view.backgroundColor = .clear
let overlay = UIWindow(windowScene: scene)
overlay.rootViewController = hosting
overlay.backgroundColor = .clear
overlay.windowLevel = .alert + 1
overlay.isUserInteractionEnabled = false
overlay.isHidden = false
OverlayWindow.retained = overlay
}
}
// UIWindow is not retained by UIKit once we drop our reference, so hold it.
private enum OverlayWindow {
static var retained: UIWindow?
}
@@ -0,0 +1,157 @@
#ifndef OGS_CHKGFX_RENDER_CONST_SHADER_IOS_H
#define OGS_CHKGFX_RENDER_CONST_SHADER_IOS_H
// OpenGL ES 3.0 (GLSL ES 3.00) shaders for the iOS build. They mirror the
// macOS GLSL 150 sources in renderConstShader.swift, with the desktop-only
// bits replaced:
// - "#version 150" -> "#version 300 es"
// - fragment shaders need an explicit float precision qualifier
// raylib's LoadShaderFromMemory() passes the code straight to
// glShaderSource(), so the version/precision lines must live here.
//
// They live in C (not Swift) because the iOS bridge
// (components/render/ios/renderIOS.mm) feeds them to raybri_init() directly.
static const char RENDER_SHADER_MAIN_FS[] =
"#version 300 es\n"
"precision highp float;\n"
"in vec3 vWorldPos;\n"
"in vec3 vNormal;\n"
"in vec2 vTexCoord;\n"
"in vec4 vLightClip;\n"
"uniform sampler2D texture0;\n"
"uniform sampler2D u_normalMap;\n"
"uniform float u_normalStrength;\n"
"uniform sampler2D u_shadowMap;\n"
"uniform vec3 u_lightPos;\n"
"uniform vec3 u_lightColor;\n"
"uniform vec3 u_ambient;\n"
"uniform vec3 u_albedoColor;\n"
"uniform float u_lightEnabled;\n"
"uniform float u_emissive;\n"
"uniform float u_shadowBias;\n"
"uniform float u_shadowSoftness;\n"
"uniform float u_isGround;\n"
"uniform vec3 u_gridColor;\n"
"uniform vec3 u_cameraPos;\n"
"uniform vec3 u_fogColor;\n"
"uniform float u_fogStart;\n"
"uniform float u_fogEnd;\n"
"out vec4 finalColor;\n"
"bool isPixelInShadowMap(vec3 proj)\n"
"{\n"
" return proj.x >= 0.0 && proj.x <= 1.0 &&\n"
" proj.y >= 0.0 && proj.y <= 1.0 &&\n"
" proj.z >= 0.0 && proj.z <= 1.0;\n"
"}\n"
"float shadowFactor()\n"
"{\n"
" vec3 proj = vLightClip.xyz / vLightClip.w;\n"
" proj = proj * 0.5 + 0.5;\n"
" if (!isPixelInShadowMap(proj)) return 0.0;\n"
" int radius = int(u_shadowSoftness);\n"
" vec2 texel = 1.0 / vec2(textureSize(u_shadowMap, 0));\n"
" float sum = 0.0;\n"
" float count = 0.0;\n"
" for (int x = -radius; x <= radius; x++)\n"
" {\n"
" for (int y = -radius; y <= radius; y++)\n"
" {\n"
" float d = texture(u_shadowMap, proj.xy + vec2(float(x), float(y)) * texel).r;\n"
" sum += (proj.z - u_shadowBias > d) ? 1.0 : 0.0;\n"
" count += 1.0;\n"
" }\n"
" }\n"
" return sum / count;\n"
"}\n"
"mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)\n"
"{\n"
" vec3 dp1 = dFdx(p);\n"
" vec3 dp2 = dFdy(p);\n"
" vec2 duv1 = dFdx(uv);\n"
" vec2 duv2 = dFdy(uv);\n"
" vec3 dp2perp = cross(dp2, N);\n"
" vec3 dp1perp = cross(N, dp1);\n"
" vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;\n"
" vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;\n"
" float invmax = inversesqrt(max(dot(T, T), dot(B, B)));\n"
" return mat3(T * invmax, B * invmax, N);\n"
"}\n"
"void main()\n"
"{\n"
" vec4 texel = texture(texture0, vTexCoord);\n"
" vec3 albedo = texel.rgb * u_albedoColor;\n"
" vec3 N = normalize(vNormal);\n"
" if (u_normalStrength > 0.0)\n"
" {\n"
" vec3 nm = texture(u_normalMap, vTexCoord).rgb * 2.0 - 1.0;\n"
" nm.xy *= u_normalStrength;\n"
" N = normalize(cotangentFrame(N, vWorldPos, vTexCoord) * nm);\n"
" }\n"
" if (u_isGround > 0.5)\n"
" {\n"
" vec2 uv = vWorldPos.xz;\n"
" vec2 gg = abs(fract(uv - 0.5) - 0.5) / fwidth(uv);\n"
" float line = min(gg.x, gg.y);\n"
" float grid = 1.0 - min(line, 1.0);\n"
" albedo = mix(albedo, u_gridColor, grid * 0.7);\n"
" }\n"
" vec3 color = albedo * u_ambient;\n"
" if (u_lightEnabled > 0.5)\n"
" {\n"
" vec3 toLight = u_lightPos - vWorldPos;\n"
" float dist = length(toLight);\n"
" vec3 L = toLight / max(dist, 0.0001);\n"
" float diff = max(dot(N, L), 0.0);\n"
" float attenuation = 1.0 / (1.0 + 0.14 * dist + 0.07 * dist * dist);\n"
" float shadow = shadowFactor();\n"
" color += albedo * u_lightColor * diff * attenuation * (1.0 - shadow);\n"
" }\n"
" color += albedo * u_emissive;\n"
" float fogDist = length(u_cameraPos - vWorldPos);\n"
" float fog = clamp((fogDist - u_fogStart) / (u_fogEnd - u_fogStart), 0.0, 1.0);\n"
" color = mix(color, u_fogColor, fog);\n"
" finalColor = vec4(color, 1.0);\n"
"}\n";
static const char RENDER_SHADER_MAIN_VS[] =
"#version 300 es\n"
"in vec3 vertexPosition;\n"
"in vec2 vertexTexCoord;\n"
"in vec3 vertexNormal;\n"
"uniform mat4 mvp;\n"
"uniform mat4 matModel;\n"
"uniform mat4 u_lightMVP;\n"
"out vec3 vWorldPos;\n"
"out vec3 vNormal;\n"
"out vec2 vTexCoord;\n"
"out vec4 vLightClip;\n"
"void main()\n"
"{\n"
" vec4 worldPos = matModel * vec4(vertexPosition, 1.0);\n"
" vWorldPos = worldPos.xyz;\n"
" vNormal = normalize(mat3(matModel) * vertexNormal);\n"
" vTexCoord = vertexTexCoord;\n"
" vLightClip = u_lightMVP * vec4(vertexPosition, 1.0);\n"
" gl_Position = mvp * vec4(vertexPosition, 1.0);\n"
"}\n";
static const char RENDER_SHADER_SHADOW_FS[] =
"#version 300 es\n"
"precision highp float;\n"
"out vec4 finalColor;\n"
"void main()\n"
"{\n"
" finalColor = vec4(1.0);\n"
"}\n";
static const char RENDER_SHADER_SHADOW_VS[] =
"#version 300 es\n"
"in vec3 vertexPosition;\n"
"uniform mat4 mvp;\n"
"void main()\n"
"{\n"
" gl_Position = mvp * vec4(vertexPosition, 1.0);\n"
"}\n";
#endif
+82
View File
@@ -0,0 +1,82 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include "raylib.h"
#include "raybri.h"
#include "renderConstShaderIOS.h"
// iOS entry points. The prebuilt raylib.framework (ghera/raylib-iOS) owns
// main()/AppDelegate/SceneDelegate and a full-screen EGL/ANGLE window; it
// calls these three callbacks (see src/platforms/rcore_ios.c). We therefore
// drive the same raybri_* scene as the macOS build from here, and present the
// SwiftUI UI as a transparent overlay window stacked above raylib's window.
//
// The prebuilt framework already ran rlLoadExtensions()/rlglInit() inside
// InitWindow(), so raybri_init() only compiles the shaders and builds meshes.
#if defined(__cplusplus)
extern "C" void rcios_present_overlay(void); // implemented in Swift (RenderIOS.swift)
#else
void rcios_present_overlay(void);
#endif
static double sr_start_time = 0.0;
static int sr_window_ready = 0;
double rcios_time_now(void)
{
return CACurrentMediaTime();
}
extern "C" void ios_ready(void)
{
SetTraceLogLevel(LOG_WARNING);
// Size is ignored on PLATFORM_IOS; the fork sizes the window itself.
InitWindow(0, 0, "OGS chkgfx");
// On iOS raylib's InitWindow loaded the GL functions and called rlglInit,
// so raybri_init() must not repeat it (see raybri.c, RAYBRI_PLATFORM_IOS).
raybri_init(
RENDER_SHADER_MAIN_VS,
RENDER_SHADER_MAIN_FS,
RENDER_SHADER_SHADOW_VS,
RENDER_SHADER_SHADOW_FS,
(raybri_config){ 45.0f, 0.1f, 1000.0f, 256, 40.0f }
);
// Same layout as the macOS app bundle: <bundle>/cube/cube.gltf
NSString *gltf = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"cube/cube.gltf"];
raybri_load_model([gltf UTF8String]);
sr_start_time = rcios_time_now();
sr_window_ready = 1;
// Show ChkgfxView (the SwiftUI title) above the raylib scene.
rcios_present_overlay();
}
extern "C" void ios_update(bool viewSizeChanged)
{
if (!sr_window_ready) return;
int w = GetRenderWidth();
int h = GetRenderHeight();
raybri_resize(w, h);
float t = (float)(rcios_time_now() - sr_start_time);
raybri_set_rotation(t * 6.0f, t * 18.0f, 0.0f);
BeginDrawing();
raybri_render();
EndDrawing();
}
extern "C" void ios_destroy(void)
{
if (!sr_window_ready) return;
raybri_shutdown();
CloseWindow();
sr_window_ready = 0;
}
@@ -0,0 +1,46 @@
import Combine
import Foundation
// On iOS the renderer is the prebuilt raylib framework's own full-screen
// window (see components/render/ios/renderIOS.mm: ios_ready/ios_update/
// ios_destroy). The macOS render component is not used there, but the
// generated dialect.swift still refers to RenderComponent/RenderVM and to the
// renderInit/renderStartTimer/renderLoadCubeModel/renderDeinit oneliners, so
// we provide inert counterparts to keep the dialect compiling.
//
// The real work happens in renderIOS.mm, which talks to the very same
// raybri_* bridge functions these stubs would call on macOS.
final class RenderVM: ObservableObject {
var didPrepareGL = false
var pixelSize = [Int32]()
var startTime: CFTimeInterval = 0
static private(set) weak var shared: RenderVM?
init() {
Self.shared = self
}
}
class RenderComponent {
let ctrl = DialectController(RenderContext())
static private(set) weak var singleton: RenderComponent?
init() {
Self.singleton = self
renderRegisterShoulds(ctrl)
}
func setup() {
renderSet(F.didSetup, true)
}
}
func renderInit(_ vm: RenderVM) {}
func renderStartTimer(_ vm: RenderVM) {}
func renderLoadCubeModel(_ vm: RenderVM) {}
func renderDeinit(_ vm: RenderVM) {}