one shader for texture and color + Time's API change

This commit is contained in:
Erris
2026-01-23 15:43:26 +01:00
parent 33574d783a
commit c8cfed58da
13 changed files with 94 additions and 29 deletions

View File

@@ -10,6 +10,7 @@ namespace OpenEngine {
class OE_API Input
{
public:
virtual ~Input() = default;
Input(const Input&) = delete;
Input& operator=(const Input&) = delete;

View File

@@ -26,6 +26,7 @@ namespace OpenEngine {
void UploadInt(const std::string &name, int value) const;
void UploadFloat(const std::string &name, float value) const;
void UploadMat4(const std::string &name, const glm::mat4& value) const;
void UploadVec2(const std::string &name, const glm::vec2& value) const;
void UploadVec3(const std::string &name, const glm::vec3& value) const;
void UploadVec4(const std::string &name, const glm::vec4& value) const;
@@ -33,6 +34,7 @@ namespace OpenEngine {
virtual void SetInt(const std::string &name, int value) const override;
virtual void SetFloat(const std::string &name, float value) const override;
virtual void SetMat4(const std::string &name, const glm::mat4& value) const override;
virtual void SetVec2(const std::string &name, const glm::vec2& value) const override;
virtual void SetVec3(const std::string &name, const glm::vec3& value) const override;
virtual void SetVec4(const std::string &name, const glm::vec4& value) const override;

View File

@@ -1,7 +1,7 @@
#ifndef OPENGL_TEXTURE_HPP
#define OPENGL_TEXTURE_HPP
#include <cstdint>
#include <GL/gl.h>
#include <renderer/texture.hpp>
namespace OpenEngine {
@@ -9,6 +9,7 @@ namespace OpenEngine {
{
public:
OpenGLTexture2D(const std::string& path);
OpenGLTexture2D(uint32_t width, uint32_t height);
virtual ~OpenGLTexture2D();
virtual uint32_t GetWidth() const override { return width; };
@@ -16,11 +17,15 @@ namespace OpenEngine {
virtual void Bind(uint32_t slot = 0) const override;
virtual void SetData(void* data, uint32_t size) override;
private:
std::string path;
uint32_t width, height;
uint32_t id;
GLenum internal_format, data_format;
};
}

View File

@@ -24,6 +24,7 @@ namespace OpenEngine {
virtual void SetInt(const std::string &name, int value) const = 0;
virtual void SetFloat(const std::string &name, float value) const = 0;
virtual void SetMat4(const std::string &name, const glm::mat4& value) const = 0;
virtual void SetVec2(const std::string &name, const glm::vec2& value) const = 0;
virtual void SetVec3(const std::string &name, const glm::vec3& value) const = 0;
virtual void SetVec4(const std::string &name, const glm::vec4& value) const = 0;
};

View File

@@ -14,11 +14,14 @@ namespace OpenEngine {
virtual uint32_t GetHeight() const = 0;
virtual void Bind(uint32_t slot = 0) const = 0;
virtual void SetData(void* data, uint32_t size) = 0;
};
class Texture2D : public Texture
{
public:
static Ref<Texture2D> Create(uint32_t width, uint32_t height);
static Ref<Texture2D> Create(const std::string& path);
};
}

View File

@@ -27,6 +27,8 @@ namespace OpenEngine {
imgui_layer = std::make_shared<ImGuiLayer>();
QueueOverlayPush(imgui_layer);
Time::Init();
}
Application::~Application()
@@ -37,7 +39,7 @@ namespace OpenEngine {
void Application::Run()
{
while(running) {
Time::Get().Update();
Time::Update();
layer_stack.UpdateLayers();
for (auto layer : layer_stack)

View File

@@ -10,7 +10,7 @@ namespace OpenEngine {
std::chrono::high_resolution_clock::time_point current_frame
= std::chrono::high_resolution_clock::now();
delta_time = (current_frame - previous_frame);
previous_frame = current_frame;
instance->delta_time = (current_frame - instance->previous_frame);
instance->previous_frame = current_frame;
}
}

View File

@@ -1,7 +1,6 @@
#include <pch.hpp>
#include "opengl/opengl_renderer_api.hpp"
#include <glm/ext/vector_float4.hpp>
#include <opengl/opengl_renderer_api.hpp>
#include <glad/glad.h>
@@ -32,5 +31,6 @@ namespace OpenEngine {
void OpenGLRendererAPI::DrawIndexed(const std::shared_ptr<VertexArray>& vertex_array)
{
glDrawElements(GL_TRIANGLES, vertex_array->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
}
}

View File

@@ -190,6 +190,10 @@ namespace OpenEngine {
{
UploadMat4(name, value);
}
void OpenGLShader::SetVec2(const std::string &name, const glm::vec2& value) const
{
UploadVec2(name, value);
}
void OpenGLShader::SetVec3(const std::string &name, const glm::vec3& value) const
{
UploadVec3(name, value);
@@ -215,6 +219,10 @@ namespace OpenEngine {
{
glUniformMatrix4fv(glGetUniformLocation(id, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
}
void OpenGLShader::UploadVec2(const std::string &name, const glm::vec2& value) const
{
glUniform2fv(glGetUniformLocation(id, name.c_str()), 1, glm::value_ptr(value));
}
void OpenGLShader::UploadVec3(const std::string &name, const glm::vec3& value) const
{
glUniform3fv(glGetUniformLocation(id, name.c_str()), 1, glm::value_ptr(value));

View File

@@ -10,6 +10,23 @@
#include <stb_image.h>
namespace OpenEngine {
OpenGLTexture2D::OpenGLTexture2D(uint32_t width, uint32_t height)
: width(width), height(height)
{
internal_format = GL_RGBA8;
data_format = GL_RGBA;
glCreateTextures(GL_TEXTURE_2D, 1, &id);
glTextureStorage2D(id, 1, internal_format, width, height);
glTextureParameteri(id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureParameteri(id, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(id, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
OpenGLTexture2D::OpenGLTexture2D(const std::string& path)
: path(path)
{
@@ -21,24 +38,27 @@ namespace OpenEngine {
width = image_width;
height = image_height;
GLenum internal_format, data_format = 0;
GLenum _internal_format, _data_format = 0;
if (channels == 4) {
internal_format = GL_RGBA8;
data_format = GL_RGBA;
_internal_format = GL_RGBA8;
_data_format = GL_RGBA;
} else if (channels == 3) {
internal_format = GL_RGB8;
data_format = GL_RGB;
_internal_format = GL_RGB8;
_data_format = GL_RGB;
}
OE_CORE_ASSERT(internal_format & data_format, "Image format is unsupported!");
internal_format = _internal_format;
data_format = _data_format;
OE_CORE_ASSERT(_internal_format & _data_format, "Image format is unsupported!");
glCreateTextures(GL_TEXTURE_2D, 1, &id);
glTextureStorage2D(id, 1, internal_format, width, height);
glTextureStorage2D(id, 1, _internal_format, width, height);
glTextureParameteri(id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureSubImage2D(id, 0, 0, 0, width, height, data_format, GL_UNSIGNED_BYTE, data);
glTextureSubImage2D(id, 0, 0, 0, width, height, _data_format, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
@@ -47,6 +67,14 @@ namespace OpenEngine {
{
glDeleteTextures(1, &id);
}
void OpenGLTexture2D::SetData(void* data, uint32_t size)
{
uint32_t bpp = data_format == GL_RGBA ? 4 : 3;
OE_CORE_ASSERT(size == width * height * bpp, "Data must be entire texture!");
glTextureSubImage2D(id, 0, 0, 0, width, height, data_format, GL_UNSIGNED_BYTE, data);
}
void OpenGLTexture2D::Bind(uint32_t slot) const
{
glBindTextureUnit(slot, id);

View File

@@ -20,13 +20,13 @@ namespace OpenEngine {
void OrthographicCameraController::Translate(const glm::vec3& vector)
{
float velocity = translation_speed * Time::Get().DeltaTime();
float velocity = translation_speed * Time::DeltaTime();
camera_position += vector * velocity;
}
void OrthographicCameraController::Rotate(float speed)
{
float multiple = rotation_speed * Time::Get().DeltaTime();
float multiple = rotation_speed * Time::DeltaTime();
camera_rotation += speed * multiple;
}

View File

@@ -1,3 +1,5 @@
#include "renderer/texture.hpp"
#include <cstdint>
#include <pch.hpp>
#include <renderer/render_command.hpp>
@@ -12,8 +14,8 @@ namespace OpenEngine {
struct Renderer2DData
{
Ref<VertexArray> vertex_array;
Ref<Shader> flat_color_shader;
Ref<Shader> texture_shader;
Ref<Texture2D> white_texture;
};
static Renderer2DData* renderer_data;
@@ -46,7 +48,11 @@ namespace OpenEngine {
index_buffer.reset(IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t)));
renderer_data->vertex_array->SetIndexBuffer(index_buffer);
renderer_data->flat_color_shader = Shader::Create("assets/shaders/flat_color.glsl");
renderer_data->white_texture = Texture2D::Create(1, 1);
uint32_t white_texture_data = 0xffffffff;
renderer_data->white_texture->SetData(&white_texture_data, sizeof(uint32_t));
renderer_data->texture_shader = Shader::Create("assets/shaders/texture.glsl");
renderer_data->texture_shader->Bind();
@@ -60,11 +66,9 @@ namespace OpenEngine {
void Renderer2D::BeginScene(const OrthographicCamera& camera)
{
renderer_data->flat_color_shader->Bind();
renderer_data->flat_color_shader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix());
renderer_data->texture_shader->Bind();
renderer_data->texture_shader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix());
renderer_data->texture_shader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix());
}
void Renderer2D::EndScene()
@@ -77,13 +81,12 @@ namespace OpenEngine {
}
void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D>& texture)
{
renderer_data->texture_shader->Bind();
renderer_data->texture_shader->SetVec4("u_Color", glm::vec4(1.0f));
texture->Bind();
glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) *
glm::scale(glm::mat4(1.0f), {size.x, size.y, 1.0f});
renderer_data->flat_color_shader->SetMat4("u_Transform", transform);
texture->Bind();
renderer_data->texture_shader->SetMat4("u_Transform", transform);
renderer_data->vertex_array->Bind();
RenderCommand::DrawIndexed(renderer_data->vertex_array);
@@ -95,12 +98,12 @@ namespace OpenEngine {
}
void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color)
{
renderer_data->flat_color_shader->Bind();
renderer_data->flat_color_shader->SetVec4("u_Color", color);
renderer_data->texture_shader->SetVec4("u_Color", color);
renderer_data->white_texture->Bind();
glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) *
glm::scale(glm::mat4(1.0f), {size.x, size.y, 1.0f});
renderer_data->flat_color_shader->SetMat4("u_Transform", transform);
renderer_data->texture_shader->SetMat4("u_Transform", transform);
renderer_data->vertex_array->Bind();
RenderCommand::DrawIndexed(renderer_data->vertex_array);

View File

@@ -1,15 +1,27 @@
#include <pch.hpp>
#include <core.hpp>
#include <open_engine/renderer/texture.hpp>
#include <open_engine/renderer/renderer.hpp>
#include <open_engine/opengl/opengl_texture.hpp>
namespace OpenEngine {
Ref<Texture2D> Texture2D::Create(uint32_t width, uint32_t height)
{
switch (Renderer::GetApi()) {
case RendererAPI::API::None : OE_CORE_ASSERT(false, "No render API selected!"); return nullptr;
case RendererAPI::API::OpenGL : return CreateRef<OpenGLTexture2D>(width, height);
}
OE_CORE_ASSERT(false, "Selected API not supported");
return nullptr;
}
Ref<Texture2D> Texture2D::Create(const std::string &path)
{
switch (Renderer::GetApi()) {
case RendererAPI::API::None : OE_CORE_ASSERT(false, "No render API selected!"); return nullptr;
case RendererAPI::API::OpenGL : return std::make_shared<OpenGLTexture2D>(path);
case RendererAPI::API::OpenGL : return CreateRef<OpenGLTexture2D>(path);
}
OE_CORE_ASSERT(false, "Selected API not supported");