From ec8427770a70ce609ea54dc588e8af953622bbc0 Mon Sep 17 00:00:00 2001 From: CatWindow Date: Mon, 17 Aug 2026 01:06:07 +0300 Subject: [PATCH] fov --- binding-mri/graphics-binding.cpp | 19 ++++++++++++++++--- src/graphics.h | 10 ++++++---- src/shader.cpp | 21 ++++++++++----------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/binding-mri/graphics-binding.cpp b/binding-mri/graphics-binding.cpp index d816578..7ecf4cd 100644 --- a/binding-mri/graphics-binding.cpp +++ b/binding-mri/graphics-binding.cpp @@ -224,7 +224,7 @@ DEF_GRA_PROP_B(Frameskip) _rb_define_module_function(module, prop_name_s "=", graphics##Set##PropName); \ } -static VALUE graphicsWindowMoved(VALUE self){ +static VALUE graphicsWindowMoved(VALUE){ RUBY_CONNECTION conn->connection = shState->windowSignals.moved.Connect([conn](int x, int y){ shState->rubyDispatcher().invoke([conn, x, y]{ @@ -239,7 +239,7 @@ static VALUE graphicsWindowMoved(VALUE self){ return TypedData_Wrap_Struct(rb_cRubyConnection, &rubyConnection_type, conn); } -static VALUE graphicsWindowResized(VALUE self){ +static VALUE graphicsWindowResized(VALUE){ RUBY_CONNECTION conn->connection = shState->windowSignals.resized.Connect([conn](int w, int h){ shState->rubyDispatcher().invoke([conn, w, h]{ @@ -254,7 +254,7 @@ static VALUE graphicsWindowResized(VALUE self){ return TypedData_Wrap_Struct(rb_cRubyConnection, &rubyConnection_type, conn); } -static VALUE graphicsViewportResized(VALUE self){ +static VALUE graphicsViewportResized(VALUE){ RUBY_CONNECTION conn->connection = shState->graphicsSignals.resized.Connect([conn](int w, int h){ //shState->rubyDispatcher().invoke([conn, w, h]{ @@ -269,6 +269,15 @@ static VALUE graphicsViewportResized(VALUE self){ return TypedData_Wrap_Struct(rb_cRubyConnection, &rubyConnection_type, conn); } +static VALUE graphicsGetFOV(VALUE) { + return DBL2NUM(shState->graphics().globalFov); +} + +static VALUE graphicsSetFOV(VALUE, VALUE fov) { + shState->graphics().globalFov = NUM2DBL(fov); + return Qnil; +} + void graphicsBindingInit(){ VALUE module = rb_define_module("Graphics"); @@ -278,6 +287,10 @@ void graphicsBindingInit(){ rb_define_module_function(module, "window_resized", RUBY_METHOD_FUNC(graphicsWindowResized), 0); rb_define_module_function(module, "viewport_resized", RUBY_METHOD_FUNC(graphicsViewportResized), 0); + + rb_define_module_function(module, "fov", RUBY_METHOD_FUNC(graphicsGetFOV), 0); + rb_define_module_function(module, "fov=", RUBY_METHOD_FUNC(graphicsSetFOV), 1); + // Functions _rb_define_module_function(module, "x", graphicsPosX); _rb_define_module_function(module, "y", graphicsPosY); diff --git a/src/graphics.h b/src/graphics.h index aa99402..629474f 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -61,10 +61,12 @@ public: void setVsync(int value); /* Non-standard extension */ - DECL_ATTR( Fullscreen, bool ) - DECL_ATTR( ShowCursor, bool ) - DECL_ATTR( Smooth, bool ) - DECL_ATTR( Frameskip, bool ) + DECL_ATTR( Fullscreen, bool ) + DECL_ATTR( ShowCursor, bool ) + DECL_ATTR( Smooth, bool ) + DECL_ATTR( Frameskip, bool ) + + float globalFov = 70; /* */ Scene *getScreen() const; diff --git a/src/shader.cpp b/src/shader.cpp index 2e41c7b..95f6302 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -243,23 +243,22 @@ void ShaderBase::applyPerspectiveProj() const float width = (float)vp.w; const float height = (float)vp.h; - const float cameraDistance = 1000.0f; + const float camDist = (height * 0.5f) / SDL_tan(shState->graphics().globalFov * PI / 360.0f); - const float nearPlane = 1.0f; + const float nearPlane = 0.01f; const float farPlane = 10000.0f; - const float sx = 2.0f * cameraDistance / width; - const float sy = 2.0f * cameraDistance / height; + const float sx = 2.0f * camDist / width; + const float sy = 2.0f * camDist / height; - const float A = -(farPlane + nearPlane) / (farPlane - nearPlane); - - const float B = -(2.0f * farPlane * nearPlane) / (farPlane - nearPlane); + const float A = (farPlane + nearPlane) / (farPlane - nearPlane); + const float B = (2.0f * farPlane * nearPlane) / (farPlane - nearPlane); GLfloat mat[16] = { - sx, 0, 0, 0, - 0, sy, 0, 0, - 0, 0, A, -1, - -cameraDistance, -cameraDistance, B, cameraDistance + sx, 0, 0, 0, + 0, sy, 0, 0, + 0, 0, -A, -1, + -camDist, -camDist, A * camDist - B, camDist }; gl.UniformMatrix4fv(projMat.u_mat, 1, GL_FALSE, mat);