Wireframe 3D engine problem

This is a discussion on Wireframe 3D engine problem within the Game Programming forums, part of the General Programming Boards category; hi, i coded a small wireframe engine, but i am having some problems with more complex forms, like toroids. with ...

  1. #1
    Code Ripper
    Join Date
    Jun 2004
    Posts
    30

    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
    		float xx=sin(j*DegToRad)*20;
    		float yy=cos(j*DegToRad)*20;
    
            //walk in 20 degrees steps
    		for(int i=0;i<360;i+=20)
    		{
    		    //build toroidal face
    			float x=sin(i*DegToRad)*100;
    			float y=cos(i*DegToRad)*100;
    			add_vector(toroide,x,y+yy,xx);
    		}
    		
    		//link the vectors generated in step above, forming a a circular poligon
    		for(int i=1;i<=360/20;++i)
    		{
    			add_line(toroide,c+i,((i==360/20)?c+1:c+i+1));
    		}
    		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));
    			add_line(toroide,s,r);
    			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 :-(
    Attached Images Attached Images   

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Does the Toroid rotate? If so, does the toroid stay distorted, or does it fix itself after rotating some more?

  3. #3
    Code Ripper
    Join Date
    Jun 2004
    Posts
    30
    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. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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. #5
    Code Ripper
    Join Date
    Jun 2004
    Posts
    30
    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. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    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. #7
    Code Ripper
    Join Date
    Jun 2004
    Posts
    30
    bubba,

    im 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
    Last edited by jmgk; 03-25-2006 at 08:41 AM.

  8. #8
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    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.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  9. #9
    Code Ripper
    Join Date
    Jun 2004
    Posts
    30
    jeremy,

    and you know how i should be calculating it?

    jmgk

  10. #10
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    perhaps this will help,
    http://mathworld.wolfram.com/Torus.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D rotation problem, help please!
    By GOBLIN-85 in forum Game Programming
    Replies: 22
    Last Post: 03-13-2009, 04:37 PM
  2. A 3D rotation problem
    By GOBLIN-85 in forum C++ Programming
    Replies: 0
    Last Post: 03-08-2009, 07:17 AM
  3. Small 3D Art Gallery I made
    By BobMcGee123 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-06-2005, 06:03 PM
  4. Question on 3D engine
    By kawk in forum Game Programming
    Replies: 4
    Last Post: 06-28-2005, 06:02 PM
  5. Lists for 3D Engine
    By Zeeshan in forum Game Programming
    Replies: 0
    Last Post: 10-20-2001, 11:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21