I’ve just spent a happy hour writing some code to take the array of vertices and generate the surface normals perpendicular to each face at whatever angle, intended for situations where I’ve designed simple 3d shapes by calculating and inputting the vertices by hand.
It works very well, takes a vector reference and populates it. However it did occur to me that there might be tools with OpenGL libraries to do this already? Did I re-invent the wheel? Is there an easier way?
the wheel is already there, its called “glm”
you just need to know what to do with that wheel …
vec3 normal = normalize(cross(A, B)) … would be an example direction to “roll” that wheel along ^^
Yes, I’m using glm cross and normalize in my code.
My code takes the vertice array, strips away the other stuff like texture coords, color etc.
Then it find the two sides and applies the cross product and normalize on them.
Then it creates a new array with the surface normals, and if required it can stuff them back into an array with the original array data.
I just felt this was such a fundamental requirement that it must have already been done.
Note that GLM has
triangleNormal in glm/gtx/normal.hpp.
Anything that operates on an entire mesh is going to be dependent upon the format of the mesh and so isn’t going to exist outside of a library which deals with the particular mesh format (OpenGL is a rendering API, not a modelling library).
triangleNormal - that’s the kind of thing I was looking for. Taking the triangle indices would have saved me the step of subtracting to calculate two sides and concerning myself with cross product order to ensure the right direction on the normal. Thank you, that’s answered the question.
As for operating on an entire mesh, an example would be glVertexAttribPointer which sorts out the various components, so it should be possible to supply format hints in parameters.
I am going to create a function to do this as an exercise, I don’t see it as much practical use because I think normals come for free in obj files.