0

Here is my code to begin with, this is the vertex shader

"#version 400\n"
      "layout(location =  0) in vec2 vp;"
      "layout(location = 1) in vec2 tex;"
      "out vec2 texCoord;"
      "void main () {"
      "  gl_Position = vec4 (vp, 0.0f, 1.0f);"
      "  texCoord = tex; "
      "}";

Quiet common and basic

So basically what i am trying to understand, does the vertex shader run for every vertex attribute separately ? Or it only runs for one attribute individually?

As far as i have understood if i give it as input vertices for a triangle

  x    y    U   V
{-0.5,-0.5, 0.0, 0.0
 0.5, -0.5 , 1.0, 0.0
 0.0  0.0 , 0.5, 1.0 };

Does that mean that the vertex shader will run for both of the vertex attributes and produce each individual fragment that is within the area of both triangles (sample) to result in the xy coordinates for both of my defined triangles for each attribute?

Or does the vertex shader only run for the gl_Position to produce the xy coordinates for the area in the first attribute i.e vp?

hopjopper
  • 99
  • 8

1 Answers1

1

The entire shader program runs once per vertex. So in this case, it runs three times. It doesn't work per-attribute.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • okay then at rasterization does it run for each of the triangles specified? – hopjopper Feb 06 '16 at 05:22
  • 1
    @hopjopper you're mixing a lot of unrelated things. Rasterisation doesn't care about vertex shaders - it is already too late on that phase. VS doesn't run for triangles but for individual vertices. Take a look at https://www.opengl.org/wiki/Rendering_Pipeline_Overview – keltar Feb 06 '16 at 06:17
  • okay what i am trying to understand , and hopefully somebody knows, is this, for texture coordinates, when you give it to your vertex shaders and it passes it onto other stages of pipeline, does it create an array of points between the extremities of the square/triangle/shape so that later in your fragment shader when you use texture(sampler2d, textCoordinates) does it automatically access each index of the array and use it to apply the texels generated from your vertices – hopjopper Feb 06 '16 at 06:48
  • http://pastebin.com/UjrA3mnh i am not sure what is going wrong here – hopjopper Feb 06 '16 at 06:49
  • @hopjopper no it does not create extra vertices, tesselation is completely different thing. Fragment shader gets interpolated coordinates depending on its position between vertices. Math is quite simple here. As for your problems, you've broken your vertex layout with line `0.0 -0.5, 0.5, 0.0 }` - see missing `,` between first two numbers. – keltar Feb 06 '16 at 07:16
  • okay so that is definitely working , but what i would like to understand is this :"Fragment shader gets interpolated coordinates depending on its position between vertices. Math is quite simple here." i dont understand what that means, can you explain this interpolated a bit and i am a beginner in OpenGL – hopjopper Feb 06 '16 at 07:20
  • so to summarise what i dont get is where does the texture coordinates come from? i certainly didnt supply them, but i do know that the fragments get generated for the vertices in the vertex shader, but does that mean they also get generated fro the texture coordinates between the vertices supplied in second vertex attribute? i.e texCoord? – hopjopper Feb 06 '16 at 07:27
  • @hopjopper http://stackoverflow.com/questions/24441631/how-exactly-does-opengl-do-perspectively-correct-linear-interpolation#24460895 – keltar Feb 06 '16 at 07:33
  • @keltar does all this described happen pre fragment shader i.e rasterization or post fragment shader ? – hopjopper Feb 06 '16 at 09:35