Move OpenGL backend classes from tests/OpenGL to engine/

- Relocated OpenGLDevice, OpenGLShader, OpenGLBuffer, OpenGLVertexArray, OpenGLTexture to engine/
- Updated engine/CMakeLists.txt to include OpenGL backend source files
- Updated tests/OpenGL/CMakeLists.txt to use engine backend
- Added OpenGLTexture class implementation
This commit is contained in:
2026-03-16 17:22:45 +08:00
parent 434ba0f336
commit fee738b0b9
13 changed files with 354 additions and 36 deletions

View File

@@ -0,0 +1,31 @@
#pragma once
#include <string>
#include <GLFW/glfw3.h>
namespace XCEngine {
namespace RHI {
class OpenGLBuffer {
public:
OpenGLBuffer();
~OpenGLBuffer();
bool InitializeVertexBuffer(const void* data, size_t size);
bool InitializeIndexBuffer(const void* data, size_t size);
void Shutdown();
void Bind() const;
void Unbind() const;
unsigned int GetID() const { return m_buffer; }
size_t GetSize() const { return m_size; }
private:
unsigned int m_buffer;
size_t m_size;
bool m_isIndexBuffer;
};
} // namespace RHI
} // namespace XCEngine

View File

@@ -0,0 +1,42 @@
#pragma once
#include <string>
#include <GLFW/glfw3.h>
namespace XCEngine {
namespace RHI {
struct OpenGLDeviceInfo {
std::string vendor;
std::string renderer;
std::string version;
int majorVersion;
int minorVersion;
};
class OpenGLDevice {
public:
OpenGLDevice();
~OpenGLDevice();
bool CreateRenderWindow(int width, int height, const char* title, bool enableDebug = false);
bool InitializeWithExistingWindow(GLFWwindow* window);
void Shutdown();
GLFWwindow* GetWindow() const { return m_window; }
const OpenGLDeviceInfo& GetDeviceInfo() const { return m_deviceInfo; }
void SwapBuffers();
bool PollEvents();
void SetShouldClose(bool shouldClose);
bool ShouldClose() const;
private:
GLFWwindow* m_window;
OpenGLDeviceInfo m_deviceInfo;
bool m_initialized;
bool m_ownsWindow;
};
} // namespace RHI
} // namespace XCEngine

View File

@@ -0,0 +1,34 @@
#pragma once
#include <string>
#include <GLFW/glfw3.h>
namespace XCEngine {
namespace RHI {
class OpenGLShader {
public:
OpenGLShader();
~OpenGLShader();
bool CompileFromFile(const char* vertexPath, const char* fragmentPath);
bool Compile(const char* vertexSource, const char* fragmentSource);
void Shutdown();
void Use() const;
void SetInt(const std::string& name, int value) const;
void SetFloat(const std::string& name, float value) const;
void SetVec3(const std::string& name, float x, float y, float z) const;
void SetVec3(const std::string& name, const float* values) const;
void SetMat4(const std::string& name, const float* value) const;
unsigned int GetID() const { return m_program; }
private:
unsigned int m_program;
bool CheckCompileErrors(unsigned int shader, const char* type);
bool CheckLinkErrors(unsigned int program);
};
} // namespace RHI
} // namespace XCEngine

View File

@@ -0,0 +1,33 @@
#pragma once
#include <string>
#include <GLFW/glfw3.h>
namespace XCEngine {
namespace RHI {
class OpenGLTexture {
public:
OpenGLTexture();
~OpenGLTexture();
bool Initialize2D(int width, int height, int channels, const void* data, bool generateMipmap = true);
bool LoadFromFile(const char* path, bool flipVertical = true);
void Shutdown();
void Bind(int slot = 0) const;
void Unbind() const;
unsigned int GetID() const { return m_texture; }
int GetWidth() const { return m_width; }
int GetHeight() const { return m_height; }
private:
unsigned int m_texture;
int m_width;
int m_height;
int m_channels;
};
} // namespace RHI
} // namespace XCEngine

View File

@@ -0,0 +1,44 @@
#pragma once
#include <string>
#include <GLFW/glfw3.h>
#include <vector>
namespace XCEngine {
namespace RHI {
struct VertexAttribute {
unsigned int index;
int count;
unsigned int type;
bool normalized;
size_t stride;
size_t offset;
};
class OpenGLVertexArray {
public:
OpenGLVertexArray();
~OpenGLVertexArray();
bool Initialize();
void AddVertexBuffer(unsigned int buffer, const VertexAttribute& attribute);
void SetIndexBuffer(unsigned int buffer, unsigned int type);
void Shutdown();
void Bind() const;
void Unbind() const;
unsigned int GetID() const { return m_vao; }
unsigned int GetIndexBuffer() const { return m_indexBuffer; }
unsigned int GetIndexCount() const { return m_indexCount; }
private:
unsigned int m_vao;
unsigned int m_indexBuffer;
unsigned int m_indexCount;
int m_vertexBufferCount;
};
} // namespace RHI
} // namespace XCEngine