OpenGL pipe along the way

So, I am playing with OpenGL and trying to figure out how to draw funny shapes.

Right now, I'm working on a pipe. I can just draw a straight tube:

void tube(GLfloat radius, GLfloat segment_length) {
    glPolygonMode(GL_BACK, GL_NONE);
    glPolygonMode(GL_FRONT, GL_FILL);

    glPushMatrix(); {
        GLfloat z1 = 0.0;
        GLfloat z2 = segment_length;

        GLfloat y_offset = 0.0;
        GLfloat y_change = 0.00;

        int i = 0;
        int j = 0;
        for (j = 0; j < 20; j++) {
            glPushMatrix(); {
                glBegin(GL_TRIANGLE_STRIP); {
                    for (i = 360; i >= 0; i--) {
                        GLfloat theta = i * pi/180;
                        GLfloat x = radius * cos(theta);
                        GLfloat y = radius * sin(theta) + y_offset;

                        glVertex3f(x, y, z1);
                        glVertex3f(x, y, z2);
                    }
                } glEnd();
            } glPopMatrix();

            // attach the front of the next segment to the back of the previous
            z1 = z2;
            z2 += segment_length;

            // make some other adjustments
            y_offset += y_change;
        }
    } glPopMatrix();
}

However, I did not understand how to get the handset to follow any predetermined path, like a spiral, or even a simple line. If you change y_change to something like 0.01, it will shift each pipe segment segment by 0.01 in the y direction. This is great, but how can I make each point of the segment in this direction? In other words, now each segment is drawn so that all of them are directed in the same direction, and the direction is not the direction of the tube (since with y_change = 0.01 the direction is slightly "up").

, . , , , .

+5
3

, .. n- n + 1- . : ", ?"

, . , , t, , . , :

path(t): R → R³, t ↦ ( a·sin(k·t), b·cos(k·t), c·t )

, - , , . :

tangent(t): R → R³, t ↦ ( k·a·cos(k·t), -k·b·sin(k·t), c ) = d/dt path(t)

, , , t .

, 3d-. , , , , :

normal(t): R → R³, t ↦ ( -k²·a·sin(k·t), -k²·b·cos(k·t), 0 ) = d/dt tangent(t) = d²/dt² path(t)

.

, , . , .

, , , t , . (t). , P_n x-y, :

for t in [k..l]:
    for p in P_n:
        yield_vertex( path(t).x + binormal(t).x * p.x, 
                      path(t).y + normal(t).y * p.y, 
                      path(t).z )

, , OpenGL, - , . , , , , .

+17

.

+1

All Articles