Jump to content
  • Advertisement
Sign in to follow this  
Kametec

OpenGL Shader program linking issue

This topic is 1956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I'm getting started with OpenGL. I'm trying to get my first program working, but I have no luck so far and I have no idea why. My test program is inspired by the first program in a book named "OpenGL Programming Guide, The Official Guide to Learning OpenGL, Version 4.3, Eight Edition".

 

I've written both vertex and fragment shaders, I've written code to initialize FreeGlut, GLEW, compile the shaders and link the shader program. But I have issues with the linking proccess. The output of glGetProgramInfoLog is this:

 

Vertex shader(s) failed to link, fragment shader(s) failed to link.
Vertex link error: INVALID_OPERATION.
ERROR: error(#97) No program main found
fragment link error: INVALID_OPERATION.
ERROR: error(#97) No program main found

 

I've written definition of my shaders into files, their contents are:

Vertex shader in triangles.vert:

 

#version 400 core

layout(location = 0) in vec4 vPosition;

void main()
{
    gl_Position = vPosition;
}

 

Fragment shader in triangles.frag:

 

#version 400 core

out vec4 fColor;

void main()
{
    fColor = vec4(0.0, 0.0, 1.0, 1.0);
}

 

I believe that the error suggests that there is something wrong with my shaders, but I don't know what. Both have void main() present.

 

For completeness' sake I include my C++ program which takes care about compiling and linking the shaders:

 

int main(int argc, char** argv)
{
    GLuint vShad,fShad,prg;
    GLchar **str = new GLchar*[1];
    GLubyte i;
    GLchar c;
    GLint err;

    str[0] = new GLchar[256];
    for (i=0; i<255;i++)
        str[0] = NULL;
    str[0][255] = NULL;
    i = 0;

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(512,512);
    glutInitContextVersion(4,0);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow(argv[0]);
    if (glewInit())
        return -1;

    //Code omitted: vertices and buffers 

    //vertex shader
    std::ifstream ifs("F:\\dev\\project\\test\\test\\triangles.vert");
    while (ifs.get(c)) //Reading contents of the file into GLchar**
    {
        str[0] = c;
        i++;
    }
    vShad = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vShad,1,(const char **)str,NULL);
    glCompileShader(vShad);
    glGetShaderiv(vShad,GL_COMPILE_STATUS,&err);
    if (err == GL_FALSE)
    {
        //Code omitted: Fetching compiler log
        return 1;
    }

    //fragment shader
    for (i=0; i<255;i++)
        str[0] = NULL;
    str[0][255] = NULL;
    i = 0;
    ifs.open("F:\\dev\\project\\test\\test\\triangles.frag");
    while (ifs.get(c)) //Reading contents of the file into GLchar**
    {
        str[0] = c;
        i++;
    }
    fShad = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fShad,1,(const char **)str,NULL);
    glCompileShader(fShad);
    glGetShaderiv(fShad,GL_COMPILE_STATUS,&err);
    if (err == GL_FALSE)
    {
        //Code omitted: Fetching compiler log
        return 2;
    }

    //shader program
    prg = glCreateProgram();
    glAttachShader(prg,vShad);
    glAttachShader(prg,fShad);
    glLinkProgram(prg);
    glGetProgramiv(prg,GL_LINK_STATUS,&err);
    if (err == GL_FALSE)
    {
        //Code omitted: Fetching linker log
        return 3;
    }
    glUseProgram(prg);

    //Code omitted

    return 0;
}

 

A few notes:

I had to reduce #version 430 core to #version 400 core, because FreeGlut couldn't initialize OpenGL 4.3 context on my hardware.

I am sure that shaders are compiled successfully. My main() returns 3, indicating an error with linking.

I have omitted code which I believe to be irrelevant to my issue.

Both FreeGlut and GLEW are initialized.

I'm using MS Visual Studio 2010 Express, FreeGlut 2.8.1 and GLEW 1.9.0.

 

I would be really grateful for any advice on where should I check for error and what kind of error might that be.

