Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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 1445 times)
0 Members and 1 Guest are viewing this topic.
Offline rubenknex

Junior Newbie





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

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

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)
Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2010-05-24 17: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).

Offline rubenknex

Junior Newbie





« Reply #2 - Posted 2010-05-24 17: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

 Huh
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline rubenknex

Junior Newbie





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

Yay it works Cheesy, forgot to add the normal coords  Lips Sealed
Offline lhkbob

JGO Knight


Medals: 32



« Reply #4 - Posted 2010-05-25 17: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (88 views)
2013-05-17 21:29:12

alaslipknot (96 views)
2013-05-16 21:24:48

gouessej (128 views)
2013-05-16 00:53:38

gouessej (123 views)
2013-05-16 00:17:58

theagentd (131 views)
2013-05-15 15:01:13

theagentd (119 views)
2013-05-15 15:00:54

StreetDoggy (161 views)
2013-05-14 15:56:26

kutucuk (184 views)
2013-05-12 17:10:36

kutucuk (185 views)
2013-05-12 15:36:09

UnluckyDevil (191 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.132 seconds with 21 queries.