仅在调用GLCREATESHADER时仅使用CONAN库,包括带有多个CMAKE文件/程序的柯南库。

发布于 2025-02-11 14:29:14 字数 8779 浏览 1 评论 0原文

目前正在开发C ++和OpenGL的小型游戏。

我 为此,我正在利用柯南(没有它,这有点烦人)。

事实是,当我正确地导入CLION和CMAKE和C ++的所有内容都识别该程序每次从Glad中调用GlCreateShader方法时都会失败的库。我不知道为什么?我的猜测是,我可能会错误地包含库,但我不确定。...

的例外是:异常0xc0000005在地址0x000000遇到:用户模式数据执行预防(dep)违反位置0x00000000

,是我的cmake文件:

root cmake

cmake_minimum_required(VERSION 3.18)
project(code)

set(CMAKE_CXX_STANDARD 17)

add_subdirectory(externalCode/glad)
add_subdirectory(externalCode/glfw)
add_subdirectory(externalCode/glm)
add_subdirectory(externalCode/assimp)
add_subdirectory(externalCode/soloud)
add_subdirectory(engine)
add_subdirectory(game)

引擎cmake

cmake_minimum_required(VERSION 3.18)

set(SOURCE_FILES src/main.cpp ../engine/src/GraphicsEngine/shader.cpp ../engine/include/engine/GraphicsEngine/shader.h ../engine/src/FileManager/FileManager.cpp ../engine/include/engine/FileManager/FileManager.h ../engine/src/GraphicsEngine/mesh.cpp ../engine/include/engine/GraphicsEngine/mesh.h ../engine/include/engine/GraphicsEngine/Components/Model.h ../engine/src/GraphicsEngine/GraphicsSystem.cpp ../engine/include/engine/GraphicsEngine/Scene.h ../engine/src/InputSystem/InputManager.cpp ../engine/include/engine/InputSystem/InputManager.h ../engine/src/GraphicsEngine/Utils/TransformUtils.cpp ../engine/include/engine/GraphicsEngine/Components/Transform.h ../engine/src/Tools/Grid.cpp ../engine/include/engine/Tools/Grid.h ../engine/src/Tools/CubicNode.cpp ../engine/include/engine/Tools/CubicNode.h ../engine/include/engine/Tools/Node.h src/ShipController.cpp src/ShipController.h)

set(EXE_FILE Game)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(${EXE_FILE} ${SOURCE_FILES})

target_compile_features(${EXE_FILE} PRIVATE cxx_std_17)

target_link_libraries(${EXE_FILE} PRIVATE Engine)

游戏cmake

cmake_minimum_required(VERSION 3.18)

file(GLOB_RECURSE HEADER_LIST CONFIGURE_DEPENDS "include/**.h")
file(GLOB_RECURSE PRIVATE_HEADER_LIST CONFIGURE_DEPENDS "src/**.h")
file(GLOB_RECURSE SOURCE_LIST CONFIGURE_DEPENDS "src/**.cpp")

set(ENGINE_NAME Engine)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_library(${ENGINE_NAME} ${HEADER_LIST} ${PRIVATE_HEADER_LIST} ${SOURCE_LIST})

target_compile_features(${ENGINE_NAME} PRIVATE cxx_std_17)
set_target_properties(${ENGINE_NAME} PROPERTIES LINKER_LANGUAGE CXX)

target_link_libraries(${ENGINE_NAME} PUBLIC glfw glad glm assimp soloud ${CONAN_LIBS})

target_include_directories(${ENGINE_NAME} PUBLIC include/)

因此项目结构如下:我有一个cmake文件(第一个)添加所有库和两个单独的项目。另外,我为“引擎”(更抽象的零件)和一个混凝土游戏项目都有一个CMAKE文件,在该项目中我设置了所有内容,并生成了实际的可执行文件。这两个项目都包括柯南文件,因为否则我会发现它找不到库。

最后但并非最不重要的是,这是引发例外的着色器类:

Shinder.h

#pragma once

#include "glm/mat4x4.hpp"
#include "glm/vec4.hpp"
#include "glm/vec3.hpp"
#include "glad/glad.h"
#include "glm/gtc/type_ptr.hpp"

#include <filesystem>
#include <string>

#include "engine/FileManager/FileManager.h"


namespace gl3::engine::Graphics {
    class shader {
    public:
        shader(const std::filesystem::path &vertexShaderAsset, const std::filesystem::path &fragmentShaderAsset);