Thanks in advance.

 

edit: Code omitted comments are now in right places.

edit2: Glut window, profile and context initialization code included, unnecessary empty lines in code removed

edit3: Code responsible for reading files updated

Edited by Kametec

Share this post


Link to post
Share on other sites
Advertisement

No, I haven't tried glGetError() yet, if that's what you meant by your reply.

So I've included following code:

GLenum errCode;
GLchar *errString;
errCode = glGetError();
if (errCode != GL_NO_ERROR)
{
    errString = (GLchar *)gluErrorString(errCode);
    cout << errString << endl;
}

With this result:

invalid enumerant

Share this post


Link to post
Share on other sites

i usually (at least in testing) call glGetError() after each line to ensure i get the root error and not a trickle error.

so either your error is above the glGetProgram line OR the envirnoment you are using doesn't support GL_LINK_STATUS

Share this post


Link to post
Share on other sites

Hmm... At least from what I know you have to append a "/n" after reading each line of the shader.

Share this post


Link to post
Share on other sites

Hmm... At least from what I know you have to append a "/n" after reading each line of the shader.

 

You have to append new lines only if you're using a reading function, which discards them. Such as std::istream::getline. I am sure I'm passing the file content as is, including line breaks. I believe I would be getting compile errors otherwise.

 

 

i usually (at least in testing) call glGetError() after each line to ensure i get the root error and not a trickle error.

so either your error is above the glGetProgram line OR the envirnoment you are using doesn't support GL_LINK_STATUS

 

I've included a function which calls glGetError() and writes the error, together with an info string passed as parameter, to cout. I've got this output:

Entering main:
invalid operation
 
Initializing GLchar **str:
invalid operation
 
glutInit:
invalid operation
 
glutInitDisplayMode:
invalid operation
 
glutInitWindowSize:
invalid operation
 
glutInitContextVersion:
invalid operation
 
glutInitContextProfile:
invalid operation
 
glutInitCreateWindow:
no error
 
glewInit:
invalid enumerant
 
glGenVertexArrays:
no error
 
glBindVertexArray:
no error
 
glGenBuffers:
no error
 
glBindBuffer:
no error
 
glBufferData:
no error
 
Opening triangles.vert:
no error
 
Reading triangles.vert:
no error
 
Creating vertex shader:
no error
 
Assigning vertex shader a source:
no error
 
Compiling vertex shader:
no error
 
Checking compile status:
no error
 
Checking info log length:
no error
 
Reinitializing GLchar **str:
no error
 
Opening triangles.frag:
no error
 
Reading triangles.frag:
no error
 
Creating fragment shader:
no error
 
Assigning fragment shader a source:
no error
 
Compiling fragment shader:
no error
 
Checking compile status:
no error
 
Checking info log length:
no error
 
Creating shader program:
no error
 
Attaching vertex shader:
no error
 
Attaching fragment shader:
no error
 
Linking the program:
no error
 
Checking link status:
no error
 
Checking info log length:
no error
 
Getting info log:
no error
 
linker_log:
Vertex shader(s) failed to link, fragment shader(s) failed to link.
Vertex link error: INVALID OPERATION.
ERROR: error(#97) No program main found
fragment link error: INVALID OPERATION.
ERROR: error(#97) No program main found
 
Writing info log:
no error

Share this post


Link to post
Share on other sites

You need to have a context in use before calling glewInit, so just after glutInit isn't probably enough, call it after you've created the window and context. And btw, what happens when you read the last character from the text file? Isn't that appending EOF character into your string?

Share this post


Link to post
Share on other sites
On a sidenote: while(is.good()) is not the way you usually should check and read from the STL streams.
 
	
std::ifstream inFile("Data\\UI\\Debug\\ModelView.js");
char c = '\0';
std::string content;

while(inFile.get(c)) {
	content += c;
}

Share this post


Link to post
Share on other sites

I've included your recommendation into my code.

My error still stands unchanged, I think the method of reading source files is not responsible for sure now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!