@@ -14,6 +14,27 @@ 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!"); @@ -30,18 +51,37 @@ namespace Infinite { glBindVertexArray(m_VertexArray);
- float vertices[3 * 3] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f + float vertices[3 * 7] = { + -0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f, + 0.5f, -0.5f, 0.0f, 0.2f, 0.3f, 0.8f, 1.0f, + 0.0f, 0.5f, 0.0f, 0.8f, 0.8f, 0.2f, 1.0f };
m_VertexBuffer.reset(VertexBuffer::Create(vertices, sizeof(vertices)));
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + { + BufferLayout layout = { + { ShaderDataType::Float3, "a_Position" }, + { ShaderDataType::Float4, "a_Color" } + }; + + m_VertexBuffer->SetLayout(layout); + }
- glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr); + 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++; + }
uint32_t indices[3] = { 0, 1, 2 }; m_IndexBuffer.reset(IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t))); @@ -51,10 +91,15 @@ namespace Infinite { #version 330 core layout(location = 0) in vec3 a_Position; + layout(location = 1) in vec4 a_Color; out vec3 v_Position; + out vec4 v_Color; void main() { v_Position = a_Position; + v_Color = a_Color; gl_Position = vec4(a_Position, 1.0); } )"; @@ -63,10 +108,14 @@ namespace Infinite { #version 330 core layout(location = 0) out vec4 color; in vec3 v_Position; + in vec4 v_Color; void main() { color = vec4(v_Position * 0.5 + 0.5, 1.0); + color = v_Color; } )";
|