0

I am using this website on how to compute a hermite curve under the heading The Math in Matrix Form to make a hermite curve.

Here is my code so far...

// calculate hermite curve
    GLfloat S[4][1];
    GLfloat C[4][3] = {{height, 0, 0},
                       {0, radius, 0},
                       {0, 2 * radius, 0},
                       {-2 * height, 0, 0}};
    GLfloat h[4][4] = {{ 2,-2, 1, 1},
                       {-3, 3,-2,-1},
                       { 0, 0, 1, 0},
                       { 1, 0, 0, 0}};

    unsigned int rows;
    unsigned int cols;
    float val;
    for (int x = 0; x < l_vertices; x++) {
        float segment = (float)x / l_vertices;
        S[0][0] = pow(segment, 3);
        S[1][0] = pow(segment, 2);
        S[2][0] = pow(segment, 1);
        S[3][0] = 1;
        GLfloat midwayArray[4][4] = {{ 0, 0, 0, 0},
                                     { 0, 0, 0, 0},
                                     { 0, 0, 0, 0},
                                     { 0, 0, 0, 0}};
        GLfloat finalArray[4][1] = { 0, 0, 0, 0};
        rows = 4;
        cols = 4;
        for (unsigned int i = 0; i < rows; i++) {
            for (unsigned int j = 0; j < cols; j++) {
                for (unsigned int k = 0; k < 1; k++) {
                    val = S[i][k] * h[k][j];
                    midwayArray[i][j] += val; 
                }
            }
        }

        rows = 4;
        cols = 1;
        for (unsigned int i = 0; i < rows; i++) {
            for (unsigned int j = 0; j < cols; j++) {
                for (unsigned int k = 0; k < 4; k++) {
                    val = midwayArray[i][k] * C[k][j];
                    finalArray[i][j] += val;
                }
            }
        }
        profileCurve[0][x] = finalArray[0][0];
        profileCurve[1][x] = finalArray[1][0];
        profileCurve[2][x] = finalArray[2][0];
    }

But I am getting mainly zeros for some reason. I think my matrix multiplication is not being done correctly.

Also, just some information on how my code currently works. The first inner nested for loop is multiplying S and h just like the website says. Then the second inner nested for loop is multiplying the result of the previous matrix multiplication with C just like the website says.

I think assign the result coordinates of the final array for that iteration of x to my profileCurve array which is going to store all the coordinates that make up the hermite curve.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Ogen
  • 6,499
  • 7
  • 58
  • 124
  • You are addressing the rows and columns in a weird order for software that is supposed to be OpenGL-based. However, you are *consistently* doing it in row-major order in your code, so assuming you never try to pass the results of any of this to OpenGL as a Matrix you should be fine. Is `profileCurve` supposed to be a matrix? If so, you might need to transpose it to make GL happy. – Andon M. Coleman May 08 '14 at 23:12
  • Im sorry I dont completely understand what you're saying. (I'm a complete beginner at openGL). `profileCurve` is `[3][10]` in this case. It's just the `x, y, z` coordinates for every point on the curve. What do you mean by weird order. How can I fix it to be *GL happy?* – Ogen May 09 '14 at 08:46
  • OpenGL stores matrices in [column][row] format. But since these matrices are never sent to GL, it does not matter. – Andon M. Coleman May 09 '14 at 11:30

0 Answers0