Jump to content

  • Log In with Google      Sign In   
  • Create Account


[Solved] Shader compilation problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 thecheeselover   Members   -  Reputation: 330

Like
0Likes
Like

Posted 13 April 2013 - 06:38 PM

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

Attached Thumbnails

  • GLSLproblem.png

Edited by thecheeselover, 13 April 2013 - 07:05 PM.

Hide yo cheese! Hide yo wife!

Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6974

Like
3Likes
Like

Posted 13 April 2013 - 06:49 PM

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


[ 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 ]

#3 thecheeselover   Members   -  Reputation: 330

Like
0Likes
Like

Posted 13 April 2013 - 07:04 PM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS