Cleanup: remove projection and views and add setters for mat3 and mat4 instead.

This commit is contained in:
Roz K 2022-09-20 03:10:59 +02:00
parent 59df9e8748
commit dfc2bbc408
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
6 changed files with 51 additions and 132 deletions

View File

@ -1,4 +1,4 @@
SOURCES = cpp/opengl/render_context_glx.cpp cpp/opengl/render_opengles.cpp cpp/math.cpp cpp/render.cpp SOURCES = cpp/opengl/render_context_glx.cpp cpp/opengl/render_opengles.cpp cpp/math.cpp
OUTPUTFILE = engine.so OUTPUTFILE = engine.so
CXXFLAGS = -fpic -Wall -Werror -O2 -flto -fomit-frame-pointer -ffast-math -funroll-loops -fno-rtti -fno-exceptions CXXFLAGS = -fpic -Wall -Werror -O2 -flto -fomit-frame-pointer -ffast-math -funroll-loops -fno-rtti -fno-exceptions

View File

@ -23,10 +23,6 @@ _lib = ctypes.cdll.LoadLibrary(Path(__file__).parent / "engine.so")
def _flag(x): def _flag(x):
return 1 << x return 1 << x
INPUT_IDENTITY = 0
INPUT_VIEW_POSITION = 1
INPUT_VIEW_ORIENTATION = 2
TEXTURE_FORMAT_SRGB8_A8 = 0 TEXTURE_FORMAT_SRGB8_A8 = 0
TEXTURE_FORMAT_RGBA8 = 1 TEXTURE_FORMAT_RGBA8 = 1
TEXTURE_FORMAT_RGB10_A2 = 2 TEXTURE_FORMAT_RGB10_A2 = 2
@ -238,12 +234,29 @@ set_input_float.argtypes = (
_set_input_vec3 = _lib.rk_set_input_vec3 _set_input_vec3 = _lib.rk_set_input_vec3
_set_input_vec3.argtypes = ( _set_input_vec3.argtypes = (
ctypes.c_void_p, # input ctypes.c_void_p, # input
_vec3_t, # value _vec3_t) # value
ctypes.c_uint) # mode
def set_input_vec3(input, value, mode = INPUT_IDENTITY): def set_input_vec3(input, value):
assert len(value) == 3 assert len(value) == 3
_set_input_vec3(input, _vec3(value), mode) _set_input_vec3(input, _vec3(value))
_set_input_mat3 = _lib.rk_set_input_mat3
_set_input_mat3.argtypes = (
ctypes.c_void_p, # input
_mat3_t) # value
def set_input_mat3(input, value):
assert len(value) == 9
_set_input_mat3(input, _mat3(value))
_set_input_mat4 = _lib.rk_set_input_mat4
_set_input_mat4.argtypes = (
ctypes.c_void_p, # input
_mat4_t) # value
def set_input_mat4(input, value):
assert len(value) == 16
_set_input_mat4(input, _mat4(value))
_create_texture = _lib.rk_create_texture _create_texture = _lib.rk_create_texture
_create_texture.restype = ctypes.c_void_p _create_texture.restype = ctypes.c_void_p
@ -291,22 +304,6 @@ create_batch.argtypes = (
ctypes.c_uint, # translation_format ctypes.c_uint, # translation_format
ctypes.c_uint) # orientation_format ctypes.c_uint) # orientation_format
set_projection = _lib.rk_set_projection
set_projection.argtypes = (
ctypes.c_float, # hfov
ctypes.c_float, # ratio
ctypes.c_float, # near
ctypes.c_float) # far
_set_view = _lib.rk_set_view
_set_view.argtypes = (
_vec3_t, # position
_vec3_t) # lookat
def set_view(position, lookat):
assert len(position) == 3 and len(lookat) == 3
_set_view(_vec3(position), _vec3(lookat))
begin_frame = _lib.rk_begin_frame begin_frame = _lib.rk_begin_frame
select_texture = _lib.rk_select_texture select_texture = _lib.rk_select_texture

View File

@ -18,7 +18,6 @@
static rk_shader const * rk_current_shader = nullptr; static rk_shader const * rk_current_shader = nullptr;
static rk_vertices const * rk_current_vertices = nullptr; static rk_vertices const * rk_current_vertices = nullptr;
static bool rk_frame = false;
static void rk_printf(char const * messsage) { static void rk_printf(char const * messsage) {
printf("[RK_ENGINE] %s\n", messsage); printf("[RK_ENGINE] %s\n", messsage);
@ -146,10 +145,6 @@ rk_shader_t rk_load_shader(
rk_free_shader_source(fragment_source, fragment_length); rk_free_shader_source(fragment_source, fragment_length);
} }
shader->uniforms.view = glGetUniformLocation(shader->program, "u_view");
shader->uniforms.view_km = glGetUniformLocation(shader->program, "u_view_km");
shader->uniforms.projection = glGetUniformLocation(shader->program, "u_projection");
return shader; return shader;
} }
@ -159,17 +154,6 @@ void rk_select_shader(
if (shader) { if (shader) {
rk_current_shader = shader; rk_current_shader = shader;
glUseProgram(shader->program); glUseProgram(shader->program);
if (rk_frame) {
if (shader->uniforms.view > -1) {
glUniformMatrix4fv(shader->uniforms.view, 1, GL_FALSE, glm::value_ptr(rk_view));
}
if (shader->uniforms.view_km > -1) {
glUniformMatrix4fv(shader->uniforms.view_km, 1, GL_FALSE, glm::value_ptr(rk_view_km));
}
if (shader->uniforms.projection > -1) {
glUniformMatrix4fv(shader->uniforms.projection, 1, GL_FALSE, glm::value_ptr(rk_projection));
}
}
} }
} }
@ -193,23 +177,28 @@ void rk_set_input_float(
void rk_set_input_vec3( void rk_set_input_vec3(
rk_input_t _input, rk_input_t _input,
rk_vec3 const & value, rk_vec3 const & value) {
rk_input_mode mode) {
GLint const input = reinterpret_cast<intptr_t>(_input) - 1; GLint const input = reinterpret_cast<intptr_t>(_input) - 1;
if (rk_current_shader && input > -1) { if (rk_current_shader && input > -1) {
switch (mode) { glUniform3fv(input, 1, glm::value_ptr(value));
case RK_INPUT_IDENTITY: }
glUniform3fv(input, 1, glm::value_ptr(value)); }
break;
case RK_INPUT_VIEW_POSITION: { void rk_set_input_mat3(
glUniform3fv(input, 1, glm::value_ptr(rk_view * rk_vec4(value, 1.0))); rk_input_t _input,
break; rk_mat3 const & value) {
} GLint const input = reinterpret_cast<intptr_t>(_input) - 1;
case RK_INPUT_VIEW_ORIENTATION: { if (rk_current_shader && input > -1) {
glUniform3fv(input, 1, glm::value_ptr(rk_view * rk_vec4(value, 0.0))); glUniformMatrix3fv(input, 1, GL_FALSE, glm::value_ptr(value));
break; }
} }
}
void rk_set_input_mat4(
rk_input_t _input,
rk_mat4 const & value) {
GLint const input = reinterpret_cast<intptr_t>(_input) - 1;
if (rk_current_shader && input > -1) {
glUniformMatrix4fv(input, 1, GL_FALSE, glm::value_ptr(value));
} }
} }
@ -490,7 +479,6 @@ rk_batch_t rk_create_batch(
} }
void rk_begin_frame() { void rk_begin_frame() {
rk_frame = true;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
} }
@ -721,7 +709,6 @@ void rk_unselect_shader(
void rk_end_frame() { void rk_end_frame() {
rk_swap_buffers(); rk_swap_buffers();
rk_frame = false;
} }
void rk_destroy_batch( void rk_destroy_batch(

View File

@ -27,17 +27,10 @@ enum : rk_uint {
RK_PARAMS_BINDING = 1 RK_PARAMS_BINDING = 1
}; };
struct rk_uniforms {
GLint view;
GLint view_km;
GLint projection;
};
struct rk_shader { struct rk_shader {
GLuint vertex; GLuint vertex;
GLuint fragment; GLuint fragment;
GLuint program; GLuint program;
rk_uniforms uniforms;
}; };
struct rk_texture { struct rk_texture {

View File

@ -1,48 +0,0 @@
// Copyright (C) 2022 RozK
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "render.hpp"
rk_vec3 rk_view_origin;
rk_vec3 rk_view_lookat;
rk_mat4 rk_view;
rk_mat4 rk_view_km;
float rk_projection_hfov;
float rk_projection_ratio;
float rk_projection_near;
float rk_projection_far;
rk_mat4 rk_projection;
void rk_set_projection(
float hfov,
float ratio,
float near,
float far) {
rk_projection_hfov = hfov;
rk_projection_ratio = ratio;
rk_projection_near = near;
rk_projection_far = far;
rk_projection = glm::perspectiveRH(hfov, ratio, near, far);
}
extern void rk_set_view(
rk_vec3 const & position,
rk_vec3 const & lookat) {
rk_view_origin = position;
rk_view_lookat = lookat;
rk_view = glm::lookAtRH(position, lookat, glm::vec3(0.0f, 0.0f, 1.0f));
rk_view_km = glm::lookAtRH(position * 0.001f, lookat * 0.001f, glm::vec3(0.0f, 0.0f, 1.0f));
}

View File

@ -19,17 +19,6 @@
#include "types.hpp" #include "types.hpp"
#include "math.hpp" #include "math.hpp"
extern rk_vec3 rk_view_origin;
extern rk_vec3 rk_view_lookat;
extern rk_mat4 rk_view;
extern rk_mat4 rk_view_km; //TODO: remove from engine
extern float rk_projection_hfov;
extern float rk_projection_ratio;
extern float rk_projection_near;
extern float rk_projection_far;
extern rk_mat4 rk_projection;
typedef rk_handle_t rk_window_t; typedef rk_handle_t rk_window_t;
typedef rk_handle_t rk_shader_t; typedef rk_handle_t rk_shader_t;
typedef rk_handle_t rk_input_t; typedef rk_handle_t rk_input_t;
@ -40,12 +29,6 @@ typedef rk_handle_t rk_batch_t;
#define RK_FLAG(bit) (1 << (bit)) #define RK_FLAG(bit) (1 << (bit))
enum rk_input_mode : rk_uint {
RK_INPUT_IDENTITY = 0,
RK_INPUT_VIEW_POSITION = 1,
RK_INPUT_VIEW_ORIENTATION = 2
};
enum rk_texture_format : rk_uint { enum rk_texture_format : rk_uint {
RK_TEXTURE_FORMAT_SRGB8_A8 = 0, RK_TEXTURE_FORMAT_SRGB8_A8 = 0,
RK_TEXTURE_FORMAT_RGBA8 = 1, RK_TEXTURE_FORMAT_RGBA8 = 1,
@ -118,8 +101,15 @@ RK_EXPORT void rk_set_input_float(
RK_EXPORT void rk_set_input_vec3( RK_EXPORT void rk_set_input_vec3(
rk_input_t input, rk_input_t input,
rk_vec3 const & value, rk_vec3 const & value);
rk_input_mode mode);
RK_EXPORT void rk_set_input_mat3(
rk_input_t input,
rk_mat3 const & value);
RK_EXPORT void rk_set_input_mat4(
rk_input_t input,
rk_mat4 const & value);
RK_EXPORT rk_texture_t rk_create_texture( RK_EXPORT rk_texture_t rk_create_texture(
rk_uint slot, rk_uint slot,