# Need help with line normals

Posted by owen
 owen Need help with line normals July 20, 2011 03:17AM Registered: 11 years agoPosts: 363
I was progressing nicely until I added a light and my lines started drawing incorrectly. Then I realised that I need to add normals to them but my normals are incorrect and I can't figure out the right combination I need (I search google as well but I already have the normals for the cube working perfectly but they don't work for these lines.) However the line cube draws perfectly as expected, I just need help ficing the normals

Can someone tell me which one of my normals are incorrect?

Code for the function is here: [codepad.org]

Also note that I know of the draw cube function in GRRLIB but it is too slow for my game (all that looping, division and arrays). And the code for the displaylist works, if anybody was having problems with using them.
 Titmouse Re: Need help with line normals July 20, 2011 09:35AM Registered: 11 years agoPosts: 99
This is what I do - took me ages to work out and works well for me.

The normal values you are using look more like something that is meant for UV texture coordinates.

I use two types of surface lighting, one where the normal’s point away from the surface (gives hard edges), and another where the normal’s simply use the x,y,z part again (gives soft edges) . Maybe swapping to the soft edge lighting should give you results straight off, as a test step.

If you need hard edges you need to look into how to calculate a normal from say 3 x,y,z points, google "Calculate the normal of a triangle" or something like that
,
```// heres what I do , RenderModelMinimalHardNorms is the method you are currently using.

void Render3D::RenderModelMinimal(HashLabel ModelName, Mtx& ModelView)
{
//soft edges
Mtx mat;
guMtxInverse(ModelView,mat);
guMtxTranspose(mat,ModelView);
DisplayListInfo& Info( m_DispayListContainer[ModelName] );
GX_CallDispList(Info.m_dispList,Info.m_dispSize);
}

void Render3D::RenderModelMinimalHardNorms(HashLabel ModelName, Mtx& ModelView)
{
//hard edges
DisplayListInfo& Info( m_DispayListContainer[ModelName] );
GX_CallDispList(Info.m_dispList,Info.m_dispSize);
}
```
 owen Re: Need help with line normals July 20, 2011 02:11PM Registered: 11 years agoPosts: 363
That's all well and good but I just want the hard-coded numbers because i won't be changing them anytime soon. The normal numbers for quads are easy to find online but I need normals for a wireframe cube.
 AlfredMantalini Re: Need help with line normals July 21, 2011 07:53PM Registered: 11 years agoPosts: 2
I think the problem is how you draw the wireframe cube. You are drawing the edges of the cube and not the faces of the cube. See, the way you draw it each line (edge) is shared between two different faces of the cube. That makes 2 normals (actually 4, one pointing inside and one pointing outside for each face) to choose from for your vertices. What ever normal you choose, it will look wrong for one face.

Just draw the edges of each face seperatly and assign the normal of the face they are forming to the vertices.
```GX_Begin(...) //Draw edges of top face GX_End();
GX_Begin(...) //Draw edges of bottom face GX_End();
...```

Another solution is to disable lightning before you draw the wireframe cubes and enable it when you're done drawing the cubes. Too many state changes may have an impact on the perfomance though.
 Titmouse Re: Need help with line normals July 21, 2011 07:55PM Registered: 11 years agoPosts: 99
Posted at the same time as the faces answer.

Owen - Is the soft edge no good for what your doing (you only need to re-use the same x,y,z values in the normal part)?
Don't forget to use guMtxInverse(ModelView,mat); as in the fist example.
I really don't think you will see much of a difference for lines.

To calculate normal’s from three points I post this code just in case you or anyone else needs this in the future (it's been typed in here so it may have mistakes, but it gives the general method needed !)
```void CalcNormal(guVector* InVec1, guVector* InVec2, guVector* InVec3, guVector* OutVec)
{
guVector Tempv1,Tempv2;
guVecSub(InVec3, InVec1, &Tempv1);
guVecSub(InVec3, InVec2, &Tempv2);
guVecCross(&Tempv2,&Tempv1,OutVec);
guVecNormalize(OutVec);
}```

Edited 1 time(s). Last edit at 07/21/2011 08:07PM by Titmouse.
 owen Re: Need help with line normals July 21, 2011 10:57PM Registered: 11 years agoPosts: 363
@AlfredMantalini hmmmm. I draw all the cube faces first. then, I draw the wire frame cube + 1 scale around it. So basically they are almost separate entities. I don't even know why I need normals when I am just drawing a line in the first place. I'll try drawing the lights last and see how that looks.
 Titmouse Re: Need help with line normals July 22, 2011 10:01AM Registered: 11 years agoPosts: 99
Yes as you say, if don't need lighting don't use normals. Just send GX_VA_POS & GX_VA_CLR0 and setup GX_VTXFMTn to match.
You found lighting looks odd without normals, not tried this myself, so you will still need to turn off lighting as stated above in the other post.

Edited 3 time(s). Last edit at 07/22/2011 10:05AM by Titmouse.
 AlfredMantalini Re: Need help with line normals July 22, 2011 10:35AM Registered: 11 years agoPosts: 2
Quote

You are drawing the edges of the cube and not the faces of the cube
what i wanted to say is that you have to draw each edge twice. otherwise you dont know wich normal you have to assign to the vertices of the edge. have a look at how glut does it. (Link).

Edited 2 time(s). Last edit at 07/22/2011 11:09AM by AlfredMantalini.
Sorry, only registered users may post in this forum.