opengl draw triangle

This commit is contained in:
ouczbs 2024-07-06 11:31:16 +08:00
parent 50f7188503
commit b9cc541028
12 changed files with 86 additions and 20 deletions

View File

@ -8,4 +8,4 @@ local utils = {
"spirv-cross-glsl","spirv-cross-cpp", "spirv-cross-core", "spirv-cross-reflect","spirv-cross-util"
}
add_requires("vulkansdk",{configs = {utils = utils}})
add_requires("opengl", "opencl-headers")
add_requires("opengl", "opencl-headers")

View File

@ -0,0 +1,11 @@
#pragma once
#include <string>
#include "asset/render/asset_enum.h"
using std::string;
namespace engineapi
{
struct EngineSetting {
public:
inline static GraphicsAPI API = GraphicsAPI::Vulkan;
};
}

View File

@ -1,5 +0,0 @@
#include "property.h"
namespace engineapi {
}

View File

@ -1,4 +1,5 @@
#include "openglapi.h"
#include "window.h"
#include "loader/opengl_glsl_loader.h"
namespace openglapi {
void RenderOpenGLApi::SwitchContext()
@ -13,13 +14,26 @@ namespace openglapi {
}
void RenderOpenGLApi::SetViewPort(uint32_t width, uint32_t height, uint32_t xOffset, uint32_t yOffset)
{
mViewPortInfo.width = width;
mViewPortInfo.height = height;
mViewPortInfo.xOffset = xOffset;
mViewPortInfo.yOffset = yOffset;
}
void RenderOpenGLApi::BeginFrame()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5f, -0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.5f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(0.0f, 0.5f);
}
void RenderOpenGLApi::EndFrame()
{
glEnd();
OpenGLWindow::GetSingletonPtr()->Present();
}
void RenderOpenGLApi::SetStaticMesh(Mesh& mesh)
{

View File

@ -1,7 +1,48 @@
#include "window.h"
#include <gl/GL.h>
namespace openglapi {
void OpenGLWindow::SetupOpenGLContext()
{
HDC hdc = GetDC(mPtr);
// 设置像素格式
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24,
8,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, pixelFormat, &pfd);
// 创建并激活 OpenGL 上下文
HGLRC hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);
}
OpenGLWindow::OpenGLWindow(int frames, uint32_t width, uint32_t height, const char* title)
:engineapi::Window(width, height, title)
{
SetupOpenGLContext();
// 设置视口和背景颜色
glViewport(0, 0, width, height);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void OpenGLWindow::Present()
{
HDC hdc = GetDC(mPtr);
SwapBuffers(hdc);
}
}

View File

@ -2,7 +2,14 @@
#include "render/window.h"
namespace openglapi {
class OpenGLWindow : public engineapi::Window {
private:
void SetupOpenGLContext();
public:
OpenGLWindow(int frames, uint32_t width, uint32_t height, const char* title);
void Present();
public:
static OpenGLWindow* GetSingletonPtr() {
return (OpenGLWindow*)engineapi::Window::GetSingletonPtr();
}
};
}

View File

@ -1,4 +1,5 @@
#include "renderapi.h"
#include "data/engine_setting.h"
#include "node/rendernode.h"
#include "node/rendernode_forward.h"
#ifdef VULKAN_API
@ -36,6 +37,7 @@ namespace engineapi {
}
RenderAPI* RenderAPI::MakeInstance()
{
GraphicsAPI api = EngineSetting::API;
#ifdef VULKAN_API
if(api == GraphicsAPI::Vulkan)
return new vulkanapi::RenderVulkanAPI();
@ -46,13 +48,13 @@ namespace engineapi {
#endif
return nullptr;
}
RenderAPI* RenderAPI::API(GraphicsAPI _api)
RenderAPI* RenderAPI::API(GraphicsAPI api)
{
if (ms_Singleton) {
delete ms_Singleton;
ms_Singleton = nullptr;
}
api = _api;
EngineSetting::API = api;
return MakeInstance();
}
}

View File

@ -40,11 +40,6 @@ namespace engineapi
public:
static RenderAPI* MakeInstance();
static GraphicsAPI API() {
return api;
}
static RenderAPI* API(GraphicsAPI _api);
private:
inline static GraphicsAPI api = GraphicsAPI::Vulkan;
static RenderAPI* API(GraphicsAPI api);
};
}

View File

@ -1,5 +1,5 @@
#include "window.h"
#include "renderapi.h"
#include "data/engine_setting.h"
#ifdef VULKAN_API
#include "vulkanapi/window.h"
#endif // VULKAN_API
@ -10,7 +10,7 @@ namespace engineapi {
Window::WindowClass Window::WindowClass::wndClass;
Window* Window::MakeInstance(int frames, uint32_t width, uint32_t height, const char* title)
{
GraphicsAPI api = RenderAPI::API();
GraphicsAPI api = EngineSetting::API;
#ifdef VULKAN_API
if(api == GraphicsAPI::Vulkan)
return new vulkanapi::VulkanWindow(frames, width, height, title);

View File

@ -8,15 +8,16 @@ target("zengine")
add_rules("volk.env", "glsl.env")
add_rules("c++.codegen",{
files = {"src/engine/render/asset/*.h",
--"src/engine/asset/asset.h",
"src/engine/data/*.h",
"src/engine/asset/res/*.h"}
})
add_deps("zlog","zlib")
add_defines("VULKAN_API", "OPENGL_API")
add_packages("vulkansdk","tinyobjloader","assimp","nlohmann_json","opencl", "opencl-headers")
add_packages("vulkansdk","tinyobjloader","assimp","nlohmann_json")
add_packages("opencl", "opencl-headers")
add_includedirs("src/engine")
add_includedirs("src/3rdparty/volk", "src/3rdparty/vulkan-memory-allocator", "src/3rdparty/template")
add_syslinks("user32", "Ole32")
add_syslinks("user32", "Ole32", "Gdi32","Opengl32")
add_files("src/*.cpp", "src/**.cpp")
add_files("src/3rdparty/**.c")
add_headerfiles("src/**.h","src/**.inl")