Ok - I followed your instructions - Am so glad you guys are lending a hand. these are my results so far:
Sphere = Light Position
No Normalization
Full Size:
http://www.jasonpetersage.com/img/Normalization_NONE.PNG
Manual Method - Pharoseer - this is your way - I know I musta goofed it though
Full Size:
http://www.jasonpetersage.com/img/Normalization_Manual.PNG
//-----------------------------------------------------------------------------------------
void NormalizeMatrix2(int p_MemID){
//-----------------------------------------------------------------------------------------
int ObjectID=0;
int Limbs=0;
int MeshFacesPerTileX=0;
int MeshFacesPerTileZ=0;
int MeshTotalFaces=0;
int MeshPolyCount=0;
int MeshVertices=0;
int TilesX=0;
int TilesZ=0;
int Tiles=0;
float TileSizeX=0;
float TileSizeZ=0;
// Our Fancy Terrain Limb Demographics - Emulating a regular data structure
// during dev - and seems sound enough to just kept it :)
//--------------------------------------------------------------
int p=0;
ObjectID= dbMemblockDword(p_MemID,p);p+=4;
Limbs= dbMemblockDword(p_MemID,p);p+=4;
MeshFacesPerTileX= dbMemblockDword(p_MemID,p);p+=4;
MeshFacesPerTileZ= dbMemblockDword(p_MemID,p);p+=4;
MeshTotalFaces= dbMemblockDword(p_MemID,p);p+=4;
MeshPolyCount= dbMemblockDword(p_MemID,p);p+=4;
MeshVertices= dbMemblockDword(p_MemID,p);p+=4;
TilesX= dbMemblockDword(p_MemID,p);p+=4;
TilesZ= dbMemblockDword(p_MemID,p);p+=4;
Tiles= dbMemblockDword(p_MemID,p);p+=4;
TileSizeX= dbMemblockFloat(p_MemID,p);p+=4;
TileSizeZ= dbMemblockFloat(p_MemID,p);p+=4;
//--------------------------------------------------------------
int GridX=MeshFacesPerTileX*TilesX;
int GridZ=MeshFacesPerTileZ*TilesZ;
//int VectorA=100;dbMakeVector3(VectorA);
//int VectorB=101;dbMakeVector3(VectorB);
//int VectorC=102;dbMakeVector3(VectorC);
//int VectorAB=103;dbMakeVector3(VectorAB);
//int VectorAC=104;dbMakeVector3(VectorAC);
float Ax=0; float Ay=0; float Az=0; float Am=0;
float Bx=0; float By=0; float Bz=0; float Bm=0;
float Cx=0; float Cy=0; float Cz=0; float Cm=0;
float ABx=0; float ABy=0; float ABz=0;
float ACx=0; float ACy=0; float ACz=0;
int iLimb; int iVert=0;
int tx=0;int tz=0;int mx=0;int mz=0;//looping tiles(are meshes) and mesh has polys.
dbSetCursor(300,0);dbPrint("Normalizing");dbSync();
for(tz=0;tz<TilesZ;tz++){
for(tx=0;tx<TilesX;tx++){
iLimb=tx+(tz*TilesX);
dbLockVertexDataForLimb(ObjectID,iLimb);
for(iVert=0;iVert<MeshVertices;iVert=iVert+3){
// Normalize Vector.X (Think Pythagorean theorem)
Ax=dbGetVertexDataPositionX(iVert+0);
Ay=dbGetVertexDataPositionY(iVert+0);
Az=dbGetVertexDataPositionZ(iVert+0);
Am=dbSQRT( dbSQRT(Ax)+dbSQRT(Ay)+dbSQRT(Az) );
Ax=Ax/Am;Ay=Ay/Am;Az=Az/Am;
//dbSetVector3(VectorA,Ax,Ay,Az);
// Normalize Vector.Y (Think Pythagorean theorem)
Bx=dbGetVertexDataPositionX(iVert+1);
By=dbGetVertexDataPositionY(iVert+1);
Bz=dbGetVertexDataPositionZ(iVert+1);
Bm=dbSQRT( dbSQRT(Bx)+dbSQRT(By)+dbSQRT(Bz) );
Bx=Bx/Bm;By=By/Bm;Bz=Bz/Bm;
//dbSetVector3(VectorB,Bx,By,Bz);
// Normalize Vector.Z (Think Pythagorean theorem)
Cx=dbGetVertexDataPositionX(iVert+2);
Cy=dbGetVertexDataPositionY(iVert+2);
Cz=dbGetVertexDataPositionZ(iVert+2);
Cm=dbSQRT( dbSQRT(Cx)+dbSQRT(Cy)+dbSQRT(Cz) );
Cx=Cx/Cm;Cy=Cy/Cm;Cz=Cz/Cm;
//dbSetVector3(VectorC,Cx,Cy,Cz);
// First you need to get the vectors that define two of your three edges.
// So if you have a triangle ABC find the vectors AB and AC. To do this
// you'll just subtract all the elements in A from B to get AB and all
// the elements in A from C to get AC.
// e.g. AB = <Bx - Ax, By - Ay, Bz - Az>
ABx=Bx-Ax; ABy=By-Ay; ABz=Bz-Az;
// e.g. AC = <Cx - Ax, Cy - Ay, Cz - Az>
ACx=Cx-Ax; ACy=Cy-Ay; ACz=Cz-Az;
//------------------------Next Step
// Now, to get the normal to our triangle ABC we need to get the cross
// product of the vectors AB and AC. The order we do this in matters
// because the is effectively a positive and a negative normal. We want
// the positive one. I'll skip the in-depth explanation and get to the
// practical stuff.
//
// To calculate the cross product, simply do the following:
// e.g: Let AB = P and AC = Q (makes the next part easier to read)
//
// P = AB = <1, 0, 0>
// Q = AC = <0, 0, 1>
// P x Q = <(Py * Qz) - (Pz * Qy), (Px * Qz) - (Pz * Qx), (Px * Qy) - (Py * Qx)>
//
// Which makes sense. Our vectors AB and AC represent the positive X and Z
// axes respectively. We already now that the positive Y axis is
// perpendicular to those. Lastly, remember that order is important.
// If you follow the order listed here everything should work for you. If you
// reverse the order (say subtracting the elements in B and C from A instead
// of subtracting A from B and C) then the normal we get will point in the wrong
// direction. Also, it's important to notice that P x Q != Q x P.
// P x Q = AB x AC
// Store "Cross Product" in Vector A
Ax=(ABy*ACz)-(ABz*ACy);
Ay=(ABx*ACz)-(ABz*ACx);
Az=(ABx*ACy)-(ABy*ACx);
dbSetVertexDataNormals(iVert,Ax,Ay,Az);
dbSetVertexDataNormals(iVert+1,Ax,Ay,Az);
dbSetVertexDataNormals(iVert+2,Ax,Ay,Az);
};
dbUnlockVertexData();
};
};
};
//-----------------------------------------------------------------------------------------
DarkGDK 3DMaths - TParkin - this is your way - I probably goofed this too
Full Size:
http://www.jasonpetersage.com/img/NormalizationVia3DMaths.PNG
//-----------------------------------------------------------------------------------------
void NormalizeMatrix3(int p_MemID){
//-----------------------------------------------------------------------------------------
int ObjectID=0;
int Limbs=0;
int MeshFacesPerTileX=0;
int MeshFacesPerTileZ=0;
int MeshTotalFaces=0;
int MeshPolyCount=0;
int MeshVertices=0;
int TilesX=0;
int TilesZ=0;
int Tiles=0;
float TileSizeX=0;
float TileSizeZ=0;
// Our Fancy Terrain Limb Demographics - Emulating a regular data structure
// during dev - and seems sound enough to just kept it :)
//--------------------------------------------------------------
int p=0;
ObjectID= dbMemblockDword(p_MemID,p);p+=4;
Limbs= dbMemblockDword(p_MemID,p);p+=4;
MeshFacesPerTileX= dbMemblockDword(p_MemID,p);p+=4;
MeshFacesPerTileZ= dbMemblockDword(p_MemID,p);p+=4;
MeshTotalFaces= dbMemblockDword(p_MemID,p);p+=4;
MeshPolyCount= dbMemblockDword(p_MemID,p);p+=4;
MeshVertices= dbMemblockDword(p_MemID,p);p+=4;
TilesX= dbMemblockDword(p_MemID,p);p+=4;
TilesZ= dbMemblockDword(p_MemID,p);p+=4;
Tiles= dbMemblockDword(p_MemID,p);p+=4;
TileSizeX= dbMemblockFloat(p_MemID,p);p+=4;
TileSizeZ= dbMemblockFloat(p_MemID,p);p+=4;
//--------------------------------------------------------------
int GridX=MeshFacesPerTileX*TilesX;
int GridZ=MeshFacesPerTileZ*TilesZ;
float Ax=0; float Ay=0; float Az=0;
float Bx=0; float By=0; float Bz=0;
float Cx=0; float Cy=0; float Cz=0;
int iLimb; int iVert=0;
int tx=0;int tz=0;
dbSetCursor(300,0);dbPrint("Normalizing");dbSync();
dbMakeVector3( 1000 ); // v0
dbMakeVector3( 1001 ); // v1
dbMakeVector3( 1002 ); // v2
dbMakeVector3( 1003 ); // v01
dbMakeVector3( 1004 ); // v02
dbMakeVector3( 1005 ); // normal
for(tz=0;tz<TilesZ;tz++){
for(tx=0;tx<TilesX;tx++){
iLimb=tx+(tz*TilesX);
dbLockVertexDataForLimb(ObjectID,iLimb);
for(iVert=0;iVert<MeshVertices;iVert=iVert+3){
// Set your v0, v1, and v2 from your vertex data (from memblock)
// You would replace this with your code to set these vectors
// based on reading your mesh from the memblock
Ax=dbGetVertexDataPositionX(iVert+0);
Ay=dbGetVertexDataPositionY(iVert+0);
Az=dbGetVertexDataPositionZ(iVert+0);
dbSetVector3( 1000, Ax, Ay, Az );
Bx=dbGetVertexDataPositionX(iVert+1);
By=dbGetVertexDataPositionY(iVert+1);
Bz=dbGetVertexDataPositionZ(iVert+1);
dbSetVector3( 1001, Bx, By, Bz );
Cx=dbGetVertexDataPositionX(iVert+2);
Cy=dbGetVertexDataPositionY(iVert+2);
Cz=dbGetVertexDataPositionZ(iVert+2);
dbSetVector3( 1002, Cx, Cy, Cz );
// Now create v01 and v02
dbSubtractVector3( 1003, 1001, 1000 );
dbSubtractVector3( 1004, 1002, 1000 );
// Normalize v01 and v02
dbNormalizeVector3( 1003, 1003 );
dbNormalizeVector3( 1004, 1004 );
// Cross v01 and v02 to get the normal
dbCrossProductVector3( 1005, 1003, 1004 );
Ax=dbXVector3(1005);
Ay=dbYVector3(1005);
Az=dbZVector3(1005);
dbSetVertexDataNormals(iVert,Ax,Ay,Az);
dbSetVertexDataNormals(iVert+1,Ax,Ay,Az);
dbSetVertexDataNormals(iVert+2,Ax,Ay,Az);
};
dbUnlockVertexData();
};
};
dbDeleteVector3(1000 ); // v0
dbDeleteVector3( 1001 ); // v1
dbDeleteVector3( 1002 ); // v2
dbDeleteVector3( 1003 ); // v01
dbDeleteVector3( 1004 ); // v02
dbDeleteVector3( 1005 ); // normal
};
//-----------------------------------------------------------------------------------------
Dark Basic "MATRIX" Classic Tutorial - translated to DarkGDK and retro fitted to my "poly mesh"
Full Size:
http://www.jasonpetersage.com/img/Normalization_DBCTutorial4Matrix.PNG
//-----------------------------------------------------------------------------------------
void NormalizeMatrix(int p_MemID){
//-----------------------------------------------------------------------------------------
int ObjectID=0;
int Limbs=0;
int MeshFacesPerTileX=0;
int MeshFacesPerTileZ=0;
int MeshTotalFaces=0;
int MeshPolyCount=0;
int MeshVertices=0;
int TilesX=0;
int TilesZ=0;
int Tiles=0;
float TileSizeX=0;
float TileSizeZ=0;
// Our Fancy Terrain Limb Demographics - Emulating a regular data structure
// during dev - and seems sound enough to just kept it :)
//--------------------------------------------------------------
int p=0;
ObjectID= dbMemblockDword(p_MemID,p);p+=4;
Limbs= dbMemblockDword(p_MemID,p);p+=4;
MeshFacesPerTileX= dbMemblockDword(p_MemID,p);p+=4;
MeshFacesPerTileZ= dbMemblockDword(p_MemID,p);p+=4;
MeshTotalFaces= dbMemblockDword(p_MemID,p);p+=4;
MeshPolyCount= dbMemblockDword(p_MemID,p);p+=4;
MeshVertices= dbMemblockDword(p_MemID,p);p+=4;
TilesX= dbMemblockDword(p_MemID,p);p+=4;
TilesZ= dbMemblockDword(p_MemID,p);p+=4;
Tiles= dbMemblockDword(p_MemID,p);p+=4;
TileSizeX= dbMemblockFloat(p_MemID,p);p+=4;
TileSizeZ= dbMemblockFloat(p_MemID,p);p+=4;
//--------------------------------------------------------------
int GridX=MeshFacesPerTileX*TilesX;
int GridZ=MeshFacesPerTileZ*TilesZ;
int z=0;int x=0;
float h8=0;float h4=0;float h=0;
float dx=0;float x2=0;float x1=0;
float y1=0;float y2=0;float ax=0;
float az=0;float dz=0;float dy=0;
float h2=0;float z1=0;float z2=0;
float nx=0;float ny=0;float nz=0;
for(x=0; x<=GridX;x++){
for(z=0; z<=GridZ;z++){
h8=GetVertY(p_MemID,x,z-1);
h4=GetVertY(p_MemID,x-1,z);
h=GetVertY(p_MemID,x,z);
h2=h;
x1=(x-1)*25.0;y1=h;
x2=(x+0)*25.0;y2=h4;
dx=x2-x1;
dy=y2-y1;
ax=dbAtanFull(dx,dy);
ax=dbWrapValue(90-ax);
z1=(z-1)*25.0;y1=h2;
z2=(z+0)*25.0;y2=h8;
dz=z2-z1;
dy=y2-y1;
az=dbAtanFull(dz,dy);
az=dbWrapValue(90-az);
nx=dbSin(ax);
ny=dbCos(ax);
nz=dbSin(az);
SetVertNormal(p_MemID,x,z,nx,ny,nz);
};
};
};
//-----------------------------------------------------------------------------------------
Now... I am so looking for suggestions. Meanwhile - I'm going to try to take all I've learned from you guys - and what someone told me about atanfull and see what I can do while I wait for some New Eyes ... That is - any eyes but my own
Thanx To All!