glCullFace using Interleaved VAO and VBO

Started by Knokk, January 26, 2014, 01:52:54

Previous topic - Next topic

Knokk

I'm playing around with VAO's and VBO's (interleaved). I'm using a standard vertex and fragment shader to do basic vertex positioning and color passing.

Consider my vertex shader:
#version 150 core

uniform mat4 projection;
uniform mat4 view;

in vec4 in_Position;
in vec4 in_Color;

out vec4 pass_Color;

void main(void)
{
    gl_Position = projection * view * in_Position;
    pass_Color = in_Color;
}

This is very basic! we do some attribute work with in_Position and in_Color and pass in_Color to the fragment shader as pass_Color.

My question is, how would I do this with normals? I know how to send the data to the shader, that's not the problem. The problem is getting (for example) back face culling to work with the normal data I'm sending to the shader. Everyone knows how to do it in immediate mode:
glNormal3f();

But I'm using VAO's and VBO's:
glVertexAttribPointer();

For example, I can do lighting without openGL ever knowing what a vertex's normal value is because I can do all the calculations in shaders. However, for glCullFace(GL_BACK) to work, openGL needs to know what the normal value is. Once again I'm using VAO's and VBO's and I'm sending vertex, color, and normal data using glVertexAttribPointer. What is the best way to go about this?

spasi

OpenGL does not use normals to determine if a face must be culled or not. Normals is per vertex information (different vertices of the same face can point to different directions), whereas culling must happen per face. From the spec: "The first step of polygon rasterization is to determine if the polygon is back-facing or front-facing. This determination is made based on the sign of the (clipped or unclipped) polygon’s area computed in window coordinates.". The test against the sign of this calculation is controlled using glFrontFace, with the default being GL_CCW, which basically means you should submit your vertices in a consistent counter-clockwise order.

Knokk

Huh interesting. I always thought it was more complex than that... So in short, if I want something to work with back face culling, I just need to arrange the vertices in counter-clockwise order?

Knokk

It worked! I had a feeling I was forgetting something very simple... Thanks a bunch. :)