[Solved] Shader compilation problem

Started by
1 comment, last by bdubreuil 11 years ago

Hello,

I had been following this tutorial on how to create/compile/link shader http://arcsynthesis.org/gltut/Basics/Tut01%20Making%20Shaders.html until I tested the code. I tried to compile the same vertex and fragment shaders as the author but I got some errors which I naturally didn't understand. I have uploaded the errors I got from OpenGL as an attached image to my post. I don't think the mistakes come from the shaders themselves but more likely from the C++ code. Do you know what are my mistakes?

VertexShader.vert


#version 330

layout(location = 0) in vec4 position;
void main()
{
    gl_Position = position;
}

FragmentShader.frag


#version 330

void main()
{
    gl_FragColor = vec4(0.7, 0.7, 0.6, 1.0);
}

My code:


GLuint CreateShader(GLenum eShaderType, const std::string &strShaderFile)
{
    GLuint shader = glCreateShader(eShaderType);
    const char *strFileData = strShaderFile.c_str();
    glShaderSource(shader, 1, &strFileData, NULL);

    glCompileShader(shader);

    GLint status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
    if (status == GL_FALSE)
    {
        GLint infoLogLength;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);

        GLchar *strInfoLog = new GLchar[infoLogLength + 1];
        glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);

        const char *strShaderType = NULL;
        switch(eShaderType)
        {
        case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
        case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
        case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
        }

        std::cout << "Compile failure in " << strShaderType << "shader:\n" << strInfoLog << "\n";

        delete[] strInfoLog;
    }

 return shader;
}


GLuint CreateProgram(const std::vector<GLuint> &shaderList)
{
    GLuint program = glCreateProgram();

    for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
     glAttachShader(program, shaderList[iLoop]);

    glLinkProgram(program);

    GLint status;
    glGetProgramiv (program, GL_LINK_STATUS, &status);
    if (status == GL_FALSE)
    {
        GLint infoLogLength;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);

        GLchar *strInfoLog = new GLchar[infoLogLength + 1];
        glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
        std::cout << "Linker failure: " << strInfoLog << "\n";
        delete[] strInfoLog;
    }

    for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
        glDetachShader(program, shaderList[iLoop]);

    return program;
}


int main()

{

...

shaderList.push_back(CreateShader(GL_VERTEX_SHADER, "VertexShader.vert"));
shaderList.push_back(CreateShader(GL_FRAGMENT_SHADER, "FragmentShader.frag"));

GLuint shaderProgram = CreateProgram(shaderList);

std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);

...

}


Thank you smile.png

thecheeselover

Hide yo cheese! Hide yo wife!

Advertisement

Let me guess. You're calling CreateShader(GL_VERTEX_SHADER, "VertexShader.vert")? glShaderSource() takes the shader source, not the file name. Load the file, and then pass the contents of the file to your CreateShader() function (instead of passing the file name).

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Let me guess. You're calling CreateShader(GL_VERTEX_SHADER, "VertexShader.vert")? glShaderSource() takes the shader source, not the file name. Load the file, and then pass the contents of the file to your CreateShader() function (instead of passing the file name).

Dammit you're totally right tongue.png I thought strShaderFile meant the path. Thank you!

Hide yo cheese! Hide yo wife!

This topic is closed to new replies.

Advertisement