Implemented 2d methods for the immediate renderer. Also various cleanups.

This commit is contained in:
Relintai 2024-01-04 21:33:41 +01:00
parent 15b1514cd0
commit 272df2c00c
14 changed files with 525 additions and 72 deletions

View File

@ -6,6 +6,7 @@
#include "render_core/3rd_glad.h"
#include "render_core/keyboard.h"
#include "render_core/mesh_utils.h"
#include "render_immediate/renderer.h"
//#include "render_core/font.h"
void GameScene::input_event(const Ref<InputEvent> &event) {
@ -68,6 +69,62 @@ void GameScene::update(float delta) {
}
void GameScene::render() {
//render_obj();
render_immediate();
}
void GameScene::render_immediate() {
Renderer *r = Renderer::get_singleton();
//r->camera_2d_reset();
r->clear_screen(Color());
r->draw_point(Vector2(15, 15));
r->draw_point(Vector2(18, 18), Color(1, 1, 0));
r->draw_line(Vector2(20, 20), Vector2(80, 20));
r->draw_line(Vector2(20, 20), Vector2(80, 40));
r->draw_line(Vector2(20, 30), Vector2(80, 30), Color(1, 1, 0));
r->draw_line(Vector2(20, 40), Vector2(80, 40), Color(1, 1, 0), 4);
r->draw_line(Vector2(20, 40), Vector2(80, 60), Color(1, 1, 0), 4);
r->draw_line_rect(Rect2(100, 100, 40, 40));
r->draw_line_rect(Rect2(150, 100, 40, 40), Color(1, 1, 0));
r->draw_line_rect(Rect2(200, 100, 40, 40), Color(1, 1, 1), 4);
r->draw_rect(Rect2(100, 150, 40, 40));
r->draw_rect(Rect2(150, 150, 40, 40), Color(1, 1, 0));
r->draw_texture(texture, Rect2(100, 200, 40, 40));
r->draw_texture(texture, Rect2(150, 200, 40, 40), Color(1, 0, 0));
r->draw_texture_clipped(texture, Rect2(20, 20, 30, 30), Rect2(100, 250, 40, 40));
r->draw_texture_clipped(texture, Rect2(20, 20, 30, 30), Rect2(150, 250, 40, 40), Color(1, 0, 0));
Transform2D t = Transform2D().rotated(Math_PI / 26.0);
r->draw_texture_tr(t, texture, Rect2(100, 300, 40, 40));
r->draw_texture_tr(t, texture, Rect2(150, 300, 40, 40), Color(1, 0, 0));
r->draw_texture_clipped_tr(t, texture, Rect2(20, 20, 30, 30), Rect2(100, 350, 40, 40));
r->draw_texture_clipped_tr(t, texture, Rect2(20, 20, 30, 30), Rect2(150, 350, 40, 40), Color(1, 0, 0));
//r->draw_mesh_2d(sprite->mesh_instance->mesh, texture, Vector2(1000, 500));
//r->draw_mesh_2d_tr(sprite->mesh_instance->mesh, texture, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1000, 500)));
//r->draw_mesh_2d_mat(sprite->mesh_instance->mesh, material, Vector2(1000, 500));
//r->draw_mesh_2d_mat_tr(sprite->mesh_instance->mesh, material, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1000, 600)));
r->draw_text_2d("draw_text_2d1", _font, Vector2(700, 100));
r->draw_text_2d("draw_text_2d2", _font, Vector2(800, 100), Color(1, 1, 0));
r->draw_text_2d_tf("draw_text_2d_tf1", _font, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1000, 500)));
r->draw_text_2d_tf("draw_text_2d_tf2", _font, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1200, 500)), Color(0, 1, 0));
r->draw_text_2d_tf_material("draw_text_2d_tf_material1", _font, _font_test_mat, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1000, 800)));
r->draw_text_2d_tf_material("draw_text_2d_tf_material2", _font, _font_test_mat, Transform2D().rotated(Math_PI / 26.0).translated(Vector2(1200, 800)), Color(1, 0, 0));
}
void GameScene::render_obj() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
@ -82,11 +139,6 @@ void GameScene::render() {
camera->bind();
//sprite->render();
//material->bind();
//color_material->bind();
//mesh->render();
static float rotmi = 0;
mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
@ -122,7 +174,7 @@ GameScene::GameScene() {
_font_test_sprite = memnew(Sprite);
_font_test_mat = memnew(FontMaterial());
_font_test_mat.instance();
_font_test_mat->texture = _font->get_texture();
_font_test_sprite->mesh_instance->material = _font_test_mat;
_font_test_sprite->width = _font->get_atlas_width();
@ -135,7 +187,7 @@ GameScene::GameScene() {
_font_test_mi = memnew(MeshInstance2D());
_font_test_mi->material = _font_test_mat;
_font_test_mi->mesh = _font_test_mesh.ptr();
_font_test_mi->mesh = _font_test_mesh;
//_font_test_mi->transform.scale(Vector2(10, 10));
_font_test_mi->transform.set_origin(Vector2(1000, 400));
@ -146,13 +198,13 @@ GameScene::GameScene() {
image->load_from_file("icon.png");
//image->bumpmap_to_normalmap();
texture = new Texture();
texture.instance();
//texture->load_image("icon.png");
texture->create_from_image(image);
//ha a textúrának van alpha csatornája:
//texture->load_image("download.bmp", GL_RGBA, GL_RGBA);
material = new TextureMaterial2D();
material.instance();
material->texture = texture;
sprite = new Sprite();
@ -167,7 +219,7 @@ GameScene::GameScene() {
sprite->update_mesh();
tile_map = new TileMap();
tile_map->material = material;
tile_map->material = material.ptr();
tile_map->atlas_size_x = 2;
tile_map->atlas_size_y = 2;
@ -206,7 +258,7 @@ GameScene::GameScene() {
mesh = memnew(Mesh());
//cmaterial = memnew(ColoredMaterial());
//cmaterial->color = glm::vec4(1, 1, 0, 1);
color_material = memnew(ColorMaterial());
color_material.instance();
//mesh->clear();
@ -214,11 +266,11 @@ GameScene::GameScene() {
mesh->upload();
mi = memnew(MeshInstance3D());
mi->material = color_material;
mi->material = color_material.ptr();
mi->mesh = mesh;
mi2 = memnew(MeshInstance3D());
mi2->material = color_material;
mi2->material = color_material.ptr();
mi2->mesh = mesh;
mi2->transform.origin.x = 1;
@ -264,18 +316,14 @@ GameScene::GameScene() {
_text_2d->set_text_color(Color(1, 1, 0));
_text_2d->update();
_text_2d->transform.set_origin(Vector2(1200, 250));
Renderer::initialize();
}
GameScene::~GameScene() {
Renderer::destroy();
memdelete(tile_map);
memdelete(camera);
memdelete(texture);
memdelete(sprite);
memdelete(camera);
memdelete(mesh);
memdelete(material);
memdelete(color_material);
}

View File

@ -26,6 +26,8 @@ public:
virtual void input_event(const Ref<InputEvent> &event);
virtual void update(float delta);
virtual void render();
virtual void render_immediate();
virtual void render_obj();
GameScene();
~GameScene();
@ -36,12 +38,12 @@ public:
bool down;
Ref<Image> image;
Texture *texture;
TextureMaterial2D *material;
Ref<Texture> texture;
Ref<TextureMaterial2D> material;
Ref<Font> _font;
Sprite *_font_test_sprite;
FontMaterial *_font_test_mat;
Ref<FontMaterial> _font_test_mat;
Ref<Mesh> _font_test_mesh;
MeshInstance2D *_font_test_mi;
@ -54,7 +56,7 @@ public:
Mesh *mesh;
MeshInstance3D *mi;
MeshInstance3D *mi2;
ColorMaterial *color_material;
Ref<ColorMaterial> color_material;
Text2D *_text_2d;

View File

@ -6,6 +6,8 @@
#include "render_core/render_state.h"
class ColorMaterial : public Material {
SFW_OBJECT(ColorMaterial, Material);
public:
int get_material_id() {
return 2;

View File

@ -0,0 +1,77 @@
#ifndef COLOR_MATERIAL_2D_H
#define COLOR_MATERIAL_2D_H
#include "render_core/material.h"
#include "render_core/texture.h"
#include "render_core/render_state.h"
class ColorMaterial2D : public Material {
SFW_OBJECT(ColorMaterial2D, Material);
public:
int get_material_id() {
return 17;
}
void bind_uniforms() {
set_uniform(projection_matrix_location, RenderState::projection_matrix_2d);
set_uniform(model_view_matrix_location, RenderState::model_view_matrix_2d);
}
void setup_uniforms() {
projection_matrix_location = get_uniform("u_proj_matrix");
model_view_matrix_location = get_uniform("u_model_view_matrix");
}
void unbind() {
glDisable(GL_TEXTURE_2D);
}
void setup_state() {
glEnable(GL_TEXTURE_2D);
}
const GLchar **get_vertex_shader_source() {
static const GLchar *vertex_shader_source[] = {
"uniform mat4 u_proj_matrix;\n"
"uniform mat4 u_model_view_matrix;\n"
"\n"
"attribute vec4 a_position;\n"
"attribute vec4 a_color;\n"
"\n"
"varying vec4 v_color;\n"
"\n"
"void main() {\n"
" v_color = a_color;\n"
" gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n"
"}"
};
return vertex_shader_source;
}
const GLchar **get_fragment_shader_source() {
static const GLchar *fragment_shader_source[] = {
"precision mediump float;\n"
"\n"
"varying vec4 v_color;\n"
"\n"
"void main() {\n"
" gl_FragColor = v_color;\n"
"}"
};
return fragment_shader_source;
}
ColorMaterial2D() {
projection_matrix_location = 0;
model_view_matrix_location = 0;
}
GLint projection_matrix_location;
GLint model_view_matrix_location;
};
#endif // COLORED_MATERIAL_H

View File

@ -314,7 +314,7 @@ void Font::font_face(const char *filename_ttf, float font_size, unsigned flags)
*/
}
Vector2 Font::generate_mesh(const String &p_text, Ref<Mesh> &p_into, const Color &p_color) {
Vector2 Font::generate_mesh(const String &p_text, Ref<Mesh> &p_into, const Color &p_color) const {
ERR_FAIL_COND_V(!_initialized, Vector2());
ERR_FAIL_COND_V(!p_into.is_valid(), Vector2());
@ -376,7 +376,7 @@ Vector2 Font::generate_mesh(const String &p_text, Ref<Mesh> &p_into, const Color
}
// Calculate the size of a string, in the pixel size specified. Count stray newlines too.
Vector2 Font::get_string_size(const String &p_text) {
Vector2 Font::get_string_size(const String &p_text) const {
ERR_FAIL_COND_V(!_initialized, Vector2());
float X = 0;
@ -411,7 +411,7 @@ Vector2 Font::get_string_size(const String &p_text) {
return Vector2(W * W > X * X ? W : X, Y * Y > LL * LL ? Y : LL).abs();
}
Font::FontMetrics Font::font_metrics() {
Font::FontMetrics Font::font_metrics() const {
FontMetrics m;
m.ascent = _ascent * _factor * _scale;
@ -422,18 +422,18 @@ Font::FontMetrics Font::font_metrics() {
return m;
}
int Font::get_atlas_width() {
int Font::get_atlas_width() const {
return _width;
}
int Font::get_atlas_height() {
int Font::get_atlas_height() const {
return _height;
}
Ref<Image> Font::get_image() {
Ref<Image> Font::get_image() const {
return _image;
}
Ref<Texture> Font::get_texture() {
Ref<Texture> Font::get_texture() const {
return _texture;
}

View File

@ -69,15 +69,15 @@ public:
void font_face_from_mem(const void *ttf_buffer, uint32_t ttf_len, float font_size, uint32_t flags);
// commands
Vector2 generate_mesh(const String &p_text, Ref<Mesh> &p_into, const Color &p_color = Color(1, 1, 1, 1));
Vector2 get_string_size(const String &p_text);
FontMetrics font_metrics();
Vector2 generate_mesh(const String &p_text, Ref<Mesh> &p_into, const Color &p_color = Color(1, 1, 1, 1)) const;
Vector2 get_string_size(const String &p_text) const;
FontMetrics font_metrics() const;
int get_atlas_width();
int get_atlas_height();
int get_atlas_width() const;
int get_atlas_height() const;
Ref<Image> get_image();
Ref<Texture> get_texture();
Ref<Image> get_image() const;
Ref<Texture> get_texture() const;
void unload();

View File

@ -69,6 +69,10 @@ Ref<Image> Texture::get_data() {
return Ref<Image>(img);
}
Vector2i Texture::get_size() const {
return Vector2i(_texture_width, _texture_height);
}
void Texture::upload() {
if (!_image.is_valid()) {
return;

View File

@ -1,6 +1,8 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#include "core/vector2i.h"
#include "object/resource.h"
#include "render_core/3rd_glad.h"
#include "render_core/image.h"
@ -24,6 +26,8 @@ public:
Ref<Image> get_data();
Vector2i get_size() const;
void upload();
Texture();

View File

@ -18,7 +18,7 @@ public:
set_uniform(projection_matrix_location, RenderState::projection_matrix_2d);
set_uniform(model_view_matrix_location, RenderState::model_view_matrix_2d);
if (texture) {
if (texture.is_valid()) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture());
glUniform1i(texture_location, 0);
@ -86,7 +86,6 @@ public:
model_view_matrix_location = 0;
texture_location = 0;
texture = NULL;
}
GLint projection_matrix_location;
@ -94,7 +93,7 @@ public:
GLint texture_location;
Texture *texture;
Ref<Texture> texture;
};
#endif // COLORED_MATERIAL_H

View File

@ -1,5 +1,6 @@
#include "renderer.h"
#include "render_core/color_material_2d.h"
#include "render_core/font.h"
#include "render_core/font_material.h"
#include "render_core/material.h"
@ -10,32 +11,315 @@
#include "render_core/render_state.h"
bool Renderer::get_depth_buffer_enable() const {
return _depth_buffer;
}
void Renderer::set_depth_buffer_enable(const bool p_depth_buffer) {
_depth_buffer = p_depth_buffer;
}
void Renderer::draw_point(const Vector2 &p_position, const Color &p_color) {
//Ugly but oh well
draw_rect(Rect2(p_position, Vector2(1, 1)), p_color);
}
void Renderer::draw_line(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const real_t p_width) {
_2d_mesh->clear();
Vector2 offset = (p_from - p_to).rotated(Math_PI / 2.0).normalized();
offset *= p_width / 2.0;
_2d_mesh->add_color(p_color);
_2d_mesh->add_vertex2(p_from.x - offset.x, p_from.y - offset.y);
_2d_mesh->add_color(p_color);
_2d_mesh->add_vertex2(p_to.x + offset.x, p_to.y + offset.y);
_2d_mesh->add_color(p_color);
_2d_mesh->add_vertex2(p_to.x - offset.x, p_to.y - offset.y);
_2d_mesh->add_color(p_color);
_2d_mesh->add_vertex2(p_from.x + offset.x, p_from.y + offset.y);
//mesh->add_vertex2(-w2, -h2);
//mesh->add_vertex2(w2, h2);
//mesh->add_vertex2(-w2, h2);
//mesh->add_vertex2(w2, -h2);
_2d_mesh->add_triangle(1, 0, 2);
_2d_mesh->add_triangle(0, 1, 3);
_2d_mesh->upload();
_color_material_2d->bind();
_2d_mesh->render();
}
void Renderer::draw_line_rect(const Rect2 &p_rect, const Color &p_color) {
void Renderer::draw_line_rect(const Rect2 &p_rect, const Color &p_color, const real_t p_width) {
Vector2 rect_end = p_rect.get_end();
//Ugly but oh well
draw_line(p_rect.position, Vector2(rect_end.x, p_rect.position.y), p_color, p_width);
draw_line(p_rect.position, Vector2(p_rect.position.x, rect_end.y), p_color, p_width);
draw_line(rect_end, Vector2(rect_end.x, p_rect.position.y), p_color, p_width);
draw_line(rect_end, Vector2(p_rect.position.x, rect_end.y), p_color, p_width);
}
void Renderer::draw_rect(const Rect2 &p_rect, const Color &p_color) {
_2d_mesh->clear();
Vector2 rect_end = p_rect.get_end();
_2d_mesh->add_color(p_color);
_2d_mesh->add_vertex2(p_rect.position.x, p_rect.position.y);
_2d_mesh->add_color(p_color);
_2d_mesh->add_vertex2(rect_end.x, rect_end.y);
_2d_mesh->add_color(p_color);
_2d_mesh->add_vertex2(p_rect.position.x, rect_end.y);
_2d_mesh->add_color(p_color);
_2d_mesh->add_vertex2(rect_end.x, p_rect.position.y);
_2d_mesh->add_triangle(1, 0, 2);
_2d_mesh->add_triangle(0, 1, 3);
_2d_mesh->upload();
_color_material_2d->bind();
_2d_mesh->render();
}
void Renderer::draw_texture(const Ref<Texture> &p_texture, const Rect2 &dst_rect, const Color &p_modulate) {
void Renderer::draw_texture(const Ref<Texture> &p_texture, const Rect2 &p_dst_rect, const Color &p_modulate) {
_2d_mesh->clear();
Vector2 rect_end = p_dst_rect.get_end();
_2d_mesh->add_uv(0, 0);
_2d_mesh->add_color(p_modulate);
_2d_mesh->add_vertex2(p_dst_rect.position.x, p_dst_rect.position.y);
_2d_mesh->add_uv(1, 1);
_2d_mesh->add_color(p_modulate);
_2d_mesh->add_vertex2(rect_end.x, rect_end.y);
_2d_mesh->add_uv(0, 1);
_2d_mesh->add_color(p_modulate);
_2d_mesh->add_vertex2(p_dst_rect.position.x, rect_end.y);
_2d_mesh->add_uv(1, 0);
_2d_mesh->add_color(p_modulate);
_2d_mesh->add_vertex2(rect_end.x, p_dst_rect.position.y);
_2d_mesh->add_triangle(1, 0, 2);
_2d_mesh->add_triangle(0, 1, 3);
_2d_mesh->upload();
_texture_material_2d->texture = p_texture;
_texture_material_2d->bind();
_2d_mesh->render();
}
void Renderer::draw_texture_clipped(const Ref<Texture> &p_texture, const Rect2 &p_src_rect, const Rect2 &p_dst_rect, const Color &p_modulate) {
_2d_mesh->clear();
Rect2 uv = Rect2(0, 0, 1, 1);
if (p_texture.is_valid()) {
Vector2 ts = p_texture->get_size();
Vector2 ss = p_src_rect.position;
Vector2 se = p_src_rect.get_end();
uv.position.x = ss.x / ts.x;
uv.position.x = ss.y / ts.y;
uv.size.x = se.x / ts.x;
uv.size.x = se.y / ts.y;
}
Vector2 rect_end = p_dst_rect.get_end();
_2d_mesh->add_uv(uv.position.x, uv.position.y);
_2d_mesh->add_color(p_modulate);
_2d_mesh->add_vertex2(p_dst_rect.position.x, p_dst_rect.position.y);
_2d_mesh->add_uv(uv.size.x, uv.size.y);
_2d_mesh->add_color(p_modulate);
_2d_mesh->add_vertex2(rect_end.x, rect_end.y);
_2d_mesh->add_uv(uv.position.x, uv.size.y);
_2d_mesh->add_color(p_modulate);
_2d_mesh->add_vertex2(p_dst_rect.position.x, rect_end.y);
_2d_mesh->add_uv(uv.size.x, uv.position.y);
_2d_mesh->add_color(p_modulate);
_2d_mesh->add_vertex2(rect_end.x, p_dst_rect.position.y);
_2d_mesh->add_triangle(1, 0, 2);
_2d_mesh->add_triangle(0, 1, 3);
_2d_mesh->upload();
_texture_material_2d->texture = p_texture;
_texture_material_2d->bind();
_2d_mesh->render();
}
void Renderer::draw_mesh_2d(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform2D &p_transform_2d) {
void Renderer::draw_texture_tr(const Transform2D &p_transform_2d, const Ref<Texture> &p_texture, const Rect2 &p_dst_rect, const Color &p_modulate) {
camera_2d_push_model_view_matrix(p_transform_2d);
draw_texture(p_texture, p_dst_rect, p_modulate);
camera_2d_pop_model_view_matrix();
}
void Renderer::draw_texture_clipped_tr(const Transform2D &p_transform_2d, const Ref<Texture> &p_texture, const Rect2 &p_src_rect, const Rect2 &p_dst_rect, const Color &p_modulate) {
camera_2d_push_model_view_matrix(p_transform_2d);
draw_texture_clipped(p_texture, p_src_rect, p_dst_rect, p_modulate);
camera_2d_pop_model_view_matrix();
}
void Renderer::draw_mesh_2d(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Vector2 &p_position) {
ERR_FAIL_COND(!p_mesh.is_valid());
_texture_material_2d->texture = p_texture;
Ref<Mesh> mesh = p_mesh;
camera_2d_push_model_view_matrix(Transform2D().translated(p_position));
_texture_material_2d->bind();
mesh->render();
camera_2d_pop_model_view_matrix();
}
void Renderer::draw_mesh_2d_tr(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Transform2D &p_transform_2d) {
ERR_FAIL_COND(!p_mesh.is_valid());
_texture_material_2d->texture = p_texture;
Ref<Mesh> mesh = p_mesh;
camera_2d_push_model_view_matrix(p_transform_2d);
_texture_material_2d->bind();
mesh->render();
camera_2d_pop_model_view_matrix();
}
void Renderer::draw_mesh_2d_mat(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Vector2 &p_position) {
ERR_FAIL_COND(!p_mesh.is_valid());
ERR_FAIL_COND(!p_material.is_valid());
Ref<Material> material = p_material;
Ref<Mesh> mesh = p_mesh;
camera_2d_push_model_view_matrix(Transform2D().translated(p_position));
material->bind();
mesh->render();
camera_2d_pop_model_view_matrix();
}
void Renderer::draw_mesh_2d_mat_tr(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform2D &p_transform_2d) {
ERR_FAIL_COND(!p_mesh.is_valid());
ERR_FAIL_COND(!p_material.is_valid());
Ref<Material> material = p_material;
Ref<Mesh> mesh = p_mesh;
camera_2d_push_model_view_matrix(p_transform_2d);
material->bind();
mesh->render();
camera_2d_pop_model_view_matrix();
}
void Renderer::draw_text_2d(const String &p_text, const Ref<Font> &p_font, const Vector2 &p_position, const Color &p_color) {
ERR_FAIL_COND(!p_font.is_valid());
_font_mesh->clear();
p_font->generate_mesh(p_text, _font_mesh, p_color);
_font_mesh->upload();
_font_material->texture = p_font->get_texture();
camera_2d_push_model_view_matrix(Transform2D().translated(p_position));
_font_material->bind();
_font_mesh->render();
camera_2d_pop_model_view_matrix();
}
void Renderer::draw_text_2d_tf(const String &p_text, const Ref<Font> &p_font, const Transform2D &p_transform_2d, const Color &p_color) {
camera_2d_push_model_view_matrix(p_transform_2d);
draw_text_2d(p_text, p_font, Vector2(), p_color);
camera_2d_pop_model_view_matrix();
}
void Renderer::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) {
ERR_FAIL_COND(!p_font.is_valid());
ERR_FAIL_COND(!p_material.is_valid());
_font_mesh->clear();
p_font->generate_mesh(p_text, _font_mesh, p_color);
_font_mesh->upload();
Ref<Material> material = p_material;
camera_2d_push_model_view_matrix(p_transform_2d);
material->bind();
_font_mesh->render();
camera_2d_pop_model_view_matrix();
}
void Renderer::draw_mesh_3d(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform &p_transform) {
}
void Renderer::draw_text_2d(const Ref<Font> &p_mesh, const Vector2 &p_position, const Color &p_color) {
Transform2D Renderer::camera_2d_get_current_model_view_matrix() const {
return RenderState::model_view_matrix_2d;
}
void Renderer::draw_text_2d_tf(const Ref<Font> &p_mesh, const Transform2D &p_transform_2d, const Color &p_color) {
void Renderer::camera_2d_push_model_view_matrix(const Transform2D &p_transform_2d) {
camera_2d_model_view_matrix_stack.push_back(RenderState::model_view_matrix_2d);
RenderState::model_view_matrix_2d *= p_transform_2d;
}
void Renderer::draw_text_2d_tf_material(const Ref<Font> &p_mesh, const Ref<Material> &p_material, const Transform2D &p_transform_2d) {
void Renderer::camera_2d_pop_model_view_matrix() {
if (camera_2d_model_view_matrix_stack.empty()) {
return;
}
RenderState::model_view_matrix_2d = camera_2d_model_view_matrix_stack[camera_2d_model_view_matrix_stack.size() - 1];
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 {
return camera_2d_model_view_matrix_stack.size();
}
void Renderer::camera_2d_reset() {
Vector2 size = get_window_size();
Transform canvas_transform;
canvas_transform.translate_local(-(size.x / 2.0f), -(size.y / 2.0f), 0.0f);
//canvas_transform.scale(Vector3(2.0f / size.x, 2.0f / size.y, 1.0f));
canvas_transform.scale(Vector3(2.0f / size.x, -2.0f / size.y, 1.0f));
RenderState::model_view_matrix_2d = Transform2D();
RenderState::projection_matrix_2d = canvas_transform;
camera_2d_model_view_matrix_stack.clear();
}
void Renderer::camera_3d_reset() {
}
void Renderer::clear_screen(const Color &p_color) {
glClearColor(p_color.r, p_color.g, p_color.b, p_color.a);
if (!_depth_buffer) {
glClear(GL_COLOR_BUFFER_BIT);
} else {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
//? Make it manual?
camera_2d_reset();
}
Vector2i Renderer::get_window_size() const {
@ -59,6 +343,18 @@ void Renderer::destroy() {
Renderer::Renderer() {
_singleton = this;
_depth_buffer = false;
_2d_mesh.instance();
_2d_mesh->vertex_dimesions = 2;
_3d_mesh.instance();
_font_mesh.instance();
_font_mesh->vertex_dimesions = 2;
_texture_material_2d.instance();
_font_material.instance();
_color_material_2d.instance();
}
Renderer::~Renderer() {
_singleton = NULL;

View File

@ -5,7 +5,6 @@
#include "core/rect2.h"
#include "core/transform.h"
#include "core/transform_2d.h"
#include "core/rect2.h"
#include "core/vector2i.h"
#include "object/object.h"
@ -17,25 +16,45 @@ class Texture;
class Font;
class FontMaterial;
class TextureMaterial2D;
class ColorMaterial2D;
class Renderer : public Object {
SFW_OBJECT(Renderer, Object);
public:
bool get_depth_buffer_enable() const;
void set_depth_buffer_enable(const bool p_depth_buffer);
void draw_point(const Vector2 &p_position, const Color &p_color = Color(1, 1, 1));
void draw_line(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color = Color(1, 1, 1), const real_t p_width = 1);
void draw_line_rect(const Rect2 &p_rect, const Color &p_color = Color(1, 1, 1));
void draw_line_rect(const Rect2 &p_rect, const Color &p_color = Color(1, 1, 1), const real_t p_width = 1);
void draw_rect(const Rect2 &p_rect, const Color &p_color = Color(1, 1, 1));
void draw_texture(const Ref<Texture> &p_texture, const Rect2 &dst_rect, const Color &p_modulate = Color(1, 1, 1));
void draw_texture(const Ref<Texture> &p_texture, const Rect2 &p_dst_rect, const Color &p_modulate = Color(1, 1, 1));
void draw_texture_clipped(const Ref<Texture> &p_texture, const Rect2 &p_src_rect, const Rect2 &p_dst_rect, const Color &p_modulate = Color(1, 1, 1));
void draw_texture_tr(const Transform2D &p_transform_2d, const Ref<Texture> &p_texture, const Rect2 &p_dst_rect, const Color &p_modulate = Color(1, 1, 1));
void draw_texture_clipped_tr(const Transform2D &p_transform_2d, const Ref<Texture> &p_texture, const Rect2 &p_src_rect, const Rect2 &p_dst_rect, const Color &p_modulate = Color(1, 1, 1));
void draw_mesh_2d(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Vector2 &p_position);
void draw_mesh_2d_tr(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Transform2D &p_transform_2d);
void draw_mesh_2d_mat(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Vector2 &p_position);
void draw_mesh_2d_mat_tr(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform2D &p_transform_2d);
void draw_text_2d(const String &p_text, const Ref<Font> &p_font, const Vector2 &p_position, 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_mesh_2d(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform2D &p_transform_2d);
void draw_mesh_3d(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform &p_transform);
void draw_text_2d(const Ref<Font> &p_mesh, const Vector2 &p_position, const Color &p_color = Color(1, 1, 1));
void draw_text_2d_tf(const Ref<Font> &p_mesh, const Transform2D &p_transform_2d, const Color &p_color = Color(1, 1, 1));
void draw_text_2d_tf_material(const Ref<Font> &p_mesh, const Ref<Material> &p_material, const Transform2D &p_transform_2d);
Transform2D camera_2d_get_current_model_view_matrix() const;
void camera_2d_push_model_view_matrix(const Transform2D &p_transform_2d);
void camera_2d_pop_model_view_matrix();
int get_camera_2d_model_view_matrix_stack_size() const;
void camera_2d_reset();
void camera_3d_reset();
void clear_screen(const Color &p_color);
Vector2i get_window_size() const;
float get_window_aspect() const;
@ -51,11 +70,17 @@ public:
private:
static Renderer *_singleton;
bool _depth_buffer;
Ref<Mesh> _2d_mesh;
Ref<Mesh> _3d_mesh;
Ref<Mesh> _font_mesh;
Ref<TextureMaterial2D> _texture_material_2d;
Ref<FontMaterial> _font_material;
Ref<ColorMaterial2D> _color_material_2d;
Vector<Transform2D> camera_2d_model_view_matrix_stack;
};
#endif

View File

@ -3,7 +3,7 @@
#include "render_objects/camera_2d.h"
void MeshInstance2D::render() {
if (!mesh) {
if (!mesh.is_valid()) {
return;
}
@ -11,7 +11,7 @@ void MeshInstance2D::render() {
Camera2D::current_camera->set_model_view_matrix(mat_orig * transform);
if (material) {
if (material.is_valid()) {
material->bind();
}
@ -29,8 +29,6 @@ void MeshInstance2D::render() {
}
MeshInstance2D::MeshInstance2D() {
material = NULL;
mesh = NULL;
}
MeshInstance2D::~MeshInstance2D() {
children.clear();

View File

@ -10,17 +10,17 @@
class MeshInstance2D {
public:
void render();
void render();
MeshInstance2D();
~MeshInstance2D();
MeshInstance2D();
~MeshInstance2D();
Material *material;
Mesh *mesh;
Ref<Material> material;
Ref<Mesh> mesh;
Transform2D transform;
Transform2D transform;
Vector<MeshInstance2D *> children;
Vector<MeshInstance2D *> children;
};
#endif // MESH_INSTACE_H

View File

@ -16,7 +16,7 @@ void Sprite::render() {
}
void Sprite::update_mesh() {
Mesh *mesh = mesh_instance->mesh;
Ref<Mesh> mesh = mesh_instance->mesh;
mesh->clear();
@ -57,10 +57,9 @@ void Sprite::update_mesh() {
mesh->upload();
}
Sprite::Sprite() :
Object2D() {
mesh_instance = new MeshInstance2D();
mesh_instance->mesh = new Mesh(2);
Sprite::Sprite() {
mesh_instance = memnew(MeshInstance2D());
mesh_instance->mesh = Ref<Mesh>(memnew(Mesh(2)));
width = 1;
height = 1;
@ -72,6 +71,5 @@ Sprite::Sprite() :
}
Sprite::~Sprite() {
delete mesh_instance->mesh;
delete mesh_instance;
}