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.

DarkBASIC Discussion / 3d normals?

Author
Message
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 24th Apr 2009 23:59
what are they and how r they used in memblock meshes?

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Apr 2009 02:49
The short explanation is the normal is a vector perpendicular to a line or plane. Since 3d objects are made up of faces (usually triangles) a normal is perpendicular to the face. For a mesh or 3d object, the normal influences how light is reflected. This influences color and shading. In memblock meshes, there is a location in the mesh that contains the normals. There should be the same number of normals as there are vertices. These are called vertex normals.

For a longer explanation, read the code snippet (I copied most of this from an explanation I wrote a while ago on the forums...)



Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Apr 2009 05:21
ok, so two more questions, what are vectors and how do i input values for normals into memblock meshes?

There are only 10 kinds of people in the world, those who understand binary and those who dont
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 25th Apr 2009 07:32
Quote: "how do i input values for normals into memblock meshes?"


Write memblock dword (probably Dword but idk never looked at memblock meshes lol)

New Site! Check it out \/
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Apr 2009 07:36
i know HOW, i meant how do i know what VALUES to input into the normals slots

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 25th Apr 2009 12:24
Quote: "what are vectors"

Think of a vector as an arrow pointing somewhere in space. It has a start point, an end point, and a length. Because it has a definite start point and an end point, it has a direction. Now think of all these little arrows pointing outward from the faces of the 3d object. They are imaginary visually, but they are a calculation mathematically to disperse light across the 3d surface. I've included a visual representation in an attachment so you can get an idea of what a normal is.

Quote: " how do i input values for normals into memblock meshes"

You calculate a face normal using the cross product of two edges along a face. You then average the face normals that touch a single vertex to get that vertexes normal.

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 25th Apr 2009 18:09
ok so the vector is to 3d geometry what the line segment is to 2d geometry, and the normal of a face/polygon is the 3d version of a perpendicular bisector

and im having a blonde moment on this normal calculation stuff, how bout an example on that?

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 26th Apr 2009 00:23
Well, a normal is simply a perpendicular to anything. It tells which direction the surface of something is facing - line or plane, 2d or 3d. Looking at a 2d set of axes, x and y, the normal to the x axis is (0,+-1) or +-90 degrees.

And a vector can be 2d as well. Basically any ray of a fixed length is a vector. Velocity is a vector. It starts from 0 and it's final value is determined by a distance covered in an amount of time. It's acceleration determines the direction. If falling towards the earth is considered negative acceleration, then throwing a ball straight up would have a + velocity and on the way down the ball would have a - velocity. So, the velocity could be plotted on a 2d coordinate system as a vector.

As far as an example, try looking up the calculation of the cross product of the edges or vectors of a plane. If you can understand the math, you'll own the solution and perhaps start getting your teeth into a whole series of 3d possibilities.

The normal is not only useful for lighting but can be critical in finding the ground height of a terrain at any given position. If you know the normal to a particular face of the terrain the position is on, if you have one of the vertices that forms the face, you can get the height of the position. That's because the normal points in a direction and that tells us which direction the face is facing or how that particular face is tilted. That tilt, and the face vertex to the position vertex, can calculate the position height.

Take it a step further but along the same path and ray casting collision detection becomes more evidient. If you cast a ray towards an object, if the ray passes through the face of an object, you can determine the exact position of contact by using the normal. You can also calculate how the ray will be reflected.

Anyway, for now, start by looking up the math for cross product.

Enjoy your day.
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 26th Apr 2009 09:15
I can try to explain the math (I use this stuff ALL the time).

Cross product: Is simply V1 x V2 for the notation(the x is the notation for cross product).

This can be turned into a Matrix determinant where:

Note that here, the < and > are brackets used to show a vector and are NOT greater than and less than signs. These WON'T work in DBC, but are mathematical notation for vectors.

If you have not been exposed to matrices in math yet, I will warn you now, it is dry until you take a physics class or something and the teacher/professor actually tells you how to use them.

The x, y, and z are what are known as unit vectors. Alternatively, i j and k are usually used, but I use x y and z because it makes more sense. A unit vector is a vector that is 1 unit long in a specific direction, in this case, the direction of each of the three coordinate axes. They are insignificant in this case, though if they are something OTHER than 1 in an axial (right word?) direction, it WILL make a difference.

The V1x V1y V1z etc are the components of the vector itself. So, lets say you have a vector that goes from (0,0,0) to (3,1,16), the components would be V1x=3 V1y=1 and V1z=16. So, a general rule to find the components are V1x=Endx-Startx, V1y=Endy-Starty, V1z=Endz-Startz.

The same goes for V2 components.

