Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (569) Games in Android Showcase (154) games submitted by our members Games in WIP (618) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Problem with calculating the normal vector of a quad  (Read 2439 times) 0 Members and 1 Guest are viewing this topic.
rubenknex

Junior Newbie

 « Posted 2010-05-24 09:54:46 »

Hello I'm ruben and I'm new to this forum

I have a problem with the calculation of the normal vector of a quad, my function for calculating the normal always return a vector with coordinates between -1 and 1, the lighting works and all but I want
to draw a line for each vector to see if everything is correct.

here is my function for getting the normal of a quad (i use the first 3 vertices of the quad because the normal points in the same direction anyway).

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 `public void calculateNormal(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, Vec3f normal)   {      Vec3f a = new Vec3f();      Vec3f b = new Vec3f();            a.x = x1 - x2;      a.y = y1 - y2;      a.z = z1 - z2;            b.x = x2 - x3;      b.y = y2 - y3;      b.z = z2 - z3;            normal.x = (a.y * b.z) - (a.z * b.y);      normal.y = (a.z * b.x) - (a.x * b.z);      normal.z = (a.x * b.y) - (a.y * b.x);            float combined = (normal.x*normal.x) + (normal.y*normal.y) + (normal.z*normal.z);      float length = (float)Math.sqrt(combined);            normal.x /= length;      normal.y /= length;      normal.z /= length;   }`

Also the size of the terrain which im lighting is 1024*1024 (with a heightmap)
lhkbob

JGO Knight

Medals: 32

 « Reply #1 - Posted 2010-05-24 15:16:54 »

I believe it should be:
 1  2  3 `b.x = x3 - x2;b.y = y3 - y2;b.z = z3 - z2;`

because you want a and b to be the direction vectors both emanating from the same vertex (in this case v2) when you cross them. Off the top of my head, everything else looks okay.

If you want to draw a line for each normal, you'd have to generate geometry (or use glVertex) to create line segments starting at a vertex and ending at (vertex + SCALE * normal).

rubenknex

Junior Newbie

 « Reply #2 - Posted 2010-05-24 15:24:03 »

Yes but is it right that the coordinates of my normals are always something like this:
 1 `x:0,071653  y:-0,955364  z:-0,286609`

I don't get how the lighting works with coordinates between -1 and 1 while the maps dimensions are 1024x1024, wouldnt a normal be like
 1 `x:234  y:90  z:512`

 Games published by our own members! Check 'em out!
rubenknex

Junior Newbie

 « Reply #3 - Posted 2010-05-24 15:54:01 »

Yay it works , forgot to add the normal coords
lhkbob

JGO Knight

Medals: 32

 « Reply #4 - Posted 2010-05-25 15:25:38 »

Yes, a normal is a vector that is "perpendicular" to the surface/vertex it's on. It's also a normalized vector so it's length should always be 1, which means each coordinate will be between [-1, 1]. The lighting works by taking the dot product between the normal and the light's direction (or direction to the point light) and then does some other calculations.

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Riven (22 views) 2015-04-16 10:48:47 Duke0200 (35 views) 2015-04-16 01:59:01 Fairy Tailz (27 views) 2015-04-14 20:13:12 Riven (28 views) 2015-04-12 21:36:37 bus hotdog (45 views) 2015-04-10 02:39:32 CopyableCougar4 (46 views) 2015-04-10 00:51:04 BurntPizza (46 views) 2015-04-06 22:06:58 ags1 (50 views) 2015-04-02 10:58:48 Riven (49 views) 2015-04-01 18:27:05 ags1 (66 views) 2015-03-31 10:55:12
 BurntPizza 24x theagentd 21x wessles 15x 65K 12x Rayvolution 12x kingroka123 11x alwex 10x KevinWorkman 9x kevglass 8x SHC 8x ra4king 8x phu004 8x Hanksha 7x Olo 7x Ecumene 7x chrislo27 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org