I am trying to roll my own lighting shader, and I was wondering how you go about defining normals per-face vs per-vertex. My test geometry is a simple cube, so currently I have a normalized vector pointing away from the center of the cube for each normal, a total of 8 normals to go with the 8 vertices.
So: how would you go about defining and using per-face normals for flat shading?
mooglinux - there is no such thing as a “face” in Codea, only vertices. There are two triangles per side, so there are 2 x 3 x 6 = 36 vertices in total (not 8 - you are referring to corners, I think).
You have to define a normal for each vertex. So each cube face has 6 vertices, and all of them will have the same normal.
The 3D demo project has example code for a cube, although it doesn’t include normals.
If this isn’t clear, please say. I’m not sure how much you know already.
I’ve been basing my experiments off of the 3D demo project. And what that code does is create a list of 8 vertices. Then, it constructs the triangles from those 8 vertices. Which is how I have seen it done in various books I’ve browsed about 3D graphics programming with OpenGL: make one array of the vertices themselves, and then another array of pointers to those vertices in the correct order to construct the triangles.
Or does Codea not use vertex lists at all?
@mooglinux - yep, Codea uses vertex lists. Your “array of pointers” will consist of 36 vertex positions (made up of the original 8 positions, of course).
Then you need to create an array of normal vectors, one for each of the 36 vertices, and in the same sequence, and assign it to the normals property of the mesh.
Okay, I have assigned normal vectors the same way the vertices have been assigned, which seems correct. Now my shader does draw the cube correctly. (Previously when I included anything with the normals in my fragment shader, the cube did not appear at all)
When I try to make it lit, the cube shows up as completely black. But that is an issue with the actual shader, not the normals. I think.
@mooglinux - you probably have an error in the shader code. If you share your code, maybe we can help
I started a new thread for the shader code.