sculpto
gl_vertex_array.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file mesh.h
3 * \brief OpenGL mesh (vertex_array) class definition module.
4 * Mesh stores vertex and index buffer and implement their binding during render.
5 *
6 * \author Sabitov Kirill
7 * \date 27 June 2022
8 *********************************************************************/
9
10#include "sclpch.h"
11#include "gl_vertex_array.h"
12
13inline constexpr GLenum scl::gl_vertex_array::GetGLShaderVariableType(shader_variable_type Type)
14{
15 switch (Type)
16 {
18 return GL_BOOL;
19
24 return GL_INT;
25
31 return GL_FLOAT;
32 }
33
34 SCL_CORE_ASSERT(0, "Unknown shader veriable type.");
35 return GLenum();
36}
37
39{
40 SCL_CORE_ASSERT(VertexBuffer->GetVertexLayout().GetCount() != 0,
41 "Vertex Buffer has no layout!");
42
43 this->VertexBuffer = VertexBuffer;
44 this->Bind();
45 VertexBuffer->Bind();
46
47 const auto &layout = VertexBuffer->GetVertexLayout();
48 for (const auto &element : layout)
49 {
50 switch (element.Type)
51 {
57 glEnableVertexAttribArray(element.Index);
58 glVertexAttribIPointer(element.Index,
60 GetGLShaderVariableType(element.Type),
61 layout.GetSize(),
62 (const void *)(u64)element.Offset);
63 break;
68 glEnableVertexAttribArray(element.Index);
69 glVertexAttribPointer(element.Index,
71 GetGLShaderVariableType(element.Type),
72 GL_FALSE,
73 layout.GetSize(),
74 (const void *)(u64)element.Offset);
75 break;
77 {
79 for (uint8_t i = 0; i < count; i++)
80 {
81 glEnableVertexAttribArray(element.Index);
82 glVertexAttribPointer(element.Index,
83 count,
84 GetGLShaderVariableType(element.Type),
85 GL_FALSE,
86 layout.GetSize(),
87 (const void *)(element.Offset + sizeof(float) * count * i));
88 glVertexAttribDivisor(element.Index, 1);
89 }
90 break;
91 }
92 default:
93 SCL_CORE_ASSERT(0, "Unknown element type!");
94 break;
95 }
96 }
97
98 VertexBuffer->Unbind();
99 this->Unbind();
100}
101
103{
104 this->IndexBuffer = IndexBuffer;
105 this->Bind();
106 IndexBuffer->Bind();
107 this->Unbind();
108 IndexBuffer->Unbind();
109}
110
112 vertex_array(MeshType)
113{
114 glCreateVertexArrays(1, &Id);
115 SCL_CORE_ASSERT(Id != 0, "Vertex array OpenGL primitive creation error.");
116 SCL_CORE_SUCCES("OpenGL Vertex array with id {} created.", Id);
117}
118
120{
121 glDeleteVertexArrays(1, &Id);
122 SCL_CORE_INFO("OpenGL Vertex array with id {} freed.", Id);
123}
124
126{
127 glBindVertexArray(Id);
128}
129
131{
132 glBindVertexArray(0);
133}
#define SCL_CORE_ASSERT(expr,...)
Definition: assert.h:69
void SetIndexBuffer(const shared< index_buffer > &IndexBuffer)
gl_vertex_array(mesh_type MeshType)
void Unbind() const override
void Bind() const override
void SetVertexBuffer(const shared< vertex_buffer > &VertexBuffer)
static u32 GetShaderVariableComponentsCount(shader_variable_type Type)
#define SCL_CORE_SUCCES(...)
Definition: log.h:42
#define SCL_CORE_INFO(...)
Definition: log.h:41
std::shared_ptr< T > shared
Definition: smart_ptr.h:15
shader_variable_type
mesh_type
Definition: vertex_array.h:18
uint32_t u32
Definition: math_common.h:21
uint64_t u64
Definition: math_common.h:23
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.