opengl draw cube

This commit is contained in:
ouczbs 2024-07-11 18:00:07 +08:00
parent 7ef0bfad0e
commit 71df2efd4c
16 changed files with 345 additions and 48 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ vsxmake*/
engine/logs/zengine.log
tools/
cpp_input.h
logs/zengine.log

View File

@ -9,3 +9,4 @@ local utils = {
}
add_requires("vulkansdk",{configs = {utils = utils}})
add_requires("glew")
add_requires("glfw", "glad", {configs = {runtimes = "MD"}})

View File

@ -21,8 +21,11 @@ namespace refl {
parray(std::vector<C>& vec) : m_cls(&TypeInfo<C>::StaticClass){
m_count = vec.size();
if (m_count > 0) {
malloc_list(m_ptr, m_count);
std::move(vec.begin(), vec.end(), m_ptr);
m_ptr = (T*)malloc(m_count * sizeof(C));
C* ptr = (C*)m_ptr;
for (int i = 0; i < m_count; i++) {
std::construct_at(ptr + i, std::move(vec[i]));
}
}
}
parray(parray<T>& parr) : m_cls(parr.m_cls)
@ -56,6 +59,9 @@ namespace refl {
int size() const noexcept {
return m_count;
}
int data_size()const noexcept {
return m_count * m_cls->size;
}
int capicty() const noexcept {
return m_count * m_cls->size;
}

View File

@ -0,0 +1,186 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// build and compile our shader program
// ------------------------------------
// vertex shader
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
-0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // top
};
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}

View File

@ -53,4 +53,10 @@ target("yaml_zlib")
add_packages("benchmark")
add_includedirs("test/yaml")
add_files("test/yaml/*.cpp")
add_headerfiles("test/yaml/*.h", "test/yaml/*.natvis")
add_headerfiles("test/yaml/*.h", "test/yaml/*.natvis")
target("render_test")
set_kind("binary")
add_packages("glad","glfw")
add_files("test/render/*.cpp")

View File

@ -1,8 +1,8 @@
/engine/assets/shader/simple.vert:
- 020486aa-8987-4bd1-9de9-eeb7ab82c437
/engine/assets/asset/mesh1.asset:
- 7a01231c-c35d-4b74-b20f-4ca6823995d9
- 0a2ef3e8-db70-4d3f-94b1-09f9f92519cd
- 7a01231c-c35d-4b74-b20f-4ca6823995d9
/engine/assets/models/cube.obj:
- 3f6a5afc-2f21-47c4-86ba-9ddb9852d8f4
- b91ca918-6b3c-4647-8773-d93d2807cb98

View File

@ -4,5 +4,5 @@
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(1.0, 0.0, 0.0, 1.0);
outColor = vec4(0.0, 1.0, 0.0, 1.0);
}

View File

@ -1,24 +1,7 @@
#version 450
#extension GL_GOOGLE_include_directive : enable
layout (location = 0) in vec2 aPos;
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texture;
layout (location = 2) in vec3 normal;
layout (location = 3) in vec3 tangent;
layout (location = 4) in vec4 weights;
layout (location = 5) in uvec4 bones;
//out vec4 outPosition;
out gl_PerVertex {
vec4 gl_Position;
};
vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
void main() {
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
}
void main()
{
gl_Position = vec4(aPos, 0.0, 1.0);
}

View File

@ -1,4 +1,47 @@
#include "backend.h"
#include "openglapi.h"
#include <GL/glew.h>
#include "zlog.h"
namespace openglapi {
bool Backend::CheckShaderProgram(unsigned int program)
{
GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::unique_ptr<GLchar[]>infoLog = std::make_unique<GLchar[]>(maxLength);
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
// We don't need the program anymore.
// Don't leak shaders either.
// Use the infoLog as you see fit.
zlog::error("Link Shaders Failed!:{0}", &infoLog[0]);
// In this simple program, we'll just leave
return false;
}
GL_CHECK();
return true;
}
bool Backend::CheckShaderCompile(unsigned int shader)
{
GLint isCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(shader, maxLength, &maxLength, &infoLog[0]);
// We don't need the shader anymore.
glDeleteShader(shader);
// Use the infoLog as you see fit.
zlog::error("Compile Shader Failed!:{0}", &infoLog[0]);
return false;
}
GL_CHECK();
return true;
}
}

View File

