Skip to content

Commit

Permalink
Now trying to render triangle
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Nov 14, 2022
1 parent 974fe04 commit 00be11c
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 17 deletions.
20 changes: 20 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ namespace Arcane
return m_IndexBuffer;
}

void OpenGLVertexBuffer::Bind()
{
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
}

void OpenGLVertexBuffer::UnBind()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

////////////////////////////////////////////////////////
//// Index Buffer
////////////////////////////////////////////////////////
Expand All @@ -42,4 +52,14 @@ namespace Arcane
{
return m_Count;
}

void OpenGLIndexBuffer::Bind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
}

void OpenGLIndexBuffer::UnBind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
}
6 changes: 6 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace Arcane

void AddIndexBuffer(IndexBuffer* _indexBuffer) override;
IndexBuffer* GetIndexBuffer() override;

void Bind();
void UnBind();
private:
IndexBuffer* m_IndexBuffer;
uint32_t m_VBO;
Expand All @@ -27,6 +30,9 @@ namespace Arcane
OpenGLIndexBuffer(void* data, uint32_t count);

uint32_t GetCount();

void Bind();
void UnBind();
private:
uint32_t m_EBO;
uint32_t m_Count;
Expand Down
28 changes: 28 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLPipeline.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
#include "OpenGLPipeline.h"

#include "OpenGLShader.h"
#include "OpenGLVertexDescriptor.h"

namespace Arcane
{
OpenGLPipeline::OpenGLPipeline(PipelineSpecification& spec)
{
m_Spec = spec;
}

void OpenGLPipeline::BindShader()
{
OpenGLShader* shader = static_cast<OpenGLShader*>(m_Spec.shader);
shader->Bind();
}

void OpenGLPipeline::BindVertexDescriptor()
{
OpenGLVertexDescriptor* vertexDescriptor = static_cast<OpenGLVertexDescriptor*>(m_Spec.descriptor);
vertexDescriptor->Bind();
}

void OpenGLPipeline::UnbindShader()
{
OpenGLShader* shader = static_cast<OpenGLShader*>(m_Spec.shader);
shader->UnBind();
}

void OpenGLPipeline::UnbindVertexDescriptor()
{
OpenGLVertexDescriptor* vertexDescriptor = static_cast<OpenGLVertexDescriptor*>(m_Spec.descriptor);
vertexDescriptor->UnBind();
}
}
8 changes: 7 additions & 1 deletion Arcane/src/Arcane/Platform/OpenGL/OpenGLPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ namespace Arcane
{
public:
OpenGLPipeline(PipelineSpecification& spec);

void BindShader();
void BindVertexDescriptor();

void UnbindShader();
void UnbindVertexDescriptor();
private:

PipelineSpecification m_Spec;
};
}
27 changes: 22 additions & 5 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,28 @@ namespace Arcane

void OpenGLRenderer::BeginRenderPass(RenderPass* renderPass)
{
if (renderPass->GetRenderPassSpecs().SwapchainFramebuffer)
return;

OpenGLFramebuffer* framebuffer = static_cast<OpenGLFramebuffer*>(renderPass->GetRenderPassSpecs().TargetFramebuffer);
framebuffer->Bind();
if (renderPass->GetRenderPassSpecs().SwapchainFramebuffer) {
OpenGLFramebuffer* framebuffer = static_cast<OpenGLFramebuffer*>(renderPass->GetRenderPassSpecs().TargetFramebuffer);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(
framebuffer->GetSpecs().ClearColor.x,
framebuffer->GetSpecs().ClearColor.y,
framebuffer->GetSpecs().ClearColor.z,
framebuffer->GetSpecs().ClearColor.w
);
}
else {
OpenGLFramebuffer* framebuffer = static_cast<OpenGLFramebuffer*>(renderPass->GetRenderPassSpecs().TargetFramebuffer);
framebuffer->Bind();

glClear(GL_COLOR_BUFFER_BIT);
glClearColor(
framebuffer->GetSpecs().ClearColor.x,
framebuffer->GetSpecs().ClearColor.y,
framebuffer->GetSpecs().ClearColor.z,
framebuffer->GetSpecs().ClearColor.w
);
}
}

void OpenGLRenderer::EndRenderPass(RenderPass* renderPass)
Expand Down
14 changes: 14 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace Arcane
auto vertexSpirv = readFile(vertexShader);
spirv_cross::CompilerGLSL glsl(std::move(vertexSpirv));
std::string source = glsl.compile();
std::cout << source << std::endl;

GLuint vertexShaderProgram = glCreateShader(GL_VERTEX_SHADER);
const GLchar* shaderSource = (const GLchar*)source.c_str();
Expand All @@ -66,12 +67,15 @@ namespace Arcane
}
}

std::cout << std::endl;

