PDA

View Full Version : flat shading with VBO



thinks
07-23-2009, 02:40 AM
I am rendering models read from files with flat shading using Vertex Buffer Objects (VBO). Now, most mesh file formats consist of a list of vertices, followed by a list of faces, where each face consists simply of small set of vertex indices (3 indices per face for a triangle mesh). This is in many ways convenient for most tasks, as the vertices can be parsed and things like normals and tex-coords can be computed, and the face-indices can be parsed in a similar fashion. All this can then be uploaded to a VBO and rendered without any problems.

Now, in my application I want to use flat shading. By that I mean that shading is computed on a per face (i.e. triangle) basis. My understanding is that when using VBO's one must supply normal and tex-coord information on a per vertex basis, so in my case I need to duplicate vertices that are used in two or more faces, is this correct? What this means is that I will always end up having 3*num_tris number of vertices in my vertex buffer, and 3*num_tris indices in my index buffer.

I am wondering if there is any work-around to this problem. My geometry data is static, so I could just go for a display list and let the driver optimize it, but since I like to think that the future should be embraced I would prefer working with buffers of some sort. As an additional note I am using a vertex and a fragment shader in my rendering, so setting glShadeModel(GL_FLAT) probably won't work, or would it?

Thanks,

T

dletozeun
07-23-2009, 03:01 AM
Since the normal attributes vary for each face index, you have to build and index array which is indexing the normal array and consequently you have no choice but duplicate all remaining attributes like vertex position, uv,...

glShadeModel just enable or disable color interpolation between vertex colors in fixed pipeline. Remember that fixed pipeline performs gouraud shading and not phong shading. Without shader there are no normal interpolation but color interpolation.

thinks
07-23-2009, 03:10 AM
Since the normal attributes vary for each face index, you have to build and index array which is indexing the normal array and consequently you have no choice but duplicate all remaining attributes like vertex position, uv,...

This is what I suspected. Thanks for confirming.


glShadeModel just enable or disable color interpolation between vertex colors in fixed pipeline. Remember that fixed pipeline performs gouraud shading and not phong shading. Without shader there are no normal interpolation but color interpolation.

Yes, in my case I don't want any interpolation on the face so I need to make sure of this myself in the shader-pair. Thus, because I can only "see" (process) one vertex at a time I need to make sure that the color is computed in the same way for each vertex of a face, hence the duplication. This may have consequences for positional lighting, but I can use directional lighting for now.

Ilian Dinev
07-23-2009, 04:10 AM
you can use a dFdx() trick to compute the triangle's normal inside a frag-shader. There's also the "varying vec4 tmp:flat" interpolation-modifier, for SM4.0 cards.