Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
OpenGLRenderer.hpp
1/*
2Copyright (C) 2016-2022,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#ifndef AEONGAMES_OPENGLRENDERER_HPP
17#define AEONGAMES_OPENGLRENDERER_HPP
18
19#include <unordered_map>
20#include "aeongames/Renderer.hpp"
23#include "OpenGLFunctions.hpp"
24#include "OpenGLBuffer.hpp"
25#include "OpenGLMesh.hpp"
26#include "OpenGLPipeline.hpp"
27#include "OpenGLMaterial.hpp"
28#include "OpenGLTexture.hpp"
29#include "OpenGLWindow.hpp"
30#ifdef Status
31#undef Status
32#endif
34
35namespace AeonGames
36{
37 class Mesh;
38 class Pipeline;
39 class Material;
40 class UniformBuffer;
41 class OpenGLMesh;
42 class OpenGLModel;
44 class OpenGLRenderer : public Renderer
45 {
46 public:
48 OpenGLRenderer ( void* aWindow );
50 void LoadMesh ( const Mesh& aMesh ) final;
51 void UnloadMesh ( const Mesh& aMesh ) final;
53 void* GetContext() const;
55 GLuint GetVertexArrayObject() const;
57 GLuint GetOverlayProgram() const;
59 GLuint GetOverlayQuad() const;
60
62 void BindMesh ( const Mesh& aMesh );
64 void BindPipeline ( const Pipeline& aPipeline );
66 void SetMaterial ( const Material& aMaterial );
67
69 void SetSkeleton ( const BufferAccessor& aSkeletonBuffer ) const;
71 void SetMatrices ( const OpenGLBuffer& aMatricesBuffer ) const;
72 void LoadPipeline ( const Pipeline& aPipeline ) final;
73 void UnloadPipeline ( const Pipeline& aPipeline ) final;
74 void LoadMaterial ( const Material& aMaterial ) final;
75 void UnloadMaterial ( const Material& aMaterial ) final;
76 void LoadTexture ( const Texture& aTexture ) final;
77 void UnloadTexture ( const Texture& aTexture ) final;
79 GLuint GetTextureId ( const Texture& aTexture );
80
81 void AttachWindow ( void* aWindowId ) final;
82 void DetachWindow ( void* aWindowId ) final;
83 void SetProjectionMatrix ( void* aWindowId, const Matrix4x4& aMatrix ) final;
84 void SetViewMatrix ( void* aWindowId, const Matrix4x4& aMatrix ) final;
85 void SetClearColor ( void* aWindowId, float R, float G, float B, float A ) final;
86 void ResizeViewport ( void* aWindowId, int32_t aX, int32_t aY, uint32_t aWidth, uint32_t aHeight ) final;
87 void BeginRender ( void* aWindowId ) final;
88 void EndRender ( void* aWindowId ) final;
89 void Render ( void* aWindowId,
90 const Matrix4x4& aModelMatrix,
91 const Mesh& aMesh,
92 const Pipeline& aPipeline,
93 const Material* aMaterial = nullptr,
94 const BufferAccessor* aSkeleton = nullptr,
96 uint32_t aVertexStart = 0,
97 uint32_t aVertexCount = 0xffffffff,
98 uint32_t aInstanceCount = 1,
99 uint32_t aFirstInstance = 0 ) const final;
100 const Frustum& GetFrustum ( void* aWindowId ) const final;
101 BufferAccessor AllocateSingleFrameUniformMemory ( void* aWindowId, size_t aSize ) final;
102#if defined(_WIN32)
103 bool MakeCurrent ( HDC aDeviceContext = nullptr );
104#elif defined(__unix__)
105 Display* GetDisplay() const;
106 bool MakeCurrent ( ::Window aWindow = None );
107#endif
108 protected:
110 void InitializeOverlay();
112 void FinalizeOverlay();
113#if defined(_WIN32)
114 HWND mWindowId {};
115 HDC mDeviceContext{};
116 HGLRC mOpenGLContext{};
117#elif defined(__unix__)
118 GLXContext mOpenGLContext {None};
119#endif
135 std::unordered_map<size_t, OpenGLPipeline> mPipelineStore{};
136 std::unordered_map<size_t, OpenGLMaterial> mMaterialStore{};
137 std::unordered_map<size_t, OpenGLMesh> mMeshStore{};
138 std::unordered_map<size_t, OpenGLTexture> mTextureStore{};
139 std::unordered_map<void*, OpenGLWindow> mWindowStore{};
140 private:
141 static std::atomic<size_t> mRendererCount;
142#if defined(__unix__)
143 static Display* mDisplay;
144#endif
145 };
146}
147#endif
Header for 4x4 matrix class.
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
Header for Transform component class.
Provides access to a region within a memory pool buffer.
View frustum defined by six clipping planes, extracted from a projection (or view-projection) matrix.
Definition Frustum.hpp:35
Represents a surface material with uniform properties and texture samplers.
Definition Material.hpp:38
4 by 4 matrix in colum mayor order.
Definition Matrix4x4.hpp:35
Represents a polygon mesh with vertex attributes and index data.
Definition Mesh.hpp:31
OpenGL GPU buffer wrapper implementing the Buffer interface.
OpenGL mesh wrapper managing vertex and index buffers.
OpenGL shader program pipeline with attribute and uniform reflection.
void LoadPipeline(const Pipeline &aPipeline) final
Loads a rendering pipeline (shaders and state) into the renderer.
void FinalizeOverlay()
Release overlay shader program and quad buffer.
void UnloadTexture(const Texture &aTexture) final
Unloads a texture from GPU memory.
void LoadMaterial(const Material &aMaterial) final
Loads material data into the renderer.
void BindMesh(const Mesh &aMesh)
Bind a mesh for subsequent draw calls.
void SetViewMatrix(void *aWindowId, const Matrix4x4 &aMatrix) final
Sets the view matrix for a specific window surface.
void InitializeOverlay()
Initialize overlay shader program and quad buffer.
BufferAccessor AllocateSingleFrameUniformMemory(void *aWindowId, size_t aSize) final
Allocates uniform buffer memory that is valid for a single frame.
GLuint GetOverlayQuad() const
Get the overlay screen-quad buffer.
std::unordered_map< size_t, OpenGLMaterial > mMaterialStore
Loaded material cache.
void UnloadMesh(const Mesh &aMesh) final
Unloads mesh data from GPU memory.
void SetProjectionMatrix(void *aWindowId, const Matrix4x4 &aMatrix) final
Sets the projection matrix for a specific window surface.
void BeginRender(void *aWindowId) final
Begins a render pass for the given window surface.
void EndRender(void *aWindowId) final
Ends the current render pass for the given window surface.
std::unordered_map< void *, OpenGLWindow > mWindowStore
Attached window map.
void SetMatrices(const OpenGLBuffer &aMatricesBuffer) const
Bind the matrices uniform buffer for the current draw.
const Frustum & GetFrustum(void *aWindowId) const final
Returns the view frustum for the given window surface.
void SetSkeleton(const BufferAccessor &aSkeletonBuffer) const
Upload skeleton joint matrices for skinned rendering.
void LoadMesh(const Mesh &aMesh) final
Loads mesh data into GPU memory.
std::unordered_map< size_t, OpenGLPipeline > mPipelineStore
Loaded pipeline cache.
OpenGLRenderer(void *aWindow)
Construct from a native window handle.
GLuint mVertexArrayObject
General VAO.
std::unordered_map< size_t, OpenGLTexture > mTextureStore
Loaded texture cache.
void BindPipeline(const Pipeline &aPipeline)
Bind a pipeline (shader program) for rendering.
void Render(void *aWindowId, const Matrix4x4 &aModelMatrix, const Mesh &aMesh, const Pipeline &aPipeline, const Material *aMaterial=nullptr, const BufferAccessor *aSkeleton=nullptr, Topology aTopology=Topology::TRIANGLE_LIST, uint32_t aVertexStart=0, uint32_t aVertexCount=0xffffffff, uint32_t aInstanceCount=1, uint32_t aFirstInstance=0) const final
Issues a draw call for a mesh with the given pipeline and optional material.
void UnloadPipeline(const Pipeline &aPipeline) final
Unloads a rendering pipeline from the renderer.
OpenGLPipeline * mCurrentPipeline
Currently bound pipeline.
void SetMaterial(const Material &aMaterial)
Set the active material for rendering.
void LoadTexture(const Texture &aTexture) final
Loads a texture into GPU memory.
void UnloadMaterial(const Material &aMaterial) final
Unloads material data from the renderer.
std::unordered_map< size_t, OpenGLMesh > mMeshStore
Loaded mesh cache.
void SetClearColor(void *aWindowId, float R, float G, float B, float A) final
Sets the color to be used to clear the window background.
void ResizeViewport(void *aWindowId, int32_t aX, int32_t aY, uint32_t aWidth, uint32_t aHeight) final
Resizes the specific window surface's viewport.
void AttachWindow(void *aWindowId) final
Attach a Window as a rendering surface.
void DetachWindow(void *aWindowId) final
Detach a Window as a rendering surface.
GLuint GetOverlayProgram() const
Get the overlay shader program identifier.
GLuint GetVertexArrayObject() const
Get the shared vertex array object.
GLuint GetTextureId(const Texture &aTexture)
Get the OpenGL texture identifier for a loaded texture.
void * GetContext() const
Get the platform-specific OpenGL rendering context.
Rendering pipeline resource.
Definition Pipeline.hpp:122
Abstract base class for rendering backends.
Definition Renderer.hpp:44
Represents a 2D texture image resource.
Definition Texture.hpp:33
OpenGLBuffer mOverlayQuad
Overlay quadrilateral.
GLuint mOverlayProgram
Raw overlay shader program.
<- This is here just for the literals
Definition AABB.hpp:31
Topology
Primitive topology types for rendering.
Definition Pipeline.hpp:38
@ TRIANGLE_LIST
Independent triangles.
Definition Pipeline.hpp:45