Compare commits
3 Commits
1855778fa7
...
dfc2bbc408
Author | SHA1 | Date | |
---|---|---|---|
dfc2bbc408 | |||
59df9e8748 | |||
601336f1f9 |
2
Makefile
2
Makefile
@ -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
|
||||
|
||||
CXXFLAGS = -fpic -Wall -Werror -O2 -flto -fomit-frame-pointer -ffast-math -funroll-loops -fno-rtti -fno-exceptions
|
||||
|
70
__init__.py
70
__init__.py
@ -16,16 +16,13 @@
|
||||
import ctypes
|
||||
import struct
|
||||
from array import array
|
||||
from pathlib import Path
|
||||
|
||||
_lib = ctypes.cdll.LoadLibrary("./engine/engine.so")
|
||||
_lib = ctypes.cdll.LoadLibrary(Path(__file__).parent / "engine.so")
|
||||
|
||||
def _flag(x):
|
||||
return 1 << x
|
||||
|
||||
INPUT_IDENTITY = 0
|
||||
INPUT_VIEW_POSITION = 1
|
||||
INPUT_VIEW_ORIENTATION = 2
|
||||
|
||||
TEXTURE_FORMAT_SRGB8_A8 = 0
|
||||
TEXTURE_FORMAT_RGBA8 = 1
|
||||
TEXTURE_FORMAT_RGB10_A2 = 2
|
||||
@ -162,6 +159,28 @@ def mat3_mul_mat3(ret, a, b):
|
||||
assert len(ret) == 9 and len(a) == 9 and len(b) == 9
|
||||
_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.argtypes = (
|
||||
_vec4_t, # ret
|
||||
@ -215,12 +234,29 @@ set_input_float.argtypes = (
|
||||
_set_input_vec3 = _lib.rk_set_input_vec3
|
||||
_set_input_vec3.argtypes = (
|
||||
ctypes.c_void_p, # input
|
||||
_vec3_t, # value
|
||||
ctypes.c_uint) # mode
|
||||
_vec3_t) # value
|
||||
|
||||
def set_input_vec3(input, value, mode = INPUT_IDENTITY):
|
||||
def set_input_vec3(input, value):
|
||||
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.restype = ctypes.c_void_p
|
||||
@ -268,22 +304,6 @@ create_batch.argtypes = (
|
||||
ctypes.c_uint, # translation_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
|
||||
|
||||
select_texture = _lib.rk_select_texture
|
||||
|
16
cpp/math.cpp
16
cpp/math.cpp
@ -38,6 +38,22 @@ void rk_mat3_mul_mat3(
|
||||
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(
|
||||
rk_vec4 & ret,
|
||||
rk_mat4 const & a,
|
||||
|
12
cpp/math.hpp
12
cpp/math.hpp
@ -41,6 +41,18 @@ RK_EXPORT void rk_mat3_mul_mat3(
|
||||
rk_mat3 const & a,
|
||||
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_vec4 & ret,
|
||||
rk_mat4 const & a,
|
||||
|
@ -18,7 +18,6 @@
|
||||
|
||||
static rk_shader const * rk_current_shader = nullptr;
|
||||
static rk_vertices const * rk_current_vertices = nullptr;
|
||||
static bool rk_frame = false;
|
||||
|
||||
static void rk_printf(char const * 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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -159,17 +154,6 @@ void rk_select_shader(
|
||||
if (shader) {
|
||||
rk_current_shader = shader;
|
||||
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(
|
||||
rk_input_t _input,
|
||||
rk_vec3 const & value,
|
||||
rk_input_mode mode) {
|
||||
rk_vec3 const & value) {
|
||||
GLint const input = reinterpret_cast<intptr_t>(_input) - 1;
|
||||
if (rk_current_shader && input > -1) {
|
||||
switch (mode) {
|
||||
case RK_INPUT_IDENTITY:
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -490,7 +479,6 @@ rk_batch_t rk_create_batch(
|
||||
}
|
||||
|
||||
void rk_begin_frame() {
|
||||
rk_frame = true;
|
||||
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() {
|
||||
rk_swap_buffers();
|
||||
rk_frame = false;
|
||||
}
|
||||
|
||||
void rk_destroy_batch(
|
||||
|
@ -27,17 +27,10 @@ enum : rk_uint {
|
||||
RK_PARAMS_BINDING = 1
|
||||
};
|
||||
|
||||
struct rk_uniforms {
|
||||
GLint view;
|
||||
GLint view_km;
|
||||
GLint projection;
|
||||
};
|
||||
|
||||
struct rk_shader {
|
||||
GLuint vertex;
|
||||
GLuint fragment;
|
||||
GLuint program;
|
||||
rk_uniforms uniforms;
|
||||
};
|
||||
|
||||
struct rk_texture {
|
||||
|
@ -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));
|
||||
}
|
@ -19,17 +19,6 @@
|
||||
#include "types.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_shader_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))
|
||||
|
||||
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 {
|
||||
RK_TEXTURE_FORMAT_SRGB8_A8 = 0,
|
||||
RK_TEXTURE_FORMAT_RGBA8 = 1,
|
||||
@ -118,8 +101,15 @@ RK_EXPORT void rk_set_input_float(
|
||||
|
||||
RK_EXPORT void rk_set_input_vec3(
|
||||
rk_input_t input,
|
||||
rk_vec3 const & value,
|
||||
rk_input_mode mode);
|
||||
rk_vec3 const & value);
|
||||
|
||||
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_uint slot,
|
||||
|
Loading…
Reference in New Issue
Block a user