[ad_1]
I’m no the place close to an expert in shaders however I am at present learning OpenGL and I do know a pair issues. I’ll strive my greatest to assist by answering the questions I do find out about.
What’s uniform sampler2D texture? Is that solely within the fragment shader? Does it want a texture handed in or does it use a default texture?
To have the ability to work on generated textures, the fragment shader wants a approach to entry that texture’s object.
GLSL has a built-in information kind that does precisely that and that may be the sampler
. The sampler postfix defines what kind the feel object is anticipated to be of, for instance: sampler2D
.
(There’s additionally sampler1D
and sampler3D
however that is completely irrelevant).
To reply among the questions I present a easy instance:
Vertex Shader
#model 330 core
structure (location = 0) in vec3 aPos;
structure (location = 1) in vec3 aColor;
out vec3 ourColor;
void major()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
}
Fragment Shader
#model 330 core
out vec4 FragColor;
in vec3 ourColor;
void major()
{
FragColor = vec4(ourColor, 1.0f);
}
Typically I see fragment shaders which write “in vec2 some_var” and state that its handed in from the vertex shader however then I do not see the vertex shader really passing something to the fragment shader? Is that this regular?
To move an output from the vertex shader to the fragment shader, you have to specify that the variable is an output through the use of the out
key phrase.
Within the instance above, out vec3 ourColor;
is outputted from the vertex shader to the fragment shader.
Now however, as a way to settle for inputs, the fragment shader must count on it, to specify that the fragment shader shall be receiving enter from the vertex shader, we use the in
key phrase.
Within the instance above, that may be in vec3 ourColor;
.
To summarize, the out
key phrase is used to specify that some variable is an output and the in
key phrase specifies {that a} sure variable kind is an anticipated enter. This works as a result of the desired out
and in
variables have matching sorts which is vec3
.
One factor to notice is that the vertex shader ought to obtain an enter so it has one thing to work on, in any other case it will not be capable of do something. That enter is in type of vertex information.
Then again, fragment shaders are additionally required to output a vec4
color output variable.
For the vertex shader proven above, these can be:
structure (location = 0) in vec3 aPos;
structure (location = 1) in vec3 aColor;
For the fragment shader proven above, this may be:
out vec4 FragColor;
EDIT:
To observe up in your revised query:
To place it merely, Uniforms are one other approach to ship information to shaders.
It is very important notice that uniforms are world which means that they’re accessible by all shaders at any level.
is the code I offered a vertex shader or a fraction shader? As a result of it desires an in vec2?
The primary commentary we might make within the shader code you’ve got offered to find out which kind of shader that is, is that the shader outputs a vec4, which is the required output by the fragment shader, however this is not concrete proof that it is a fragment shader.
To have the ability to higher determine which shader you are at present , it is very important have an concept concerning the position of every shader.
Put merely, the vertex shader additionally permits us to do stuff with the attributes of the vertices it receives as enter, such because the place.
The fragment shader’s accountability is principally to calculate the ultimate color of pixels which might be in between the vertices.
Within the shader code you’ve got offered within the query, we are able to decide that the operations utilized are extra associated to calculating the ultimate color and outputting it utilizing out vec4 colour;
, which is the what the fragment shader is accountable of.
If you would like to know extra about shaders, I extremely, extremely, advocate studying the “Shaders” part within the “Getting Began” chapter of the LearnOpenGL ebook.
Hyperlink to the “Shaders” part: Shaders Part
[ad_2]