Implemented 3d immediate renderer api.

This commit is contained in:
Relintai 2024-01-13 20:44:02 +01:00
parent e2ff0f1969
commit 56ab8fef14
7 changed files with 304 additions and 48 deletions

View File

@ -223,14 +223,15 @@ void GameScene::render() {
_mesh_utils_test_mi->render(); _mesh_utils_test_mi->render();
_mesh_utils_test_mi->render(); _mesh_utils_test_mi->render();
} else if (render_type == 12) {
render_immediate_3d();
} }
} }
void GameScene::render_immediate() { void GameScene::render_immediate() {
Renderer *r = Renderer::get_singleton(); Renderer *r = Renderer::get_singleton();
//r->camera_2d_reset();
r->clear_screen(Color()); r->clear_screen(Color());
r->camera_2d_projection_set_to_window();
r->draw_point(Vector2(15, 15)); r->draw_point(Vector2(15, 15));
r->draw_point(Vector2(18, 18), Color(1, 1, 0)); r->draw_point(Vector2(18, 18), Color(1, 1, 0));
@ -306,6 +307,30 @@ void GameScene::render_obj() {
//TextRenderer::get_singleton()->font_print("test"); //TextRenderer::get_singleton()->font_print("test");
} }
void GameScene::render_immediate_3d() {
Renderer *r = Renderer::get_singleton();
r->clear_screen(Color());
r->camera_2d_projection_set_to_window();
r->camera_3d_bind();
r->camera_3d_projection_set_to_perspective(AppWindow::get_singleton()->get_aspect());
static float rotmi = 0;
_mesh_utils_test->clear();
MeshUtils::create_simple_test_cone(_mesh_utils_test);
_mesh_utils_test->upload();
Transform tf;
tf.basis = Basis(Vector3(1, 0, 0), rotmi);
tf.origin.z -= 2;
r->draw_mesh_3d_vertex_colored(_mesh_utils_test, tf);
rotmi += 0.01;
}
GameScene::GameScene() { GameScene::GameScene() {
render_type = 0; render_type = 0;

View File

@ -24,7 +24,7 @@ class GameScene : public Scene {
public: public:
enum RenderTypes { enum RenderTypes {
RENDER_TYPE_MAX = 12 RENDER_TYPE_MAX = 13
}; };
virtual void input_event(const Ref<InputEvent> &event); virtual void input_event(const Ref<InputEvent> &event);
@ -32,6 +32,7 @@ public:
virtual void render(); virtual void render();
virtual void render_immediate(); virtual void render_immediate();
virtual void render_obj(); virtual void render_obj();
virtual void render_immediate_3d();
GameScene(); GameScene();
~GameScene(); ~GameScene();

View File

@ -1,14 +1,17 @@
//--STRIP //--STRIP
#include "renderer.h" #include "renderer.h"
#include "render_core/app_window.h"
#include "render_core/color_material.h"
#include "render_core/color_material_2d.h" #include "render_core/color_material_2d.h"
#include "render_core/colored_material.h"
#include "render_core/font.h" #include "render_core/font.h"
#include "render_core/font_material.h" #include "render_core/font_material.h"
#include "render_core/material.h" #include "render_core/material.h"
#include "render_core/mesh.h" #include "render_core/mesh.h"
#include "render_core/texture.h" #include "render_core/texture.h"
#include "render_core/texture_material.h"
#include "render_core/texture_material_2d.h" #include "render_core/texture_material_2d.h"
#include "render_core/app_window.h"
#include "render_core/render_state.h" #include "render_core/render_state.h"
//--STRIP //--STRIP
@ -270,31 +273,117 @@ void Renderer::draw_text_2d_tf_material(const String &p_text, const Ref<Font> &p
} }
void Renderer::draw_mesh_3d(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform &p_transform) { void Renderer::draw_mesh_3d(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform &p_transform) {
ERR_FAIL_COND(!p_mesh.is_valid());
ERR_FAIL_COND(!p_material.is_valid());
Ref<Mesh> mesh = p_mesh;
Ref<Material> material = p_material;
camera_3d_push_model_view_matrix(p_transform);
material->bind();
mesh->render();
camera_3d_pop_model_view_matrix();
}
void Renderer::draw_mesh_3d_colored(const Ref<Mesh> &p_mesh, const Color &p_color, const Transform &p_transform) {
ERR_FAIL_COND(!p_mesh.is_valid());
Ref<Mesh> mesh = p_mesh;
_colored_material_3d->color = p_color;
camera_3d_push_model_view_matrix(p_transform);
_colored_material_3d->bind();
mesh->render();
camera_3d_pop_model_view_matrix();
}
void Renderer::draw_mesh_3d_vertex_colored(const Ref<Mesh> &p_mesh, const Transform &p_transform) {
ERR_FAIL_COND(!p_mesh.is_valid());
Ref<Mesh> mesh = p_mesh;
camera_3d_push_model_view_matrix(p_transform);
_color_material_3d->bind();
mesh->render();
camera_3d_pop_model_view_matrix();
}
void Renderer::draw_mesh_3d_textured(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Transform &p_transform) {
ERR_FAIL_COND(!p_mesh.is_valid());
ERR_FAIL_COND(!p_texture.is_valid());
_texture_material_3d->texture = p_texture;
Ref<Mesh> mesh = p_mesh;
camera_3d_push_model_view_matrix(p_transform);
_texture_material_3d->bind();
mesh->render();
camera_3d_pop_model_view_matrix();
} }
Transform2D Renderer::camera_2d_get_current_model_view_matrix() const { void Renderer::camera_2d_bind() {
return RenderState::model_view_matrix_2d; RenderState::model_view_matrix_2d = _camera_2d_model_view_matrix;
RenderState::projection_matrix_2d = _camera_2d_projection_matrix;
} }
void Renderer::camera_2d_push_model_view_matrix(const Transform2D &p_transform_2d) { Transform Renderer::camera_2d_get_current_projection_matrix() const {
camera_2d_model_view_matrix_stack.push_back(RenderState::model_view_matrix_2d); return _camera_2d_projection_matrix;
RenderState::model_view_matrix_2d *= p_transform_2d;
} }
void Renderer::camera_2d_pop_model_view_matrix() { void Renderer::camera_2d_push_projection_matrix(const Transform &p_transform) {
if (camera_2d_model_view_matrix_stack.empty()) { _camera_2d_projection_matrix_stack.push_back(_camera_2d_projection_matrix);
_camera_2d_projection_matrix *= p_transform;
RenderState::projection_matrix_2d = _camera_2d_projection_matrix;
}
void Renderer::camera_2d_pop_projection_matrix() {
if (_camera_2d_projection_matrix_stack.empty()) {
return; return;
} }
RenderState::model_view_matrix_2d = camera_2d_model_view_matrix_stack[camera_2d_model_view_matrix_stack.size() - 1]; _camera_2d_projection_matrix = _camera_2d_projection_matrix_stack[_camera_2d_projection_matrix_stack.size() - 1];
camera_2d_model_view_matrix_stack.resize(camera_2d_model_view_matrix_stack.size() - 1);
RenderState::projection_matrix_2d = _camera_2d_projection_matrix;
_camera_2d_projection_matrix_stack.resize(_camera_2d_projection_matrix_stack.size() - 1);
}
int Renderer::get_camera_2d_projection_matrix_stack_size() const {
return _camera_2d_projection_matrix_stack.size();
}
Transform2D Renderer::camera_2d_get_current_model_view_matrix() const {
return _camera_2d_model_view_matrix;
}
void Renderer::camera_2d_push_model_view_matrix(const Transform2D &p_transform_2d) {
_camera_2d_model_view_matrix_stack.push_back(_camera_2d_model_view_matrix);
_camera_2d_model_view_matrix *= p_transform_2d;
RenderState::model_view_matrix_2d = _camera_2d_model_view_matrix;
}
void Renderer::camera_2d_pop_model_view_matrix() {
if (_camera_2d_model_view_matrix_stack.empty()) {
return;
}
_camera_2d_model_view_matrix = _camera_2d_model_view_matrix_stack[_camera_2d_model_view_matrix_stack.size() - 1];
RenderState::model_view_matrix_2d = _camera_2d_model_view_matrix;
_camera_2d_model_view_matrix_stack.resize(_camera_2d_model_view_matrix_stack.size() - 1);
} }
int Renderer::get_camera_2d_model_view_matrix_stack_size() const { int Renderer::get_camera_2d_model_view_matrix_stack_size() const {
return camera_2d_model_view_matrix_stack.size(); return _camera_2d_model_view_matrix_stack.size();
} }
void Renderer::camera_2d_reset() { void Renderer::camera_2d_projection_set_to_window() {
Vector2 size = get_window_size(); Vector2 size = get_window_size();
Transform canvas_transform; Transform canvas_transform;
@ -305,10 +394,105 @@ void Renderer::camera_2d_reset() {
RenderState::model_view_matrix_2d = Transform2D(); RenderState::model_view_matrix_2d = Transform2D();
RenderState::projection_matrix_2d = canvas_transform; RenderState::projection_matrix_2d = canvas_transform;
camera_2d_model_view_matrix_stack.clear(); _camera_2d_model_view_matrix_stack.clear();
} }
void Renderer::camera_3d_reset() { void Renderer::camera_3d_bind() {
RenderState::camera_transform_3d = _camera_3d_camera_transform_matrix;
RenderState::model_view_matrix_3d = _camera_3d_model_view_matrix;
RenderState::projection_matrix_3d = _camera_3d_projection;
}
Transform Renderer::camera_3d_get_current_camera_transform_matrix() const {
return _camera_3d_camera_transform_matrix;
}
void Renderer::camera_3d_push_camera_transform_matrix(const Transform &p_transform) {
_camera_3d_camera_transform_matrix_stack.push_back(_camera_3d_camera_transform_matrix);
_camera_3d_camera_transform_matrix *= p_transform;
RenderState::camera_transform_3d = _camera_3d_camera_transform_matrix;
}
void Renderer::camera_3d_pop_camera_transform_matrix() {
if (_camera_3d_camera_transform_matrix_stack.empty()) {
return;
}
_camera_3d_camera_transform_matrix = _camera_3d_camera_transform_matrix_stack[_camera_3d_camera_transform_matrix_stack.size() - 1];
RenderState::camera_transform_3d = _camera_3d_camera_transform_matrix;
_camera_3d_camera_transform_matrix_stack.resize(_camera_3d_camera_transform_matrix_stack.size() - 1);
}
int Renderer::get_camera_3d_camera_transform_matrix_stack_size() const {
return _camera_3d_camera_transform_matrix_stack.size();
}
Transform Renderer::camera_3d_get_current_model_view_matrix() const {
return _camera_3d_model_view_matrix;
}
void Renderer::camera_3d_push_model_view_matrix(const Transform &p_transform) {
_camera_3d_model_view_matrix_stack.push_back(_camera_3d_model_view_matrix);
_camera_3d_model_view_matrix *= p_transform;
RenderState::model_view_matrix_3d = _camera_3d_model_view_matrix;
}
void Renderer::camera_3d_pop_model_view_matrix() {
if (_camera_3d_model_view_matrix_stack.empty()) {
return;
}
_camera_3d_model_view_matrix = _camera_3d_model_view_matrix_stack[_camera_3d_model_view_matrix_stack.size() - 1];
RenderState::model_view_matrix_3d = _camera_3d_model_view_matrix;
_camera_3d_model_view_matrix_stack.resize(_camera_3d_model_view_matrix_stack.size() - 1);
}
int Renderer::get_camera_3d_model_view_matrix_stack_size() const {
return _camera_3d_model_view_matrix_stack.size();
}
// Aspect Ratio = w / h
void Renderer::camera_3d_projection_set_to_orthographic(float aspect_ratio, float size, float znear, float zfar, bool vaspect) {
_camera_3d_projection.set_orthogonal(
size,
aspect_ratio,
znear,
zfar,
vaspect);
RenderState::projection_matrix_3d = _camera_3d_projection;
}
void Renderer::camera_3d_projection_set_to_perspective(float aspect_ratio, float size, float znear, float zfar, bool vaspect, float fov) {
_camera_3d_projection.set_perspective(
fov,
aspect_ratio,
znear,
zfar,
vaspect);
RenderState::projection_matrix_3d = _camera_3d_projection;
}
void Renderer::camera_3d_projection_set_to_frustum(float aspect_ratio, float size, float znear, float zfar, bool vaspect, float offset) {
_camera_3d_projection.set_frustum(
size,
aspect_ratio,
offset,
znear,
zfar,
vaspect);
RenderState::projection_matrix_3d = _camera_3d_projection;
}
Projection Renderer::camera_3d_get_projection_matrix() const {
return _camera_3d_projection;
}
void Renderer::camera_3d_set_projection_matrix(const Projection &p_projection) {
_camera_3d_projection = p_projection;
RenderState::projection_matrix_3d = _camera_3d_projection;
} }
void Renderer::clear_screen(const Color &p_color) { void Renderer::clear_screen(const Color &p_color) {
@ -319,9 +503,6 @@ void Renderer::clear_screen(const Color &p_color) {
} else { } else {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} }
//? Make it manual?
camera_2d_reset();
} }
Vector2i Renderer::get_window_size() const { Vector2i Renderer::get_window_size() const {
@ -357,6 +538,10 @@ Renderer::Renderer() {
_texture_material_2d.instance(); _texture_material_2d.instance();
_font_material.instance(); _font_material.instance();
_color_material_2d.instance(); _color_material_2d.instance();
_texture_material_3d.instance();
_color_material_3d.instance();
_colored_material_3d.instance();
} }
Renderer::~Renderer() { Renderer::~Renderer() {
_singleton = NULL; _singleton = NULL;

View File

@ -21,6 +21,9 @@ class Font;
class FontMaterial; class FontMaterial;
class TextureMaterial2D; class TextureMaterial2D;
class ColorMaterial2D; class ColorMaterial2D;
class TextureMaterial;
class ColorMaterial;
class ColoredMaterial;
class Renderer : public Object { class Renderer : public Object {
SFW_OBJECT(Renderer, Object); SFW_OBJECT(Renderer, Object);
@ -48,24 +51,50 @@ public:
void draw_text_2d_tf(const String &p_text, const Ref<Font> &p_font, const Transform2D &p_transform_2d, const Color &p_color = Color(1, 1, 1)); void draw_text_2d_tf(const String &p_text, const Ref<Font> &p_font, const Transform2D &p_transform_2d, const Color &p_color = Color(1, 1, 1));
void draw_text_2d_tf_material(const String &p_text, const Ref<Font> &p_font, const Ref<Material> &p_material, const Transform2D &p_transform_2d, const Color &p_color = Color(1, 1, 1)); void draw_text_2d_tf_material(const String &p_text, const Ref<Font> &p_font, const Ref<Material> &p_material, const Transform2D &p_transform_2d, const Color &p_color = Color(1, 1, 1));
//add draw colored, draw textured, also with and without transform -> p_transform give default arg void draw_mesh_3d(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform &p_transform = Transform());
void draw_mesh_3d(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform &p_transform); void draw_mesh_3d_colored(const Ref<Mesh> &p_mesh, const Color &p_color, const Transform &p_transform = Transform());
void draw_mesh_3d_vertex_colored(const Ref<Mesh> &p_mesh, const Transform &p_transform = Transform());
void draw_mesh_3d_textured(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Transform &p_transform = Transform());
//TODO //2D Camera API
//camera set transform
//3d cam matrix stack void camera_2d_bind();
//3d cam proj setup
//reset -> rename camera_2d_projection_setup(), push etc Transform camera_2d_get_current_projection_matrix() const;
//3d proj setup void camera_2d_push_projection_matrix(const Transform &p_transform);
//3d camera matrix stack void camera_2d_pop_projection_matrix();
int get_camera_2d_projection_matrix_stack_size() const;
Transform2D camera_2d_get_current_model_view_matrix() const; Transform2D camera_2d_get_current_model_view_matrix() const;
void camera_2d_push_model_view_matrix(const Transform2D &p_transform_2d); void camera_2d_push_model_view_matrix(const Transform2D &p_transform_2d);
void camera_2d_pop_model_view_matrix(); void camera_2d_pop_model_view_matrix();
int get_camera_2d_model_view_matrix_stack_size() const; int get_camera_2d_model_view_matrix_stack_size() const;
void camera_2d_reset();
void camera_3d_reset(); void camera_2d_projection_set_to_window();
//3D Camera API
void camera_3d_bind();
Transform camera_3d_get_current_camera_transform_matrix() const;
void camera_3d_push_camera_transform_matrix(const Transform &p_transform);
void camera_3d_pop_camera_transform_matrix();
int get_camera_3d_camera_transform_matrix_stack_size() const;
Transform camera_3d_get_current_model_view_matrix() const;
void camera_3d_push_model_view_matrix(const Transform &p_transform);
void camera_3d_pop_model_view_matrix();
int get_camera_3d_model_view_matrix_stack_size() const;
// Aspect Ratio = w / h
void camera_3d_projection_set_to_orthographic(float aspect_ratio, float size = 1.0, float znear = 0.05, float zfar = 100, bool vaspect = false);
void camera_3d_projection_set_to_perspective(float aspect_ratio, float size = 1.0, float znear = 0.05, float zfar = 100, bool vaspect = false, float fov = 70);
void camera_3d_projection_set_to_frustum(float aspect_ratio, float size = 1.0, float znear = 0.05, float zfar = 100, bool vaspect = false, float offset = 0);
Projection camera_3d_get_projection_matrix() const;
void camera_3d_set_projection_matrix(const Projection &p_projection);
// Other Helpers
void clear_screen(const Color &p_color); void clear_screen(const Color &p_color);
@ -93,7 +122,22 @@ private:
Ref<FontMaterial> _font_material; Ref<FontMaterial> _font_material;
Ref<ColorMaterial2D> _color_material_2d; Ref<ColorMaterial2D> _color_material_2d;
Vector<Transform2D> camera_2d_model_view_matrix_stack; Transform _camera_2d_projection_matrix;
Transform2D _camera_2d_model_view_matrix;
Vector<Transform> _camera_2d_projection_matrix_stack;
Vector<Transform2D> _camera_2d_model_view_matrix_stack;
Projection _camera_3d_projection;
Transform _camera_3d_camera_transform_matrix;
Transform _camera_3d_model_view_matrix;
Vector<Transform> _camera_3d_camera_transform_matrix_stack;
Vector<Transform> _camera_3d_model_view_matrix_stack;
Ref<TextureMaterial> _texture_material_3d;
Ref<ColorMaterial> _color_material_3d;
Ref<ColoredMaterial> _colored_material_3d;
}; };
//--STRIP //--STRIP

View File

@ -28,8 +28,8 @@ public:
down = pressed; down = pressed;
} else if (scancode == KEY_D) { } else if (scancode == KEY_D) {
right = pressed; right = pressed;
} }
if (k->get_physical_scancode() == KEY_SPACE) { if (k->get_physical_scancode() == KEY_SPACE) {
if (pressed) { if (pressed) {
immediate = !immediate; immediate = !immediate;
@ -83,9 +83,10 @@ public:
virtual void render_immediate() { virtual void render_immediate() {
Renderer *r = Renderer::get_singleton(); Renderer *r = Renderer::get_singleton();
//r->camera_2d_reset(); //r->camera_2d_projection_set_to_window();
r->clear_screen(Color()); r->clear_screen(Color());
r->camera_2d_projection_set_to_window();
r->draw_point(Vector2(15, 15)); r->draw_point(Vector2(15, 15));
r->draw_point(Vector2(18, 18), Color(1, 1, 0)); r->draw_point(Vector2(18, 18), Color(1, 1, 0));

View File

@ -7,13 +7,14 @@ class GameApplication : public Application {
public: public:
virtual void input_event(const Ref<InputEvent> &event) {} virtual void input_event(const Ref<InputEvent> &event) {}
virtual void update(float delta) { virtual void update(float delta) {
_font_pos.x += 500 * delta; _font_pos.x += 500 * delta;
if (_font_pos.x > 1800) { if (_font_pos.x > 1800) {
_font_pos.x = 0; _font_pos.x = 0;
} }
} }
virtual void render() { virtual void render() {
Renderer::get_singleton()->clear_screen(Color()); Renderer::get_singleton()->clear_screen(Color());
Renderer::get_singleton()->camera_2d_projection_set_to_window();
Renderer::get_singleton()->draw_text_2d("Test!", _font, _font_pos); Renderer::get_singleton()->draw_text_2d("Test!", _font, _font_pos);
} }
@ -29,11 +30,10 @@ public:
Renderer::destroy(); Renderer::destroy();
} }
Vector2 _font_pos; Vector2 _font_pos;
Ref<Font> _font; Ref<Font> _font;
}; };
int main(int argc, char **argv) { int main(int argc, char **argv) {
Application *application = memnew(GameApplication()); Application *application = memnew(GameApplication());

View File

@ -7,13 +7,14 @@ class GameApplication : public Application {
public: public:
virtual void input_event(const Ref<InputEvent> &event) {} virtual void input_event(const Ref<InputEvent> &event) {}
virtual void update(float delta) { virtual void update(float delta) {
_font_pos.x += 500 * delta; _font_pos.x += 500 * delta;
if (_font_pos.x > 1800) { if (_font_pos.x > 1800) {
_font_pos.x = 0; _font_pos.x = 0;
} }
} }
virtual void render() { virtual void render() {
Renderer::get_singleton()->clear_screen(Color()); Renderer::get_singleton()->clear_screen(Color());
Renderer::get_singleton()->camera_2d_projection_set_to_window();
Renderer::get_singleton()->draw_text_2d("Test!", _font, _font_pos); Renderer::get_singleton()->draw_text_2d("Test!", _font, _font_pos);
} }
@ -29,11 +30,10 @@ public:
Renderer::destroy(); Renderer::destroy();
} }
Vector2 _font_pos; Vector2 _font_pos;
Ref<Font> _font; Ref<Font> _font;
}; };
int main(int argc, char **argv) { int main(int argc, char **argv) {
Application *application = memnew(GameApplication()); Application *application = memnew(GameApplication());