Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Normal vector calculus for OpenGL

CytCyt Member Posts: 557
[b][red]This message was edited by the Cyt at 2002-2-15 9:44:44[/red][/b][hr]
Hi all.

I'm trying to calculate some decent normal vectors for some triangles. However, the current state is quite miserable.

My initial object is quite simple: It's simply an z(x,y) grid where x and y both range from 0 to 10. I've decided to draw the triangles using GL_TRIANGLE_STRIP, which works perfectly. However, the lights are causing me lot's of problems - and it seems to be the normal vector calculus that is simply wrong :-(

Say, I have three 3D points: Point0,Point1, and Point2: I then calculate Point2-Point0 and Point1-0 and put them in the 3D Points P1m0, and P2m0 (This code could probably be more efficient, but that matter can wait):
[code]
P1m0.X=Point1.X-Point0.X;
P1m0.Y=Point1.Y-Point0.Y;
P1m0.Z=Point1.Z-Point0.Z;
P2m0.X=Point2.X-Point0.X;
P2m0.Y=Point2.Y-Point0.Y;
P2m0.Z=Point2.Z-Point0.Z;
[/code]

From my beatiful Schaum, I then use these two vectors to get a Scalar product, which, AFAIK, is the normal vector of the surface:
NormalX,NormalY,NormalZ are doubles.
[code]
NormalX=P1m0.Y*P2m0.Z-P1m0.Z*P2m0.Y;
NormalY=P1m0.Z*P2m0.X-P1m0.X*P2m0.Z;
NormalZ=P1m0.X*P2m0.Y-P1m0.Y*P2m0.X;
[/code]
And then I normalise:
[code]
NormalScaler=1./sqrt(NormalX*NormalX + NormalY*NormalY + NormalZ*NormalZ);
NormalX*=NormalScaler;
NormalY*=NormalScaler;
NormalZ*=NormalScaler;
glNormal3d(NormalX,NormalY,NormalZ);
glVertex3s(Point2.X,Point2.Y,Point2.Z); // The two last ones were of course Point0 and Point1
[/code]

In the lighting, I then do this:
[code]
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
// Make some arbitrary light values...
float fLightPos[4] =
{
0.1f,0.1f,.5f,1.f
};
glLightfv(GL_LIGHT0, GL_POSITION, fLightPos);

// Rotate the b*tch
glTranslatef(0.f,0.f,(float)-nZDist);
glRotated(dTheta,1.,0.,0.);
glRotated(dPhi,0.,0.,1.);

glCallList(1);
[/code]

Can anyone find out why it looks like crap from this?

And please forgive me if it turns out that I've misunderstood something completely. This is only my second day of OpenGL...

Regards.


----------

Added 15/Feb/2002:
I found out about it... I've even made a small normal vector routine. If anybody's interested:
[code]
void MakeNormalVectorf(float Ax,float Ay,float Az,float Bx,float By,float Bz)
{
float fTemp;
fTemp=Ay*Bz-Az*By; // X
Bz=Az*Bx-Ax*Bz; // Y
Az=Ax*By-Ay*Bx; // Z
Ax=1.f/(float)sqrt(fTemp*fTemp + Bz*Bz + Az*Az); // Scaler
glNormal3f(fTemp*Ax,Bz*Ax,Az*Ax);
}
[/code]

Comments

  • Chris BrownChris Brown USAMember Posts: 4,496 ✭✭

    _______ ( http://forcoder.org ) free ebooks and video tutorials about // PL/SQL C Java Go C# MATLAB Scratch Ruby Visual Basic Assembly Visual Basic .NET Perl R PHP Objective-C JavaScript Delphi Swift Python C++ Clojure Lisp Transact-SQL Ada D Scheme COBOL Logo FoxPro Rust Apex SAS Awk Alice ML Kotlin Hack Dart F# Bash LabVIEW Crystal Lua Prolog VBScript Scala Fortran ABAP Erlang Julia // __

Sign In or Register to comment.