Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / 3d normals... how to calculate?

Author
Message
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 2nd Dec 2014 18:21
can normals be calculated from simple math for a 3d object?

whats the best way to go about calculating normals within agk basic? I am capable of creating an object in basic with a vertex array and then saving the file out as a .obj to load back in.

the problem is i dont know how to calculate the normals.

all i know is that the normals are used to calculate light reflection. Is there a limit range a normal can have? -1.0 to 1.0?

any information about normals would be great.

Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 2nd Dec 2014 18:39
This link will take you to the particular video you need but the entire video series is well worth spending some time with:
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 2nd Dec 2014 20:45
I think there's a way you could knock something up, real cheap like without spending too much time on math.

Take a dummy object, anything, hide it, then position it at a vertex location in a polygon. Then, point that object at the mid point between the remaining 2 verts. Now, store the objects position then rotate the object locally on X by -90 OR 90 degrees, and move it forward locally by 1.0 units on Z. Now, the difference between that stored 3D position, and the current position of the dummy object is the normal of that polygon... but may be negative or positive - like the only way to know which way to rotate (X90 or X-90) would be to check the polygons that the vertex is part of, and try and work out an average position - then pick the direction that results in a point furthest away from the average positions.

I'd store the normals of each polygon, then average it for verts that are part of several polygons.

It's cheap and nasty I know, but it might prove useful when implementing the proper heavy math techniques. I saw some proper normal generation code from Kevin Verbeek a while ago, but I doubt I still have it. If it turns up, I'll post it - it was for DBPro but the process is abstracted so it should be easy to apply to AGK.

I am the one who knocks...
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Dec 2014 20:55
If your normal is -1, then you'd be looking at the backface of the polygon. Backface culling is often used to ignore rendering the back of a polygon because often times you would never see them anyway. For instance, a cube. Unless you're inside the cube, you'd never see them and so we ignoring rendering them. It's a performance thing, but that's when a negative normal would be used.


When you have 3 verts that define your polygon, let's say ABC, the first step is to make 2 directional vectors that lie on the polygon's surface. The order of the vertices (clockwise or counterclockwise) determine if your normal will be positive or negative.

V1 = A-B
V2 = B-C

And then, if memory serves me correctly, you simply take the cross product of V1 and V2.

N = V1 x V2

The last step would then be to normalize this to get your unit vector.


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 2nd Dec 2014 21:01
some old darkbasic code.
normal is length =1 , in mind a pin on a surface/triangle



AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 2nd Dec 2014 21:02
Sorry VanB, that wouldn't.

Apart from the fact the moving and rotating dummy objects is far more processor intensive than a bit of simple vector maths, a normal is a unit vector.
A unit vector is defined by having a value of 1 if all of it's components are added.
If you move 1 unit along Z then you are assuming that X and Y are both equal to zero. Which of course would only be correct for a single direction out of an infinite combination of directions.

The math needed is in the video and (I think) very easy but if anything else needs explaining then the rest of the videos in the series should get you there.

If you do use the method VanB describes then the math to convert the result to a unit vector is simple.

A unit vector is a vector divided by its length.
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 2nd Dec 2014 21:05
Yeah, I was just about to say it just wouldn't work. Plus I can't find Kevils old code... But it's all good, I've got a lasagne in the oven.

I am the one who knocks...
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 3rd Dec 2014 01:24 Edited at: 3rd Dec 2014 01:30
hey thanks this gives me allot to go off of.

Id really like to make an editor within agk... but had no clew where to begin trying to figure out normals.

the rest of it is easy to figure out.

@ markus... im not familiar with udt's (i think thats what we call them)
how does udt memory work? I've never understood it. Types into globals?? the way a function can return multiple values?

dumb question...sorry guys i just never used them ever...

Im use to using globals to be able to return multiple results from a function

edit:
could you give me an example if my triangle was this:

v1[0,0,0]
v2[10,5,0]
v3[10,0,0]

indexed as v1,v2,v3 in that order which should be clockwise to make the triangle.
how would i calc the normals for this particular triangle? if you have time.... thanks!

this would be a total of nine normals output to get them all correct?

Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 3rd Dec 2014 01:41 Edited at: 3rd Dec 2014 01:43
No it wouldn't be nine normals, it would be one.
The triangle forms a plane and the normal is a line perpendicular to that plane.

