Author Topic: Faces  (Read 7042 times)

0 Members and 1 Guest are viewing this topic.

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 899
  • Eufloria: Yes
Re: Faces
« Reply #90 on: June 22, 2011, 06:21:40 PM »
The flickering may be the loops not completing before the next fram is drawn, or something. Or it may be that they so long that it's below the 'framerate' for the human eye.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Faces
« Reply #91 on: June 22, 2011, 06:28:15 PM »
Or it may be that a part of the algorythm is failing and the parts are flickering?

Or the calculations take so long time to do(doubting this one though)...

Better check if the last ones usually flickers, the last IDs :P

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Faces
« Reply #92 on: June 22, 2011, 10:27:02 PM »
I don't think it's a case of too much stuff per loop.  Bizarrely, if I turn on my (currently not working correctly) code to check plane depth, the flickering disappears!  There is no reason why it should do that.  Very strange...


I guess I should post the code that is producing the erroneous behaviour.


Code: [Select]
for faceID = 0,numberoffaces do

-- detect numberoffedges
numberofedgesinthisface = -1
for i = 0,100 do
if face[faceID][i] ~= nil then
numberofedgesinthisface = numberofedgesinthisface + 1
else
break
end
end


passededges = 0
-- this counter is used to check how many edges are passed through to the left of this point

for faceEdgeNumber = 0,numberofedgesinthisface do
-- for each edge in this face

edgeID = face[faceID][faceEdgeNumber]
-- use the ID of the selected edge

v1 = edgefrom[edgeID]
v2 = edgeto[edgeID]
-- set v1 and v2 equal to the ID's of the vertices that join this edge

partoftheface = false

if v1 ~= vertexID and v2 ~= vertexID then
-- make sure the point passed to us in this function is not the same point mentioned in v1 or v2...

if math.floor((vertex2dX[v2] - vertex2dX[v1])) == 0 or math.ceil((vertex2dX[v2] - vertex2dX[v1])) == 0 then
-- vertical edge detected

if vertex2dY[v2] <= vertex2dY[v1] then
minv = vertex2dY[v2]
maxv = vertex2dY[v1]
else
minv = vertex2dY[v1]
maxv = vertex2dY[v2]
end

if vertex2dX[vertexID] > vertex2dX[v2] and vertex2dY[vertexID] > minv and vertex2dY[vertexID] < maxv then
passededges = passededges + 1
end

elseif (vertex2dY[v2] - vertex2dY[v1]) == 0 then
-- horizontal edge detected.  It is therefore parallel to the P line and as such will never intersect.
-- so no chance to add a passed edge here.

else
slope = (vertex2dY[v2] - vertex2dY[v1]) / (vertex2dX[v2] - vertex2dX[v1])
-- first calculate the slope of the line formed by the two coordinates

-- we need to find the X-value where the line drawn between v1 and v2 crosses the axis of vertexID

Yintercept = (-slope * vertex2dX[v1]) + vertex2dY[v1]
-- this is the Y-intercept

vertexXintercept = (vertex2dY[vertexID] - Yintercept) / slope
-- vertexXintercept is equal to the X-value where the line crosses the P line
-- ie vertexXintercept is equal to the value of X when this edge reaches Y = vertex2dY[vertexID]

if vertex2dX[v2] <= vertex2dX[v1] then
minv = vertex2dX[v2]
maxv = vertex2dX[v1]
else
minv = vertex2dX[v1]
maxv = vertex2dX[v2]
end

if vertex2dX[vertexID] > vertexXintercept and vertexXintercept > minv and vertexXintercept < maxv then
passededges = passededges + 1
end
end

else
partoftheface = true
break
end
end

if math.floor(passededges / 2) ~= math.ceil(passededges / 2) and partoftheface == false then
-- the vertex is inside a face
draw = false
end
end


This part of the code is the part which checks if a vertex is "inside" a face, based on the 2D coordinates.
These pictures from earlier in the thread show the basic technique for doing this:








Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Faces
« Reply #93 on: June 22, 2011, 10:31:07 PM »
Sh*t...

I really have to say: It's to much for my brain to catch up with, stupid boat trip I was on, making me skip all the posts you and Pilchard did before :/

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Faces
« Reply #94 on: June 23, 2011, 06:20:17 PM »
Breakthrough! :D

