@@ -14,27 +14,6 @@ namespace Infinite {
Application* Application::s_Instance = nullptr;
- static GLenum ShaderDataTypeToOpenGLBaseType(ShaderDataType type) - { - switch (type) - { - case Infinite::ShaderDataType::Float: return GL_FLOAT; - case Infinite::ShaderDataType::Float2: return GL_FLOAT; - case Infinite::ShaderDataType::Float3: return GL_FLOAT; - case Infinite::ShaderDataType::Float4: return GL_FLOAT; - case Infinite::ShaderDataType::Mat3: return GL_FLOAT; - case Infinite::ShaderDataType::Mat4: return GL_FLOAT; - case Infinite::ShaderDataType::Int: return GL_INT; - case Infinite::ShaderDataType::Int2: return GL_INT; - case Infinite::ShaderDataType::Int3: return GL_INT; - case Infinite::ShaderDataType::Int4: return GL_INT; - case Infinite::ShaderDataType::Bool: return GL_BOOL; - } - - IFN_CORE_ASSERT(false, "Unknown ShaderDataType!"); - return 0; - }
Application::Application() { IFN_CORE_ASSERT(!s_Instance, "Application already exists!"); @@ -46,9 +25,7 @@ namespace Infinite { m_ImGuiLayer = new ImGuiLayer(); PushOverlay(m_ImGuiLayer);
- // Vertex Array - glGenVertexArrays(1, &m_VertexArray); - glBindVertexArray(m_VertexArray); - m_VertexArray.reset(VertexArray::Create());
float vertices[3 * 7] = { @@ -57,34 +34,42 @@ namespace Infinite { 0.0f, 0.5f, 0.0f, 0.8f, 0.8f, 0.2f, 1.0f };
- m_VertexBuffer.reset(VertexBuffer::Create(vertices, sizeof(vertices))); + std::shared_ptr<VertexBuffer> vertexBuffer; + vertexBuffer.reset(VertexBuffer::Create(vertices, sizeof(vertices))); + BufferLayout layout = { + { ShaderDataType::Float3, "a_Position" }, + { ShaderDataType::Float4, "a_Color" } + };
- { - BufferLayout layout = { - { ShaderDataType::Float3, "a_Position" }, - { ShaderDataType::Float4, "a_Color" } - }; + vertexBuffer->SetLayout(layout); + m_VertexArray->AddVertexBuffer(vertexBuffer);
+ uint32_t indices[3] = { 0, 1, 2 }; + std::shared_ptr<IndexBuffer> indexBuffer; + indexBuffer.reset(IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t))); + m_VertexArray->SetIndexBuffer(indexBuffer);
- m_VertexBuffer->SetLayout(layout); - }
- uint32_t index = 0; - const auto& layout = m_VertexBuffer->GetLayout(); - for (const auto& element: layout) - { - glEnableVertexAttribArray(index); - glVertexAttribPointer(index, - element.GetComponentCount(), - ShaderDataTypeToOpenGLBaseType(element.Type), - element.Normalized ? GL_TRUE : GL_FALSE, - layout.GetStride(), - (const void*)element.Offset); - index++; - }
+ m_SquareVertexArray.reset(VertexArray::Create()); + + float squareVertices[3 * 4] = { + -0.75f, -0.75f, 0.0f, + 0.75f, -0.75f, 0.0f, + 0.75f, 0.75f, 0.0f, + -0.75f, 0.75f, 0.0f + }; + std::shared_ptr<VertexBuffer> squareVertexBuffer; + squareVertexBuffer.reset(VertexBuffer::Create(squareVertices, sizeof(squareVertices))); + squareVertexBuffer->SetLayout({ + { ShaderDataType::Float3, "a_Position" } + }); + m_SquareVertexArray->AddVertexBuffer(squareVertexBuffer); + + uint32_t squareIndices[6] = { 0, 1, 2, 2, 3, 0 }; + std::shared_ptr<IndexBuffer> squareIndexBuffer; + squareIndexBuffer.reset(IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))); + m_SquareVertexArray->SetIndexBuffer(squareIndexBuffer);
- uint32_t indices[3] = { 0, 1, 2 }; - m_IndexBuffer.reset(IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t)));
std::string vertexSrc = R"( @@ -119,9 +104,35 @@ namespace Infinite { } )";
+ m_Shader.reset(new Shader(vertexSrc, fragmentSrc));
- m_Shader.reset(new Shader(vertexSrc, fragmentSrc));
+ std::string blueShaderVertexSrc = R"( + #version 330 core + + layout(location = 0) in vec3 a_Position; + out vec3 v_Position; + void main() + { + v_Position = a_Position; + gl_Position = vec4(a_Position, 1.0); + } + )"; + + std::string blueShaderFragmentSrc = R"( + #version 330 core + + layout(location = 0) out vec4 color; + in vec3 v_Position; + void main() + { + color = vec4(0.2, 0.3, 0.8, 1.0); + } + )"; + + m_BlueShader.reset(new Shader(blueShaderVertexSrc, blueShaderFragmentSrc));
}
@@ -156,16 +167,17 @@ namespace Infinite { glClearColor(0.1f, 0.1f, 0.1f, 1); glClear(GL_COLOR_BUFFER_BIT);
- m_Shader->Bind(); - glBindVertexArray(m_VertexArray); - glDrawElements(GL_TRIANGLES, m_IndexBuffer->GetCount(), GL_UNSIGNED_INT, nullptr); + m_BlueShader->Bind(); + m_SquareVertexArray->Bind(); + glDrawElements(GL_TRIANGLES, m_SquareVertexArray->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr);
+ m_Shader->Bind(); + m_VertexArray->Bind(); + glDrawElements(GL_TRIANGLES, m_VertexArray->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr);
for (Layer* layer : m_LayerStack) layer->OnUpdate();
- // auto [x, y] = Input::GetMousePosition(); - // IFN_CORE_TRACE("{0}, {1}", x, y);
m_ImGuiLayer->Begin(); for (Layer* layer : m_LayerStack)
|