        // explicit move constructor
        shader(shader &&other) noexcept {
            std::swap(this->shaderProgram, other.shaderProgram);
            std::swap(this->vertexShader, other.vertexShader);
            std::swap(this->fragmentShader, other.fragmentShader);
        }

        void use() const;

        void setVector(const std::string &uniform, glm::vec4 vector) const;
        void setVector3(const std::string &uniform, glm::vec3 vector) const;
        void setMatrix(const std::string &uniform, glm::mat4 matrix) const;
        void setBool(const std::string &name, bool value) const;
        void setInt(const std::string &name, int value) const;
        void setFloat(const std::string &name, float value) const;

        ~shader();

    private:
        unsigned int shaderProgram = 0;
        unsigned int vertexShader = 0;
        unsigned int fragmentShader = 0;
    };
}

Shinder.cpp

#pragma once

#include "engine/GraphicsEngine/shader.h"


namespace gl3::engine::Graphics {

    struct glStatusData {
        int success;
        const char* shaderName;
        char infoLog[GL_INFO_LOG_LENGTH];
    };

    unsigned int loadAndCompileShader(GLuint shaderType, const std::filesystem::path &shaderAssetPath){

        auto shaderAsset = filesystem::FileManager::getAssetFileFrom(shaderAssetPath);
        const char* shaderSource = shaderAsset.c_str();
        unsigned int shader = glCreateShader(shaderType);

        glShaderSource(shader, 1, &shaderSource, nullptr);
        glCompileShader(shader);

        {
            glStatusData compilationStatus{};

            if(shaderType == GL_VERTEX_SHADER) compilationStatus.shaderName = "VertexShader";
            if(shaderType == GL_FRAGMENT_SHADER) compilationStatus.shaderName = "FragmentShader";

            glGetShaderiv(shader, GL_COMPILE_STATUS, &compilationStatus.success);
            if(compilationStatus.success == GL_FALSE){
                glGetShaderInfoLog(shader, GL_INFO_LOG_LENGTH, nullptr, compilationStatus.infoLog);
                throw std::runtime_error("ERROR::SHADER::" + std::string(compilationStatus.shaderName)
                                                                    + "::COMPILATION_FAILDE\n"
                                                                    + std::string(compilationStatus.infoLog));
            }
        }

        return shader;
    }

    shader::shader(const std::filesystem::path &vertexShaderAsset, const std::filesystem::path &fragmentShaderAsset) {

        // Load and compile shader
        vertexShader = loadAndCompileShader(GL_VERTEX_SHADER, vertexShaderAsset);
        fragmentShader = loadAndCompileShader(GL_FRAGMENT_SHADER, fragmentShaderAsset);

        // Create a shader program, attach the shaders and link program
        shaderProgram = glCreateProgram();
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);

        // Handle the errors and throw exceptions
        {
            glStatusData linkingStatus{};
            glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkingStatus.success);
            if(linkingStatus.success == GL_FALSE){
                glGetShaderInfoLog(shaderProgram, GL_INFO_LOG_LENGTH, nullptr, linkingStatus.infoLog);
                throw std::runtime_error("ERROR::PROGRAM::LINKING_FAILED\n" + std::string(linkingStatus.infoLog));
            }
        }

        // Detach the shaders
        glDetachShader(shaderProgram, vertexShader);
        glDetachShader(shaderProgram, fragmentShader);
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
    }

    void shader::use() const {
        glUseProgram(shaderProgram);
    }

    void shader::setVector(const std::string &uniform, glm::vec4 vector) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, uniform.c_str());
        glUniform4f(uniformLocation, vector.x, vector.y, vector.z, vector.w);
    }

    void shader::setVector3(const std::string &uniform, glm::vec3 vector) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, uniform.c_str());
        glUniform3f(uniformLocation, vector.x, vector.y, vector.z);
    }

    void shader::setMatrix(const std::string &uniform, glm::mat4 matrix) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, uniform.c_str());
        glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, glm::value_ptr(matrix));
    }

    shader::~shader() {
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
        glDeleteShader(shaderProgram);
    }

    void shader::setBool(const std::string &name, bool value) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, name.c_str());
        glUniform1i(uniformLocation, (int) value);
    }

    void shader::setInt(const std::string &name, int value) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, name.c_str());
        glUniform1i(uniformLocation, (int) value);
    }

    void shader::setFloat(const std::string &name, float value) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, name.c_str());
        glUniform1f(uniformLocation, value);
    }
}

