Get TexCoords of vertices in fragment shader - opengl

Is there anyway to get the TexCoords of the vertices inside the fragment shader or to give them to the fragment shader from the vertex shader, without giving them from the normal code to the vertex shader?

If you have read any tutorial, you'd know this is pretty much required for any reasonable texturing.
VS:
attribute vec2 in_texCoord;
out vec2 fs_texCoord;
main () {
fs_texCoord = in_texCoord;
}
FS:
in vec2 fs_texCoord;
out vec4 out_color;
main () {
out_color = vec4(fs_texCoord, 0.0, 1.0);
}
All of the rules regarding interpolation/filtering still apply.

Related

How does the shader know that I'm editing color and not something else

There's this fragment shader which does work, but I do not understand its logic:
#version 330 core
out vec4 FragColor;
// the input variable from the vertex shader (same name and same type)
in vec4 vertexColor;
void main()
{
FragColor = vertexColor;
}
So how does the shader know that FragColor is supposed to represent the color of the shader, there is no assignment anywhere.
I'm saying it because in the vertex shader it's clear what is happening:
#version 330 core
// the position variable has attribute position 0
layout (location = 0) in vec3 aPos;
// specify a color output to the fragment shader
out vec4 vertexColor;
void main()
{
// see how we directly give a vec3 to vec4's constructor
gl_Position = vec4(aPos, 1.0);
// set the output variable to a dark-red color
vertexColor = vec4(0.5, 0.0, 0.0, 1.0);
}
The fragment shader does not know that FragColor represent a "color". For the shader it is just a vector with 4 components. The output(s) of the fragment shader are written into the frambuffer. A fragment shader has no other output (except depth buffer and stencil buffer). Therefore the shader does not need to know that the output variable represents a color.
See also Fragment Shader - Output buffers.

Do I need to pass color though my geometry shader to the fragment shader?

So I have three shaders in my program.
Vertex:
#version 330 core
in vec2 Inpoint;
in vec2 texCoords;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 projection;
void main()
{
TexCoords = texCoords;
gl_Position = projection * model * vec4(Inpoint, 0.0, 1.0);
}
Geometry:
#version 330 core
layout(triangles) in;
layout(triangle_strip, max_vertices = 4) out;
void main()
{
int i;
for (i = 0; i < gl_in.length(); i++)
{
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
}
And finally the fragment shader:
#version 330 core
in vec2 TexCoords;
out vec4 color;
uniform sampler2D image;
uniform vec3 spriteColor;
void main()
{
color = vec4(spriteColor, 1.0) * texture(image, TexCoords);
}
Now without the geometry shader, everything displays just fine. But as soon as I include the geometry shader, everything goes ... bad.
It acts like its not getting chords for the textures.
So, the question is, does the geometry shader need to pass the data through itself to the fragment shader? I mean the geometry shader is basically doing nothing so it shouldn't. Unless there is some giant mistake I am missing.
I tried to add a pass-though but it complains that everything needs to be an array, and even when I did make it an array it didn't quite work right.
Quoting GLSL 3.30 specs 4.3.1 Inputs :
Fragment shader inputs get per-fragment values, typically interpolated
from a previous stage's outputs
Having a geometry shader is the previous stage. So yes, your FS uses inputs from your GS and only from it.

Simple GLSL Shader (Light) causes flickering

I'm trying to implement some basic lighting and shading following the tutorial over here and here.
Everything is more or less working but I get some kind of strange flickering on object surfaces due to the shading.
I have two images attached to show you guys how this problem looks.
I think the problem is related to the fact that I'm passing vertex coordinates from vertex shader to fragment shader to compute some lighting variables as stated in the above linked tutorials.
Here is some source code (stripped out unrelated code).
Vertex Shader:
#version 150 core
in vec4 pos;
in vec4 in_col;
in vec2 in_uv;
in vec4 in_norm;
uniform mat4 model_view_projection;
out vec4 out_col;
out vec2 passed_uv;
out vec4 out_vert;
out vec4 out_norm;
void main(void) {
gl_Position = model_view_projection * pos;
out_col = in_col;
out_vert = pos;
out_norm = in_norm;
passed_uv = in_uv;
}
and Fragment Shader:
#version 150 core
uniform sampler2D tex;
uniform mat4 model_mat;
in vec4 in_col;
in vec2 passed_uv;
in vec4 vert_pos;
in vec4 in_norm;
out vec4 col;
void main(void) {
mat3 norm_mat = mat3(transpose(inverse(model_mat)));
vec3 norm = normalize(norm_mat * vec3(in_norm));
vec3 light_pos = vec3(0.0, 6.0, 0.0);
vec4 light_col = vec4(1.0, 0.8, 0.8, 1.0);
vec3 col_pos = vec3(model_mat * vert_pos);
vec3 s_to_f = light_pos - col_pos;
float brightness = dot(norm, normalize(s_to_f));
brightness = clamp(brightness, 0, 1);
gl_FragColor = out_col;
gl_FragColor = vec4(brightness * light_col.rgb * gl_FragColor.rgb, 1.0);
}
As I said earlier I guess the problem has to do with the way the vertex position is passed to the fragment shader. If I change the position values to something static no more flickering occurs.
I changed all other values to statics, too. It's the same result - no flickering if I am not using the vertex position data passed from vertex shader.
So, if there is someone out there with some GL-wisdom .. ;)
Any help would be appreciated.
Side note: running all this stuff on an Intel HD 4000 if that may provide further information.
Thanks in advance!
Ivan
The names of the out variables in the vertex shader and the in variables in the fragment shader need to match. You have this in the vertex shader:
out vec4 out_col;
out vec2 passed_uv;
out vec4 out_vert;
out vec4 out_norm;
and this in the fragment shader:
in vec4 in_col;
in vec2 passed_uv;
in vec4 vert_pos;
in vec4 in_norm;
These variables are associated by name, not by order. Except for passed_uv, the names do not match here. For example, you could use these declarations in the vertex shader:
out vec4 passed_col;
out vec2 passed_uv;
out vec4 passed_vert;
out vec4 passed_norm;
and these in the fragment shader:
in vec4 passed_col;
in vec2 passed_uv;
in vec4 passed_vert;
in vec4 passed_norm;
Based on the way I read the spec, your shader program should actually fail to link. At least in the GLSL 4.50 spec, in the table on page 43, it lists "Link-Time Error" for this situation. The rules seem somewhat ambiguous in earlier specs, though.

