0

I've created a surface of revolution and need to calculate the per vertex normals to pass into my vertex shader. I'm not sure where I'm going wrong:

    for(int i = 0, j = 0; i <= c && j <= c; i += 3, j += 9){
        GLfloat point[] = {vp1[i], vp1[i + 1], vp1[i + 2], 1.0};
        multiply(scale1, point, result);
        vp[i] = result[0];
        vp[i + 1] = result[1];
        vp[i + 2] = result[2];

        GLfloat pointC[] = {vp1[j], vp1[j + 1], vp1[j + 2], 1.0};
        GLfloat pointA[] = {vp1[j + 3], vp1[j + 4], vp1[j + 5], 1.0};
        GLfloat pointB[] = {vp1[j + 6], vp1[j + 7], vp1[j + 8], 1.0};
        GLfloat vec1[] = {pointA[0] - pointC[0], pointA[1] - pointC[1], pointA[2] - pointC[2]};
        GLfloat vec2[] = {pointB[0] - pointC[0], pointB[1] - pointC[1], pointB[2] - pointC[2]};
        crossProd(vec1, vec2, normal);
        float mag = sqrt((normal[0]*normal[0]) + (normal[1]*normal[1]) + (normal[2]*normal[2]));

        normals[j + 0] = normal[0] / mag;
        normals[j + 1] = normal[1] / mag;
        normals[j + 2] = normal[2] / mag;

        normals[j + 3] = normal[0] / mag;
        normals[j + 4] = normal[1] / mag;
        normals[j + 5] = normal[2] / mag;

        normals[j + 6] = normal[0] / mag;
        normals[j + 7] = normal[1] / mag;
        normals[j + 8] = normal[2] / mag;
    }

This code is adding my vertices to vp and then trying to calculate the normal. My surface definitely doesn't look right when rendered.

I think I'm generating the surface normal from everything I've read so far, but I'm not sure how to turn my triangle soup into per-vertex normals.

Edit: To a commenter below, three successive indices make 1 point. 9 successive indices make a triangle.

Edit 2: How do I convert this into per-vertex normals?

quela
  • 81
  • 6
  • Do three successive vertices `vp1[i], vp1[i + 1], vp1[i + 2]` form a triangle in your layout? Explain how the mesh is indexed. – wcochran Apr 15 '20 at 20:23
  • @wcochran Edited! – quela Apr 15 '20 at 20:25
  • This looks ok so far, and would be the correct way to generate face-based normals. What is the issue you are having? – Nathan Wride Apr 15 '20 at 20:30
  • @NathanWride I'm trying to calculate the per-vertex normals, but I'm not sure where to go from here. I've edited my post to clarify my question. – quela Apr 15 '20 at 20:32
  • So 'i' is traversing a vertex at a time (`i += 3`) whereas `j` is traversing a whole triangle at a time (` j += 9)`? The vertex at i` is scaled, but future vertices have yet to be scaled? – wcochran Apr 15 '20 at 20:41
  • @wcochran Correct. I'm saving the ```i``` vertices into an array to pass into the vertex shader. Should I be scaling everything before computing normals? That sounds important. – quela Apr 15 '20 at 20:42
  • 1
    Yes -- it looks like only the first vertex is scaled whereas the other are not -- this will definitely alter the normal -- other than that this code is ok. Instead of doing flat surface (i.e., a triangle at a time) it is better to use something that average neighbor normals to approximate a smooth surface. There are a variety of algorithms for this. – wcochran Apr 15 '20 at 20:51
  • 2
    To smooth the normal, you need to take each vertex and find the average normal of the connected faces (any face that the vertex is part of) – Nathan Wride Apr 15 '20 at 21:03
  • @quela What you actually do is computing face normal vectors. – Rabbid76 Apr 16 '20 at 05:24

0 Answers0