0
votes

I am learning OpenGL and try to do some simple texturing. Therefore I supply my texture coordinates as attributes.

I have a strange error with my vertex shader:

#version 150

in vec2 position;
in vec3 color;
in vec2 texcoord;

out vec3 Color;
out vec2 Texcoord;

void main()
{
    Color = color;
    Texcoord = vec2 (1.0, 0.5); /* just for debugging hardcode texcords */
    gl_Position = vec4( position, 0.0, 1.0 );
}

The shader compiles fine but when I try to link the program to which the shader belongs glGetProgramInfoLog() log says:

ERROR: Compiled vertex shader was corrupt.

When I remove

in vec2 texcoord;

The error disappears. It does not matter how I name the attribute. As soon as I add the third one I get this error.

There are no compilation errors. Just this link error.

I use a OpenGL 3.2 Core Context on OS X 10.8.3. I use GLEW.

Update:

Now I can link this:

#version 150

in vec2 position;
in vec3 color;
in vec2 texcoord;

out vec3 Color;
out vec2 Texcoord;

void main()
{
    Color = color;
    gl_Position = vec4( position, 0.0, 1.0 );
}

but as soon I add

Texcoord = texcoord;

to the main function the shader becomes corrupt. Im desperate. What is this?

1
do you have in vec2 Texcoord in your fragment shader ? (i'm not sure, but probably is is a problem that the name is equal except the case of the letters, i alway choose different names) while compiling unused variables are removed, which will prevent the linking error. - t.niese
What happens if you rename 'texcoord' to something else? - bwroga
It may help us if we could see your fragment shader as well as a small excerpt of the important OpenGL calls for compiling/linking your program. Just read the spec for #150 and I can't see anything wrong with just your vertex shader. - Hydronium
The problem was not in the shader but in my function that loads the shader file from disk. I did this: const char* shaderSource = loadFile(path.c_str()).c_str(); where loadFile() returns a std::string containing the shader as plaintext. Somehow this did not work everytime. I changed it to string shaderString = loadFile(path.c_str()); const char* shaderSource = shaderString.c_str(); . This does work. I don't know why. Please create and answer for this (not a comment). - Alexander Theißen
You're allowed (and encouraged) to answer your own questions if you find a solution. There's nothing wrong with that. In regards to this question though: you may find this question will become closed as too localized if other people cannot benefit from your solution. For example if your fix was just a typo in some part of the program which you never mention in the question then this will be likely. - Hydronium

1 Answers

2
votes

You stated above in the comment that changing your code from

const char* shaderSource = loadFile(path.c_str()).c_str();

to

string shaderString = loadFile(path.c_str());
const char* shaderSource = shaderString.c_str();

fixed your issue.

The std::string object is being destroyed as there are no references to it, which in turn would destroy the c string buffer. It is a matter of luck as to what the contents of that buffer are at the time of reading.