How to obtain smoothed normals when extruding a 2d curve (with parametric normals) into 3d?

  3d, c++, opengl

I’m extruding a sine-wave curve into 3d but when rendering, I can see that the normals are not smoothed.

The sine-wave is generated with parametric normals, as follows:

vector<CurvePoint> sineWave(int n, float x0, float y0, float step, float period)
{
    vector<CurvePoint> curve;

    for (int i = 0; i < n; i++) {
        float a = TWO_PI / period;
        float x = x0 + i * step;
        float y = y0 - sinf(x * a);
        float c = cosf(x * a);
        auto normal = glm::vec2(a * c, 1) / sqrtf(a * a * c * c + 1);

        curve.emplace_back(glm::vec2(x, y), normal);
    }

    return curve;
}

The extruding method:

void extrude(IndexedVertexBatch<XYZ.N> &batch, const Matrix &matrix, const vector<CurvePoint> &curve, GLenum frontFace, float distance)
{
    auto size = curve.size();
    if (size > 1 && distance != 0) {
        bool cw = ((frontFace == CW) && (distance > 0)) || ((frontFace == CCW) && (distance < 0));

        for (auto i = 0; i < size - 1; i++) {
            auto &p0 = curve[i].position;
            auto &p1 = curve[i + 1].position;
            auto normal = matrix.transformNormal(glm::vec3(curve[i].normal, 0));

            batch
                .addVertex(matrix.transformPoint(p0), normal)
                .addVertex(matrix.transformPoint(p1), normal)
                .addVertex(matrix.transformPoint(glm::vec3(p1, distance)), normal)
                .addVertex(matrix.transformPoint(glm::vec3(p0, distance)), normal);

            if (cw) {
                batch.addIndices(0, 3, 2, 2, 1, 0);
            } else {
                batch.addIndices(0, 1, 2, 2, 3, 0);
            }

            batch.incrementIndices(4);
        }
    }
}

The rendering (phong-like shading):

enter image description here

How can I obtain smoothed normals?

Source: Windows Questions C++

LEAVE A COMMENT