## Recommended Posts

Hi I'm currently reading the book more OpenGL game programming. I have some basic questions about the shaders which haven't been discussed in that book. We know that the equation for OpenGL matrices is: Projection matrix * View matrix * Model matrix * local vertex position = final vertex position; So when a vertex shader is applied to a vertex?For example before all these matrices are applied in a program? Another question is that what's the meaning of "fragment" in a fragment shader? Is it simply a pixel or is it a triangle for example? When it is applied to a program? ( before and after stages in a fragment processing ) Thanks

##### Share on other sites
A vertex shader typically takes a vertex's local coordinates and any model/view/projection matrices and applies the matrices to the vertex, and then emits the projection space coordinates. You don't typically apply these matrices in your program, although you can concatenate the model, view, and projection matrices in the program to reduce the number of operations in the vertex shader.

A fragment is a pixel (or texel). Once all of the vertices of a primitive are transformed through the vertex shader, the primitive is rasterized, and each pixel is sent to fragment shader for coloring. The fragment shader emits a color for the pixel, which is then written into the framebuffer or whatever target you are rendering to.

##### Share on other sites
I have still problem with fragment shader :(
The subject is complex and I need to know what stages are involved in the rasterization process .
Assuming that we have 2 triangles and one of them is in front of the other, What stages are involved in the raster level?For example, does it convert the first triangle to 40 pixels and the second one to 60 pixels ?(values are just optional for my example) We know that z buffer is done after fragment shader, so I guess these triangles are still available after raster level?Does fragment shader process all the primitives at once and send them to the fragment shader or does it converts each primitive( for example that triangle) to pixels and sends it to the fragment shader, then processes the next one and sends it to the fragment shader, etc ?
Assuming that a triangle consists of 40 pixels after raster level, does it mean that the fragment shader processes all these 40 pixels?

##### Share on other sites
Quote:
 We know that z buffer is done after fragment shader, so I guess these triangles are still available after raster level?

After rasterization the original primitives are gone. The z-buffer has a value for each pixel and is compared per-pixel, not per triangle.

Quote:
 Does fragment (do you mean vertex?) shader process all the primitives at once and send them to the fragment shader or does it converts each primitive( for example that triangle) to pixels and sends it to the fragment shader, then processes the next one and sends it to the fragment shader, etc ?

I would imagine that the GPU does not wait for every primitive to be rasterized before sending them to the fragment shader, its just a pipeline and they are processed as soon as they are available. Keep in mind there is not just one "vertex or fragment shader" in the GPU, there are hundreds that are cranking out pixels in parallel. I think your question is implementation dependent, and I can't imagine any good reason why you would want to have this information. If you would elaborate on what your problem is maybe someone could help you.

The OpenGL Machine

Unfortunately it is only from version 1.1, I could not find a digital copy of the most recent one. I have a physical copy of the version 3.1 machine, and the operations after the Fragment shader look pretty similar.

If you're talking about fragment shaders, just imagine a box around the Texel Generation, Texture Application, and Fog components. This represents what is now the fragment shader. Everything after fragment shader starting with "Coverage (antialiasing) Application" looks pretty much the same as the 3.1 poster.

##### Share on other sites
OK,
1)What about the varying variables? Assuming that we have only 3 vertexes and compute a varying variable for each vertex, how does a fragment shader understand that which varying variable must be used for which fragment?We have only 3 vertices, but the rasterization may generate multiple pixels.
2)When the output of the vertex shader is sent to the fragment shader?For example when it processes a primitive of type "type" given by glBegin(type)?

##### Share on other sites
Quote:
 1)What about the varying variables? Assuming that we have only 3 vertexes and compute a varying variable for each vertex, how does a fragment shader understand that which varying variable must be used for which fragment?We have only 3 vertices, but the rasterization may generate multiple pixels.

These are interpolated across the triangle. If one vertex has a varying value of zero, and another has a value of 1, then the pixels between those vertices will be in the range from zero to one.

Quote:
 2)When the output of the vertex shader is sent to the fragment shader?When it processes 3 vertexes that creates a triangle?

The vertices must go through primitive assembly before being rasterized, so you would need to wait at least until all vertices of a primitive are through the vertex shader.

##### Share on other sites
So when we use per fragment lighting, each fragment deals with its own interpolated varying variables. Here I have another question. If the fixed function is able to interpolate the color values of vertexes and send them to fragments, why do we need to specify color for a fragment inside a fragment shader? Just to change the behavior of the fixed function color interpolation?

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627667
• Total Posts
2978532
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 10
• 12
• 22
• 13