18#include "aeongames/Pipeline.hpp"
19#include "aeongames/Material.hpp"
20#include "aeongames/Mesh.hpp"
22#include "aeongames/Node.hpp"
24#include "aeongames/Material.hpp"
25#include "aeongames/MemoryPool.hpp"
26#include "OpenGLWindow.hpp"
27#include "OpenGLRenderer.hpp"
28#include "OpenGLFunctions.hpp"
37 void OpenGLWindow::Initialize()
39 mMatrices.Initialize (
sizeof (
float ) * 16 * 3, GL_DYNAMIC_DRAW );
40 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
41 OPENGL_CHECK_ERROR_THROW;
42 glEnable ( GL_BLEND );
43 OPENGL_CHECK_ERROR_THROW;
44 glDepthFunc ( GL_LESS );
45 OPENGL_CHECK_ERROR_THROW;
46 glEnable ( GL_DEPTH_TEST );
47 OPENGL_CHECK_ERROR_THROW;
48 glCullFace ( GL_BACK );
49 OPENGL_CHECK_ERROR_THROW;
50 glEnable ( GL_CULL_FACE );
51 OPENGL_CHECK_ERROR_THROW;
53 glClearColor ( 0.5f, 0.5f, 0.5f, 1.0f );
54 OPENGL_CHECK_ERROR_THROW;
55 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
56 OPENGL_CHECK_ERROR_THROW;
63 mOpenGLRenderer.MakeCurrent ( mDeviceContext );
64#elif defined(__unix__)
65 mOpenGLRenderer.MakeCurrent ( mWindowId );
67 glClearColor ( R, G, B, A );
70 static GLenum GetIndexType (
const Mesh& aMesh )
75 return GL_UNSIGNED_BYTE;
77 return GL_UNSIGNED_SHORT;
79 return GL_UNSIGNED_INT;
82 throw std::runtime_error (
"Invalid Index Size." );
85 static const std::unordered_map<Topology, GLenum> TopologyMap
102 mOpenGLRenderer{ aOpenGLRenderer },
106 mMemoryPoolBuffer{aOpenGLRenderer}
108 mOpenGLRenderer.MakeCurrent ( mWindowId );
109 mFrameBuffer.Initialize();
110 mMemoryPoolBuffer.Initialize (
static_cast<GLsizei
> ( 8_mb ) );
111 XWindowAttributes xwa;
112 XGetWindowAttributes ( mDisplay, mWindowId, &xwa );
113 glViewport ( xwa.x, xwa.y, xwa.width, xwa.height );
118 mOpenGLRenderer { aOpenGLWindow.mOpenGLRenderer },
119 mFrameBuffer{std::move ( aOpenGLWindow.mFrameBuffer ) },
120 mMemoryPoolBuffer{std::move ( aOpenGLWindow.mMemoryPoolBuffer ) },
121 mMatrices{std::move ( aOpenGLWindow.mMatrices ) }
123 std::swap ( mDisplay, aOpenGLWindow.mDisplay );
124 std::swap ( mWindowId, aOpenGLWindow.mWindowId );
125 std::swap ( mFrustum, aOpenGLWindow.mFrustum );
126 std::swap ( mProjectionMatrix, aOpenGLWindow.mProjectionMatrix );
127 std::swap ( mViewMatrix, aOpenGLWindow.mViewMatrix );
130 OpenGLWindow::~OpenGLWindow()
132 if ( mWindowId != None )
134 mOpenGLRenderer.MakeCurrent();
135 mMemoryPoolBuffer.Finalize();
136 mMatrices.Finalize();
137 mFrameBuffer.Finalize();
143 void OpenGLWindow::SwapBuffers()
145 glXSwapBuffers ( mDisplay, mWindowId );
150 mOpenGLRenderer{ aOpenGLRenderer },
151 mWindowId{ aWindow },
152 mDeviceContext{GetDC ( mWindowId ) },
155 mMemoryPoolBuffer{aOpenGLRenderer}
158 GetWindowRect ( mWindowId, &rect );
159 PIXELFORMATDESCRIPTOR pfd{};
160 pfd.nSize =
sizeof ( PIXELFORMATDESCRIPTOR );
162 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
163 pfd.iPixelType = PFD_TYPE_RGBA;
166 pfd.iLayerType = PFD_MAIN_PLANE;
167 int pf = ChoosePixelFormat ( mDeviceContext, &pfd );
168 SetPixelFormat ( mDeviceContext, pf, &pfd );
169 mOpenGLRenderer.MakeCurrent ( mDeviceContext );
170 mFrameBuffer.Initialize();
171 mMemoryPoolBuffer.Initialize (
static_cast<GLsizei
> ( 8_mb ) );
172 glViewport ( rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top );
173 OPENGL_CHECK_ERROR_THROW;
178 mOpenGLRenderer { aOpenGLWindow.mOpenGLRenderer },
179 mFrameBuffer{std::move ( aOpenGLWindow.mFrameBuffer ) },
180 mMemoryPoolBuffer{std::move ( aOpenGLWindow.mMemoryPoolBuffer ) },
181 mMatrices{std::move ( aOpenGLWindow.mMatrices ) }
183 std::swap ( mWindowId, aOpenGLWindow.mWindowId );
184 std::swap ( mFrustum, aOpenGLWindow.mFrustum );
185 std::swap ( mDeviceContext, aOpenGLWindow.mDeviceContext );
186 std::swap ( mProjectionMatrix, aOpenGLWindow.mProjectionMatrix );
187 std::swap ( mViewMatrix, aOpenGLWindow.mViewMatrix );
190 OpenGLWindow::~OpenGLWindow()
192 if ( mWindowId !=
nullptr )
194 mOpenGLRenderer.MakeCurrent();
195 mMemoryPoolBuffer.Finalize();
196 mMatrices.Finalize();
197 mFrameBuffer.Finalize();
198 ReleaseDC ( mWindowId, mDeviceContext );
203 void OpenGLWindow::SwapBuffers()
205 ::SwapBuffers ( mDeviceContext );
216 uint32_t aVertexStart,
217 uint32_t aVertexCount,
218 uint32_t aInstanceCount,
219 uint32_t aFirstInstance )
const
221 mOpenGLRenderer.BindPipeline ( aPipeline );
223 mMatrices.WriteMemory ( 0,
sizeof (
float ) * 16, aModelMatrix.
GetMatrix4x4() );
224 mOpenGLRenderer.SetMatrices ( mMatrices );
228 mOpenGLRenderer.SetMaterial ( *aMaterial );
233 mOpenGLRenderer.SetSkeleton ( *aSkeleton );
237 mOpenGLRenderer.BindMesh ( aMesh );
240 glDrawElementsInstanced ( TopologyMap.at ( aTopology ), ( aVertexCount != 0xffffffff ) ? aVertexCount : aMesh.
GetIndexCount(),
241 GetIndexType ( aMesh ),
reinterpret_cast<const uint8_t*
> ( 0 ) + aMesh.
GetIndexSize() *aVertexStart, aInstanceCount );
242 OPENGL_CHECK_ERROR_NO_THROW;
246 glDrawArraysInstanced ( TopologyMap.at ( aTopology ), aVertexStart, ( aVertexCount != 0xffffffff ) ? aVertexCount : aMesh.
GetVertexCount(), aInstanceCount );
247 OPENGL_CHECK_ERROR_NO_THROW;
253 return mMemoryPoolBuffer.Allocate ( aSize );
259 mOpenGLRenderer.MakeCurrent ( mDeviceContext );
260#elif defined(__unix__)
261 mOpenGLRenderer.MakeCurrent ( mWindowId );
264 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
265 glEnable ( GL_DEPTH_TEST );
270 mFrameBuffer.Unbind();
271 glClear ( GL_COLOR_BUFFER_BIT );
272 OPENGL_CHECK_ERROR_NO_THROW;
273 glDisable ( GL_DEPTH_TEST );
274 OPENGL_CHECK_ERROR_NO_THROW;
276 glGetIntegerv ( GL_VIEWPORT, dims );
277 OPENGL_CHECK_ERROR_NO_THROW;
278 glBlitNamedFramebuffer (
279 mFrameBuffer.GetFBO(),
291 OPENGL_CHECK_ERROR_NO_THROW;
294 glUseProgram ( mOpenGLRenderer.GetOverlayProgram() );
295 OPENGL_CHECK_ERROR_NO_THROW;
296 glBindBuffer ( GL_ARRAY_BUFFER, mOpenGLRenderer.GetOverlayQuad() );
297 OPENGL_CHECK_ERROR_NO_THROW;
298 glBindTexture ( GL_TEXTURE_2D, mOverlay.GetTextureId() );
299 OPENGL_CHECK_ERROR_NO_THROW;
300 glEnableVertexAttribArray ( 0 );
301 OPENGL_CHECK_ERROR_NO_THROW;
302 glVertexAttribPointer ( 0, 2, GL_FLOAT, GL_FALSE,
sizeof (
float ) * 4, 0 );
303 OPENGL_CHECK_ERROR_NO_THROW;
304 glEnableVertexAttribArray ( 1 );
305 OPENGL_CHECK_ERROR_NO_THROW;
306 glVertexAttribPointer ( 1, 2, GL_FLOAT, GL_FALSE,
sizeof (
float ) * 4,
reinterpret_cast<const void*
> (
sizeof (
float ) * 2 ) );
307 OPENGL_CHECK_ERROR_NO_THROW;
308 glDrawArrays ( GL_TRIANGLE_FAN, 0, 4 );
309 OPENGL_CHECK_ERROR_NO_THROW;
312 mMemoryPoolBuffer.Reset();
324 1.0f, 0.0f, 0.0f, 0.0f,
325 0.0f, 1.0f, 0.0f, 0.0f,
326 0.0f, 0.0f, -1.0f, 0.0f,
327 0.0f, 0.0f, 0.0f, 1.0f
329 mFrustum = mProjectionMatrix * mViewMatrix;
330 mMatrices.WriteMemory (
sizeof (
float ) * 16,
sizeof (
float ) * 16, mProjectionMatrix.GetMatrix4x4() );
335 mViewMatrix = aMatrix;
336 mFrustum = mProjectionMatrix * mViewMatrix;
337 mMatrices.WriteMemory (
sizeof (
float ) * 16 * 2,
sizeof (
float ) * 16, mViewMatrix.GetMatrix4x4() );
342 return mProjectionMatrix;
356 if ( aWidth && aHeight )
359 mOpenGLRenderer.MakeCurrent ( mDeviceContext );
360#elif defined(__unix__)
361 mOpenGLRenderer.MakeCurrent ( mWindowId );
363 OPENGL_CHECK_ERROR_NO_THROW;
364 glViewport ( aX, aY, aWidth, aHeight );
365 OPENGL_CHECK_ERROR_THROW;
366 mFrameBuffer.Resize ( aWidth, aHeight );
Header for the axis aligned bounding box class.
Header for the frustum class.
Defines log severity levels and stream output for the AeonGames engine.
Header for the Scene 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.
Represents a surface material with uniform properties and texture samplers.
4 by 4 matrix in colum mayor order.
DLL const float *const GetMatrix4x4() const
Get a pointer to the internal matrix data.
Represents a polygon mesh with vertex attributes and index data.
DLL uint32_t GetIndexSize() const
Get the size in bytes of a single index.
DLL uint32_t GetIndexCount() const
Get the total number of indices.
DLL uint32_t GetVertexCount() const
Get the total number of vertices.
OpenGL rendering backend implementing the Renderer interface.
OpenGL per-window rendering context and state.
const Matrix4x4 & GetProjectionMatrix() const
Get the current projection matrix.
const Frustum & GetFrustum() const
Get the view frustum derived from the current matrices.
void SetClearColor(float R, float G, float B, float A)
Set the clear color for this window.
void SetProjectionMatrix(const Matrix4x4 &aMatrix)
Set the projection matrix for this window.
OpenGLWindow(OpenGLWindow &&aOpenGLWindow)
Move constructor.
void EndRender()
End the current frame and present.
void BeginRender()
Begin rendering a new frame.
void WriteOverlayPixels(int32_t aXOffset, int32_t aYOffset, uint32_t aWidth, uint32_t aHeight, Texture::Format aFormat, Texture::Type aType, const uint8_t *aPixels)
Write pixel data to the overlay texture.
const Matrix4x4 & GetViewMatrix() const
Get the current view matrix.
void Render(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
BufferAccessor AllocateSingleFrameUniformMemory(size_t aSize)
Allocate transient uniform memory for the current frame.
void SetViewMatrix(const Matrix4x4 &aMatrix)
Set the view matrix for this window.
void ResizeViewport(int32_t aX, int32_t aY, uint32_t aWidth, uint32_t aHeight)
Resize the rendering viewport.
Rendering pipeline resource.
Type
Pixel component data type.
Format
Pixel channel layout format.
<- This is here just for the literals
Topology
Primitive topology types for rendering.
@ TRIANGLE_STRIP
Connected triangle strip.
@ PATCH_LIST
Patch list for tessellation.
@ LINE_LIST_WITH_ADJACENCY
Line list with adjacency information.
@ TRIANGLE_LIST_WITH_ADJACENCY
Triangle list with adjacency information.
@ POINT_LIST
List of individual points.
@ LINE_STRIP
Connected line segments.
@ LINE_LIST
Pairs of vertices forming individual lines.
@ TRIANGLE_LIST
Independent triangles.
@ TRIANGLE_STRIP_WITH_ADJACENCY
Triangle strip with adjacency information.
@ TRIANGLE_FAN
Triangles sharing a common vertex.
@ LINE_STRIP_WITH_ADJACENCY
Line strip with adjacency information.