@ -1,4 +1,9 @@
#pragma once
#define GL_CHECK() {if(GLenum err = glGetError())zlog::error("glError: {}", err);}
namespace openglapi {
class Backend {
public:
static bool CheckShaderProgram(unsigned int program);
static bool CheckShaderCompile(unsigned int shader);
};
}

View File

@ -1,6 +1,7 @@
#include "opengl_glsl_loader.h"
#include "asset/file_handle.h"
#include "render/tool/glsl_to_spirv.h"
#include "../backend.h"
#include <GL/glew.h>
using namespace engineapi;
namespace openglapi {
@ -27,13 +28,18 @@ namespace openglapi {
}
else {
string glsl = handle.ReadAll<string>();
#if 1
program->Compile(shader_kind, glsl.data());
#else
auto spirv = GlslToSpirv::ToSpirv(glsl, shader_kind, path.GetFileName());
if (spirv) {
program->Load(shader_kind, *spirv);
}
#endif
}
return program;
}
//renderdoc mesh input 显示有问题 //GL_SHADER_BINARY_FORMAT_SPIR_V_ARB
void glShaderProgram::Load(int _type, const std::vector<uint32_t>& spirv)
{
shaderc_shader_kind type = (shaderc_shader_kind)_type;
@ -42,22 +48,49 @@ namespace openglapi {
case shaderc_shader_kind::shaderc_vertex_shader:
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderBinary(1, &vertexShader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, spirv.data(), spirv.size() * sizeof(uint32_t));
glSpecializeShaderARB(vertexShader, "main", 0, nullptr, nullptr);
glShaderBinary(1, &vertexShader, GL_SHADER_BINARY_FORMAT_SPIR_V, spirv.data(), spirv.size() * sizeof(uint32_t));
glSpecializeShader(vertexShader, "main", 0, nullptr, nullptr);
mPtr = vertexShader;
break;
}
case shaderc_shader_kind::shaderc_fragment_shader:
{
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderBinary(1, &fragmentShader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, spirv.data(), spirv.size() * sizeof(uint32_t));
glSpecializeShaderARB(fragmentShader, "main", 0, nullptr, nullptr);
glShaderBinary(1, &fragmentShader, GL_SHADER_BINARY_FORMAT_SPIR_V, spirv.data(), spirv.size() * sizeof(uint32_t));
glSpecializeShader(fragmentShader, "main", 0, nullptr, nullptr);
mPtr = fragmentShader;
break;
}
default:
break;
}
Backend::CheckShaderCompile(mPtr);
}
void glShaderProgram::Compile(int _type, const char* source)
{
shaderc_shader_kind type = (shaderc_shader_kind)_type;
switch (type)
{
case shaderc_shader_kind::shaderc_vertex_shader:
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &source, NULL);
glCompileShader(vertexShader);
mPtr = vertexShader;
break;
}
case shaderc_shader_kind::shaderc_fragment_shader:
{
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &source, NULL);
glCompileShader(fragmentShader);
mPtr = fragmentShader;
break;
}
default:
break;
}
Backend::CheckShaderCompile(mPtr);
}
}

View File