The normal 'points' out from the plane and defines the direction of the plane. Which is why normals are vital for calculating lighting.
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 3rd Dec 2014 01:51 Edited at: 3rd Dec 2014 01:55
1?? are you sure?

i think its 3 normals per vertex dot in 3d space?? am i wrong?? cause thats not how its written in a 3d format.

example .obj
this defines the normals for 1 vertex point of the 3 vertex points in an indexed triangle. Each triangle face....acording to code shows that there are infact 9 normals per triangle face

Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 3rd Dec 2014 02:09 Edited at: 3rd Dec 2014 02:12
Yes I'm sure!

You seem to be confused by what a normal is.
A normal is a direction in 3D space. In order to represent a direction you need 3 components - X, Y & Z, so this:
defines a single normal - not three.

This normal:
is pointing straight up the Y axis.

You can have vertex normals and surface normals. Since your original post talked about calculating the lighting, then you will need to know which direction a surface is facing.
A surface is defined by a triangle. So, as I said, one triangle will have one surface normal - not nine.
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 3rd Dec 2014 02:22 Edited at: 3rd Dec 2014 02:30
Quote: "vn 0.000000 -0.356965 -0.934118 defines a single normal - not three."

i agree with this...its 3 values to define a normal. {x,y,z}

so it takes 3 normals on a triangle face then? lol! I can tell im not on the same page as you.

i think your telling me that i need to make a triangle measurement to define 1 vertex normal? not the actual 3 vertex points that define a 3d triangle shape mesh. am i starting to get close?

so im talking about a mesh... a triangle mesh

vertex dot1[0,0,0], with normal of[?,?,?]
vertex dot2[10,5,0], with normal of[?,?,?]
vertex dot3[10,0,0], with normal of [?,?,?]

when indexed i created a triangle mesh

[v1,v2,v3]= 3d triangle mesh

im hoping to solve for the normals of this triangle so it reflects properly as a 3d model when a light source hits it.

edit:
Quote: "surface normals."
yes thats what im after i think

SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 3rd Dec 2014 02:40
you mentioned this being strieghtup
0.0 1.0 0.0

so if i understand correctly with what you said here is im just solving for a direction? would all of the 3 points making up a triangle face mesh use the exact same normals? (direction)? if so this should be easy for me to solve measuring the angle and normalizing it between the value of -1 to 1.0 ?? or is it not as easy as that?

Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 3rd Dec 2014 09:32
No, that's not it at all.

The x,y,z components of a normal may appear to be the same as the x,y,z components of a vertex (point representing a triangle) but they are very different things.

The components of a vertex represent a position in 3D space but the components of a normal represent a direction. A direction doesn't have a position because position is irrelevant. A direction is the same thing regardless of position.

You have got your three points in space the represent your triangle. In order to find the surface normal of that triangle you will need to perform a cross product and then normalize the result.

The math you need to do that is explained very eloquently in the video I linked you to.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd Dec 2014 09:53
the last agk v1 or v2 can handle utds, ist just a struct.
darkbasic had a problem in past.
here how it works in agk.





AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 3rd Dec 2014 10:29
Here you go, this is the relevant functions from my math library:

Define 3D vector


Calculate Cross Product


Calculate surface normal from a triangle


Subtract Vectors


Normalize a vector


Get vector magnitude (length)


I recommend that you create your own math library containing functions like this that you can simply plug into any project.
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 3rd Dec 2014 15:19
thanks guys. I was having difficulty understanding the video you posted but i think you have cleared it up fairly well. i will do some testing with the code you posted and hopefully learn something from it.
thanks markus for a udt example... im going to dive into that also and see where i can benifit from using udts.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Dec 2014 17:38 Edited at: 3rd Dec 2014 17:40
Quote: "edit:
could you give me an example if my triangle was this:

v1[0,0,0]
v2[10,5,0]
v3[10,0,0]

indexed as v1,v2,v3 in that order which should be clockwise to make the triangle.
how would i calc the normals for this particular triangle? if you have time.... thanks!
"


It makes one, and my post explained exactly how to calculate it.

N = [0, 0, -1]



Here's a link to a library of vector functions I made for AGK.
http://forum.thegamecreators.com/?m=forum_view&t=192569&b=6


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 4th Dec 2014 02:05
thanks! i will test this on my matrix code for agk since all it was missing was the normals.

Login to post a reply

Server time is: 2024-11-25 11:17:44
Your offset time is: 2024-11-25 11:17:44