-3

I get this error when I try to link program shader, vertex it s problem. The path is good I checked.

This is shader code:

class Shader
{
private:
    GLuint ID;
//load from filepath
    std::string loadShaderSource(const char* shaderFile)
    {
        std::string sCode;
        std::ifstream sFile;

        sFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);

        try
        {
            sFile.open(shaderFile);

            std::stringstream sStream;

            sStream >> sFile.rdbuf();

            sCode = sStream.str();  
            
            sFile.close();
        }

        catch (const std::ifstream::failure& e)
        {
            std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULY_READ: " << e.what() << std::endl;
        }

        const char* shaderCode = sCode.c_str();
        return shaderCode;
    }
//create shader
    GLuint loadShader(GLenum shaderType, const char* shaderFile)
    {
        char infoLog[1024];
        GLint succes;

        GLuint shader = glCreateShader(shaderType);

        std::string s_src = this->loadShaderSource(shaderFile);
        const GLchar* src = s_src.c_str();

        glShaderSource(shader, 1, &src, NULL);
        glCompileShader(shader);

        glGetShaderiv(shader, GL_COMPILE_STATUS, &succes);
        if (!succes)
        {
            glGetShaderInfoLog(shader, 1024, NULL, infoLog);
            std::cout << "ERROR::SHADER::COMPILE_FAILED: " << shaderFile << "\n";
            std::cout << infoLog << "\n";
        }

        return shader;
    }
//Link vertex and fragment
    void linkProgram(GLuint vertexShader, GLuint fragmentShader)
    {
        char infoLog[1024];
        GLint succes;

        this->ID = glCreateProgram();

        glAttachShader(this->ID, vertexShader);
        glAttachShader(this->ID, fragmentShader);

        glLinkProgram(this->ID);

        glGetProgramiv(this->ID, GL_LINK_STATUS, &succes);
        if (!succes)
        {
            glGetProgramInfoLog(this->ID, 1024, NULL, infoLog);
            std::cout << "ERROR::SHADER::LINKING_PROGRAM_FAILED\n";
            std::cout << infoLog << "\n";
        }

        glUseProgram(0);
    }

public:
    Shader(const char* vertexPath, const char* fragmentPath)
    {
        GLuint vertexShader = 0;
        GLuint fragmentShader = 0;

        vertexShader = loadShader(GL_VERTEX_SHADER, vertexPath);
        fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentPath);

        this->linkProgram(vertexShader, fragmentShader);

        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
    }

This is vertex file:

#version 330 core
layout (location = 0) in vec3 aPos;

void main()
{
  gl_Position = vec4(aPos, 1.0f);
};

This is error what appear in console when i m running code, i tested a triangle , it s appear but on black (in fragment shader is red) and get this error in console:

ERROR::SHADER::LINKING_PROGRAM_FAILED
Vertex info
-----------
(0) : error C5145: must write to gl_Position
genpfault
  • 49,394
  • 10
  • 79
  • 128
Mircic
  • 1
  • Edit in a [mcve]. Feel free to use [this](https://github.com/genpfault/glfw-mcve-base/blob/master/src/main.cpp) as a base. – genpfault May 15 '22 at 21:42
  • 1
    `std::string s_src = this->loadShaderSource(shaderFile);` after this line did you print the content of s_src to make sure it contained the entire shader? Because honestly `sFile.open(shaderFile); std::stringstream sStream; sStream >> sFile.rdbuf();` This looks fishy. In fact in the first answer [here](https://stackoverflow.com/questions/116038/how-do-i-read-an-entire-file-into-a-stdstring-in-c) the stream operator goes the other way which makes more sense, you want to take the file buffer *into* the string stream, not the other way around (because the stringstream is empty) – Borgleader May 16 '22 at 00:11
  • The return statement of the loadShader method also looks fishy. You return the internal pointer of a local variable. Why not return `sCode` directly? – BDL May 16 '22 at 06:22
  • 1
    @BDL that part is inefficient but not incorrect – user253751 May 16 '22 at 09:49
  • The problem was `sStream >> sFile.rdbuf();` operator should be `sStream << sFile.rdbuf();` – Mircic May 19 '22 at 20:22

0 Answers0