Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
GraphicsAPI.h
Go to the documentation of this file.
1/**
2 * @file GraphicsAPI.h
3 * @ingroup mnd_graphics
4 * @brief Thin wrapper around raw OpenGL 3.3 Core Profile calls.
5 *
6 * GraphicsAPI sits between the application and GLEW/GL. It creates and
7 * manages GPU resources (VBOs, IBOs, shader programs) and issues draw calls.
8 * All other engine code — particularly RenderQueue — goes through this class
9 * rather than calling gl* functions directly, making the rendering layer easy
10 * to swap out or extend without touching game code.
11 *
12 * Owned by the Engine singleton; access via Engine::GetInstance().GetGraphicsAPI().
13 *
14 * @see RenderQueue, ShaderProgram, Mesh, Material
15 */
16
17#pragma once
18#include <memory>
19#include <string>
20#include <vector>
21
22#include "GL/glew.h"
23
24namespace mnd
25{
26
27class ShaderProgram;
28class Material;
29class Mesh;
30
31/**
32 * @brief OpenGL 3.3 Core Profile abstraction layer.
33 *
34 * Responsibilities:
35 * - Initialise default render state (depth test, blending …).
36 * - Compile and link GLSL programs via CreateShaderProgram().
37 * - Allocate VBO / IBO objects on the GPU.
38 * - Bind materials and meshes to the current GL state before draw calls.
39 */
41{
42public:
43 /**
44 * @brief Set up initial OpenGL state (depth test, face culling, etc.).
45 * @return true on success.
46 */
47 bool Init();
48
49 /**
50 * @brief Compile vertex + fragment GLSL source and link into a program.
51 * @param vertexSource GLSL source string for the vertex stage.
52 * @param fragmentSource GLSL source string for the fragment stage.
53 * @return Shared pointer to the linked ShaderProgram.
54 */
55 std::shared_ptr<ShaderProgram> CreateShaderProgram(const std::string &vertexSource,
56 const std::string &fragmentSource);
57
58 /**
59 * @brief Returns the built-in default shader (Blinn-Phong + diffuse texture).
60 *
61 * Loaded once during Init(). Materials that do not specify their own
62 * shader fall back to this program.
63 */
64 const std::shared_ptr<ShaderProgram> &GetDefaultShaderProgram();
65
66 /**
67 * @brief Upload a flat array of floats to a new GPU vertex buffer.
68 * @param vertices Interleaved vertex data (position, normal, UV, …).
69 * @return GL handle to the created VBO.
70 */
71 GLuint CreateVertexBuffer(const std::vector<float> &vertices);
72
73 /**
74 * @brief Upload a flat array of u32 indices to a new GPU index buffer.
75 * @param indices Triangle list indices into the vertex buffer.
76 * @return GL handle to the created EBO.
77 */
78 GLuint CreateIndexBuffer(const std::vector<uint32_t> &indices);
79
80 /**
81 * @brief Set the colour that glClear() fills the framebuffer with.
82 * @param r,g,b,a Linear RGBA components in [0, 1].
83 */
84 void SetClearColor(float r, float g, float b, float a);
85
86 /// Clear the colour and depth buffers ready for the next frame.
87 void ClearBuffers();
88
89 /**
90 * @brief Bind a compiled shader program to the current GL state.
91 * @param shaderProgram The program to activate; must not be nullptr.
92 */
93 void BindShaderProgram(ShaderProgram *shaderProgram);
94
95 /**
96 * @brief Upload all material parameters as uniforms to the active shader.
97 * @param material Material whose float / texture params are applied.
98 */
100
101 /**
102 * @brief Bind the mesh's VAO so the next draw call uses its geometry.
103 * @param mesh Mesh whose VAO/VBO/EBO layout is activated.
104 */
105 void BindMesh(Mesh *mesh);
106
107 void UnbindMesh(Mesh *mesh);
108
109 /**
110 * @brief Issue a draw call for the bound mesh.
111 *
112 * Uses indexed drawing (glDrawElements) when the mesh has an EBO,
113 * otherwise falls back to glDrawArrays.
114 *
115 * @param mesh The mesh to draw (must already be bound via BindMesh).
116 */
117 void DrawMesh(Mesh *mesh);
118
119private:
120 /// Fallback shader compiled on Init(); used when no material provides one.
121 std::shared_ptr<ShaderProgram> m_defaultShaderProgram;
122};
123
124} // namespace mnd
unsigned int GLuint
Definition GLForward.h:11
std::shared_ptr< Material > material
std::shared_ptr< Mesh > mesh
OpenGL 3.3 Core Profile abstraction layer.
Definition GraphicsAPI.h:41
const std::shared_ptr< ShaderProgram > & GetDefaultShaderProgram()
Returns the built-in default shader (Blinn-Phong + diffuse texture).
void BindShaderProgram(ShaderProgram *shaderProgram)
Bind a compiled shader program to the current GL state.
void ClearBuffers()
Clear the colour and depth buffers ready for the next frame.
void BindMaterial(Material *material)
Upload all material parameters as uniforms to the active shader.
void UnbindMesh(Mesh *mesh)
void DrawMesh(Mesh *mesh)
Issue a draw call for the bound mesh.
void SetClearColor(float r, float g, float b, float a)
Set the colour that glClear() fills the framebuffer with.
std::shared_ptr< ShaderProgram > CreateShaderProgram(const std::string &vertexSource, const std::string &fragmentSource)
Compile vertex + fragment GLSL source and link into a program.
GLuint CreateIndexBuffer(const std::vector< uint32_t > &indices)
Upload a flat array of u32 indices to a new GPU index buffer.
bool Init()
Set up initial OpenGL state (depth test, face culling, etc.).
GLuint CreateVertexBuffer(const std::vector< float > &vertices)
Upload a flat array of floats to a new GPU vertex buffer.
void BindMesh(Mesh *mesh)
Bind the mesh's VAO so the next draw call uses its geometry.
Shader + uniform storage that defines how a surface is rendered.
Definition Material.h:54
Immutable GPU mesh (VAO + VBO + optional EBO).
Definition Mesh.h:43
Linked GLSL program with cached uniform locations.