I am having a propblem figuring out what is going on in my simplest of shaders. Here is my vertex shader:
#version 410 core
in vec3 position;
in vec2 textureCoords;
out vec2 color;
void main(void)
{
gl_Position = vec4(position.x, position.y, position.z, 1.0);
color = vec2(1.0,0.5);
}
And the fragment shader:
#version 410 core
in vec2 color;
out vec4 out_color;
void main(void)
{
out_color = vec4(0.0, 1.0, 0.0, 1.0);
}
Note, the color variable being passed out from the VS to FS doesnt really do anything. These shaders give me the expected result which is the following image:
Now, if i merely pass the textureCoords from the VS to the FS using the color variable without any change to the FS it shifts my image to the right. So the modified VS (and the ONLY modification to shaders) is:
#version 410 core
in vec3 position;
in vec2 textureCoords;
out vec2 color;
void main(void)
{
gl_Position = vec4(position.x, position.y, position.z, 1.0);
color = textureCoords;
}
This gives the unexpected following render (triangle shifts to the top-right):
I dont understand why this is happening since I am not even using the color var in the FS.
Why is this happening ? I am merely trying to pass a vec2 to the FS ! What am I not seeing here ?
Here is some additional info: GL Information: Supported GL version: 4.1 NVIDIA-10.16.34 355.10.05.35f05 Supported shading lang: 4.10
Complete code here: complete code Entry point is tests.cpp
Here are my current vertex arrays:
GLfloat PossibleVertices[] = {
// Left bottom triangle
0.0f, 0.5f, 0.0f, // v0
-0.5f, -0.5f, 0.0f, // v1
0.5f, -0.5f, 0.0f
};
GLuint Indices[] = {
0, 1, 2
};
GLfloat TextureCoords[] =
{
0.5f, 1.0f, // v0
0.0f, 0.0f, // v1
1.0f, 0.0f
};
// Load data to VAO
LoadToVAO(PossibleVertices, 9, Indices, 3, TextureCoords, 6);
...
LoadToVAO
(
GLfloat Positions[], GLuint PosArrySize,
GLuint Indices[], GLuint IndArrySize,
GLfloat TexCoords[], GLuint TCArrySize
)
{
GLuint VaoID = CreateVAO();
BindIndicesBufferVBO(Indices, IndArrySize); // Buffer Index - optimization
StoreDataInAttrList(0, 3, Positions, PosArrySize);
StoreDataInAttrList(1, 2, TexCoords, TCArrySize);
UnbindVAO();
CGCore::RawModel* ret = new CGCore::RawModel(VaoID, IndArrySize);
return ret;
}
Here is the function called for actually storing data to the VAO. Called for both Vertex arrays:
void CGCore::Loader::StoreDataInAttrList(GLuint AttrNumber, GLuint AttrSize, GLfloat Data[], GLuint DataSize)
{
GLuint VboID;
glGenBuffers(1,&VboID);
VBOContainer.push_back(VboID);
glBindBuffer(GL_ARRAY_BUFFER, VboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*DataSize, Data, GL_STATIC_DRAW);
glVertexAttribPointer(AttrNumber, AttrSize, GL_FLOAT, GL_FALSE, 0, (void*) 0); // write to VAO
glBindBuffer(GL_ARRAY_BUFFER, 0); // unbind current VBO
}
Here is the attr binding code:
void BindAttributes()
{
BindAttribute(0, "position");
BindAttribute(1, "textureCoords");
}
// and also ...
void BindAttribute(int Attrib, const GLchar* VarName)
{
glBindAttribLocation(ProgramID, Attrib, VarName);
}
And finally the rendering code:
void CGCore::Renderer::Render(CGCore::TexturedModel* TexturedModelObj)
{
CGCore::RawModel* Model = TexturedModelObj->GetModel();
glBindVertexArray(Model->GetVaoID());
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glActiveTexture(GL_TEXTURE0);
glDrawElements( GL_TRIANGLES, Model->GetVertexCount(), GL_UNSIGNED_INT, (void*) 0);
glBindTexture(GL_TEXTURE_2D, TexturedModelObj->GetTexture()->GetTextureID());
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindVertexArray(0);
}
Sorry for the long post. Thanx!