Advertisement

Fragment Position

Started by October 03, 2018 05:20 PM
5 comments, last by babaliaris 6 years, 4 months ago

Take a Look at my Shaders:

 

Vertex Shader:


#version 330 core

layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;


out vec3 FragPos;
out vec3 Normal;

void main()
{
	gl_Position = proj * view * model * vec4(aPos, 1.0);

	FragPos = vec3(model * vec4(aPos, 1.0));

	Normal = normalize(mat3(transpose(inverse(model))) * aNormal);
}

 

 

Fragment Shader:


#version 330 core

out vec4 Color;

uniform vec3 viewPos;

struct Material
{

	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
	float shininess;
};

uniform Material material;


struct Light
{
	
	vec3 position;
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};

uniform Light light;

in vec3 FragPos;
in vec3 Normal;

void main()
{

	//Calculating the Light Direction From The Fragment Towards the light source.
	vec3 lightDirection = normalize(light.position - FragPos);

	//Calculating the camera direction.
	vec3 camDirection = normalize(viewPos - FragPos);

	//Calculating the reflection of the light.
	vec3 reflection = reflect(-lightDirection, Normal);

	//Calculating the Diffuse Factor.
	float diff = max( dot(Normal, lightDirection), 0.0f );

	//Calculate Specular.
	float spec = pow( max( dot(reflection, camDirection), 0.0f ), material.shininess);

	//Create the 3 components.
	vec3 ambient  = material.ambient * light.ambient;
	vec3 diffuse  = (material.diffuse * diff) * light.diffuse;
	vec3 specular = (material.specular * spec) * light.specular;

	//Calculate the final fragment color.
	Color = vec4(ambient + diffuse + specular, 1.0f);
}

 

I can't understand how this:


FragPos = vec3(model * vec4(aPos, 1.0));

is actually the fragment position. This is just the vertex position transformed to world coordinates. The vertex shader is gonna get called n times where n is the number of vertices, so the above code is also going to get called n times. The actual fragments are a lot more so how can the above code generate all the fragments positions? Also what is a fragments position? Is it the (x,y) you need to move on the screen in order to find that pixel? I don't think so because i read that while you are in the fragment shader the actual pixels on the screen have not been determined yet because the viewport transformation happens at the end of the fragment shader.


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

Out of curiosity, what is this code based on? Is it based on a book or tutorial or something? (The answer to that question might provide some insight here.)

Advertisement

@Zakwayda It looks like it's based on learnopengl.com: https://learnopengl.com/Lighting/Basic-Lighting

 

@babaliaris There is a step between vertex and fragment shaders called rasterization, where triangles are broken into fragments. Look up the graphics pipeline.

Here's an excerpt from http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Chapter-1%3a-The-Graphics-Pipeline.html

Quote

Rasterization

gl1-rasterization-01.png

The rasterizer takes each triangle, clips it and discards parts that are outside of the screen, and breaks the remaining visible parts into pixel-sized fragments. As mentioned above, the vertex shader's varying outputs are also interpolated across the rasterized surface of each triangle, assigning a smooth gradient of values to each fragment. For example, if the vertex shader assigns a color value to each vertex, the rasterizer will blend those colors across the pixelated surface as shown in the diagram.

The relevant part here is that "the vertex shader's varying outputs are also interpolated across the rasterized surface of each triangle", which means that the fragment shader is called more times than the vertex shader, and aPos from the vertex output will be interpolated into multiple values for multiple fragment shader calls.

Yes it's from LearnOpenGLcom . Thank you for your answer.


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

6 hours ago, babaliaris said:

Yes it's from LearnOpenGLcom . Thank you for your answer.

You would likely do well reading through this series of articles too: https://fgiesen.wordpress.com/2011/07/09/a-trip-through-the-graphics-pipeline-2011-index/

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

18 hours ago, CrazyCdn said:

You would likely do well reading through this series of articles too: https://fgiesen.wordpress.com/2011/07/09/a-trip-through-the-graphics-pipeline-2011-index/

Thank you! This is what I needed!!! 


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

This topic is closed to new replies.

Advertisement