Can I use a vertex shader to display model normals?

I am currently using VBO for the texture coordinates, normals and vertices of the model (3DS) which I draw with "glDrawArrays (GL_TRIANGLES, ...);". For debugging purposes, I want to (temporarily) show the normals while drawing my model. Should I use immediate mode to draw each line from vert to vert + normal -OR- fill another VBO with vert and vert + normal to draw all normals ... -OR- is there a way for the vertex shader to use the vertex and normal data has already passed when drawing the model for the V + N computation used when drawing the normals?

+2


source to share


3 answers


No, it is not possible to draw additional lines from the vertex shader.

The vertex shader is not about creating geometry, but about computing the vertex. Using vertex shaders when you say glDrawArrays(GL_TRIANGLES,0,3)

it is what exactly determines what you draw, i.e. One triangle. After the processing reaches the vertex shader, you can change the properties of the vertices of that triangle, rather than altering in any way the shape or shape, topology and / or amount of geometry.



What you are looking for is what OpenGL 3.2 defines as a geometry shader , which allows you to output an arbitrary geometry number / topology from the shader. Note, however, that this is only supported by OpenGL 3.2, that there are not many cards / drivers now (it has been for several months).

However, I must point out that displaying normals (in most engines supporting some kind of debugging) is usually done with traditional line rendering, with an additional vertex buffer that is filled with the appropriate positions (P, P + C * N) for each grid position, where C is a constant representing the length you want to use to display normals. It's not that hard to write ...

+6


source


You can get close to this by drawing the geometry twice. Once you draw it as usual. The second time draw the geometry as GL_POINTS and attach a vertex shader that offsets each vertex position along the normal vertex.

This will cause your model to have many points floating above the surface. Each point will show the direction of the normal from the vertex it corresponds to.



It's not ideal, but it might be enough, depending on what you're hoping to use it for.

UPDATE: AHA! And if you pass a constant scaling factor to a vertex shader and ask your application to interpolate that factor between 0 and 1 over time, your points rendered by the vertex shader will animate over time, starting at the vertex they are applied to and then resetting towards its normal.

+1


source


You can probably get a more or less correct effect with a cleverly written vertex shader, but it will be a lot of work. Since this is still for debugging purposes, it seems best to just draw a few lines; the performance hit won't be severe.

0


source







All Articles