任何帮助都将不胜感激。老实说,我不知道从哪里开始.....

I'm currently developing a small game native in C++ and OpenGL....

So now I am at the point where I wanna include Nvidia PhysX to get the nice physics simulations. For that, I'm utilizing Conan (Without it, it's kinda annoying).

The thing is when I imported everything in Clion correctly and cmake and c++ both recognize the libraries the program fails every time when calling the glCreateShader method from glad. And I don't know why? My guess is that I may be falsely included the libraries but I'm not really sure....

The exception that is form is: Exception 0xc0000005 encountered at address 0x000000: User-mode data execution prevention (DEP) violation at location 0x00000000

These are my cmake files:

Root cmake

cmake_minimum_required(VERSION 3.18)
project(code)

set(CMAKE_CXX_STANDARD 17)

add_subdirectory(externalCode/glad)
add_subdirectory(externalCode/glfw)
add_subdirectory(externalCode/glm)
add_subdirectory(externalCode/assimp)
add_subdirectory(externalCode/soloud)
add_subdirectory(engine)
add_subdirectory(game)

Engine cmake

cmake_minimum_required(VERSION 3.18)

set(SOURCE_FILES src/main.cpp ../engine/src/GraphicsEngine/shader.cpp ../engine/include/engine/GraphicsEngine/shader.h ../engine/src/FileManager/FileManager.cpp ../engine/include/engine/FileManager/FileManager.h ../engine/src/GraphicsEngine/mesh.cpp ../engine/include/engine/GraphicsEngine/mesh.h ../engine/include/engine/GraphicsEngine/Components/Model.h ../engine/src/GraphicsEngine/GraphicsSystem.cpp ../engine/include/engine/GraphicsEngine/Scene.h ../engine/src/InputSystem/InputManager.cpp ../engine/include/engine/InputSystem/InputManager.h ../engine/src/GraphicsEngine/Utils/TransformUtils.cpp ../engine/include/engine/GraphicsEngine/Components/Transform.h ../engine/src/Tools/Grid.cpp ../engine/include/engine/Tools/Grid.h ../engine/src/Tools/CubicNode.cpp ../engine/include/engine/Tools/CubicNode.h ../engine/include/engine/Tools/Node.h src/ShipController.cpp src/ShipController.h)

set(EXE_FILE Game)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(${EXE_FILE} ${SOURCE_FILES})

target_compile_features(${EXE_FILE} PRIVATE cxx_std_17)

target_link_libraries(${EXE_FILE} PRIVATE Engine)

Game cmake

cmake_minimum_required(VERSION 3.18)

file(GLOB_RECURSE HEADER_LIST CONFIGURE_DEPENDS "include/**.h")
file(GLOB_RECURSE PRIVATE_HEADER_LIST CONFIGURE_DEPENDS "src/**.h")
file(GLOB_RECURSE SOURCE_LIST CONFIGURE_DEPENDS "src/**.cpp")

set(ENGINE_NAME Engine)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_library(${ENGINE_NAME} ${HEADER_LIST} ${PRIVATE_HEADER_LIST} ${SOURCE_LIST})

target_compile_features(${ENGINE_NAME} PRIVATE cxx_std_17)
set_target_properties(${ENGINE_NAME} PROPERTIES LINKER_LANGUAGE CXX)

target_link_libraries(${ENGINE_NAME} PUBLIC glfw glad glm assimp soloud ${CONAN_LIBS})

target_include_directories(${ENGINE_NAME} PUBLIC include/)

So the project structure is as follows: I have one cmake file(the first) that adds all the libraries and the two separate projects. Also, I have one cmake file each for the "engine"(The more abstract parts) and a concrete game project where I set up everything and an actual executable is generated. Both of these projects include the Conan files because else I get an exception that it can't find the libraries.

Last but not least this is the shader class that throws the exception:

shader.h

#pragma once

#include "glm/mat4x4.hpp"
#include "glm/vec4.hpp"
#include "glm/vec3.hpp"
#include "glad/glad.h"
#include "glm/gtc/type_ptr.hpp"

#include <filesystem>
#include <string>

#include "engine/FileManager/FileManager.h"


namespace gl3::engine::Graphics {
    class shader {
    public:
        shader(const std::filesystem::path &vertexShaderAsset, const std::filesystem::path &fragmentShaderAsset);

        // explicit move constructor
        shader(shader &&other) noexcept {
            std::swap(this->shaderProgram, other.shaderProgram);
            std::swap(this->vertexShader, other.vertexShader);
            std::swap(this->fragmentShader, other.fragmentShader);
        }

