Jump to content

Drawing A 3D Object?

- - - - -

  • Please log in to reply
8 replies to this topic

#1
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Hello.

I have a program that is supposed to render 3D objects, using 2D drawing tools. The 3D objects are stored as meshes (points & faces type). The program converts all the 3D points into 2D points, and then draws the faces as polygons, in order from most to least distant faces.


Here's an example 3D box:
Posted Image
(link)





Here's the definition of the above box:
		<object> 

			<points> 

				vector= figure1 

				1 (0, 0, 0) 

				2 (0, 0, 100) 

				3 (800, 0, 100) 

				4 (800, 0, 0) 

				5 (0, 500, 0) 

				6 (0, 500, 100) 

				7 (800, 500, 100) 

				8 (800, 500, 0) 

			</points> 

			<faces> 

				1, 2, 3, 4 = #FF0000 

				and 

				5, 6, 7, 8 = #0000FF 

				and 

				2, 6, 7, 3 = #00FF00 

				and 

				1, 2, 6, 5 = #FF00FF 

				and 

				3, 4, 8, 7 = #FFFF00 

				and 

				1, 4, 8, 5 = #00FFFF 

			</faces> 

		</object> 





The program averages the z value of the points of each face, and orders the faces based on that. The problem, however, is that sometimes the average is not quite the best thing to do; for example, take a look at this:
Posted Image
(link)




It's the same box, just from another angle and position. The average of the points of the face on the right is closer to the camera than that of the face on top.


So I wanted to ask, do you guys have any ideas for how to figure out which face comes before which?


Thanks.
RR

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

RhetoricalRuvim said:

So I wanted to ask, do you guys have any ideas for how to figure out which face comes before which?

No one has any ideas? :(

#3
rohalamin

rohalamin

    Newbie

  • Members
  • PipPip
  • 15 posts
hey friend!
really you do good work.
bravo!
:cool:

#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
There is a problem with the way sorting is done; I am trying to think of ideas of how to improve the sorting to fix the problem demonstrated on the latter screenshot.

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I still don't have the answer. I tried asking my math teachers about this, but they don't know either.

Is there even an answer? I mean, there must be some way, so there must be an answer; it's probably just a hard question to answer. I'll think some more, but if anyone gets any ideas, that would be nice.

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Okay, I have an idea. I'll try it out, and say if it works.

#7
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Just a random idea, but maybe try basing the drawing order on the Z-value of a vertex of a face closest to the viewer instead of averaging the Z-values together for the entire face.
Latinamne loqueris?

#8
rohalamin

rohalamin

    Newbie

  • Members
  • PipPip
  • 15 posts

RhetoricalRuvim said:

I still don't have the answer. I tried asking my math teachers about this, but they don't know either.

Is there even an answer? I mean, there must be some way, so there must be an answer; it's probably just a hard question to answer. I'll think some more, but if anyone gets any ideas, that would be nice.

Oh yes!
The math teachers haven't very info.
:sleep:

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think you'll need quite a bit of math for this.
What I'm thinking about is the following
--You need coordinates of the "camera" -the viewpoint

Before drawing the face, make sure it's visible by checking all the points of it.
You check them by drawing an imaginary line (X1,Y1,X2,Y2) from each point(X2,Y2) to the viewpoint(X1,Y1).
And then make sure that line does not intersect any other face. Simple as that :-P
There's math that can do this.
If all lines of every point of the face don't intersect with any of the other faces. Draw the face.

(Google gave me this : Intersection of Rays/Segments with Triangles and Intersection of a line and a facet I didn't quite read it, but judging by the images it seems to be that)

I'm sure Wingedpanther has some book at his book shelf with this matter covered with dust :D




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users