2
\$\begingroup\$

I am quite new to 3D graphical programming and thus far only understand that normal somehow define the direction in which a vertex faces and therefore the direction in which light is reflected. I have now idea how they are calculated though, only that they are defined by a Vector3.

For a visualizer that I am creating I am importing a bunch of coordinate which represent layer upon layer of line segments. At the moment I am only using a vertex buffer and adding the start and end point of each line and then rendering a linelist.

The thing is now that I need to calculate the normal for the vertices of these line segments so that I can get some realistic lighting. I have no idea how to calculate these normal but I know they all face sideways and not up or down. To calculate them all I have are the start and end positions of each line segment. The below image is a representation of what I think I need to do in the case of an example layer:

enter image description here

The red arrows represent the normal that should be calculates, the blue text represent the coordinates of the vertices and the green numbers represent their indices.

I would greatly appreciate it if someone could please explain to me how I should calculate these normal.

\$\endgroup\$
6
  • \$\begingroup\$ do you want to calculate normals for faces? or for line segments ? is it in 2D or 3D ? \$\endgroup\$ Commented Nov 3, 2013 at 11:43
  • \$\begingroup\$ I am working in 3d space with a collection of layers of line segments, the line segments therefore also have z coordinates. I do not have any faces though and want to calculate normals for the line segments. \$\endgroup\$ Commented Nov 3, 2013 at 12:17
  • \$\begingroup\$ as I said in my answer a 3d line segment have infinitely many normals. So I don't know what you want should mean? A normal is a geometric attribute which a 3D line doesn't have. maybe you should post an image so we can understand what are you working on. \$\endgroup\$ Commented Nov 3, 2013 at 13:02
  • \$\begingroup\$ is the Z always 0 ? \$\endgroup\$ Commented Nov 3, 2013 at 13:15
  • \$\begingroup\$ No, the z increases for every layer \$\endgroup\$ Commented Nov 3, 2013 at 13:17

1 Answer 1

2
\$\begingroup\$

The red arrows represent the normal that should be calculates.

2D is the only case that those red lines (or flipped) are the normals. for calculating a normal for 2D line segment (which is the title indicate), there are only two vectors perpendicular to any given vector, hence you can do the following:

 V = {x, y} 
 Vperp = {-y,x}

[Edit based on comments] Your case is simply 2D in 3D space you only need to deal with Z as constant. (Which is actually the case of 2D where is always Z=0) I am assuming the layer are in plane Z = const value. This is also equivalent to rotating the vector by 90 degrees.

 V = {x, y, Z} // Z is const regardless of the value
 Vperp = {-y,x, Z}

In case Z was not const for each layer you only need to multiply the vector by a rotation matrix, but you need to figure out the axis that you should rotate around.

Otherwise for calculating a normal for 3D line segment, Well there are infinitely many perpendicular lines on a 3D line segments.

For calculating the normal for a face (which is I think is what the question wants) you can do the following:

Given a triangle with A,B,C, you need to take the cross product for any two non-parallel edges and then normalize it. This will give you a face normal. In order to calculate the normal for each vertex you need to average normal of each face that shares the vertex.

Vec3 a,b,c;
Vec3 faceNormal = (a-b) cross (c-a);
faceNormal = normalize(faceNormal);

for_each Face that share vertex:
    vertexNormal += FaceNormal;

vertexNormal = normalize(vertexNormal);   
\$\endgroup\$
1
  • \$\begingroup\$ I am working in 3d space with a collection of layers of line segments, the line segments therefore also have z coordinates. I do not have any faces though and want to calculate normal for the line segments, how should I do that? \$\endgroup\$ Commented Nov 3, 2013 at 12:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.