Skip to main content
GameDev.net gamedev.net
🔒 Locked

[Solved] Shader compilation problem

Started by bdubreuil Apr 14, 2013 at 12:38 AM 1 replies 1.8k views
Original Post
bdubreuil
bdubreuil

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!
Cornstalks
Cornstalks

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

bdubreuil
bdubreuil

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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.