diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 360f1097..abf5355e 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -132,12 +132,14 @@ add_library(XCEngine STATIC include/XCEngine/RHI/OpenGL/OpenGLVertexArray.h include/XCEngine/RHI/OpenGL/OpenGLTexture.h include/XCEngine/RHI/OpenGL/OpenGLPipelineState.h + include/XCEngine/RHI/OpenGL/OpenGLCommandList.h src/RHI/OpenGL/OpenGLDevice.cpp src/RHI/OpenGL/OpenGLShader.cpp src/RHI/OpenGL/OpenGLBuffer.cpp src/RHI/OpenGL/OpenGLVertexArray.cpp src/RHI/OpenGL/OpenGLTexture.cpp src/RHI/OpenGL/OpenGLPipelineState.cpp + src/RHI/OpenGL/OpenGLCommandList.cpp ) target_include_directories(XCEngine PUBLIC diff --git a/engine/include/XCEngine/RHI/OpenGL/OpenGLCommandList.h b/engine/include/XCEngine/RHI/OpenGL/OpenGLCommandList.h new file mode 100644 index 00000000..d52eaa32 --- /dev/null +++ b/engine/include/XCEngine/RHI/OpenGL/OpenGLCommandList.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include + +namespace XCEngine { +namespace RHI { + +class OpenGLBuffer; +class OpenGLVertexArray; +class OpenGLShader; +class OpenGLTexture; + +enum class PrimitiveType { + Points, + Lines, + LineStrip, + Triangles, + TriangleStrip, + TriangleFan +}; + +class OpenGLCommandList { +public: + OpenGLCommandList(); + ~OpenGLCommandList(); + + void Clear(float r, float g, float b, float a, unsigned int buffers); + + void SetPipelineState(void* pipelineState); + void SetVertexBuffer(unsigned int buffer, size_t offset, size_t stride); + void SetIndexBuffer(unsigned int buffer, unsigned int type); + + void Draw(PrimitiveType type, unsigned int vertexCount, unsigned int startVertex); + void DrawIndexed(PrimitiveType type, unsigned int indexCount, unsigned int startIndex, int baseVertex); + + void BindVertexArray(unsigned int vao); + void UseShader(unsigned int program); + + void SetViewport(int x, int y, int width, int height); + +private: + unsigned int m_primitiveType; + unsigned int m_currentVAO; + unsigned int m_currentProgram; +}; + +} // namespace RHI +} // namespace XCEngine diff --git a/engine/src/RHI/OpenGL/OpenGLCommandList.cpp b/engine/src/RHI/OpenGL/OpenGLCommandList.cpp new file mode 100644 index 00000000..a4a5be1a --- /dev/null +++ b/engine/src/RHI/OpenGL/OpenGLCommandList.cpp @@ -0,0 +1,74 @@ +#define GLFW_INCLUDE_NONE +#include "XCEngine/RHI/OpenGL/OpenGLCommandList.h" +#include +#include + +namespace XCEngine { +namespace RHI { + +OpenGLCommandList::OpenGLCommandList() + : m_primitiveType(GL_TRIANGLES) + , m_currentVAO(0) + , m_currentProgram(0) +{ +} + +OpenGLCommandList::~OpenGLCommandList() { +} + +void OpenGLCommandList::Clear(float r, float g, float b, float a, unsigned int buffers) { + glClearColor(r, g, b, a); + unsigned int glBuffers = 0; + if (buffers & 0x1) glBuffers |= GL_COLOR_BUFFER_BIT; + if (buffers & 0x2) glBuffers |= GL_DEPTH_BUFFER_BIT; + if (buffers & 0x4) glBuffers |= GL_STENCIL_BUFFER_BIT; + glClear(glBuffers); +} + +void OpenGLCommandList::SetPipelineState(void* pipelineState) { +} + +void OpenGLCommandList::SetVertexBuffer(unsigned int buffer, size_t offset, size_t stride) { +} + +void OpenGLCommandList::SetIndexBuffer(unsigned int buffer, unsigned int type) { +} + +static unsigned int ToGLPrimitiveType(PrimitiveType type) { + switch (type) { + case PrimitiveType::Points: return GL_POINTS; + case PrimitiveType::Lines: return GL_LINES; + case PrimitiveType::LineStrip: return GL_LINE_STRIP; + case PrimitiveType::Triangles: return GL_TRIANGLES; + case PrimitiveType::TriangleStrip: return GL_TRIANGLE_STRIP; + case PrimitiveType::TriangleFan: return GL_TRIANGLE_FAN; + default: return GL_TRIANGLES; + } +} + +void OpenGLCommandList::Draw(PrimitiveType type, unsigned int vertexCount, unsigned int startVertex) { + m_primitiveType = ToGLPrimitiveType(type); + glDrawArrays(m_primitiveType, startVertex, vertexCount); +} + +void OpenGLCommandList::DrawIndexed(PrimitiveType type, unsigned int indexCount, unsigned int startIndex, int baseVertex) { + m_primitiveType = ToGLPrimitiveType(type); + glDrawElements(m_primitiveType, indexCount, GL_UNSIGNED_INT, (void*)(startIndex * sizeof(unsigned int))); +} + +void OpenGLCommandList::BindVertexArray(unsigned int vao) { + m_currentVAO = vao; + glBindVertexArray(vao); +} + +void OpenGLCommandList::UseShader(unsigned int program) { + m_currentProgram = program; + glUseProgram(program); +} + +void OpenGLCommandList::SetViewport(int x, int y, int width, int height) { + glViewport(x, y, width, height); +} + +} // namespace RHI +} // namespace XCEngine