# OpenGLDevice OpenGL 设备的实现,基于 GLFW 和现代 OpenGL。 ## 头文件 ```cpp #include ``` ## 继承关系 ``` RHIDevice (interface) └── OpenGLDevice (implementation) ``` ## 公共成员函数 ### 初始化与销毁 #### `bool Initialize(const RHIDeviceDesc& desc) override` 初始化 OpenGL 上下文和设备。 #### `void Shutdown() override` 关闭设备,销毁窗口(如果拥有)。 #### `bool CreateRenderWindow(int width, int height, const char* title, bool enableDebug = false)` 创建渲染窗口并初始化 OpenGL 上下文。 #### `bool InitializeWithExistingWindow(GLFWwindow* window)` 使用已有的 GLFW 窗口初始化。 ### 窗口操作 #### `GLFWwindow* GetWindow() const` 获取 GLFW 窗口指针。 #### `void SwapBuffers()` 交换前后缓冲区。 #### `bool PollEvents()` 轮询窗口事件。 #### `void SetShouldClose(bool shouldClose)` 设置关闭标志。 #### `bool ShouldClose() const` 检查是否应该关闭。 ### 资源创建 #### `RHIBuffer* CreateBuffer(const BufferDesc& desc) override` #### `RHITexture* CreateTexture(const TextureDesc& desc) override` #### `RHISwapChain* CreateSwapChain(const SwapChainDesc& desc) override` #### `RHICommandList* CreateCommandList(const CommandListDesc& desc) override` #### `RHICommandQueue* CreateCommandQueue(const CommandQueueDesc& desc) override` #### `RHIShader* CompileShader(const ShaderCompileDesc& desc) override` #### `RHIPipelineState* CreatePipelineState(const PipelineStateDesc& desc) override` #### `RHIFence* CreateFence(const FenceDesc& desc) override` #### `RHISampler* CreateSampler(const SamplerDesc& desc) override` ### 设备信息 #### `const RHICapabilities& GetCapabilities() const override` 获取 OpenGL 功能支持信息。 #### `const RHIDeviceInfo& GetDeviceInfo() const override` 获取设备详细信息。 #### `void* GetNativeDevice() override` 返回窗口指针。 #### `void* GetNativeHandle() const` 返回窗口指针。 ## 内部成员 | 成员 | 类型 | 描述 | |------|------|------| | `m_window` | `GLFWwindow*` | GLFW 窗口 | | `m_deviceInfo` | `RHIDeviceInfo` | 设备信息 | | `m_capabilities` | `RHICapabilities` | 功能支持 | | `m_initialized` | `bool` | 是否已初始化 | | `m_ownsWindow` | `bool` | 是否拥有窗口 | ## 使用示例 ```cpp OpenGLDevice device; device.CreateRenderWindow(1920, 1080, "XCEngine", true); RHIShader* shader = device.CompileShader(shaderDesc); RHIPipelineState* pso = device.CreatePipelineState(psoDesc); while (!device.ShouldClose()) { device.PollEvents(); // Render... device.SwapBuffers(); } ```