views:

320

answers:

3

I'm currently using a VBO for the texture coordinates, normals and the vertices of a (3DS) model I'm drawing with "glDrawArrays(GL_TRIANGLES, ...);". For debugging I want to (temporarily) show the normals when drawing my model. Do I have to use immediate mode to draw each line from vert to vert+normal -OR- stuff another VBO with vert and vert+normal to draw all the normals… -OR- is there a way for the vertex shader to use the vertex and normal data already passed in when drawing the model to compute the V+N used when drawing the normals?

A: 

It's probably possible to get more or less the right effect with a cleverly written vertex shader, but it'd be a lot of work. Since this is for debugging purposes anyway, it seems better to just draw a few lines; the performance hit will not be severe.

David Seiler
After doing all the work to put all the textcoors, normals and verts into a VBO it just seems like a lot of work to do all that again just to see the normals… We're not talking a few lines… more like 10K+. Hoping for an easier (temporary) solution… thanks for the response. ;-)
geowar
@geowar: 10k lines of source? What are you doing with your model that requires that much code? Anyway, I don't see why it matters; just pull the vertexes and normals out of the array you're passing to glDrawArrays(), enter line mode, and draw a bunch of lines.
David Seiler
not 10K lines of source; 10K+ data points already in the VBO. Using a geometry shader allows the VBO data to be reused to draw the normals (for debugging).
geowar
+3  A: 

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

A vertex shader is not about creating geometry, it is about doing per vertex computation. Using vertex shaders, when you say glDrawArrays(GL_TRIANGLES,0,3), this is what specifies exactly what you will draw, i.e. 1 triangle. Once processing reaches the vertex shader, you can only alter the properties of the vertices of that triangle, not modify in any way, shape or form, the topology and/or count of the geometry.

What you're looking for is what OpenGL 3.2 defines as a geometry shader, that allows to output arbitrary geometry count/topology out of a shader. Note however that this is only supported through OpenGL 3.2, that not many cards/drivers support right now (it's been out for a few months now).

However, I must point out that showing normals (in most engines that support some kind of debugging) is usually done with the traditional line rendering, with an additional vertex buffer that gets filled in with the proper positions (P, P+C*N) for each mesh position, where C is a constant that represents the length you want to use to show the normals. It is not that complex to write...

Bahbar
geometry shader: that's what I'm looking for… Thanks!
geowar
A: 

You could approximate this by drawing the geometry twice. Once draw it as you normally would. The second time, draw the geometry as GL_POINTS, and attach a vertex shader which offsets each vertex position by the vertex normal.

This would result in your model having a set of points floating over the surface. Each point would show the direction of the normal from the vertex it corresponds to.

This isn't perfect, but might be sufficient, depending on what it is you're hoping to use it for.

UPDATE: AHA! And if you pass in a constant scaling factor to the vertex shader, and have your application interpolate that factor between 0 and 1 as time goes by, your points rendered by the vertex shader will animate over time, starting at the vertex they apply to, and then floating off in the direction of its normal.

Tartley