GLuint fragmentProgram = 0;
// Setup Fragment Source
{
auto fragmentSpirv = readFile(fragmentShader);
spirv_cross::CompilerGLSL glsl(std::move(fragmentSpirv));
std::string source = glsl.compile();
std::cout << source << std::endl;

GLuint fragmentShaderProgram = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* shaderSource = (const GLchar*)source.c_str();
Expand Down Expand Up @@ -145,5 +149,15 @@ namespace Arcane
{
return uint32_t();
}

void OpenGLShader::Bind()
{
glUseProgram(m_ShaderProgram);
}

void OpenGLShader::UnBind()
{
glUseProgram(0);
}

}
3 changes: 3 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace Arcane
DescriptorSet* GetMaterialDescriptor() override;
std::vector<ShaderVariable> GetMaterialVariables() override;
uint32_t GetMaterialSize() override;

void Bind();
void UnBind();
private:
uint32_t m_ShaderProgram;
};
Expand Down
89 changes: 89 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLVertexDescriptor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "OpenGLVertexDescriptor.h"

namespace Arcane
{
OpenGLVertexDescriptor::OpenGLVertexDescriptor(std::initializer_list<VertexType> vertexTypes)
{
// Generate VAO
glGenVertexArrays(1, &m_VAO);
glBindVertexArray(m_VAO);

// Calculate Stride
uint32_t stride = 0;
for (auto element : vertexTypes)
{
switch (element)
{
case VertexType::float1:
{
stride += sizeof(float);
break;
}
case VertexType::float2:
{
stride += sizeof(float) * 2;
break;
}
case VertexType::float3:
{
stride += sizeof(float) * 3;
break;
}
case VertexType::float4:
{
stride += sizeof(float) * 4;
break;
}
default:
break;
}
}

// Attribute Arrays
uint32_t count = 0;
uint32_t offset = 0;
for (auto element : vertexTypes)
{
switch (element)
{
case VertexType::float1:
{
stride += sizeof(float);
break;
}
case VertexType::float2:
{
stride += sizeof(float) * 2;
break;
}
case VertexType::float3:
{
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)offset);
glEnableVertexAttribArray(count);
offset += 3 * sizeof(float);
count += 1;
break;
}
case VertexType::float4:
{
stride += sizeof(float) * 4;
break;
}
default:
break;
}
}

glBindVertexArray(0);
}

void OpenGLVertexDescriptor::Bind()
{
glBindVertexArray(m_VAO);
}

void OpenGLVertexDescriptor::UnBind()
{
glBindVertexArray(0);
}
}
21 changes: 21 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLVertexDescriptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "Arcane/Renderer/VertexDescriptor.h"

#include <glad/glad.h>
#include <iostream>

namespace Arcane
{
class OpenGLVertexDescriptor : public VertexDescriptor
{
public:
OpenGLVertexDescriptor(std::initializer_list<VertexType> vertexTypes);

void Bind();
void UnBind();
private:
uint32_t m_VAO;

};
}
5 changes: 5 additions & 0 deletions Arcane/src/Arcane/Renderer/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// -- Vulkan
#include "Arcane/Platform/Vulkan/VulkanBuffer.h"

// -- OpenGL
#include "Arcane/Platform/OpenGL/OpenGLBuffer.h"

namespace Arcane {

////////////////////////////////////////////////////////
Expand All @@ -15,6 +18,7 @@ namespace Arcane {
switch (RendererAPI::Current())
{
case RendererAPIType::Vulkan: return new VulkanVertexBuffer(data, size);
case RendererAPIType::OpenGL: return new OpenGLVertexBuffer(data, size);
default: return nullptr;
}
}
Expand All @@ -27,6 +31,7 @@ namespace Arcane {
switch (RendererAPI::Current())
{
case RendererAPIType::Vulkan: return new VulkanIndexBuffer(data, count);
case RendererAPIType::OpenGL: return new OpenGLIndexBuffer(data, count);
default: return nullptr;
}
}
Expand Down
4 changes: 4 additions & 0 deletions Arcane/src/Arcane/Renderer/VertexDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
// -- Vulkan
#include "Arcane/Platform/Vulkan/VulkanVertexDescriptor.h"

// -- OpenGL
#include "Arcane/Platform/OpenGL/OpenGLVertexDescriptor.h"

namespace Arcane {
VertexDescriptor* VertexDescriptor::Create(std::initializer_list<VertexType> vertexTypes)
{
switch (RendererAPI::Current())
{
case RendererAPIType::Vulkan: return new VulkanVertexDescriptor(vertexTypes);
case RendererAPIType::OpenGL: return new OpenGLVertexDescriptor(vertexTypes);
default: return nullptr;
}

Expand Down
7 changes: 7 additions & 0 deletions EnchantingTable/src/EditorAssets/Shaders/Triangle.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#version 330
layout (location = 0) out vec4 FragColor;

void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
7 changes: 7 additions & 0 deletions EnchantingTable/src/EditorAssets/Shaders/Triangle.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#version 330
layout (location = 0) in vec3 aPos;

void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
Binary file not shown.
Binary file not shown.

0 comments on commit 00be11c

Please sign in to comment.