On the cross product, the second term, that is, the Y component of the normal, is negative because you jump a row. In the cross product, if you switch a row or a column, it will invert the sign (make it negative, or if it is already negative, make it positive. What this also means is that If you want the normal to point the other direction, you can do V2 x V1 instead of V1 x V2. It is still the same normal, it just goes in the opposite direction.

Make sense?

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 26th Apr 2009 20:53
no, but im sure it will when i get exposed to matrices, what math class usually teaches those?

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 27th Apr 2009 02:22
Usually algebra 2 will teach them. The important part for you right now would be the breakdown after the matrices (that is, the =<(....)> part).

You can google matrices, but they are kinda counter intuitive.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 27th Apr 2009 03:31
ok if algebra 2 teaches it then ill learn it next year, thanks for ll the help

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 27th Apr 2009 09:37
@That1Smart Guy
Definately read up on cross product and matrix math. It will help you with understanding 3d overall. But in the mean time, you can actually cheat a little bit for the vertex normals.

The following method isn't always accurate because it doesn't account for face tilt (the face normals) but can produce some good results without much math. The idea is to use the normalized vertex as the vertex normal.

For example, if you make a cube in DBC 1.20, it's created with 16 vertices: I'm guessing 2 for each corner. That's a strange count for a cube. I would expect 8 or 24, but not 16. Each of the vertices start at the center of the cube and point outward to a corner. If you were to normalize each of those vertices, you would have a set of possible vertex normals for the cube. I tested this against a cube in DBC. DBC's normals were quite different. At first I couldn't figure out why the values didn't match, then it occured to me that DBC was calculating the face normals of the cube and assigning them to each vertex normal.

In the following example, I create a cube in DBC, create 2 memblocks from the mesh, display the normalized vertex values and the actual normal values. I then write the normalized vertex values to one of the memblocks and create a second cube out of it. The cube on the left is the original DBC cube with it's normals. The cube on the right is the new one with the normalized vertices.



The original DBC cube handles the lighting a bit better than the new cube. It's a little more subtle and spread out and gradated well across the faces. However, if we run the same code and use a DBC cylinder instead, the normals from the normalized vertex method looks a LOT better than DBC's normals. The new cylinder's lighting is consistant to the light source and has a very smooth gradation from light to shadow:



So, this method is pretty easy to implement and depending on the shape of your object may yield desirable results.

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 27th Apr 2009 15:52
ok, im still not understanding though what the normal value represents in terms of 3d

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 27th Apr 2009 19:02
In 3d, normal (remember that in math the word NORMAL means PERPENDICULAR) is just a vector that points in the direction that the face is visible. If you go inside a box, you won't be able to see it. But if you flip the normals, you will be able to see it from inside but not outside.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 27th Apr 2009 23:02
ok but in making a memblock of a mesh what do the normals values mean, are they angles or what?

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 28th Apr 2009 01:29 Edited at: 28th Apr 2009 14:14
Quote: "ok but in making a memblock of a mesh what do the normals values mean, are they angles or what?"


Remember we are dealing with 3d. That's 3 dimensions or an X Y and Z coordinate system. Meshes are built by defining positions in 3d space using (x,y,z) locations.

Just to reiterate, a NORMAL is a VECTOR at 90 degrees away from a line or surface. A VECTOR, in simple terms is a line segment of a particular length with a direction - sometimes called a ray when the length is finite as opposed to half-infinite.

In 3d, the normal's values are identified by the END POINT of the vector. It is assumed that the start point is a relative (0,0,0) from the center of the face itself - that means if we had little arrows representing the normals we would only be interested in the value at the arrow head. So if the arrow that represents our normal goes from (0,0,0) to (1,2,3), we'd only be interested in (1,2,3): the end point. So it's the end point of the arrow that is written into the memblock.

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 28th Apr 2009 04:48 Edited at: 28th Apr 2009 05:19
ok so the values represent the endpoint of the vector opposite (0,0,0), got it

but one more question, is there any simple way to compute these values without this cross product stuff?

[edit]
ok i read up a little on matrices and i somewhat get it

then i read up on cross product, but am confused on much of it, can anyone make sense of this?

http://en.wikipedia.org/wiki/Cross_product

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 28th Apr 2009 08:47
Quote: "<V1y*V2z-V1z*V2y,-(V1x*V2z-V1z*V2x),V1x*V2y-V1y*V2x> "


That is the matrix breakdown. Like I said before, matrices are weird, so feel free to drink the kool aid with this one until you learn about them, though if you want to know, I could try to breakdown the math of the 3x3 matrices for you, just don't ask why because I don't understand why it works myself. The V1x, V1y, and V1z are the x,y, and z components of vector 1 (from vertex a to b) and the V2x,V2y, and V2z are the x,y, and z components of vector 2 (from a to c). Where the cross will be the location for the new vector that is created, that is, the normal.

Isn't the 0,0,0 the center of the face itself? Or is it the global one?

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 28th Apr 2009 14:17 Edited at: 28th Apr 2009 14:33
Quote: "Isn't the 0,0,0 the center of the face itself? "

Yes and no. The normal's origin is really the point where the two edges meet that were used in calculating the cross product. But, conceptually, if you are thinking of a face normal, it doesn't hurt to imagine the origin at the center of the face. The normal is a perpendicular vector in relation to the whole plane that the two vectors (edges) are on. The plane is actually infinite but the normal would be the same anywhere on it.

The way I wrote it in the previous post doesn't sound right, does it?

Changed...

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 28th Apr 2009 15:42
ok after some thought im gettin closer to understanding cross products

so according to the above webpage, this is the formula for finding the cross product (x) of two vectors:

a x b = a*b*sin(z)*n

where x means cross product, a and b somehow relate to the two input vectors, z is the angle between the vectors, and n is a third vector that is supposed to be perpendicular to the plane containing the input vectors

what im not getting is what do a,b,n represent of the vectors, are they the lengths or what?

and also wouldnt n BE the normal?

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 28th Apr 2009 18:02
Quote: "and also wouldnt n BE the normal?"


Sounds like it. I would still suggest just using a determinant breakdown like I posted above.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 28th Apr 2009 19:33
great now were throwin determinants into the mix, ne1 care to explain those

p.s. I should SO get extra credit in math for this thread

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 28th Apr 2009 23:50
Determinant is just the term (I believe) for the solution of the matrix. That is how I have heard it used before so that's how I use it. Basically I mean the big equation enclosed by < and > that I posted earlier

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 28th Apr 2009 23:58
ok but r the x,y,z the coordinates of the midpoint on the vector or what?

also could some1 answer my a,b,n from above?

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 29th Apr 2009 00:31 Edited at: 29th Apr 2009 00:34
Quote: "also could some1 answer my a,b,n from above?"


I think the formula you posted is the length. Use BN2's matrix break down to calculate the normal.

nx=V1y*V2z-V1z*V2y
ny=-(V1x*V2z-V1z*V2x) or V1z*V2x-V1x*V2z
nz=V1x*V2y-V1y*V2x

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 29th Apr 2009 00:39 Edited at: 29th Apr 2009 00:41
how did he get that formula?

what vectors do i use?

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 29th Apr 2009 01:07 Edited at: 29th Apr 2009 01:10
You really need to learn some basics about vectors. It will only make things easier to understand.

The process is this:

Create the vertices that will make a 3d object. How you do that is up to you. Could be a mathematical formula - like for a sphere - or you could hand code each vertex one at a time.

Make faces from the vertices. These will be triangles. Use a clockwise winding order. That means if you start at the left most point of the triangle, the next vertex would be the top, and the final vertex would be the right. Or any combination as long as the order is clockwise.

Create face indexes that identify which 3 vertices make up a face.

For each face, use the vert indexes to find the edges of each face. These edges are the vectors you will be using to calculate the face normal

v1 = facevert2-facevert1
v2 = facevert3-facevert1

Now that you have the face edge vectors, calculate the face normal using the cross product.

Normalize the normal (though not absolutely necessary)

Do 1 of the following for vertex normals
1. assign each face normal to the individual vertex normals of the face

or

2. Find all of the face normals of the faces that touch an individual vertex and average the face normals and assign that value to the vertex normal

The mehtod will be determined by how you built your faces. If you used a set of independent vertices for each face, then you could use method 1 or 2

If you repeat the use of the same vertices ( a cube can be made from 8 vertices or you could even use 24 ) then use method 2.

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 29th Apr 2009 02:41
ok so if im gettin u right then to find the x of vector 1 (that is wat v1 is right?) id take the x of face vertex 2 - the x of face vertex 1?

then id fill that into bn2s matrix equation with vector 1's y, v1z, v2x, v2y, v2z?

please excuse me if im totally off cuz im a long ways away from learnin this stuff in school

There are only 10 kinds of people in the world, those who understand binary and those who dont
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 29th Apr 2009 03:12
I think you got it...

If facevert 1 = (2,0,5)
and facevert 2 = (10,-3,9)
and favevert 3 = (4,3,3)

then V1 = (10,-3,9) - (2,0,5) = (8,-3,4)
then V2 = (4,3,3) - (2,0,5) = (2,3,-2)

v1x=8
v1y=-3
v1z=4

v2x=2
v2y=3
v2z=-2

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 29th Apr 2009 03:39
ok so then i fill in the matrix equation above to get the normals coordinates and those are what go into the memblock right?

There are only 10 kinds of people in the world, those who understand binary and those who dont
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 29th Apr 2009 03:40
That looks about right

@latch
Where does facevert 3 come in?

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 29th Apr 2009 04:39
@BN2
Quote: "Where does facevert 3 come in?"

Assuming each face is a triangle, we use the 3 points of the triangle to figure out the vectors we need for the normal calculation. Here's an example that uses DBC's make object triangle. I'll create a triangle as if it were a face in a larger 3d object. I'll print the normals from the memblock as DBC has calculated them, and then I'll use the methods we've been talking about to caclulate our own set of vertex normals. You'll see how face vert 3 is used to find one of the vectors we need. Since vertex and vector both start with a v, I'll call a vertex a vert and just leave v for vector:



Enjoy your day.
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 29th Apr 2009 06:33
OH! Nevermind, I was thinking vert was for vector...sigh...it has been a long week.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose

Login to post a reply

Server time is: 2025-08-08 20:38:38
Your offset time is: 2025-08-08 20:38:38