2
votes

i have installed OpenGL 4.6 and i'am using visual studio 2019. i had decided to do a project in C++ with opengl but since i have no previous experience with them i decided to follow this tutorial but now my shaders aren't compiling and i have no idea why.

my main code:

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

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

void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

float vertex[] = {
     0.0f,  0.5f, 0.0f, 0.2f, 0.4f, 0.6f,  // top center
     0.5f, -0.5f, 0.0f, 0.2f, 0.4f, 0.6f,  // bottom right
    -0.5f, -0.5f, 0.0f, 0.2f, 0.4f, 0.6f,  // bottom left
     0.0f,  0.0f, -0.5f,  0.2f, 0.4f, 0.6f // top left 
};

unsigned int indices[] = {  // note that we start from 0!
    0, 1, 2
};

int main() {

    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    std::cout << glfwGetVersionString();

    GLFWwindow* window = glfwCreateWindow(800, 600, "Demo", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    glViewport(0, 0, 800, 600);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    GLuint VBO, VAO, EBO;

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glGenBuffers(1, &EBO);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);


    unsigned int vertexShader;
    vertexShader = glCreateShader(GL_VERTEX_SHADER);

    std::string ContentVert;
    std::ifstream infileVert;
    infileVert.open("vertex.glsl");
    if (infileVert.is_open())
    {
        while (getline(infileVert, ContentVert))
        {
            std::cout << ContentVert << '\n';
        }
        infileVert.close();
    }
    const GLchar* const vertexShadersrc = { ContentVert.c_str() };

    glShaderSource(vertexShader, 1, &vertexShadersrc, NULL);
    glCompileShader(vertexShader);

    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;
    }

    unsigned int fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    std::string ContentFrag;
    std::ifstream infileFrag;
    infileFrag.open("demo.glsl");
    if (infileFrag.is_open())
    {
        while (getline(infileFrag, ContentFrag))
        {
            std::cout << ContentFrag << '\n';
        }
        infileFrag.close();
    }
    const GLchar* const fragShaderSrc = { ContentFrag.c_str() };

    glShaderSource(fragmentShader, 1, &fragShaderSrc, NULL);
    glCompileShader(fragmentShader);

    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }

    unsigned int shaderProgram;
    shaderProgram = glCreateProgram();

    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }

    glUseProgram(shaderProgram);

    glBindFragDataLocation(shaderProgram, 0, "outColor");

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    while (!glfwWindowShouldClose(window))
    {
        processInput(window);

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shaderProgram);
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);

    glfwTerminate();
    return 0;
}

console output:

#version 330

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

out vec3 ourColor;

void main()
{
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
    ourColor = aColor;
}
ERROR::SHADER::VERTEX::COMPILATION_FAILED
WARNING: 0:1: '' : #version directive missing
ERROR: 0:1: '}' : syntax error syntax error


#version 330

out vec4 FragColor;  
in vec3 ourColor;

void main() {
    FragColor = vec4(ourColor, 1.0);
} 
ERROR::SHADER::FRAGMENT::COMPILATION_FAILED
WARNING: 0:1: '' : #version directive missing
ERROR: 0:1: '}' : syntax error syntax error

ERROR::SHADER::PROGRAM::LINKING_FAILED
Attached vertex shader is not compiled

vertex shader:

#version 330

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

out vec3 ourColor;

void main()
{
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
    ourColor = aColor;
}

fragment shader:

#version 330

out vec4 FragColor;  
in vec3 ourColor;

void main() {
    FragColor = vec4(ourColor, 1.0);
} 
1
std::getline() does not append data to the string, but replaces it's content.churill

1 Answers

3
votes

The issue is the way how you try to read the file

while (getline(infileVert, ContentVert))

getline reads a single line from the file and stores it to ContentVert. I does not append to ContentVert.
At the end, just the last line of the shader is stored in ContentVert. The content of the last line is "}". That explains the error message:

WARNING: 0:1: '' : #version directive missing
ERROR: 0:1: '}' : syntax error syntax error

I recommend to use std::istreambuf_iterator to read the entire file, from its begin to its end:

std::ifstream infileVert("vertex.glsl", std::fstream::in);
std::string ContentVert;
if ( sourceFile.is_open() )
    ContentVert = std::string(std::istreambuf_iterator<char>(infileVert),
                              std::istreambuf_iterator<char>());