Introduce chkgfx (#3)

This commit was merged in pull request #3.
This commit is contained in:
kornerr committed 2026-10-22 05:17:21 +02:00
1 parent 1515a9483b
commit fb28dd55c7
77 files changed
+1598 -543

No files matched your search

@@ -0,0 +1,47 @@
import AppKit
final class RenderGLView: NSOpenGLView {
override init(frame frameRect: NSRect) {
guard
let format = NSOpenGLPixelFormat(attributes: RENDER_OPENGL_ATTRS)
else {
fatalError("ERROR Could not create required OpenGL pixel format")
}
super.init(frame: frameRect, pixelFormat: format)!
wantsBestResolutionOpenGLSurface = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareOpenGL() {
super.prepareOpenGL()
renderSet(F.didPrepareGL, true)
}
override func reshape() {
super.reshape()
needsDisplay = true
let ps = renderPixelSize(self)
renderSet(F.reshapePixelSize, ps)
}
override func viewDidChangeBackingProperties() {
super.viewDidChangeBackingProperties()
needsDisplay = true
let ps = renderPixelSize(self)
renderSet(F.backingPixelSize, ps)
}
override func draw(_ dirtyRect: NSRect) {
let vm = RenderVM.shared!
if vm.didPrepareGL {
renderDraw(vm)
}
}
deinit {
renderSet(F.willDeinit, true)
}
}
@@ -0,0 +1,11 @@
import AppKit
import SwiftUI
/// Render view for SwiftUI
struct RenderView: NSViewRepresentable {
func makeNSView(context: Context) -> RenderGLView {
RenderVM.shared!.glView
}
func updateNSView(_ nsView: RenderGLView, context: Context) {}
}
+14
View File
@@ -0,0 +1,14 @@
class RenderComponent {
let ctrl = DialectController(RenderContext())
static private(set) weak var singleton: RenderComponent?
init() {
Self.singleton = self
renderRegisterEffects(ctrl)
renderRegisterShoulds(ctrl)
}
func setup() {
renderSet(F.didSetup, true)
}
}
+20
View File
@@ -0,0 +1,20 @@
import AppKit
import RaylibBridge
func renderDraw(_ vm: RenderVM) {
vm.glView.openGLContext?.makeCurrentContext()
raybri_resize(vm.pixelSize[0], vm.pixelSize[1])
let t = Float(CACurrentMediaTime() - vm.startTime)
raybri_set_rotation(t * 6.0, t * 18.0, 0.0)
raybri_render()
vm.glView.openGLContext?.flushBuffer()
}
func renderPixelSize(_ view: NSView) -> [Int32] {
let backing = view.convertToBacking(view.bounds)
let w = Int32(max(1, backing.width))
let h = Int32(max(1, backing.height))
return [w, h]
}
+24
View File
@@ -0,0 +1,24 @@
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_MODEL_CUBE = "/cube/cube.gltf"
let RENDER_OPENGL_ATTRS: [NSOpenGLPixelFormatAttribute] = [
UInt32(NSOpenGLPFAOpenGLProfile), UInt32(NSOpenGLProfileVersion3_2Core),
UInt32(NSOpenGLPFAColorSize), 24,
UInt32(NSOpenGLPFAAlphaSize), 8,
UInt32(NSOpenGLPFADepthSize), 24,
UInt32(NSOpenGLPFADoubleBuffer),
UInt32(NSOpenGLPFAAccelerated),
0
]
+162
View File
@@ -0,0 +1,162 @@
let RENDER_SHADER_MAIN_FS = """
#version 150
in vec3 vWorldPos;
in vec3 vNormal;
in vec2 vTexCoord;
in vec4 vLightClip;
uniform sampler2D texture0;
uniform sampler2D u_normalMap;
uniform float u_normalStrength;
uniform sampler2D u_shadowMap;
uniform vec3 u_lightPos;
uniform vec3 u_lightColor;
uniform vec3 u_ambient;
uniform vec3 u_albedoColor;
uniform float u_lightEnabled;
uniform float u_emissive;
uniform float u_shadowBias;
uniform float u_shadowSoftness;
uniform float u_isGround;
uniform vec3 u_gridColor;
uniform vec3 u_cameraPos;
uniform vec3 u_fogColor;
uniform float u_fogStart;
uniform float u_fogEnd;
out vec4 finalColor;
bool isPixelInShadowMap(vec3 proj)
{
return proj.x >= 0.0 && proj.x <= 1.0 &&
proj.y >= 0.0 && proj.y <= 1.0 &&
proj.z >= 0.0 && proj.z <= 1.0;
}
float shadowFactor()
{
vec3 proj = vLightClip.xyz / vLightClip.w;
proj = proj * 0.5 + 0.5;
if (!isPixelInShadowMap(proj)) return 0.0;
int radius = int(u_shadowSoftness);
vec2 texel = 1.0 / vec2(textureSize(u_shadowMap, 0));
float sum = 0.0;
float count = 0.0;
for (int x = -radius; x <= radius; x++)
{
for (int y = -radius; y <= radius; y++)
{
float d = texture(u_shadowMap, proj.xy + vec2(float(x), float(y)) * texel).r;
sum += (proj.z - u_shadowBias > d) ? 1.0 : 0.0;
count += 1.0;
}
}
return sum / count;
}
mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
{
vec3 dp1 = dFdx(p);
vec3 dp2 = dFdy(p);
vec2 duv1 = dFdx(uv);
vec2 duv2 = dFdy(uv);
vec3 dp2perp = cross(dp2, N);
vec3 dp1perp = cross(N, dp1);
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
float invmax = inversesqrt(max(dot(T, T), dot(B, B)));
return mat3(T * invmax, B * invmax, N);
}
void main()
{
vec4 texel = texture(texture0, vTexCoord);
vec3 albedo = texel.rgb * u_albedoColor;
vec3 N = normalize(vNormal);
if (u_normalStrength > 0.0)
{
vec3 nm = texture(u_normalMap, vTexCoord).rgb * 2.0 - 1.0;
nm.xy *= u_normalStrength;
N = normalize(cotangentFrame(N, vWorldPos, vTexCoord) * nm);
}
if (u_isGround > 0.5)
{
vec2 uv = vWorldPos.xz;
vec2 gg = abs(fract(uv - 0.5) - 0.5) / fwidth(uv);
float line = min(gg.x, gg.y);
float grid = 1.0 - min(line, 1.0);
albedo = mix(albedo, u_gridColor, grid * 0.7);
}
vec3 color = albedo * u_ambient;
if (u_lightEnabled > 0.5)
{
vec3 toLight = u_lightPos - vWorldPos;
float dist = length(toLight);
vec3 L = toLight / max(dist, 0.0001);
float diff = max(dot(N, L), 0.0);
float attenuation = 1.0 / (1.0 + 0.14 * dist + 0.07 * dist * dist);
float shadow = shadowFactor();
color += albedo * u_lightColor * diff * attenuation * (1.0 - shadow);
}
color += albedo * u_emissive;
float fogDist = length(u_cameraPos - vWorldPos);
float fog = clamp((fogDist - u_fogStart) / (u_fogEnd - u_fogStart), 0.0, 1.0);
color = mix(color, u_fogColor, fog);
finalColor = vec4(color, 1.0);
}
"""
let RENDER_SHADER_MAIN_VS = """
#version 150
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
uniform mat4 mvp;
uniform mat4 matModel;
uniform mat4 u_lightMVP;
out vec3 vWorldPos;
out vec3 vNormal;
out vec2 vTexCoord;
out vec4 vLightClip;
void main()
{
vec4 worldPos = matModel * vec4(vertexPosition, 1.0);
vWorldPos = worldPos.xyz;
vNormal = normalize(mat3(matModel) * vertexNormal);
vTexCoord = vertexTexCoord;
vLightClip = u_lightMVP * vec4(vertexPosition, 1.0);
gl_Position = mvp * vec4(vertexPosition, 1.0);
}
"""
let RENDER_SHADER_SHADOW_FS = """
#version 150
out vec4 finalColor;
void main()
{
finalColor = vec4(1.0);
}
"""
let RENDER_SHADER_SHADOW_VS = """
#version 150
in vec3 vertexPosition;
uniform mat4 mvp;
void main()
{
gl_Position = mvp * vec4(vertexPosition, 1.0);
}
"""
+46
View File
@@ -0,0 +1,46 @@
import Foundation
import RaylibBridge
import QuartzCore
func renderDeinit(_ vm: RenderVM) {
print("ИГР renderD")
vm.timer?.invalidate()
vm.glView.openGLContext?.makeCurrentContext()
raybri_shutdown()
}
func renderInit(_ vm: RenderVM) {
vm.glView.openGLContext?.makeCurrentContext()
// Enable VSync
var swapInterval: Int32 = 1
vm.glView.openGLContext?.setValues(&swapInterval, for: .swapInterval)
// Init Raylib
raybri_init(
RENDER_SHADER_MAIN_VS,
RENDER_SHADER_MAIN_FS,
RENDER_SHADER_SHADOW_VS,
RENDER_SHADER_SHADOW_FS,
RENDER_CONFIG
)
}
func renderLoadCubeModel(_ vm: RenderVM) {
let path = Bundle.main.resourceURL!.path + RENDER_MODEL_CUBE
vm.glView.openGLContext?.makeCurrentContext()
raybri_load_model(path)
}
func renderStartTimer(_ vm: RenderVM) {
// Make a time mark for calculating time delta
vm.startTime = CACurrentMediaTime()
// Schedule repaints every once in FPS seconds
let timer = Timer(timeInterval: 1.0 / RENDER_FPS, repeats: true) { [weak vm] _ in
vm?.glView.needsDisplay = true
}
vm.timer = timer
// .common means holding buttons in UI does not pause this rendering timer
RunLoop.main.add(timer, forMode: .common)
}
+16
View File
@@ -0,0 +1,16 @@
import Combine
import Foundation
final class RenderVM: ObservableObject {
var didPrepareGL = false
let glView = RenderGLView(frame: .zero)
var pixelSize = [Int32]()
var startTime: CFTimeInterval = 0
var timer: Timer?
static private(set) weak var shared: RenderVM?
init() {
Self.shared = self
}
}