OpenGL - vertex shader color not being passed to fragment shader

I'm trying to pass a color from the vertex shader to the fragment shader and set gl_FragColor to this value. When I seem to do this, the color of the object I made flashes all different colors in no discernible pattern. However, when I set the color in the fragment shader to something similar to this gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) the color is perfectly white with no flashing. I'll post my shaders below:
Vertex Shader:
#version 130
//not using these for now
//uniform mat4 uniform_modelMatrix;
//uniform mat4 uniform_viewMatrix;
//uniform mat4 uniform_projectionMatrix;
in vec3 in_Position;
in vec4 in_Color;
void main(void)
{
//Set Position to XYZW
vec4 position = vec4(in_Position.xyz, 1.0);
gl_Position = position;
//Set Passed Color to white
out_Color = vec4(1.0, 1.0, 1.0, 1.0);
}
Fragment Shader:
#version 130
in vec4 pass_Color;
void main(void)
{
//Set Color to color from vertex shader - doesn't work
gl_FragColor = pass_Color;
//Set Color to white anyways - works
//gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
I don't know if I'm passing the values incorrectly in the vertex shader, or if I'm not receiving them properly in the fragment shader. Can anyone spot anything wrong?
So my buddy helped me out, I had the out variable in the vertex shader named out_Color (which I also just realized I didn't copy properly). But I had this declaration in the vertex shader out vec4 out_Color. I also had in the fragment shader in vec4 pass_Color. As soon as I changed the names to both be pass_Color the program ran perfectly.
So, for future use, if you're passing information between shaders, make sure the names of these variables are the same.

Fishy glGetAttribLocation returns -1

I'm trying to use a shader program that consists of two shaders.
Ortho.vert:
uniform vec2 ViewOrigin;
uniform vec2 ViewSize;
in vec2 Coord;
void main ()
{
gl_Position = vec4((Coord.x - ViewOrigin.x) / ViewSize.x,
1.0f - (Coord.y - ViewOrigin.y) / ViewSize.y,
0.0f, 1.0f);
}
Tiles.frag:
uniform sampler2D Texture;
uniform sampler2D LightMap;
in vec2 TextureCoord;
in vec2 LightMapCoord;
void main ()
{
vec4 textureColor = texture2D(Texture, TextureCoord);
vec4 lightMapColor = texture2D(LightMap, LightMapCoord);
gl_FragColor = vec4(textureColor.rgb * lightMapColor.rgb, 1.0f);
}
glGetAttribLocation is returning -1 for TextureCoord and LightMapCoord. I've read about instances where the compiler optimizes away attributes that aren't used, but in this example you can see that they are clearly used. Is there something about the OpenGL state (glEnable, etc.) that is required in order to enable samplers? I'm not sure what else could be wrong here. Any help is appreciated.
Attributes cannot go directly into the fragment shader. Their full name is vertex attributes, which means that they provide values per vertex, and are inputs to the vertex shader.
Fragment shaders can have in variables, but they do not correspond to vertex attributes. They need to match up with out variables of the vertex shader.
So what you need to do to get this working is to define the attributes as inputs to the vertex shader, and then pass the values from the vertex shader to the fragment shader. In the vertex shader, this could look like this:
uniform vec2 ViewOrigin;
uniform vec2 ViewSize;
in vec2 Coord;
in vec2 TextureCoord;
in vec2 LightMapCoord;
out FragTextureCoord;
out FragLightMapCoord;
void main ()
{
gl_Position = vec4((Coord.x - ViewOrigin.x) / ViewSize.x,
1.0f - (Coord.y - ViewOrigin.y) / ViewSize.y,
0.0f, 1.0f);
FragTextureCoord = TextureCoord;
FragLightMapCoord = LightMapCoord;
}
Then in the fragment shader, you declare in variables that match the out variables of the fragment shader. These variables will receive the per-fragment interpolated values of the what you wrote to the corresponding out variables in the vertex shader:
uniform sampler2D Texture;
uniform sampler2D LightMap;
in vec2 FragTextureCoord;
in vec2 FragLightMapCoord;
void main ()
{
vec4 textureColor = texture2D(Texture, FragTextureCoord);
vec4 lightMapColor = texture2D(LightMap, FragLightMapCoord);
gl_FragColor = vec4(textureColor.rgb * lightMapColor.rgb, 1.0f);
}
Having to receive the attribute values in the vertex shader, and explicitly passing the exact same values through to the fragment shader, may look cumbersome. The important thing to realize is that this is just a special case of a much more generic mechanism. Instead of simply passing the attribute value directly to the out variable in the vertex shader, you can obviously apply any kind of computation to calculate the values of the out values, which is often necessary in more complex shaders.