0

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.....

  • I'm not sure to understand. Your program works fine without this additional conan stuff allowing to link PhysX (`include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)`, `conan_basic_setup()` & `${CONAN_LIBS}`)? – SpacePotatoes Jun 29 '22 at 16:54
  • Exactly. When I do not include anything from Conan everything works fine(the lines you mentioned) – Jonas Schindler Jun 29 '22 at 20:51
  • This looks like a binary compatibility issue. It might be alleviated making sure that the default configuration (the one used at ``conan install``), matches the actual build configuration. It is possible for example that your hit some minor incompatibility due to c++17. The best could be to submit a ticket in https://github.com/conan-io/conan, specifying also the ``conanfile``, the Conan command used and the full install output. Then, maybe be aware that with modern ``CMakeDeps`` and ``CMakeToolchain`` integrations, no need to modify your CMakeLists.txt, it is transparent. – drodri Jul 06 '22 at 08:56

0 Answers0