Compare commits

..

No commits in common. "dfc2bbc408949b14043f496a2f44ba95631a1cae" and "1855778fa7439c308b8a79282163c52f5f93f0c6" have entirely different histories.

8 changed files with 133 additions and 103 deletions

View File

@ -1,4 +1,4 @@
SOURCES = cpp/opengl/render_context_glx.cpp cpp/opengl/render_opengles.cpp cpp/math.cpp SOURCES = cpp/opengl/render_context_glx.cpp cpp/opengl/render_opengles.cpp cpp/math.cpp cpp/render.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

@ -16,13 +16,16 @@
import ctypes import ctypes
import struct import struct
from array import array from array import array
from pathlib import Path
_lib = ctypes.cdll.LoadLibrary(Path(__file__).parent / "engine.so") _lib = ctypes.cdll.LoadLibrary("./engine/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
@ -159,28 +162,6 @@ def mat3_mul_mat3(ret, a, b):
assert len(ret) == 9 and len(a) == 9 and len(b) == 9 assert len(ret) == 9 and len(a) == 9 and len(b) == 9
_mat3_mul_mat3(_mat3(ret), _mat3(a), _mat3(b)) _mat3_mul_mat3(_mat3(ret), _mat3(a), _mat3(b))
_mat4_projection = _lib.rk_mat4_projection
_mat4_projection.argtypes = (
_mat4_t, # ret
ctypes.c_float, # hfov
ctypes.c_float, # ratio
ctypes.c_float, # near
ctypes.c_float) # far
def mat4_projection(ret, hfov, ratio, near, far):
assert len(ret) == 16
_mat4_projection(_mat4(ret), hfov, ratio, near, far)
_mat4_lookat = _lib.rk_mat4_lookat
_mat4_lookat.argtypes = (
_mat4_t, # ret
_vec3_t, # position
_vec3_t) # lookat
def mat4_lookat(ret, position, lookat):
assert len(ret) == 16 and len(position) == 3 and len(lookat) == 3
_mat4_lookat(_mat4(ret), _vec3(position), _vec3(lookat))
_mat4_mul_vec4 = _lib.rk_mat4_mul_vec4 _mat4_mul_vec4 = _lib.rk_mat4_mul_vec4
_mat4_mul_vec4.argtypes = ( _mat4_mul_vec4.argtypes = (
_vec4_t, # ret _vec4_t, # ret
@ -234,29 +215,12 @@ 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): def set_input_vec3(input, value, mode = INPUT_IDENTITY):
assert len(value) == 3 assert len(value) == 3
_set_input_vec3(input, _vec3(value)) _set_input_vec3(input, _vec3(value), mode)
_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
@ -304,6 +268,22 @@ 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

@ -38,22 +38,6 @@ void rk_mat3_mul_mat3(
ret = a * b; ret = a * b;
} }
void rk_mat4_projection(
rk_mat4 & ret,
float hfov,
float ratio,
float near,
float far) {
ret = glm::perspectiveRH(hfov, ratio, near, far);
}
void rk_mat4_lookat(
rk_mat4 & ret,
rk_vec3 const & position,
rk_vec3 const & lookat) {
ret = glm::lookAtRH(position, lookat, glm::vec3(0.0f, 0.0f, 1.0f));
}
void rk_mat4_mul_vec4( void rk_mat4_mul_vec4(
rk_vec4 & ret, rk_vec4 & ret,
rk_mat4 const & a, rk_mat4 const & a,

View File

@ -41,18 +41,6 @@ RK_EXPORT void rk_mat3_mul_mat3(
rk_mat3 const & a, rk_mat3 const & a,
rk_mat3 const & b); rk_mat3 const & b);
RK_EXPORT void rk_mat4_projection(
rk_mat4 & ret,
float hfov,
float ratio,
float near,
float far);
RK_EXPORT void rk_mat4_lookat(
rk_mat4 & ret,
rk_vec3 const & position,
rk_vec3 const & lookat);
RK_EXPORT void rk_mat4_mul_vec4( RK_EXPORT void rk_mat4_mul_vec4(
rk_vec4 & ret, rk_vec4 & ret,
rk_mat4 const & a, rk_mat4 const & a,

View File

@ -18,6 +18,7 @@
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);
@ -145,6 +146,10 @@ 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;
} }
@ -154,6 +159,17 @@ 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));
}
}
} }
} }
@ -177,28 +193,23 @@ 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) {
case RK_INPUT_IDENTITY:
glUniform3fv(input, 1, glm::value_ptr(value)); glUniform3fv(input, 1, glm::value_ptr(value));
break;
case RK_INPUT_VIEW_POSITION: {
glUniform3fv(input, 1, glm::value_ptr(rk_view * rk_vec4(value, 1.0)));
break;
}
case RK_INPUT_VIEW_ORIENTATION: {
glUniform3fv(input, 1, glm::value_ptr(rk_view * rk_vec4(value, 0.0)));
break;
} }
} }
void rk_set_input_mat3(
rk_input_t _input,
rk_mat3 const & value) {
GLint const input = reinterpret_cast<intptr_t>(_input) - 1;
if (rk_current_shader && input > -1) {
glUniformMatrix3fv(input, 1, GL_FALSE, glm::value_ptr(value));
}
}
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));
} }
} }
@ -479,6 +490,7 @@ 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);
} }
@ -709,6 +721,7 @@ 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,10 +27,17 @@ 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 {

48
cpp/render.cpp Normal file
View File

@ -0,0 +1,48 @@
// 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,6 +19,17 @@
#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;
@ -29,6 +40,12 @@ 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,
@ -101,15 +118,8 @@ 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,