PDA

View Full Version : Vertex shader not passing information to Fragment shader.



GrahamGoring
02-23-2018, 09:56 AM
Hullo! I'm taking my first steps into the exciting world of OpenGL 3.x and shaders and whatnot and while I've had a small amount of success (I have a polygon on screen and it's displaying the colour at the top left of the texture I've loaded) I'm having problems getting it to accept the UV co-ordinates I'm passing in. I've confirmed that the data going into the texture co-ord VBO is correct (I've got 2 VBOs and an IBO in a VAO as my structure), however the information doesn't get passed from the vertex shader (which I'm pretty sure it's getting to, because the vertex position data certainly is).

The vertex shader is:



#version 140

in vec2 in_VertexPos2D;
in vec2 in_TexturePos2D;
out vec2 out_TexturePos2D;

void main()
{
out_TexturePos2D = in_TexturePos2D;
gl_Position = vec4( in_VertexPos2D.x, in_VertexPos2D.y, 0, 1 );
}


And the fragment shader is:


#version 140

in vec2 in_TexturePos2D;

uniform sampler2D LTextureUnit;

out vec4 LFragment;

void main()
{
LFragment = texture2D( LTextureUnit, in_TexturePos2D );
}


Now, if I manually set a UV value of, say (0.1,0.1) in the fragment shader and tell it to draw that, then the texture changes to the colour of that pixel (so obviously the texture is loaded and being sampled from).
But if I try changing it so that the value of out_TexturePos2D in the vertex shader is set to the same value, then it doesn't work.

The shaders have built okay and linked without complaint, so what incredibly obvious and dumb noob mistake am I making? Because I'm pretty sure that's what's happening. :P

Thanks in advance for your understand, patience and knowledge.

EDIT: Apologies to everyone as I just realised what a massive idiot I am. The outputted variable name from the vertex shader has to match the inputted one in the fragment shader. I am a dunce.

WorldDj
02-24-2018, 01:22 AM
You're missing something here, in order for shaders to communicate with each other there must be an OUTPUT that has the same data type and name of an INPUT in another shader, so applying this to your shaders will clearly tell you why no varying variables were passed... And of course having a program that both the shaders are attached to and are linked.


#version 140//WRONG VERSION USE #version 330 core
in vec2 in_VertexPos2D;
in vec2 in_TexturePos2D; //This is your input from the OPENGL API to vertix shader.
out vec2 out_TexturePos2D; //This is your output to the fragment shader.

void main()
{
out_TexturePos2D = in_TexturePos2D;
gl_Position = vec4( in_VertexPos2D.x, in_VertexPos2D.y, 0, 1 );
}

#version 140 //WRONG VERSION USE #version 330 core

in vec2 in_TexturePos2D; //This has a different name from the output of the vertex shader so it should be (in vec2 out_TexturePos2D;)

uniform sampler2D LTextureUnit;

out vec4 LFragment;

void main()
{
LFragment = texture2D( LTextureUnit, in_TexturePos2D );
}