From wikipedia:

Quote
Most implementations of the ray casting algorithm consecutively check intersections of a ray with all sides of the polygon in turn. In this case the following problem must be addressed. If the ray passes exactly through a vertex of a polygon, then it will intersect 2 segments at their endpoints. While it is OK for the case of the topmost vertex in the example or the vertex between crossing 4 and 5, the case of the rightmost vertex (in the example) requires that we count one intersection for the algorithm to work correctly. A similar problem arises with horizontal segments that happen to fall on the ray. The issue is solved as follows: If the intersection point is a vertex of a tested polygon side, then the intersection counts only if the second vertex of the side lies below the ray. This is effectively equivalent to considering vertices on the ray as lying slightly above the ray.

This is my problem.  I have thought about it, and I am 100% sure this is what the problem is.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Faces
« Reply #95 on: June 23, 2011, 06:23:39 PM »
Hmmm, by the way annikk, what is your ultimate purpose of this 3d thing?

It would be awesome with some sort of sun in the background of the map :D

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Faces
« Reply #96 on: June 23, 2011, 06:38:17 PM »
FIXED.  YEAH BABY!!!!"!  fejwpqw JPJWF PJWFPFWFW!"!"

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Faces
« Reply #97 on: June 23, 2011, 06:40:51 PM »
Witness my perfectly vertical/horizontal cube, and it's gleaming east vertices, in their steadfast unflickering glory !


Now to give vertices their depth perception...

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Faces
« Reply #98 on: June 23, 2011, 06:57:07 PM »
Hmm, it has gone from smooth to a little laggy though... And when removing vertexes, try to check the z depth, I see vertexes infront of another face being removed :/

GOOD JOB THOUGH ;D
« Last Edit: June 23, 2011, 07:14:21 PM by Aino »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Faces
« Reply #99 on: June 23, 2011, 08:11:51 PM »
I will do an optimisation run soon to get it running a bit faster.  I want to get vertices to recognise when they are inside a polygon, but are closer than the face to the camera.

My current problem is that my cross product always returns (0,0,0).


Let P1, P2 and P3 be vertices that belong to the face being checked.
This code is supposed to calculate the plane's normal based on the three sets of coordinates.

Code: [Select]
PV1x = Px2 - Px1
PV1y = Py2 - Py1
PV1z = Pz2 - Pz1

PV2x = Px3 - Px1
PV2y = Py3 - Py1
PV2z = Pz3 - Pz1

-- now find the cross product, which is a line perpendicular to the plane
-- PV1 X PV2 = normal vector!

i = (PV1y * PV2z) - (PV1z * PV2y)
j = (PV1z * PV2x) - (PV1x * PV2z)
k = (PV1x * PV2y) - (PV1y * PV2x)

The values of i, j and k are always 0.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Faces
« Reply #100 on: June 23, 2011, 08:22:15 PM »
Strange... Got no idea why, the code seems legit :o

Unless: What is
PV2x = Px3 - Px1
PV2y = Py3 - Py1
PV2z = Pz3 - Pz1
?

Are they required there or?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Faces
« Reply #101 on: June 23, 2011, 08:23:53 PM »
Those are required.  They are converting 2 points into a vector.

I've worked out what the problem is caused by.  My code to select 3 vertices is erroneously assigning the same vertex ID to the 2nd and 3rd vertices.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Faces
« Reply #102 on: June 23, 2011, 08:31:06 PM »
Fixed!




Now vertices disappear when they go behind a face.  :>


This marks a huge milestone.  My face is pleased.  =)  Thanks to everyone who has helped or commented so far!

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 1
  • -Receive: 20
  • Posts: 1,483
  • They'll eat you next!
  • Eufloria: Yes
Re: Faces
« Reply #103 on: June 23, 2011, 09:26:48 PM »
Well, now to make the lines disappear properly?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Faces
« Reply #104 on: June 23, 2011, 09:37:01 PM »
Doing some tidying up of the existing code for now, tidying up the comments and linespacing and removing all the debugging commands and incorrect formulas.

Then, yes... it's time to make the edges disappear.  That will require pseudo-vertices, and arrays to store the enabled/disabled state of the vertices and pseudo-vertices.  Then the drawing rule is simply Draw, Don't Draw, according to my concept diagrams from way back on page 3..
I hope my conceptual model works..