@ -10,7 +10,8 @@ namespace openglapi {
uint32_t Ptr() {
return mPtr;
}
void Load(int type, const std::vector<uint32_t>& spirv);
void Load(int _type, const std::vector<uint32_t>& spirv);
void Compile(int _type, const char* source);
};
class OpenGLGlslLoader : public engineapi::IFileLoader
{

View File

@ -4,6 +4,7 @@
#include "GL/glew.h"
#include "loader/opengl_glsl_loader.h"
#include "render/asset/mesh.h"
#include "backend.h"
struct glVertexMeta {
GLint size;
GLenum type;
@ -20,6 +21,9 @@ namespace openglapi {
// 处理初始化错误
zlog::error("GLEW Initialization failed!");
}
const GLubyte* glVersion = glGetString(GL_VERSION);
zlog::info("OpenGL Version: {}", (const char*)glVersion);
RenderAPI::Init();
OpenGLGlslLoader::Init();
window = OpenGLWindow::GetSingletonPtr();
@ -46,8 +50,9 @@ namespace openglapi {
}
void RenderOpenGLApi::EndFrame()
{
glEnd();
GL_CHECK();
window->Present();
GL_CHECK();
}
void RenderOpenGLApi::SetStaticMesh(Mesh& mesh)
{
@ -57,17 +62,14 @@ namespace openglapi {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// 绑定顶点数组对象
glBindVertexArray(VAO);
// 绑定顶点缓冲对象
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, Vertices.size(), Vertices.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, Vertices.data_size(), Vertices.data(), GL_STATIC_DRAW);
// 绑定索引缓冲对象
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, Indices.size(), Indices.data(), GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, Indices.size() * sizeof(decltype(Indices)::value_type), Indices.data(), GL_STATIC_DRAW);
auto meta = Vertices.uclass()->vtable.GetMeta("glMeta");
{
@ -79,9 +81,10 @@ namespace openglapi {
if (!field_meta) continue;
glVertexMeta v_meta = field_meta.CastTo<glVertexMeta>();
GLvoid* offset = reinterpret_cast<GLvoid*>(field.GetOffset());
glEnableVertexAttribArray(count);
glVertexAttribPointer(count, v_meta.size, v_meta.type, v_meta.normalized, size, offset);
glEnableVertexAttribArray(count);
count++;
break;
}
}
// 解绑VBO不是EBO
@ -97,6 +100,7 @@ namespace openglapi {
OpenGLPtr& meshPtr = VAOTable[mesh.GetGuid()];
glBindVertexArray(meshPtr);
glDrawElements(GL_TRIANGLES, mesh.GetIndices().size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void RenderOpenGLApi::LoadShader(Shader& shader)
{
@ -106,8 +110,9 @@ namespace openglapi {
glAttachShader(shaderProgram, vertModule);
glAttachShader(shaderProgram, fragModule);
glLinkProgram(shaderProgram);
glDeleteProgram(vertModule);
glDeleteProgram(fragModule);
ShaderTable[shader.GetGuid()] = OpenGLPtr{ shaderProgram };
glDeleteShader(vertModule);
glDeleteShader(fragModule);
ShaderTable[shader.GetGuid()] = OpenGLPtr{shaderProgram};
Backend::CheckShaderProgram(shaderProgram);
}
}

View File

@ -1,4 +1,8 @@
#include "window.h"
#include "zlog.h"
#include <wingdi.h>
#include <GL/glew.h>
#include <GL/wglew.h>
namespace openglapi {
void OpenGLWindow::SetupOpenGLContext()
{
@ -27,9 +31,27 @@ namespace openglapi {
int pixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, pixelFormat, &pfd);
// 创建并激活 OpenGL 上下文
HGLRC hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);
HGLRC tempContext = wglCreateContext(hdc);
wglMakeCurrent(hdc, tempContext);
// 加载 wglCreateContextAttribsARB
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
if (!wglCreateContextAttribsARB) {
// 处理错误
zlog::error("wglCreateContextAttribsARB load failed!");
}
// 定义属性数组
int attribs[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
WGL_CONTEXT_MINOR_VERSION_ARB, 6,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0 // 结束符号
};
// 创建 OpenGL 上下文
HGLRC hRC = wglCreateContextAttribsARB(hdc, 0, attribs);
wglMakeCurrent(hdc, hRC);
wglDeleteContext(tempContext);
}
OpenGLWindow::OpenGLWindow(int frames, uint32_t width, uint32_t height, const char* title)
:engineapi::Window(width, height, title)

View File

@ -7,14 +7,18 @@
namespace engineapi {
struct Vertex {};
struct PosVertex : public Vertex {
UPROPERTY_gl({}, glVertexMeta{ 3, GL_FLOAT, GL_FALSE })
UPROPERTY_vk({}, uint32_t{ VK_FORMAT_R32G32B32_SFLOAT })
Vector3 Position = {};
};
struct TexVertex : public Vertex {
UPROPERTY_gl({}, glVertexMeta{ 3, GL_FLOAT, GL_FALSE })
UPROPERTY_vk({}, uint32_t{ VK_FORMAT_R32G32B32_SFLOAT })
Vector3 Position = {};
UPROPERTY_gl({}, glVertexMeta{ 3, GL_FLOAT, GL_FALSE })
UPROPERTY_vk()
Vector3 Normal = {};
UPROPERTY_gl({}, glVertexMeta{ 2, GL_FLOAT, GL_FALSE })
UPROPERTY_vk()
Vector2 TexCoords = {};
};

View File

@ -4,6 +4,7 @@
#include "wrapper/descriptorpool.h"
#include <iostream>
#include "zlog.h"
#include "backend.h"
namespace vulkanapi {
CommandWorker* Backend::TransferWorker = nullptr;
CommandWorker* Backend::RenderWorker = nullptr;
@ -62,4 +63,4 @@ namespace vulkanapi {
}
return nullptr;
}
}
}