2
votes

For lighting I need the normal vectors in the fragment shader. The tutorials I read ask me to provide the normal data alongside with the vertices of a model. But aren't the normals already given by the faces specified by the vertices?

For calculating the normals I need all the three vertices of a triangle. But as far as I know we can only access a single vertex in each pass of the vertex shader.

How can I compute the normal vectors for each pixel based on the vertex data? I want to apply normal maps after that but that comes later and is not really related to this question.

1

1 Answers

2
votes

But aren't the normals already given by the faces specified by the vertices?

Yes, and no. Technically you could determine the normal per face, but usually the artist who designs a 3D model in a modeller wants to fine tune the normals to tweak lighting calculations.

normal vectors for each pixel based on the vertex data?

Just pass the normals as a varying variable (varying or out keyword, depending on the GLSL version) to the fragment shader. The normal vector will be interpolated barycentrically. You can then normalize it to unit length in the fragment shader.