Jump to content

  • Log In with Google      Sign In   
  • Create Account


Shader program linking issue


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
11 replies to this topic

#1 Kametec   Members   -  Reputation: 134

Like
0Likes
Like

Posted 09 May 2013 - 01:45 AM

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][i] = 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][i] = 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][i] = 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][i] = 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, 10 May 2013 - 03:20 AM.


Sponsor:

#2 hdxpete   Members   -  Reputation: 467

Like
0Likes
Like

Posted 09 May 2013 - 03:28 AM

have you tried this?

http://www.gamedev.net/topic/383501-conversion-of-gl-error-to-string/



#3 Kametec   Members   -  Reputation: 134

Like
0Likes
Like

Posted 09 May 2013 - 06:10 AM

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


#4 hdxpete   Members   -  Reputation: 467

Like
0Likes
Like

Posted 09 May 2013 - 07:24 PM

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



#5 TheChubu   Crossbones+   -  Reputation: 4187

Like
0Likes
Like

Posted 09 May 2013 - 08:03 PM

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


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#6 Kametec   Members   -  Reputation: 134

Like
0Likes
Like

Posted 10 May 2013 - 01:17 AM

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


#7 Sponji   Members   -  Reputation: 1225

Like
0Likes
Like

Posted 10 May 2013 - 01:44 AM

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?


Derp

#8 Kametec   Members   -  Reputation: 134

Like
0Likes
Like

Posted 10 May 2013 - 02:28 AM

glewInit is called after both window and context are created.

EOF character is not read, or at least should not, according to http://www.cplusplus.com/reference/ios/ios/good/



#9 Plerion   Members   -  Reputation: 365

Like
0Likes
Like

Posted 10 May 2013 - 02:58 AM

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;
}


#10 Kametec   Members   -  Reputation: 134

Like
0Likes
Like

Posted 10 May 2013 - 03:22 AM

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.



#11 Sponji   Members   -  Reputation: 1225

Like
1Likes
Like

Posted 10 May 2013 - 03:00 PM

Are you really sure that the shaders are correct? Why you're doing stuff like this:

 

GLchar **str = new GLchar*[1];

Not sure how that pointer works, too sleepy. I'd probably write it like this, much simpler:

 

char *str = new char[256]; 
memset(str, 0, 256);
readvertexshader();
memset(str, 0, 256);
readfragmentshader();
// ...
glShaderSource(fShad, 1, (const char**)&str, NULL);
// and now you can really make sure that you have the actual shader source there, just print it out
printf("shader source:\n%s\n", str);

Actually... I think you should have written (const char**)&str[1] before, because of that weird pointer thingie you were doing. I tried your code on my computer and seemed to work ok with those changes, had to change version numbers too though, my card doesn't support OpenGL 4.0.

 

Edit: And oh, seemed like I had to call ifs.close() too, not sure if this is only like this on Linux? I think it clears the eof bit, too lazy to check out. Sleep I goes.


Edited by Sponji, 10 May 2013 - 03:03 PM.

Derp

#12 Kametec   Members   -  Reputation: 134

Like
0Likes
Like

Posted 11 May 2013 - 11:35 AM

Hi,

thanks for your reply, it helped me to solve the problem. The error was caused by missing ifs.close(), and the second shader wasn't read correctly because of it.

 

As to why I used GLchar **str, well, I'm starting to learn C++ as well as OpenGL. I have some experience with other languages, though. I've read in documentaion that glShaderSource accepts GLchar **, so I declared my variable as GLchar **. Thinking of it as two-dimensional array helped me to get it running.

 

Now, that I am familiar with & and * operators I admit you're right, and I will use an array of GLchar passed as &arr_name in future code.

 

In summary: I've included ifs.close() and the whole issue dissapeared. I didn't do anything else, so I managed to get file reading to work properly.

Thanks again.






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