Skip to main content
gamedev.net
🔒 Locked

Torus Texturing

Started by Prebulator 1 reply 700+ views
Original Post
Prebulator
Prebulator
Hi guys, I had a look about 15 pages back and could not find any help, also the search engine is under repairs so apologies if this has been answered previously. I can put a provided texture on a cube but cannot get my head around how to load and wrap a bitmap texture around a torus. From the tuts I have looked at the co-ords will be dynamically generated. Solutions/hints in general will be gratly appreciated, solutions/hints in GL4Java will be forever immortalised. ;-) My Torus code is below. void torus(float A, float B, float C, float D, int nsides, int rings){ int i,j; float u; //u is angle of rotation around z-axis float v; //v is the angle parameter for an ellipse float du, dv; //size of the increment of u and v float cosu, sinu; float cosudu, sinudu; //cosudv = cos(u+du) float cosv, sinv; float cosvdv, sinvdv; du = 2.0f * M_PI / nsides; dv = 2.0f * M_PI / rings; //loops to go here //outer loop iterates over u u = -M_PI; for (i = nsides - 1; i >= 0; i--) { cosu = (float)Math.cos(u); sinu = (float)Math.sin(u); cosudu = (float)Math.cos(u+du); sinudu = (float)Math.sin(u+du); //the inner loop iterates over v v = -M_PI/2; for (j = rings; j >= 0; j--) { cosv = (float)Math.cos(v); sinv = (float)Math.sin(v); cosvdv = (float)Math.cos(v+dv); sinvdv = (float)Math.sin(v+dv); //vertices will be evaluated here gl.glBegin( GL_QUADS ); gl.glNormal3f((D + A*cosv)*cosv*cosu, (D + A*cosv)*cosv*sinu, (D + A*cosv)*sinv); gl.glVertex3f((D+A*cosv)*cosu, (D+A*cosv)*sinu, A*sinv); gl.glVertex3f((D+A*cosv)*cosudu, (D+A*cosv)*sinudu, A*sinv); gl.glVertex3f((D+A*cosvdv)*cosudu, (D+A*cosvdv)*sinudu, A*sinvdv); gl.glVertex3f((D+A*cosvdv)*cosu, (D+A*cosvdv)*sinu, A*sinvdv); gl.glEnd(); v += dv; } u += du; } } Cheers and Thanks.
Sully
Sully
We can define a torus by the equations:

[Note: p = Pi = 3.141592. Define q as the angle from the X axis (0 <= q <= 2p) and f as the angle from the Z axis (0.0 <= f <= p).]



Then, with R as the inner radius and r as the outer radius:

X = (R + r cos (2 pv)) cos (2 pu)
Y = r sin (2 pv)
Z = (R + r cos (2 pv)) sin (2 pu)

Then:

v = arccos (Y/R)/2p
u = [arccos ((X/(R + r*cos(2 pv))]2p

Hope this helps,
-Ron

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.