# Thread: Wireframe 3D engine problem

1. ## Wireframe 3D engine problem

hi,

i coded a small wireframe engine, but i am having some problems with more complex forms, like toroids.

with simple forms, like cubes and piramids, there are no problem.

but with the toroid, it get distorted at certain angles. i calculate the toroid as follow:

Code:
```#define DegToRad 0.017453293f

//create toroidal object
toroide=create_object();
int c=0;

//walk in 45 degrees steps
for(int j=0;j<360;j+=45)
{
//calculate circle position relative to toroid body

//walk in 20 degrees steps
for(int i=0;i<360;i+=20)
{
//build toroidal face
}

//link the vectors generated in step above, forming a a circular poligon
for(int i=1;i<=360/20;++i)
{
}
c+=360/20;

//generate next toroid face
}

//link each face to others faces
for(int i=1;i<=360/20;++i)
{
int s=i;
//vector 1 is linked to vector 1+(number of vectors in face * face), till form a ring
for(int j=1;j<=360/20;++j)
{
int r=(j==360/20)?i:i+(j*(360/20));
s=r;
}
}```
the problem, seens to me, is in the xx and yy calculation. but also can be in my vector_to_2d() function.

Code:
```void vector_to_2d(float x,float y,float z,DWORD *x0,DWORD *y0)
{
*x0=(DWORD)((MAX_X/2)+(x*((MAX_Z/2)/((MAX_Z/2)-z))));
*y0=(DWORD)((MAX_Y/2)+(y*((MAX_Z/2)/((MAX_Z/2)-z))));
}```
my MAX_X and MAX_Y are 640 and 480, respectively. as MAX_Z, i tried several values, ending with 500. all values for MAX_Z show the distortion problem, but with 500 the problem is smaller.

i dunno, but maybe the MAX_Z distortion is not related to my main distortion problem, but only a effect of perspective.

below i attach 2 pictures: 1 angle where it look right, and other wrong.

any suggestion in how correct the problem?

jmgk

ps: sorry for my bad english: i fell it is a barrier to describe my problem and my algo :-( 2. Does the Toroid rotate? If so, does the toroid stay distorted, or does it fix itself after rotating some more? 3. skorman00,

it rotate. the jpgs attached are screen captures at different moments of the rotation.

at some angles, the distortion dissapear (or better, isnt noticeable). but then reapper.

i was thinking about the problem, and maybe it is when i render the "walk in 20 degrees steps" parts: the radius is always 100, but maybe it should change based on xx or yy variables.

thanks for the interess, skorman00... i was afraid nobody would answer my call for help

jmgk 4. when you rotate, are you recalculating all the vertices, or applying a rotation value or matrix of sort?

What I'm trying to figure out is this: Are you calculating the verts wrong, or just rendering them incorrectly. 5. skorman00,

i recalculate all the vertices, and use the pure float value each time (i convert the 3d floats to 2d ints just to draw them on screen, and throw away after), so, i guess, is not a rounding problem.

the problem, seens to me, is how i calculate the vertices... seens to me i dont take in account the radius variation in each circular group of vertices (its hard to explain what i mean in my broken english )

jmgk 6. If you are creating the torus correctly then rotation, translation and other transformations will NOT affect the vertices unless you only transform a portion of them. This would mean that a portion of the vertices are staying in local space while the other's are being transformed into camera space. If the original torus vertices are correct there is NO POSSIBLE way that they can be messed up by transformations if you are running all of them through the same transformations. The worst that can happen is that your entire model will behave erratically, albeit, it will still be a coherent model of a torus.

If you are transforming each vertex and NOT storing the original model vertices in model space then you are subjecting your model to multiple rounding errors.

3D works by continuously transforming a model from local/model space to world space to view space to camera space to clip space to screen space.

So at the start of the transform, you are always in local space. Unfortunately you cannot transform once and then alter the vertices later down the pipeline. You must start with local space vertices.

Also remember rotations always happen around the origin. So if you translate to some 3D point in space prior to rotating, you have essentially moved the origin to that 3D point in space. Your model will now rotate around that point. The order of transformations is extremely important.

You cannot create vertices in world space and then do operations on them. Transforms always start in model space and transform progressively down the pipeline.

Your problem is re-computing the vertices. 7. bubba,

i´m afraid not. i do exaclty as you say i should.

i create the 3d objects in form of x,y,z vertices and lines linking the vertices. they are abstract representation, and only come to form when i draw then on screen.

after i draw them to screen, i throw them away. the rotation, in next step, is calculated once again over the abstract representation, converted to 2d, and then draw. and so on...

the object is in model space, rotated there, and then converted each time to view space.

as you said, the problem is in the torus generation. (i reach this conclusion coz i did a cube, a piramid, a tube and even a sphere, and they rotate fine)

seens i dont take in account the radius variation in the torus body caused by the z effect (i need learn more english and geometry - i cant name the things right, and coz this i cant express myself )

in short: the original torus vertices ARENT correct. and i dont know how make them correct

thank for the help - i guess my lack of geometric and english terms would let anyone mad,
jmgk

ps: i was thinking, and i guess i am using the wrong approach to generate the torus... i draw a ring in the x,y plane, then change z, and draw other, and so on, till the form a circle (and the bug is i dont take in account x,y radius variation in the process). i guess i should draw a small ring in z,y plane, change x (and y) in circular form, then draw other small ring, and so on, till i have a torus. seens more logical 8. Im lookin at your torus and it appears that the circle rings around the torus are all on the same plane. Thats why it looks alright at one angle but not the other. 9. jeremy,

and you know how i should be calculating it?

jmgk 10. perhaps this will help,
http://mathworld.wolfram.com/Torus.html Popular pages Recent additions 