Reputation: 23
I'm a Java developer and I recently switched to C++. I've been trying to make a little OpenGL program and I ran into a problem. Whenever I try compiling my shader program it gives a linking error. Here it is:
ERROR::SHADER::PROGRAML::LINKING_ERROR
Link info
---------
error: "TexCoord" not declared as an output from the previous stage
Here are the shader files:
Vertex:
#version 440 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}
Fragment
#version 440 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
// texture sampler
uniform sampler2D texture1;
void main()
{
FragColor = texture(texture1, TexCoord);
}
And here is the shader creation:
int success;
char infoLog[512];
const char* vertexSource;
const char* fragmentSource;
// Load in shader source
std::ifstream inFile;
inFile.open(vertexPath);
std::string temp = "";
std::string source = "";
if (inFile.is_open())
{
while (std::getline(inFile, temp))
source += temp + "\n";
}
else
{
std::cout << "ERROR::SHADER::VERTEX::COULD_NOT_OPEN_SOURCE_FILE" << std::endl;
}
inFile.close();
vertexSource = source.c_str();
temp = "";
source = "";
inFile.open(fragmentPath);
if (inFile.is_open())
{
while (std::getline(inFile, temp))
source += temp + "\n";
}
else
{
std::cout << "ERROR::SHADER::FRAGMENT::COULD_NOT_OPEN_SOURCE_FILE" << std::endl;
}
inFile.close();
fragmentSource = source.c_str();
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, nullptr);
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COULD_NOT_COMPILE\n" << infoLog << std::endl;
}
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COULD_NOT_COMPILE\n" << infoLog << std::endl;
}
// Program
ID = glCreateProgram();
glAttachShader(ID, vertexShader);
glAttachShader(ID, fragmentShader);
glLinkProgram(ID);
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(ID, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::PROGRAML::LINKING_ERROR\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glUseProgram(0);
To my understanding, I should only be getting this error if the two variables that I'm trying to link aren't the same name or type, but they are. I just don't understand.
Thanks.
Upvotes: 0
Views: 108
Reputation: 12879
You essentially have...
std::string source;
const char *vertexSource;
const char *fragmentSource;
...read into source...
vertexSource = source.c_str();
...read into source...
fragmentSource = source.c_str();
The pointer stored in vertexSource
will be invalidated by the second read into source
. From the documentation
The pointer obtained from c_str() may be invalidated by:
- Passing a non-const reference to the string to any standard library function, or
- Calling non-const member functions on the string, excluding operator[], at(), front(), back(), begin(), rbegin(), end() and rend().
Upvotes: 2