Skip to content
Open

Temp #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions OpenGL-Core/src/GLCore/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "Input.h"

#include <glfw/glfw3.h>
#include <glad/glad.h>

#include "KeyCodes.h"

namespace GLCore {

Expand Down Expand Up @@ -47,6 +50,15 @@ namespace GLCore {
{
EventDispatcher dispatcher(e);
dispatcher.Dispatch<WindowCloseEvent>(BIND_EVENT_FN(OnWindowClose));
dispatcher.Dispatch<KeyPressedEvent>([&](KeyPressedEvent& e) {

if (e.GetKeyCode() == HZ_KEY_ESCAPE)
{
m_Running = false;
return false;
}
});


for (auto it = m_LayerStack.end(); it != m_LayerStack.begin(); )
{
Expand All @@ -65,7 +77,10 @@ namespace GLCore {
m_LastFrameTime = time;

for (Layer* layer : m_LayerStack)
{
layer->OnUpdate(timestep);
layer->OnRender();
}

m_ImGuiLayer->Begin();
for (Layer* layer : m_LayerStack)
Expand All @@ -82,4 +97,6 @@ namespace GLCore {
return true;
}



}
1 change: 1 addition & 0 deletions OpenGL-Core/src/GLCore/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace GLCore {
inline static Application& Get() { return *s_Instance; }
private:
bool OnWindowClose(WindowCloseEvent& e);
bool OnWindowResize(WindowResizeEvent& e);
private:
std::unique_ptr<Window> m_Window;
ImGuiLayer* m_ImGuiLayer;
Expand Down
2 changes: 2 additions & 0 deletions OpenGL-Core/src/GLCore/Core/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace GLCore {
virtual void OnAttach() {}
virtual void OnDetach() {}
virtual void OnUpdate(Timestep ts) {}
virtual void OnRender() {}

virtual void OnImGuiRender() {}
virtual void OnEvent(Event& event) {}

Expand Down
2 changes: 1 addition & 1 deletion OpenGL-Core/src/GLCore/ImGui/ImGuiLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace GLCore {
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows

io.WantCaptureMouse = false;
// Setup Dear ImGui style
ImGui::StyleColorsDark();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace GLCore::Utils {
bool OrthographicCameraController::OnMouseScrolled(MouseScrolledEvent& e)
{
m_ZoomLevel -= e.GetYOffset() * 0.25f;
m_ZoomLevel = std::max(m_ZoomLevel, 0.25f);
m_ZoomLevel = std::max(m_ZoomLevel, 0.01f);
m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);
return false;
}
Expand Down
95 changes: 95 additions & 0 deletions OpenGL-Core/src/GLCore/Util/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "glpch.h"
#include "Renderer.h"

#include <iostream>

#include "stb_image.h"



Texture CreateTexture(int width, int height)
{
Texture result;
result.Width = width;
result.Height = height;

glCreateTextures(GL_TEXTURE_2D, 1, &result.Handle);

glTextureStorage2D(result.Handle, 1, GL_RGBA32F, width, height);

glTextureParameteri(result.Handle, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTextureParameteri(result.Handle, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

return result;
}

Texture LoadTexture(const std::filesystem::path& path)
{
int width, height, channels;
std::string filepath = path.string();
unsigned char* data = stbi_load(filepath.c_str(), &width, &height, &channels, 0);

if (!data)
{
std::cerr << "Failed to load texture: " << filepath << "\n";
return {};
}

GLenum format = channels == 4 ? GL_RGBA :
channels == 3 ? GL_RGB :
channels == 1 ? GL_RED : 0;

Texture result;
result.Width = width;
result.Height = height;

glCreateTextures(GL_TEXTURE_2D, 1, &result.Handle);

glTextureStorage2D(result.Handle, 1, (format == GL_RGBA ? GL_RGBA8 : GL_RGB8), width, height);

glTextureSubImage2D(result.Handle, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, data);

glTextureParameteri(result.Handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(result.Handle, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_T, GL_REPEAT);

glGenerateTextureMipmap(result.Handle);
stbi_image_free(data);

return result;
}

Framebuffer CreateFramebufferWithTexture(const Texture texture)
{
Framebuffer result;

glCreateFramebuffers(1, &result.Handle);

if (!AttachTextureToFramebuffer(result, texture))
{
glDeleteFramebuffers(1, &result.Handle);
return {};
}

return result;
}

bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture)
{
glNamedFramebufferTexture(framebuffer.Handle, GL_COLOR_ATTACHMENT0, texture.Handle, 0);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
std::cerr << "Framebuffer is not complete!" << std::endl;
return false;
}

framebuffer.ColorAttachment = texture;
return true;
}

25 changes: 25 additions & 0 deletions OpenGL-Core/src/GLCore/Util/Renderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <filesystem>

struct Texture
{
GLuint Handle = 0;
uint32_t Width = 0;
uint32_t Height = 0;
};

struct Framebuffer
{
GLuint Handle = 0;
Texture ColorAttachment;
};

Texture CreateTexture(int width, int height);
Texture LoadTexture(const std::filesystem::path& path);
Framebuffer CreateFramebufferWithTexture(const Texture texture);
bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture);
71 changes: 71 additions & 0 deletions OpenGL-Core/src/GLCore/Util/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,75 @@ namespace GLCore::Utils {
m_RendererID = program;
}

GLint CreateComputeShader(const std::string& filepath)
{
std::string shaderSource = ReadFileAsString(filepath);

GLuint shaderHandle = glCreateShader(GL_COMPUTE_SHADER);

const GLchar* source = (const GLchar*)shaderSource.c_str();
glShaderSource(shaderHandle, 1, &source, 0);

glCompileShader(shaderHandle);

GLint isCompiled = 0;
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &maxLength);

std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(shaderHandle, maxLength, &maxLength, &infoLog[0]);

std::cerr << infoLog.data() << std::endl;

glDeleteShader(shaderHandle);
return 0;
}

GLuint program = glCreateProgram();
glAttachShader(program, shaderHandle);
glLinkProgram(program);

GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);

std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);

std::cerr << infoLog.data() << std::endl;

glDeleteProgram(program);
glDeleteShader(shaderHandle);

return 0;
}

glDetachShader(program, shaderHandle);
return program;
}

GLint ReloadComputeShader(GLint ComputeID, const std::string& filepath)
{
glDeleteProgram(ComputeID);

return CreateComputeShader(filepath);
}

Shader* ReloadGraphicsShader(Shader* shader, const std::string& vertexShaderPath, const std::string& fragmentShaderPath)
{
delete shader;
Shader* newShader = Shader::FromGLSLTextFiles(vertexShaderPath, fragmentShaderPath);

return newShader;
}




}
11 changes: 11 additions & 0 deletions OpenGL-Core/src/GLCore/Util/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,30 @@ namespace GLCore::Utils {

class Shader
{

public:

~Shader();

GLuint GetRendererID() { return m_RendererID; }

static Shader* FromGLSLTextFiles(const std::string& vertexShaderPath, const std::string& fragmentShaderPath);

private:
Shader() = default;

void LoadFromGLSLTextFiles(const std::string& vertexShaderPath, const std::string& fragmentShaderPath);
GLuint CompileShader(GLenum type, const std::string& source);

private:
GLuint m_RendererID;

};


GLint CreateComputeShader(const std::string& filepath);
GLint ReloadComputeShader(GLint ComputeID, std::string& filepath);
Shader* ReloadGraphicsShader(Shader* shader, const std::string& vertexShaderPath, const std::string& fragmentShaderPath);


}
61 changes: 61 additions & 0 deletions OpenGL-Core/src/GLCore/Util/Texture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "glpch.h"
#include "Texture.h"
#include "stb_image.h"

Texture CreateTexture(int width, int height)
{
Texture result;
result.Width = width;
result.Height = height;

glCreateTextures(GL_TEXTURE_2D, 1, &result.TextureID);

glTextureStorage2D(result.TextureID, 1, GL_RGBA32F, width, height);

glTextureParameteri(result.TextureID, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTextureParameteri(result.TextureID, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTextureParameteri(result.TextureID, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(result.TextureID, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

return result;
}


Texture LoadTexture(const std::string filepath)
{
int width, height, channels;

unsigned char* data = stbi_load(filepath.c_str(), &width, &height, &channels, 0);

if (!data)
{
std::cerr << "Failed to load texture: " << filepath << "\n";
return {};
}

GLenum format = channels == 4 ? GL_RGBA :
channels == 3 ? GL_RGB :
channels == 1 ? GL_RED : 0;

Texture result;
result.Width = width;
result.Height = height;

glCreateTextures(GL_TEXTURE_2D, 1, &result.TextureID);

glTextureStorage2D(result.TextureID, 1, (format == GL_RGBA ? GL_RGBA8 : GL_RGB8), width, height);

glTextureSubImage2D(result.TextureID, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, data);

glTextureParameteri(result.TextureID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(result.TextureID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTextureParameteri(result.TextureID, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(result.TextureID, GL_TEXTURE_WRAP_T, GL_REPEAT);

glGenerateTextureMipmap(result.TextureID);
stbi_image_free(data);

return result;
}
15 changes: 15 additions & 0 deletions OpenGL-Core/src/GLCore/Util/Texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <string>

#include <glad/glad.h>

struct Texture
{
GLuint TextureID = 0;
uint32_t Width = 0;
uint32_t Height = 0;
};

Texture CreateTexture(int width, int height);
Texture LoadTexture(const std::string filepath);

Binary file modified OpenGL-Core/vendor/Glad/bin/Debug-windows-x86_64/Glad/Glad.lib
Binary file not shown.
Binary file not shown.
Loading