From 59f495edff683ab606c1612f7c920b01fd4c2699 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 5 Jan 2024 13:41:14 +0100 Subject: [PATCH] Split materials into header and cpp. --- compile_linux.sh | 12 +++ sfw/render_core/color_material.cpp | 53 ++++++++++++ sfw/render_core/color_material.h | 51 ++---------- sfw/render_core/color_material_2d.cpp | 60 ++++++++++++++ sfw/render_core/color_material_2d.h | 60 ++------------ sfw/render_core/colored_material.cpp | 49 +++++++++++ sfw/render_core/colored_material.h | 49 ++--------- sfw/render_core/font_material.cpp | 81 ++++++++++++++++++ sfw/render_core/font_material.h | 82 ++----------------- sfw/render_core/texture_material.cpp | 76 +++++++++++++++++ sfw/render_core/texture_material.h | 76 ++--------------- sfw/render_core/texture_material_2d.cpp | 77 +++++++++++++++++ sfw/render_core/texture_material_2d.h | 80 ++---------------- .../transparent_texture_material.cpp | 74 +++++++++++++++++ .../transparent_texture_material.h | 75 ++--------------- 15 files changed, 528 insertions(+), 427 deletions(-) create mode 100644 sfw/render_core/color_material.cpp create mode 100644 sfw/render_core/color_material_2d.cpp create mode 100644 sfw/render_core/colored_material.cpp create mode 100644 sfw/render_core/font_material.cpp create mode 100644 sfw/render_core/texture_material.cpp create mode 100644 sfw/render_core/texture_material_2d.cpp create mode 100644 sfw/render_core/transparent_texture_material.cpp diff --git a/compile_linux.sh b/compile_linux.sh index 8cf9fa0..84d2e66 100755 --- a/compile_linux.sh +++ b/compile_linux.sh @@ -69,6 +69,14 @@ ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/input.cpp -o sfw/rende ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/shortcut.cpp -o sfw/render_core/shortcut.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/font.cpp -o sfw/render_core/font.o +ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/color_material_2d.cpp -o sfw/render_core/color_material_2d.o +ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/color_material.cpp -o sfw/render_core/color_material.o +ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/colored_material.cpp -o sfw/render_core/colored_material.o +ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/font_material.cpp -o sfw/render_core/font_material.o +ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/texture_material_2d.cpp -o sfw/render_core/texture_material_2d.o +ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/texture_material.cpp -o sfw/render_core/texture_material.o +ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_core/transparent_texture_material.cpp -o sfw/render_core/transparent_texture_material.o + ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_immediate/renderer.cpp -o sfw/render_immediate/renderer.o ccache g++ -Wall -D_REENTRANT -g -Isfw -c sfw/render_objects/camera_3d.cpp -o sfw/render_objects/camera_3d.o @@ -105,6 +113,10 @@ ccache g++ -Wall -lm -ldl -lpthread -lX11 -D_REENTRANT -g sfw/core/aabb.o sfw/c sfw/render_core/input_event.o sfw/render_core/input_map.o \ sfw/render_core/input.o sfw/render_core/shortcut.o \ sfw/render_core/keyboard.o sfw/render_core/font.o \ + sfw/render_core/color_material_2d.o sfw/render_core/color_material.o \ + sfw/render_core/colored_material.o sfw/render_core/font_material.o \ + sfw/render_core/texture_material_2d.o sfw/render_core/texture_material.o \ + sfw/render_core/transparent_texture_material.o \ sfw/render_immediate/renderer.o \ sfw/render_objects/camera_3d.o sfw/render_objects/object_3d.o sfw/render_objects/mesh_instance_3d.o \ sfw/render_objects/object_2d.o \ diff --git a/sfw/render_core/color_material.cpp b/sfw/render_core/color_material.cpp new file mode 100644 index 0000000..91b5700 --- /dev/null +++ b/sfw/render_core/color_material.cpp @@ -0,0 +1,53 @@ + +//--STRIP +#include "color_material.h" +//--STRIP + +void ColorMaterial::bind_uniforms() { + set_uniform(projection_matrix_location, RenderState::projection_matrix_3d); + set_uniform(camera_matrix_location, RenderState::camera_transform_3d); + set_uniform(model_view_matrix_location, RenderState::model_view_matrix_3d); +} + +void ColorMaterial::setup_uniforms() { + projection_matrix_location = get_uniform("u_proj_matrix"); + camera_matrix_location = get_uniform("u_camera_matrix"); + model_view_matrix_location = get_uniform("u_model_view_matrix"); +} + +const GLchar **ColorMaterial::get_vertex_shader_source() { + static const GLchar *vertex_shader_source[] = { + "uniform mat4 u_proj_matrix;\n" + "uniform mat4 u_camera_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_camera_matrix * u_model_view_matrix * a_position;\n" + "}\n" + }; + + return vertex_shader_source; +} + +const GLchar **ColorMaterial::get_fragment_shader_source() { + static const GLchar *fragment_shader_source[] = { + "precision mediump float;" + "varying vec4 v_color;\n" + "\n" + "void main() { gl_FragColor = v_color; }\n" + }; + + return fragment_shader_source; +} + +ColorMaterial::ColorMaterial() { + projection_matrix_location = 0; + camera_matrix_location = 0; + model_view_matrix_location = 0; +} diff --git a/sfw/render_core/color_material.h b/sfw/render_core/color_material.h index f2f2ad1..308c527 100644 --- a/sfw/render_core/color_material.h +++ b/sfw/render_core/color_material.h @@ -15,54 +15,13 @@ public: return 2; } - void bind_uniforms() { - set_uniform(projection_matrix_location, RenderState::projection_matrix_3d); - set_uniform(camera_matrix_location, RenderState::camera_transform_3d); - set_uniform(model_view_matrix_location, RenderState::model_view_matrix_3d); - } + void bind_uniforms(); + void setup_uniforms(); - void setup_uniforms() { - projection_matrix_location = get_uniform("u_proj_matrix"); - camera_matrix_location = get_uniform("u_camera_matrix"); - model_view_matrix_location = get_uniform("u_model_view_matrix"); - } + const GLchar **get_vertex_shader_source(); + const GLchar **get_fragment_shader_source(); - const GLchar **get_vertex_shader_source() { - static const GLchar *vertex_shader_source[] = { - "uniform mat4 u_proj_matrix;\n" - "uniform mat4 u_camera_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_camera_matrix * u_model_view_matrix * a_position;\n" - "}\n" - }; - - return vertex_shader_source; - } - - const GLchar **get_fragment_shader_source() { - static const GLchar *fragment_shader_source[] = { - "precision mediump float;" - "varying vec4 v_color;\n" - "\n" - "void main() { gl_FragColor = v_color; }\n" - }; - - return fragment_shader_source; - } - - ColorMaterial() { - projection_matrix_location = 0; - camera_matrix_location = 0; - model_view_matrix_location = 0; - } + ColorMaterial(); GLint projection_matrix_location; GLint camera_matrix_location; diff --git a/sfw/render_core/color_material_2d.cpp b/sfw/render_core/color_material_2d.cpp new file mode 100644 index 0000000..666b6fe --- /dev/null +++ b/sfw/render_core/color_material_2d.cpp @@ -0,0 +1,60 @@ + +//--STRIP +#include "color_material_2d.h" +//--STRIP + +void ColorMaterial2D::bind_uniforms() { + set_uniform(projection_matrix_location, RenderState::projection_matrix_2d); + set_uniform(model_view_matrix_location, RenderState::model_view_matrix_2d); +} + +void ColorMaterial2D::setup_uniforms() { + projection_matrix_location = get_uniform("u_proj_matrix"); + model_view_matrix_location = get_uniform("u_model_view_matrix"); +} + +void ColorMaterial2D::unbind() { + glDisable(GL_TEXTURE_2D); +} + +void ColorMaterial2D::setup_state() { + glEnable(GL_TEXTURE_2D); +} + +const GLchar **ColorMaterial2D::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 **ColorMaterial2D::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::ColorMaterial2D() { + projection_matrix_location = 0; + model_view_matrix_location = 0; +} diff --git a/sfw/render_core/color_material_2d.h b/sfw/render_core/color_material_2d.h index cd8c332..b94ab70 100644 --- a/sfw/render_core/color_material_2d.h +++ b/sfw/render_core/color_material_2d.h @@ -16,61 +16,15 @@ public: 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 bind_uniforms(); + void setup_uniforms(); + void unbind(); + void setup_state(); - void setup_uniforms() { - projection_matrix_location = get_uniform("u_proj_matrix"); - model_view_matrix_location = get_uniform("u_model_view_matrix"); - } + const GLchar **get_vertex_shader_source(); + const GLchar **get_fragment_shader_source(); - 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; - } + ColorMaterial2D(); GLint projection_matrix_location; GLint model_view_matrix_location; diff --git a/sfw/render_core/colored_material.cpp b/sfw/render_core/colored_material.cpp new file mode 100644 index 0000000..2d3e58b --- /dev/null +++ b/sfw/render_core/colored_material.cpp @@ -0,0 +1,49 @@ +//--STRIP +#include "colored_material.h" +//--STRIP + +void ColoredMaterial::bind_uniforms() { + set_uniform(projection_matrix_location, RenderState::projection_matrix_3d); + set_uniform(model_view_matrix_location, RenderState::model_view_matrix_3d); + + glUniform4f(tri_color_uniform_location, color.r, color.g, color.b, color.a); +} + +void ColoredMaterial::setup_uniforms() { + projection_matrix_location = get_uniform("u_proj_matrix"); + model_view_matrix_location = get_uniform("u_model_view_matrix"); + + tri_color_uniform_location = get_uniform("fragment_color"); +} + +const GLchar **ColoredMaterial::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" + "\n" + "void main() {\n" + " gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n" + "}" + }; + + return vertex_shader_source; +} + +const GLchar **ColoredMaterial::get_fragment_shader_source() { + static const GLchar *fragment_shader_source[] = { + "precision mediump float;\n" + "\n" + "uniform vec4 fragment_color;\n" + "\n" + "void main() {\n" + " gl_FragColor = fragment_color;\n" + "}" + }; + + return fragment_shader_source; +} + +ColoredMaterial::ColoredMaterial() { +} diff --git a/sfw/render_core/colored_material.h b/sfw/render_core/colored_material.h index 7bad48e..93596a6 100644 --- a/sfw/render_core/colored_material.h +++ b/sfw/render_core/colored_material.h @@ -15,52 +15,13 @@ public: return 1; } - void bind_uniforms() { - set_uniform(projection_matrix_location, RenderState::projection_matrix_3d); - set_uniform(model_view_matrix_location, RenderState::model_view_matrix_3d); + void bind_uniforms(); + void setup_uniforms(); - glUniform4f(tri_color_uniform_location, color.r, color.g, color.b, color.a); - } + const GLchar **get_vertex_shader_source(); + const GLchar **get_fragment_shader_source(); - void setup_uniforms() { - projection_matrix_location = get_uniform("u_proj_matrix"); - model_view_matrix_location = get_uniform("u_model_view_matrix"); - - tri_color_uniform_location = get_uniform("fragment_color"); - } - - 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" - "\n" - "void main() {\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" - "uniform vec4 fragment_color;\n" - "\n" - "void main() {\n" - " gl_FragColor = fragment_color;\n" - "}" - }; - - return fragment_shader_source; - } - - ColoredMaterial() : - Material() { - } + ColoredMaterial(); GLint projection_matrix_location; GLint model_view_matrix_location; diff --git a/sfw/render_core/font_material.cpp b/sfw/render_core/font_material.cpp new file mode 100644 index 0000000..682ee22 --- /dev/null +++ b/sfw/render_core/font_material.cpp @@ -0,0 +1,81 @@ +//--STRIP +#include "font_material.h" +//--STRIP + +void FontMaterial::bind_uniforms() { + set_uniform(projection_matrix_location, RenderState::projection_matrix_2d); + set_uniform(model_view_matrix_location, RenderState::model_view_matrix_2d); + + if (texture.is_valid()) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture()); + glUniform1i(texture_location, 0); + } +} + +void FontMaterial::setup_uniforms() { + projection_matrix_location = get_uniform("u_proj_matrix"); + model_view_matrix_location = get_uniform("u_model_view_matrix"); + + texture_location = get_uniform("u_texture"); +} + +void FontMaterial::unbind() { + glDisable(GL_TEXTURE_2D); +} + +void FontMaterial::setup_state() { + glEnable(GL_TEXTURE_2D); +} + +const GLchar **FontMaterial::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 vec2 a_uv;\n" + "attribute vec4 a_color;\n" + "\n" + "varying vec2 v_uv;\n" + "varying vec4 v_color;\n" + "\n" + "void main() {\n" + " v_uv = a_uv;\n" + " v_color = a_color;\n" + " gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n" + "}" + }; + + return vertex_shader_source; +} + +const GLchar **FontMaterial::get_fragment_shader_source() { + static const GLchar *fragment_shader_source[] = { + "precision mediump float;\n" + "\n" + "uniform sampler2D u_texture;\n" + "\n" + "varying vec2 v_uv;\n" + "varying vec4 v_color;\n" + "\n" + "void main() {\n" + " vec4 col = texture2D(u_texture, v_uv);\n" + "\n" + " if (col.r < 0.5) {\n" + " discard;\n" + " }\n" + "\n" + " gl_FragColor = col * v_color;\n" + "}" + }; + + return fragment_shader_source; +} + +FontMaterial::FontMaterial() { + projection_matrix_location = 0; + model_view_matrix_location = 0; + + texture_location = 0; +} diff --git a/sfw/render_core/font_material.h b/sfw/render_core/font_material.h index 46b6a53..30acb0f 100644 --- a/sfw/render_core/font_material.h +++ b/sfw/render_core/font_material.h @@ -16,83 +16,15 @@ public: return 11; } - void bind_uniforms() { - set_uniform(projection_matrix_location, RenderState::projection_matrix_2d); - set_uniform(model_view_matrix_location, RenderState::model_view_matrix_2d); + void bind_uniforms(); + void setup_uniforms(); + void unbind(); + void setup_state(); - if (texture.is_valid()) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture()); - glUniform1i(texture_location, 0); - } - } + const GLchar **get_vertex_shader_source(); + const GLchar **get_fragment_shader_source(); - void setup_uniforms() { - projection_matrix_location = get_uniform("u_proj_matrix"); - model_view_matrix_location = get_uniform("u_model_view_matrix"); - - texture_location = get_uniform("u_texture"); - } - - 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 vec2 a_uv;\n" - "attribute vec4 a_color;\n" - "\n" - "varying vec2 v_uv;\n" - "varying vec4 v_color;\n" - "\n" - "void main() {\n" - " v_uv = a_uv;\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" - "uniform sampler2D u_texture;\n" - "\n" - "varying vec2 v_uv;\n" - "varying vec4 v_color;\n" - "\n" - "void main() {\n" - " vec4 col = texture2D(u_texture, v_uv);\n" - "\n" - " if (col.r < 0.5) {\n" - " discard;\n" - " }\n" - "\n" - " gl_FragColor = col * v_color;\n" - "}" - }; - - return fragment_shader_source; - } - - FontMaterial() { - projection_matrix_location = 0; - model_view_matrix_location = 0; - - texture_location = 0; - } + FontMaterial(); GLint projection_matrix_location; GLint model_view_matrix_location; diff --git a/sfw/render_core/texture_material.cpp b/sfw/render_core/texture_material.cpp new file mode 100644 index 0000000..d017099 --- /dev/null +++ b/sfw/render_core/texture_material.cpp @@ -0,0 +1,76 @@ + +//--STRIP +#include "texture_material.h" +//--STRIP + +void TextureMaterial::bind_uniforms() { + set_uniform(projection_matrix_location, RenderState::projection_matrix_3d); + set_uniform(camera_matrix_location, RenderState::camera_transform_3d); + set_uniform(model_view_matrix_location, RenderState::model_view_matrix_3d); + + if (texture.is_valid()) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture()); + glUniform1i(texture_location, 0); + } +} + +void TextureMaterial::setup_uniforms() { + projection_matrix_location = get_uniform("u_proj_matrix"); + camera_matrix_location = get_uniform("u_camera_matrix"); + model_view_matrix_location = get_uniform("u_model_view_matrix"); + + texture_location = get_uniform("u_texture"); +} + +void TextureMaterial::unbind() { + glDisable(GL_TEXTURE_2D); +} + +void TextureMaterial::setup_state() { + glEnable(GL_TEXTURE_2D); +} + +const GLchar **TextureMaterial::get_vertex_shader_source() { + static const GLchar *vertex_shader_source[] = { + "uniform mat4 u_proj_matrix;\n" + "uniform mat4 u_camera_matrix;\n" + "uniform mat4 u_model_view_matrix;\n" + "\n" + "attribute vec4 a_position;\n" + "attribute vec2 a_uv;\n" + "\n" + "varying vec2 v_uv;\n" + "\n" + "void main() {\n" + " v_uv = a_uv;\n" + " gl_Position = u_proj_matrix * u_camera_matrix * u_model_view_matrix * a_position;\n" + "}" + }; + + return vertex_shader_source; +} + +const GLchar **TextureMaterial::get_fragment_shader_source() { + static const GLchar *fragment_shader_source[] = { + "precision mediump float;\n" + "\n" + "uniform sampler2D u_texture;\n" + "\n" + "varying vec2 v_uv;\n" + "\n" + "void main() {\n" + " gl_FragColor = texture2D(u_texture, v_uv);\n" + "}" + }; + + return fragment_shader_source; +} + +TextureMaterial::TextureMaterial() { + projection_matrix_location = 0; + camera_matrix_location = 0; + model_view_matrix_location = 0; + + texture_location = 0; +} diff --git a/sfw/render_core/texture_material.h b/sfw/render_core/texture_material.h index f3f0a23..de56195 100644 --- a/sfw/render_core/texture_material.h +++ b/sfw/render_core/texture_material.h @@ -16,77 +16,15 @@ public: return 3; } - void bind_uniforms() { - set_uniform(projection_matrix_location, RenderState::projection_matrix_3d); - set_uniform(camera_matrix_location, RenderState::camera_transform_3d); - set_uniform(model_view_matrix_location, RenderState::model_view_matrix_3d); + void bind_uniforms(); + void setup_uniforms(); + void unbind(); + void setup_state(); - if (texture.is_valid()) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture()); - glUniform1i(texture_location, 0); - } - } + const GLchar **get_vertex_shader_source(); + const GLchar **get_fragment_shader_source(); - void setup_uniforms() { - projection_matrix_location = get_uniform("u_proj_matrix"); - camera_matrix_location = get_uniform("u_camera_matrix"); - model_view_matrix_location = get_uniform("u_model_view_matrix"); - - texture_location = get_uniform("u_texture"); - } - - 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_camera_matrix;\n" - "uniform mat4 u_model_view_matrix;\n" - "\n" - "attribute vec4 a_position;\n" - "attribute vec2 a_uv;\n" - "\n" - "varying vec2 v_uv;\n" - "\n" - "void main() {\n" - " v_uv = a_uv;\n" - " gl_Position = u_proj_matrix * u_camera_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" - "uniform sampler2D u_texture;\n" - "\n" - "varying vec2 v_uv;\n" - "\n" - "void main() {\n" - " gl_FragColor = texture2D(u_texture, v_uv);\n" - "}" - }; - - return fragment_shader_source; - } - - TextureMaterial() { - projection_matrix_location = 0; - camera_matrix_location = 0; - model_view_matrix_location = 0; - - texture_location = 0; - } + TextureMaterial(); GLint projection_matrix_location; GLint camera_matrix_location; diff --git a/sfw/render_core/texture_material_2d.cpp b/sfw/render_core/texture_material_2d.cpp new file mode 100644 index 0000000..420cb70 --- /dev/null +++ b/sfw/render_core/texture_material_2d.cpp @@ -0,0 +1,77 @@ +//--STRIP +#include "texture_material_2d.h" +//--STRIP + +void TextureMaterial2D::bind_uniforms() { + set_uniform(projection_matrix_location, RenderState::projection_matrix_2d); + set_uniform(model_view_matrix_location, RenderState::model_view_matrix_2d); + + if (texture.is_valid()) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture()); + glUniform1i(texture_location, 0); + } +} + +void TextureMaterial2D::setup_uniforms() { + projection_matrix_location = get_uniform("u_proj_matrix"); + model_view_matrix_location = get_uniform("u_model_view_matrix"); + + texture_location = get_uniform("u_texture"); +} + +void TextureMaterial2D::unbind() { + glDisable(GL_TEXTURE_2D); +} + +void TextureMaterial2D::setup_state() { + glEnable(GL_TEXTURE_2D); +} + +const GLchar **TextureMaterial2D::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 vec2 a_uv;\n" + "\n" + "varying vec2 v_uv;\n" + "\n" + "void main() {\n" + " v_uv = a_uv;\n" + " gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n" + "}" + }; + + return vertex_shader_source; +} + +const GLchar **TextureMaterial2D::get_fragment_shader_source() { + static const GLchar *fragment_shader_source[] = { + "precision mediump float;\n" + "\n" + "uniform sampler2D u_texture;\n" + "\n" + "varying vec2 v_uv;\n" + "\n" + "void main() {\n" + " vec4 col = texture2D(u_texture, v_uv);\n" + "\n" + " if (col.a < 0.1) {\n" + " discard;\n" + " }\n" + "\n" + " gl_FragColor = col;\n" + "}" + }; + + return fragment_shader_source; +} + +TextureMaterial2D::TextureMaterial2D() { + projection_matrix_location = 0; + model_view_matrix_location = 0; + + texture_location = 0; +} diff --git a/sfw/render_core/texture_material_2d.h b/sfw/render_core/texture_material_2d.h index f95f79f..3038e3a 100644 --- a/sfw/render_core/texture_material_2d.h +++ b/sfw/render_core/texture_material_2d.h @@ -16,80 +16,16 @@ public: return 10; } - void bind_uniforms() { - set_uniform(projection_matrix_location, RenderState::projection_matrix_2d); - set_uniform(model_view_matrix_location, RenderState::model_view_matrix_2d); + void bind_uniforms(); + void setup_uniforms(); + void unbind(); + void setup_state(); - if (texture.is_valid()) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture()); - glUniform1i(texture_location, 0); - } - } - - void setup_uniforms() { - projection_matrix_location = get_uniform("u_proj_matrix"); - model_view_matrix_location = get_uniform("u_model_view_matrix"); - - texture_location = get_uniform("u_texture"); - } - - 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 vec2 a_uv;\n" - "\n" - "varying vec2 v_uv;\n" - "\n" - "void main() {\n" - " v_uv = a_uv;\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" - "uniform sampler2D u_texture;\n" - "\n" - "varying vec2 v_uv;\n" - "\n" - "void main() {\n" - " vec4 col = texture2D(u_texture, v_uv);\n" - "\n" - " if (col.a < 0.1) {\n" - " discard;\n" - " }\n" - "\n" - " gl_FragColor = col;\n" - "}" - }; - - return fragment_shader_source; - } - - TextureMaterial2D() { - projection_matrix_location = 0; - model_view_matrix_location = 0; - - texture_location = 0; - } + const GLchar **get_vertex_shader_source(); + const GLchar **get_fragment_shader_source(); + TextureMaterial2D(); + GLint projection_matrix_location; GLint model_view_matrix_location; diff --git a/sfw/render_core/transparent_texture_material.cpp b/sfw/render_core/transparent_texture_material.cpp new file mode 100644 index 0000000..f0d1827 --- /dev/null +++ b/sfw/render_core/transparent_texture_material.cpp @@ -0,0 +1,74 @@ +//--STRIP +#include "transparent_texture_material.h" +//--STRIP + +void TransparentTextureMaterial::bind_uniforms() { + set_uniform(projection_matrix_location, RenderState::projection_matrix_3d); + set_uniform(model_view_matrix_location, RenderState::model_view_matrix_3d); + + if (texture.is_valid()) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture()); + glUniform1i(texture_location, 0); + } +} + +void TransparentTextureMaterial::setup_uniforms() { + projection_matrix_location = get_uniform("u_proj_matrix"); + model_view_matrix_location = get_uniform("u_model_view_matrix"); + + texture_location = get_uniform("u_texture"); +} + +void TransparentTextureMaterial::unbind() { + glDisable(GL_TEXTURE_2D); +} + +void TransparentTextureMaterial::setup_state() { + glEnable(GL_TEXTURE_2D); +} + +const GLchar **TransparentTextureMaterial::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 vec2 a_uv;\n" + "\n" + "varying vec2 v_uv;\n" + "\n" + "void main() {\n" + " v_uv = a_uv;\n" + " gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n" + "}" + }; + + return vertex_shader_source; +} + +const GLchar **TransparentTextureMaterial::get_fragment_shader_source() { + static const GLchar *fragment_shader_source[] = { + "precision mediump float;\n" + "\n" + "uniform sampler2D u_texture;\n" + "\n" + "varying vec2 v_uv;\n" + "\n" + "void main() {\n" + " vec4 col = texture2D(u_texture, v_uv);\n" + "\n" + " if (col.a < 0.1) {\n" + " discard;\n" + " }\n" + "\n" + " gl_FragColor = col;\n" + "}" + }; + + return fragment_shader_source; +} + +TransparentTextureMaterial::TransparentTextureMaterial() { + texture_location = 0; +} diff --git a/sfw/render_core/transparent_texture_material.h b/sfw/render_core/transparent_texture_material.h index 5f6eb73..0bae920 100644 --- a/sfw/render_core/transparent_texture_material.h +++ b/sfw/render_core/transparent_texture_material.h @@ -16,76 +16,15 @@ public: return 4; } - void bind_uniforms() { - set_uniform(projection_matrix_location, RenderState::projection_matrix_3d); - set_uniform(model_view_matrix_location, RenderState::model_view_matrix_3d); + void bind_uniforms(); + void setup_uniforms(); + void unbind(); + void setup_state(); - if (texture.is_valid()) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture->get_gl_texture()); - glUniform1i(texture_location, 0); - } - } + const GLchar **get_vertex_shader_source(); + const GLchar **get_fragment_shader_source(); - void setup_uniforms() { - projection_matrix_location = get_uniform("u_proj_matrix"); - model_view_matrix_location = get_uniform("u_model_view_matrix"); - - texture_location = get_uniform("u_texture"); - } - - 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 vec2 a_uv;\n" - "\n" - "varying vec2 v_uv;\n" - "\n" - "void main() {\n" - " v_uv = a_uv;\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" - "uniform sampler2D u_texture;\n" - "\n" - "varying vec2 v_uv;\n" - "\n" - "void main() {\n" - " vec4 col = texture2D(u_texture, v_uv);\n" - "\n" - " if (col.a < 0.1) {\n" - " discard;\n" - " }\n" - "\n" - " gl_FragColor = col;\n" - "}" - }; - - return fragment_shader_source; - } - - TransparentTextureMaterial() { - texture_location = 0; - } + TransparentTextureMaterial(); GLint projection_matrix_location; GLint model_view_matrix_location;