        void use() const;

        void setVector(const std::string &uniform, glm::vec4 vector) const;
        void setVector3(const std::string &uniform, glm::vec3 vector) const;
        void setMatrix(const std::string &uniform, glm::mat4 matrix) const;
        void setBool(const std::string &name, bool value) const;
        void setInt(const std::string &name, int value) const;
        void setFloat(const std::string &name, float value) const;

        ~shader();

    private:
        unsigned int shaderProgram = 0;
        unsigned int vertexShader = 0;
        unsigned int fragmentShader = 0;
    };
}

shader.cpp

#pragma once

#include "engine/GraphicsEngine/shader.h"


namespace gl3::engine::Graphics {

    struct glStatusData {
        int success;
        const char* shaderName;
        char infoLog[GL_INFO_LOG_LENGTH];
    };

    unsigned int loadAndCompileShader(GLuint shaderType, const std::filesystem::path &shaderAssetPath){

        auto shaderAsset = filesystem::FileManager::getAssetFileFrom(shaderAssetPath);
        const char* shaderSource = shaderAsset.c_str();
        unsigned int shader = glCreateShader(shaderType);

        glShaderSource(shader, 1, &shaderSource, nullptr);
        glCompileShader(shader);

        {
            glStatusData compilationStatus{};

            if(shaderType == GL_VERTEX_SHADER) compilationStatus.shaderName = "VertexShader";
            if(shaderType == GL_FRAGMENT_SHADER) compilationStatus.shaderName = "FragmentShader";

            glGetShaderiv(shader, GL_COMPILE_STATUS, &compilationStatus.success);
            if(compilationStatus.success == GL_FALSE){
                glGetShaderInfoLog(shader, GL_INFO_LOG_LENGTH, nullptr, compilationStatus.infoLog);
                throw std::runtime_error("ERROR::SHADER::" + std::string(compilationStatus.shaderName)
                                                                    + "::COMPILATION_FAILDE\n"
                                                                    + std::string(compilationStatus.infoLog));
            }
        }

        return shader;
    }

    shader::shader(const std::filesystem::path &vertexShaderAsset, const std::filesystem::path &fragmentShaderAsset) {

        // Load and compile shader
        vertexShader = loadAndCompileShader(GL_VERTEX_SHADER, vertexShaderAsset);
        fragmentShader = loadAndCompileShader(GL_FRAGMENT_SHADER, fragmentShaderAsset);

        // Create a shader program, attach the shaders and link program
        shaderProgram = glCreateProgram();
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);

        // Handle the errors and throw exceptions
        {
            glStatusData linkingStatus{};
            glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkingStatus.success);
            if(linkingStatus.success == GL_FALSE){
                glGetShaderInfoLog(shaderProgram, GL_INFO_LOG_LENGTH, nullptr, linkingStatus.infoLog);
                throw std::runtime_error("ERROR::PROGRAM::LINKING_FAILED\n" + std::string(linkingStatus.infoLog));
            }
        }

        // Detach the shaders
        glDetachShader(shaderProgram, vertexShader);
        glDetachShader(shaderProgram, fragmentShader);
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
    }

    void shader::use() const {
        glUseProgram(shaderProgram);
    }

    void shader::setVector(const std::string &uniform, glm::vec4 vector) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, uniform.c_str());
        glUniform4f(uniformLocation, vector.x, vector.y, vector.z, vector.w);
    }

    void shader::setVector3(const std::string &uniform, glm::vec3 vector) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, uniform.c_str());
        glUniform3f(uniformLocation, vector.x, vector.y, vector.z);
    }

    void shader::setMatrix(const std::string &uniform, glm::mat4 matrix) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, uniform.c_str());
        glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, glm::value_ptr(matrix));
    }

    shader::~shader() {
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
        glDeleteShader(shaderProgram);
    }

    void shader::setBool(const std::string &name, bool value) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, name.c_str());
        glUniform1i(uniformLocation, (int) value);
    }

    void shader::setInt(const std::string &name, int value) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, name.c_str());
        glUniform1i(uniformLocation, (int) value);
    }

    void shader::setFloat(const std::string &name, float value) const {
        unsigned int uniformLocation = glGetUniformLocation(shaderProgram, name.c_str());
        glUniform1f(uniformLocation, value);
    }
}

Any help would be really appreciated. I honestly don't know where to even start.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文