shader with state stack.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 2m26s

This commit is contained in:
2025-04-16 01:43:00 -04:00
parent 147123b202
commit 4c798ea76a
7 changed files with 25 additions and 32 deletions

View File

@@ -30,6 +30,7 @@
#include <JGL/types/VRamList.h>
#include <JGL/types/VertexArray.h>
#include <JGL/types/TextureAtlas.h>
#include <JGL/types/Shader.h>
// Fonts that are included by default.
namespace JGL::Fonts {
@@ -73,7 +74,7 @@ namespace JGL::J2D {
/// This keeps our code from, say, clobbering the OpenGL rendering context driving 3D content in between our calls.
/// @param render_target
/// @param clear_buffers
void Begin(RenderTarget* render_target = nullptr, bool clear_buffers = false);
void Begin(RenderTarget* render_target = nullptr, Shader* shader = nullptr, bool clear_buffers = false);
/// Closes a 2-D rendering context with the underlying graphics system (In this case& by default OpenGL).
/// @see Begin().

View File

@@ -36,10 +36,6 @@ public:
unsigned int Handle() const;
/// Enable this shader. All rendering performed thereafter will be affected by this shader code.
/// @see UseDefault.
void Use();
static void Use(GLuint shader_program_id);
static void UseDefault();
// TODO: Implement for hot-reloading.
void Reload();

View File

@@ -133,7 +133,7 @@ public:
image = new Texture("assets/sprites/Re3D.png", FilteringMode::MIPMAP_NEAREST, JGL::SampleRate::X16);
image_mask = new Texture("assets/sprites/alpha_mask_2.png");
j2d_render_target = new RenderTarget({540, 500}, {0,0,0,0}, false,
SampleRate::NONE, FilteringMode::MIPMAP_TRILINEAR);
SampleRate::X0, FilteringMode::MIPMAP_TRILINEAR);
//Texture::MultiplyByAlphaMask(*image, *image_mask);
@@ -149,8 +149,6 @@ public:
void display() {
Shader::UseDefault();
pulse += 20 * delta_time;
float dt = GetDeltaTime();
@@ -193,9 +191,7 @@ public:
J3D::WireframeAABB(Colors::Yellow, {0.5, 0, 0.5}, {0.125, 0.125, 0.125}, 1);
J3D::End();
shader.Use();
J2D::Begin(nullptr, true);
J2D::Begin(nullptr, &shader, true);
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
J2D::DrawSprite(image, {300, 400}, sprite_radians * 0.10f, {0.5,0.5}, {1, 1}, Colors::White);
J2D::DrawMirrorSprite(image, {400, 300}, Direction::Horizontal | Direction::Vertical, sprite_radians, {0.5,0.5}, {1, 1}, Colors::White);
@@ -235,10 +231,12 @@ public:
J2D::End();
/*
J2D::Begin();
//J2D::DrawRenderTarget(j2d_render_target, {0, 0});
//J2D::DrawSprite(image, image_mask, {0, 0}, 0.25, {0.5, 0.5}, {1,1});
J2D::End();
*/

View File

@@ -4,9 +4,9 @@
#include "internals/internals.h"
void J2D::Begin(RenderTarget* render_target, bool clear_buffers) {
void J2D::Begin(RenderTarget* render_target, Shader* shader, bool clear_buffers) {
State new_state = default_state;
state_stack.Push(State::SaveState());
state_stack.Push(State::SaveState(current_state));
glMatrixMode(GL_PROJECTION);
glPushMatrix();
@@ -31,13 +31,17 @@ void J2D::Begin(RenderTarget* render_target, bool clear_buffers) {
new_state.viewport[3] = window_size.y;
}
if (shader) {
new_state.current_shader = shader;
new_state.current_shader_handle = shader->Handle();
}
State::RestoreState(new_state);
current_state = new_state;
if (current_state.current_render_target)
RenderTarget::SetActiveGLRenderTarget(*current_state.current_render_target);
if (render_target != nullptr && clear_buffers) {
glClearColor(render_target->GetClearColor().RedChannelNormalized(), render_target->GetClearColor().GreenChannelNormalized(), render_target->GetClearColor().BlueChannelNormalized(), render_target->GetClearColor().AlphaChannelNormalized());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

View File

@@ -133,7 +133,7 @@ JGL::State* JGL::StateStack::PreviousState() {
return &states.back();
}
JGL::State JGL::State::SaveState() {
JGL::State JGL::State::SaveState(const State& state) {
State result;
result.depth_test = glIsEnabled(GL_DEPTH_TEST);
@@ -152,6 +152,8 @@ JGL::State JGL::State::SaveState() {
glGetFloatv(GL_CURRENT_COLOR, result.draw_color);
glGetIntegerv(GL_VIEWPORT, result.viewport);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&result.current_fbo);
glGetIntegerv(GL_CURRENT_PROGRAM, &result.current_shader_handle);
result.current_shader = state.current_shader;
glGetIntegerv(GL_BLEND_SRC, &result.blend_func[0]);
glGetIntegerv(GL_BLEND_DST, &result.blend_func[1]);
@@ -184,7 +186,6 @@ void JGL::State::RestoreState(const State& state) {
else
glDisable(GL_BLEND);
glActiveTexture(GL_TEXTURE0 + state.selected_texture_unit);
glClientActiveTexture(GL_TEXTURE0 + state.selected_texture_unit);
@@ -203,10 +204,14 @@ void JGL::State::RestoreState(const State& state) {
else
glDisableClientState(GL_COLOR_ARRAY);
if (state.current_shader)
glUseProgram(state.current_shader->Handle());
else
glUseProgram(0);
glBindFramebuffer(GL_FRAMEBUFFER, state.current_fbo);
glViewport(state.viewport[0], state.viewport[1], state.viewport[2], state.viewport[3]);
glClearColor(state.clear_color[0], state.clear_color[1], state.clear_color[2], state.clear_color[3]);
glColor4f(state.draw_color[0], state.draw_color[1], state.draw_color[2], state.draw_color[3]);
glBlendFunc(state.blend_func[0], state.blend_func[1]);
}

View File

@@ -24,7 +24,8 @@ public:
GLint blend_func[2];
GLuint current_fbo = 0;
RenderTarget* current_render_target = nullptr;
//Shader* current_shader = nullptr;
GLint current_shader_handle = 0;
Shader* current_shader = nullptr;
bool texture_2D = false;
bool texture_coordinate_array = false;
@@ -40,7 +41,7 @@ public:
// List of all lights in the scene.
std::vector<const LightBase*> optional_lights{};
public:
static State SaveState();
static State SaveState(const State& state);
static void RestoreState(const State& state);
};
@@ -72,7 +73,7 @@ namespace JGL::J2D {
{1, 1, 1, 1},
{0, 0, 0, 0},
{GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
0, nullptr, false, false,
0, nullptr, 0, nullptr, false, false,
false, false, true, true,
true, false, 0,
{}, {}

View File

@@ -163,22 +163,10 @@ namespace JGL {
glDeleteShader(fragment);
}
void Shader::Use() {
glUseProgram(id);
}
bool Shader::Loaded() const { return id != 0; }
unsigned int Shader::Handle() const { return id;}
void Shader::UseDefault() {
glUseProgram(0);
}
void Shader::Use(GLuint shader_program_id) {
glUseProgram(shader_program_id);
}
void Shader::SetBool(const std::string &name, bool value) const { glUniform1i(Uniform(name), (int)value); }
void Shader::SetInt(const std::string &name, int value) const { glUniform1i(Uniform(name), value); }