#OPENGL failed to find .glsl file

23 messages · Page 1 of 1 (latest)

heady daggerBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

left cedar
#

you should probably just print std::filesystem::current_path() to make sure you're working dir is what you think it is

tribal root
#

uhm... OK

#

i'll try it

left cedar
#

it's probably running from the cmake binary_dir

#

if that ends up being the case, you can use something like this in your cmakelists.txt to symlink the shader directory to some path relative to where it's going to be running from:

# permissions fix to allow this on windows:
# 1. Open gpedit.msc
# 2. Computer Configuration
#      => Windows Settings
#      => Security Settings
#      => Local Policies
#      => User Rights Assignment
#      => Create symbolic links
# 3. Add username, then reboot machine
add_custom_command(TARGET ${PROJECT_NAME}
  PRE_BUILD COMMAND
    ${CMAKE_COMMAND} -E create_symlink
      ${CMAKE_CURRENT_SOURCE_DIR}/path/to/shaders/in/src/dir
      $<TARGET_FILE_DIR:${PROJECT_NAME}>/rel/path/from/cwd
)
tribal root
#

still failed

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <filesystem>
#include <string>
#include <fstream>

std::string readShaderSource(const std::filesystem::path& filePath) noexcept {
    std::ifstream fileStream{filePath};
    if (!fileStream.is_open())
        std::print(stderr, "ERROR: Could not open shader file: {}\n", filePath.string());
    std::string shaderSource((std::istreambuf_iterator(fileStream)), std::istreambuf_iterator<char>());
    return shaderSource;
}

constexpr float vertices[] = {
    -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    0.0f, 0.5f, 0.0f
};

class OpenGL {
public:
    explicit OpenGL(const int width, const int height, const char* title)
        : m_width{width}, m_height{height}, m_title{title} {}

    void run() const;

    bool init();

    ~OpenGL() {
        glfwTerminate();
    }

private:
    GLFWwindow* m_window{};
    int m_width{}, m_height{};
    const char* m_title{};

    static void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
        glViewport(0, 0, width, height);
    }
};

bool OpenGL::init() {
    std::ios::sync_with_stdio(false);

    if (!glfwInit()) {
        std::cerr << "Failed to initialize GLFW\n";
        return false;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    m_window = glfwCreateWindow(m_width, m_height, m_title, nullptr, nullptr);
    if (!m_window) {
        std::cerr << "Failed to create GLFW Window\n";
        glfwTerminate();
        return false;
    }

    glfwMakeContextCurrent(m_window);

    if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
        std::cerr << "Failed to initialize GLAD\n";
        glfwTerminate();
        return false;
    }

    glfwSetFramebufferSizeCallback(m_window, framebuffer_size_callback);

    return true;
}

void OpenGL::run() const {
    unsigned VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    const unsigned vertexShader{glCreateShader(GL_VERTEX_SHADER)};
    const std::string vertexShaderSource{readShaderSource(std::filesystem::path{"vertexShader.glsl"})};
    const char* vertexShaderSourceCStr{vertexShaderSource.c_str()};
    glShaderSource(vertexShader, 1, &vertexShaderSourceCStr, nullptr);
    glCompileShader(vertexShader);

    /* Checking if the compilation is successful */
    int success;
    char infoLog[512]{};
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);

    if (!success) {
        glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
        std::print(stderr, "ERROR: ERROR::SHADER::VERTEX::COMPILATION_FAILED\n{}\n", infoLog);
    }

    while (!glfwWindowShouldClose(m_window)) {

        glfwSwapBuffers(m_window);
        glfwPollEvents();
    }
    glDeleteBuffers(1, &VBO);
}


int main() {
    OpenGL app{800, 600, "PhyllMpse"};

    if (!app.init())
        return 1;

    app.run();
}
#

ERROR: Could not open shader file: vertexShader.glsl

#

CMAKE

cmake_minimum_required(VERSION 3.28)
project(OPENGL_C__)

set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 26)

find_package(OpenGL REQUIRED)

set(GLFW_DIR "${CMAKE_SOURCE_DIR}/Library/glfw-3.4")
set(GLAD_DIR "Library/glad")

include_directories("${GLFW_DIR}/include" "${GLAD_DIR}/include")

add_subdirectory(${GLFW_DIR})

add_library(GLAD "${GLAD_DIR}/src/glad.c")

add_executable(OPENGL_C__
        GL2/main.cpp)

configure_file(GL2/vertexShader.glsl COPYONLY)

target_link_libraries(OPENGL_C__ PRIVATE glfw GLAD ${OPENGL_gl_LIBRARY})
ornate basin
#

but what is the current working directory of the program you run

#

say that you run a program from command line in root folder of a project, but executable is in root_folder_path/build/debug/opengl.exe

#

the current working directory may be set to root_folder_path, meaning that it will search for shader from then onwards if you gave relative path to file

#

so shader path could look like root_folder_path/shader.glsl

#

rather than root_folder_path/bin/Debug/shader.glsl or wherever you have it saved

#

usually current working directory is set to that of current path of command line, but dunno what you are using to compile, maybe CLion or what this is doesn't set cwd to project root folder, maybe to executable folder

ornate basin
#

that's fine and all but check what's the current working directory for your program when you are running it

#

std::filesystem::current_path()
print it out somewhere in a console

tribal root
#

!solved

heady daggerBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

heady daggerBOT
#

@tribal root

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

tribal root
#

what