Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
OpenGLMesh.cpp
1/*
2Copyright (C) 2016-2019,2021,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include <cassert>
18#include <unordered_map>
19#include <algorithm>
20#include "OpenGLFunctions.hpp"
21#include "aeongames/Mesh.hpp"
22#include "OpenGLMesh.hpp"
23
24namespace AeonGames
25{
26 OpenGLMesh::OpenGLMesh ( const OpenGLRenderer& aOpenGLRenderer, const Mesh& aMesh ) :
27 mOpenGLRenderer{aOpenGLRenderer}, mMesh{&aMesh}
28 {
29 if ( aMesh.GetIndexCount() != 0 )
30 {
31 mIndexBuffer.Initialize
32 ( static_cast<GLsizei> ( aMesh.GetIndexBuffer().size() ), GL_STATIC_DRAW, aMesh.GetIndexBuffer().data() );
33 }
34 assert ( aMesh.GetVertexCount() != 0 );
35 mVertexBuffer.Initialize
36 ( static_cast<GLsizei> ( aMesh.GetVertexBuffer().size() ), GL_STATIC_DRAW, aMesh.GetVertexBuffer().data() );
37 }
38
39 OpenGLMesh::~OpenGLMesh() = default;
40
42 mOpenGLRenderer{aOpenGLMesh.mOpenGLRenderer},
43 mMesh{aOpenGLMesh.mMesh},
44 mVertexBuffer{std::move ( aOpenGLMesh.mVertexBuffer ) },
45 mIndexBuffer{std::move ( aOpenGLMesh.mIndexBuffer ) }
46 {}
47
48 static std::unordered_map<Mesh::AttributeType, GLsizei> MeshTypeToOGL
49 {
50 {Mesh::BYTE, GL_BYTE},
51 {Mesh::UNSIGNED_BYTE, GL_UNSIGNED_BYTE},
52 {Mesh::SHORT, GL_SHORT},
53 {Mesh::UNSIGNED_SHORT, GL_UNSIGNED_SHORT},
54 {Mesh::HALF_FLOAT, GL_HALF_FLOAT},
55 {Mesh::INT, GL_INT},
56 {Mesh::UNSIGNED_INT, GL_UNSIGNED_INT},
57 {Mesh::FLOAT, GL_FLOAT},
58 {Mesh::FIXED, GL_FIXED},
59 {Mesh::DOUBLE, GL_DOUBLE},
60 };
61
62 void OpenGLMesh::Bind() const
63 {
64 glBindBuffer ( GL_ARRAY_BUFFER, mVertexBuffer.GetBufferId() );
65 OPENGL_CHECK_ERROR_THROW;
66 }
67
68 void OpenGLMesh::EnableAttributes ( const std::vector<OpenGLVariable>& aAttributes ) const
69 {
70 for ( GLuint i = 0; i < 8; ++i )
71 {
72 glDisableVertexAttribArray ( i );
73 }
74
75 size_t offset{0};
76 for ( auto& attribute : mMesh->GetAttributes() )
77 {
78 auto it = std::lower_bound ( aAttributes.begin(), aAttributes.end(), std::get<0> ( attribute ),
79 [] ( const OpenGLVariable & a, uint32_t b )
80 {
81 return a.name < b;
82 } );
83 if ( it == aAttributes.end() || it->name != std::get<0> ( attribute ) )
84 {
85 /* If we get here, it means the attribute is not present in aAttributes,
86 but it is present in the mesh, so skip it and continue. */
87 offset += GetAttributeTotalSize ( attribute );
88 continue;
89 }
90
91 glEnableVertexAttribArray ( it->location );
92 OPENGL_CHECK_ERROR_THROW;
93 if ( ! ( std::get<3> ( attribute ) & Mesh::AttributeFlag::INTEGER ) )
94 {
95 glVertexAttribPointer (
96 it->location,
97 std::get<1> ( attribute ),
98 MeshTypeToOGL[std::get<2> ( attribute )],
99 std::get<3> ( attribute ) & Mesh::AttributeFlag::NORMALIZED,
100 static_cast<GLsizei> ( mMesh->GetStride() ),
101 reinterpret_cast<const void*> ( offset ) );
102 OPENGL_CHECK_ERROR_THROW;
103 }
104 else
105 {
106 glVertexAttribIPointer (
107 it->location,
108 std::get<1> ( attribute ),
109 MeshTypeToOGL[std::get<2> ( attribute )],
110 static_cast<GLsizei> ( mMesh->GetStride() ),
111 reinterpret_cast<const void*> ( offset ) );
112 }
113 offset += GetAttributeTotalSize ( attribute );
114 }
115 //---Index Buffer---
116 if ( mIndexBuffer.GetSize() != 0 )
117 {
118 glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer.GetBufferId() );
119 }
120 else
121 {
122 glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, 0 );
123 }
124 OPENGL_CHECK_ERROR_THROW;
125 }
126
127 void OpenGLMesh::DisableAttributes ( const std::vector<OpenGLVariable>& aAttributes ) const
128 {
129 for ( const auto& attribute : aAttributes )
130 {
131 glDisableVertexAttribArray ( attribute.location );
132 }
133 }
134}
Represents a polygon mesh with vertex attributes and index data.
Definition Mesh.hpp:31
@ UNSIGNED_SHORT
Unsigned 16-bit integer.
Definition Mesh.hpp:52
@ HALF_FLOAT
16-bit floating point.
Definition Mesh.hpp:53
@ BYTE
Signed 8-bit integer.
Definition Mesh.hpp:49
@ DOUBLE
64-bit floating point.
Definition Mesh.hpp:58
@ SHORT
Signed 16-bit integer.
Definition Mesh.hpp:51
@ UNSIGNED_BYTE
Unsigned 8-bit integer.
Definition Mesh.hpp:50
@ FLOAT
32-bit floating point.
Definition Mesh.hpp:56
@ FIXED
Fixed-point.
Definition Mesh.hpp:57
@ INT
Signed 32-bit integer.
Definition Mesh.hpp:54
@ UNSIGNED_INT
Unsigned 32-bit integer.
Definition Mesh.hpp:55
@ INTEGER
Values are passed as integers (no conversion).
Definition Mesh.hpp:65
@ NORMALIZED
Values are normalized to [0,1] or [-1,1].
Definition Mesh.hpp:64
DLL uint32_t GetIndexCount() const
Get the total number of indices.
Definition Mesh.cpp:48
DLL const std::vector< uint8_t > & GetIndexBuffer() const
Get the raw index data buffer.
Definition Mesh.cpp:63
DLL const std::vector< uint8_t > & GetVertexBuffer() const
Get the raw vertex data buffer.
Definition Mesh.cpp:58
DLL uint32_t GetVertexCount() const
Get the total number of vertices.
Definition Mesh.cpp:53
void DisableAttributes(const std::vector< OpenGLVariable > &aAttributes) const
Disable the specified vertex attributes.
OpenGLMesh(const OpenGLRenderer &aOpenGLRenderer, const Mesh &aMesh)
Construct from a renderer and mesh resource.
void Bind() const
Bind the vertex and index buffers.
void EnableAttributes(const std::vector< OpenGLVariable > &aAttributes) const
Enable the specified vertex attributes for rendering.
OpenGL rendering backend implementing the Renderer interface.
<- This is here just for the literals
Definition AABB.hpp:31
DLL size_t GetAttributeTotalSize(const Mesh::AttributeTuple &aAttributeTuple)
Compute the total byte size of a single vertex attribute.
Definition Mesh.cpp:73
STL namespace.
Describes an OpenGL shader variable with its location and type.