PDA

View Full Version : Problem with drawing a 3D spiral

Norten
03-31-2014, 05:29 AM
Hi i have this problem i was trying coding in OpenGL and i was able to get this Line strip spiral:

gl.glLineWidth(3);
gl.glBegin(GL2.GL_LINE_STRIP);

float x,z;
float y = -10;
float angle;
int numberOfSpirals = 6;
float distance = 0.2f;

for (angle = 0.5f; angle <= (Math.PI * 2.16f * numberOfSpirals); angle += distance) {
x = (float) Math.sin(angle) * 5;
z = (float) Math.cos(angle) * 5;
gl.glColor3f(0.7f, 0.7f, 0.7f);
gl.glVertex3f(x, y, z);
y += 0.05;
}
gl.glEnd();

this code is working just fine but i want to draw spiral built from quads or triangles to make it look like real spiral.
Best i am able to do is this :

float x,z;
float y = -10;
float angle;
int numberOfSpirals = 6;
float distance = 0.2f;

for (angle = 0.5f; angle <= (Math.PI * 2.16f * numberOfSpirals); angle += distance) {
x = (float) Math.sin(angle) * 5;
z = (float) Math.cos(angle) * 5;
gl.glColor3f(0.7f, 0.7f, 0.7f);
gl.glVertex3f(x-0.5f, y-0.5f, z-0.5f);
gl.glVertex3f(x+0.5f, y+0.5f, z+0.5f);
y += 0.05;
}
gl.glEnd();

but then spiral is flat and not circle in cross-section.
Does anybody have a clue how to make it looks like real spiral using this code? Thanks a lot.

03-31-2014, 09:14 PM
You'll want to build a surface to describe the spiral. Picture a very long, thin cylinder bent in the shape of the spiral. Or in other words, the points that describe your surface will be located on small circles centered on your spiral curve, with the plane of these small circles being orthogonal to the curve. Once you calculated these surface points, build a set of triangle meshes to connect them.

It will require some reasonably straightforward math and geometry (derivative of the parametric representation of the spiral, a cross product, etc). Untested from notes on paper, the pseudo code will look something like the following. r1 is the radius of the spiral, p a parameter controlling the pitch, nTurns the number of turns, and r2 the radius of the cross-section.

v2scale = 1.0 / sqrt(r1 * r1 + p * p)
for alpha in 0 ... nTurns * 2.0 * PI
p0 = (r1 * cos(alpha), r1 * sin(alpha), p * alpha)
v1 = (cos(alpha), sin(alpha), 0.0)
v2 = v2Scale * (p * sin(alpha), - p * cos(alpha), r1)
for beta in 0 ... 2.0 * PI
surfacePoint = p0 + r2 * cos(beta) * v1 + r2 * sin(beta) * v2
end for
end for

Then build triangle meshes from these points. Probably easiest to create one strip for each small "cylinder" between two of these circles, and either using a separate draw call for each of these strips, or use something like primitive_restart to draw them all in a single call.

tonyo_au
03-31-2014, 09:15 PM
I assume you mean you wand to draw a ribbon in a spiral. This is much more complex than what you have coded. First look at creating a ribbon in a straight line.

|\ |\ |
| \| \|

now look at displacing each vertex along the spiral function