Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Creating 3d scene with correct objects in fore and backgroun
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jondmml




PostPosted: Sat May 12, 2007 4:42 pm   Post subject: Creating 3d scene with correct objects in fore and backgroun

I have been able to follow the directions of some of the major 3d tutorials and have created a cube that can have its 'camera' changed position and rotated in all three direcions, and zoomed in and out. However, when I create the sides of two cubes and place them near each other and overlap in a strange manner. I have found that the cubes appear correctly only when drawn in the correct order and if the cubes are rotated, this order oculd change.Is there any other way to make sure that this strange affect does not occur? If necessary I can post the code so you can see what's happening
Sponsor
Sponsor
Sponsor
sponsor
DIIST




PostPosted: Sat May 12, 2007 11:11 pm   Post subject: Re: Creating 3d scene with correct objects in fore and backgroun

Good idea post the code, from what im hearing it seems like you need a zbuffer or something to sort the object based on its distances from the camera. You then draw the scene from the furthest sorted object to the nearest. A good sort would be quick sort.
jondmml




PostPosted: Sun May 13, 2007 9:05 am   Post subject: Re: Creating 3d scene with correct objects in fore and backgroun

I was hoping sorting wasn't necessary but I guess its the only way. I will attach the code. Currently it is quite basic and is lacking all comments/documentation.


cubes.txt
 Description:
Must be in same dir as 3dt.t

Download
 Filename:  cubes.txt
 Filesize:  598 Bytes
 Downloaded:  103 Time(s)


3Dt.T
 Description:

Download
 Filename:  3Dt.T
 Filesize:  4.67 KB
 Downloaded:  91 Time(s)

DIIST




PostPosted: Sun May 13, 2007 11:29 am   Post subject: Re: Creating 3d scene with correct objects in fore and backgroun

Take a look at this code for a sec, it dosnt solve the problem but it may prevent future ones:
Turing:
function to2D (point, pan, position : Vector3D, centre : Vector2D, zoom : real) : Vector2D
    var screen : Vector2D
    var tempPoint : Vector3D
    tempPoint := rotatePoint (point, pan, position)
    screen.x := round (tempPoint.x / tempPoint.z * zoom + centre.x)
        screen.y := round (tempPoint.y / tempPoint.z * zoom + centre.y)
    if tempPoint.z > 0 then
        screen.existence := true
    else
    screen.existence := false
    end if
    result screen
end to2D

You should not calculate screen.x and screen.y unless tempPoint.z >1. If you get lower than one you get a huge number and turing will crash, because as temp.z approcehes zero, screen.x and screen.y approach infinity. 1/0 is undefined and will throw an error. Im looking into your error right now. Its a bit hard because your code is just a bit to procedural for my taste, but if i find it ill post it up. Wink

If you want you can check my attempt at 3d http://www.compsci.ca/v3/viewtopic.php?t=15498. It not documented either, and i haven't implemented a camera, but there is rotation. See if it helps. Its done using classes though.
zylum




PostPosted: Sun May 13, 2007 1:23 pm   Post subject: RE:Creating 3d scene with correct objects in fore and backgroun

I haven't looked at the code yet but i assume you draw each cube separately. You can fix the problem by "joining" the two objects (ie. making them one object) and drawing it that way. When you sort the polygons, they will all be sorted at once rather than the two sets sorted separately.
jondmml




PostPosted: Mon May 14, 2007 3:08 pm   Post subject: RE:Creating 3d scene with correct objects in fore and backgroun

So I took your advice and wrote a quick sort algorithm, however I do not know what values to sort. Each point now includes the distance from the camera to the point. Should I sort the squares by the distance from the middle of each square or how should I go about doing this?

EDIT: Well I tried taking the average of the four distances for each corner of the squares and sorting and displaying from highest to lowest distance... it seemed to work at first but once I rotated the camera I could rotate it in such a way that sometimes it did not work perfectly and what was supposed to be behing the square came to the front
Saad




PostPosted: Tue May 15, 2007 9:56 pm   Post subject: Re: RE:Creating 3d scene with correct objects in fore and backgroun

jondmml @ Mon May 14, 2007 3:08 pm wrote:
So I took your advice and wrote a quick sort algorithm, however I do not know what values to sort. Each point now includes the distance from the camera to the point. Should I sort the squares by the distance from the middle of each square or how should I go about doing this?


You could find the mid point of the polygon (or just the point if its one point), get the distance of it relative to the camera and then sort it. Sort it based on the distance away. Render the ones with the greatest distance first
DIIST




PostPosted: Wed May 16, 2007 7:58 pm   Post subject: Re: Creating 3d scene with correct objects in fore and backgroun

Another thing you should try and it only takes a few lines to do is cull. Culling removes faces that arent seen and sometimes speed drawing. You might get rid of the flickering that way. Thats the way i got rid it. Razz
Sponsor
Sponsor
Sponsor
sponsor
Saad




PostPosted: Wed May 16, 2007 10:01 pm   Post subject: Re: Creating 3d scene with correct objects in fore and backgroun

Can you explain the idea of culling? meaning the way to approach it?
DIIST




PostPosted: Wed May 16, 2007 10:33 pm   Post subject: Re: Creating 3d scene with correct objects in fore and backgroun

a100 @ Wed May 16, 2007 10:01 pm wrote:
Can you explain the idea of culling? meaning the way to approach it?
Basically every face has a normal and it is basically a line that is perpendicular to the face. If a normal is pointing away from you the face is facing away from you. Essentially you calculate the normal for each face. If the normal is pointing away you choose not to draw the face. An easy way to check if a normal is pointing away is to check if the z component of the normal is positive(facing away). Negetive facing you. To calculate the normal you need three nonlinear points. You need to produce two vector from the points and you need to cross them in cyclic order. Be consistant in the way you cross. Here is the math jist of it:

%Vector one being created from point p1 and p2
v1x= p2x-p1x
v1y=p2y- p1y
v1z=p2z - p1z

%Vector two being created from point p2 and p3
v2x= p3x-p2x
v2y=p3y- p2y
v2z=p3z - p2z


%Components of the normal (cross product of v1 and v2)
nx= v1y*v2z - v2y*v1z;
ny= v1z*v2x - v2z*v1x;
nz= v1x*v2y - v2x*v1y;

if nz>0 then
dont draw
else
draw
end if

Calculating the normal also helps you when lighting. If your not doing lighting you could just easily do this:

if (p2x-p1x)*(p3y- p2y) - (p3x- p2x)*(p2y-p1y)>0 then Cull!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: