3dObjectLathe.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:22k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dObjectLathe.cpp : implementation file
- //
- // glOOP (OpenGL Object Oriented Programming library)
- // Copyright (c) Craig Fahrnbach 1997, 1998
- //
- // OpenGL is a registered trademark of Silicon Graphics
- //
- //
- // This program is provided for educational and personal use only and
- // is provided without guarantee or warrantee expressed or implied.
- //
- // Commercial use is strickly prohibited without written permission
- // from ImageWare Development.
- //
- // This program is -not- in the public domain.
- //
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "glOOP.h"
- #include "3dObjectDialog.h"
- #include <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////
- // C3dObjectLathe
- IMPLEMENT_DYNAMIC(C3dObjectLathe, C3dObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dShape construction
- C3dObjectLathe::C3dObjectLathe()
- {
- // Set the attributes to default values..
- m_iType = SHAPE_OBJECT;
- m_szName.Format("Lathe %u", nLatheObjects++);
- m_fSweepAngle = 360.0f;
- m_iSegments = 20;
- m_bSmooth = TRUE;
- m_fMaxAngle = 30.0f;
- // Create our C3dPointArray object
- m_pPointArray = new C3dPointArray;
- ASSERT(m_pPointArray);
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3DWorld Destructor
- C3dObjectLathe::~C3dObjectLathe()
- {
- // Delete our point array
- if(m_pPointArray)
- delete m_pPointArray;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectLathe virtual function overrides
- void C3dObjectLathe::AddAttributePage(C3dWorld* pWorld, LPVOID pSht)
- {
- C3dObjectPropSheet* pSheet = (C3dObjectPropSheet*)pSht;
- ASSERT(pSheet);
- // Add the page to the property sheet
- pSheet->AddPage(&pSheet->m_LathePage);
- // Save the address of this object in the page
- pSheet->m_LathePage.m_pObject = this;
- }
- void C3dObjectLathe::GetShapeBounds(C3dBoundingBox* pBox)
- {
- C3dPoint* pPoint;
- // Set to large positive number
- pBox->m_fMin[X] = -(GLfloat)LARGE_NUMBER;
- pBox->m_fMin[Z] = (GLfloat)LARGE_NUMBER;
- // Set to large negative number
- pBox->m_fMax[X] = -(GLfloat)LARGE_NUMBER;
- pBox->m_fMax[Z] = -(GLfloat)LARGE_NUMBER;
- for(int i=0; i<m_pPointArray->m_iNumPoints; i++)
- {
- pPoint = &m_pPointArray->m_pPoints[i];
- if(pPoint->m_fOrigin[X] > pBox->m_fMax[X])
- pBox->m_fMax[X] = pPoint->m_fOrigin[X];
- if(pPoint->m_fOrigin[Z] > pBox->m_fMax[Z])
- pBox->m_fMax[Z] = pPoint->m_fOrigin[Z];
- if(pPoint->m_fOrigin[X] > pBox->m_fMin[X])
- pBox->m_fMin[X] = pPoint->m_fOrigin[X];
- if(pPoint->m_fOrigin[Z] < pBox->m_fMin[Z])
- pBox->m_fMin[Z] = pPoint->m_fOrigin[Z];
- }
- // Since we 'lathe' about the Z-Axis, copy the X-Axis
- // values to the Y-Axis
- pBox->m_fMax[Y] = pBox->m_fMax[X];
- pBox->m_fMin[Y] = pBox->m_fMin[X];
- // Since the points array only maps values 'typically'
- // defined along one side of the Z-Axis, negate the min
- // values to indicate the 'opposite' side..
- pBox->m_fMin[X] *= -1.0f;
- pBox->m_fMin[Y] *= -1.0f;
- }
- int C3dObjectLathe::LoadBitMapImage(CImageList* pList)
- {
- CBitmap bitmap;
- // If the image index has been stored in this object,
- // return the index.
- if(m_iBMImage > -1)
- return m_iBMImage;
- // If the image index for this object type has been
- // created, store the index for this object and
- // return the index.
- if( iObjectPlaneBMImage > -1) {
- m_iBMImage = iObjectPlaneBMImage;
- return m_iBMImage;
- }
- // The image index for this object type has not been
- // loaded and the object image index has not been
- // stored.
- //
- // Load the bitmap for the non-selected light
- bitmap.LoadBitmap(IDB_OBJECT_LATHE);
- m_iBMImage = pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- // Load the bitmap for the non-selected light
- bitmap.LoadBitmap(IDB_OBJECT_LATHE_SELECTED);
- pList->Add(&bitmap, (COLORREF)0xFFFFFF);
- bitmap.DeleteObject();
- iObjectPlaneBMImage = m_iBMImage;
- return m_iBMImage;
- }
- void C3dObjectLathe::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- // Save the Object Class header...
- szBuffer.Format("n%sC3dObjectLathe {n", szIndent);
- ar.WriteString(szBuffer);
- // Save the this objects' specific data...
- szBuffer.Format("%stSweepAngle < %f >n", szIndent, m_fSweepAngle);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stSegments < %d >n", szIndent, m_iSegments);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stNum Points < %d >n", szIndent, m_pPointArray->m_iNumPoints);
- ar.WriteString(szBuffer);
- for(int i=0; i<m_pPointArray->m_iNumPoints; i++)
- {
- szBuffer.Format("%stt < %f, %f, %f >n", szIndent, m_pPointArray->m_pPoints[i].m_fOrigin[0],
- m_pPointArray->m_pPoints[i].m_fOrigin[1],
- m_pPointArray->m_pPoints[i].m_fOrigin[2]);
- ar.WriteString(szBuffer);
- }
- szBuffer.Format("%stSmooth Faces < %d >n", szIndent, m_bSmooth);
- ar.WriteString(szBuffer);
- szBuffer.Format("%stMax Angle < %f >n", szIndent, m_fMaxAngle);
- ar.WriteString(szBuffer);
- // Save the base class object data...
- C3dObject::Serialize(ar, iVersion);
- szBuffer.Format("%s}n", szIndent); // end of object def
- ar.WriteString(szBuffer);
- }
- else
- {
- if(iVersion < 102)
- // Read the base class object data...
- C3dObject::Serialize(ar, iVersion);
- // Read the derived class data..
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft(); // Remove leading white spaces
- sscanf(szBuffer, "SweepAngle < %f >n", &m_fSweepAngle);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Segments < %d >n", &m_iSegments);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Num Points < %d >n", &m_pPointArray->m_iNumPoints);
- // Create the array of points
- if(!m_pPointArray->Create(m_pPointArray->m_iNumPoints))
- {
- // Now that the points array has been created,
- // read the point data.
- for(int i=0; i<m_pPointArray->m_iNumPoints; i++)
- {
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "< %f, %f, %f >n", &m_pPointArray->m_pPoints[i].m_fOrigin[0],
- &m_pPointArray->m_pPoints[i].m_fOrigin[1],
- &m_pPointArray->m_pPoints[i].m_fOrigin[2]);
- }
- }
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Smooth Faces < %d >n", &m_bSmooth);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Max Angle < %f >n", &m_fMaxAngle);
- if(iVersion < 102)
- // Read all child objects...
- LoadChildObjects(ar, iVersion);
- else
- // Read the base class object data...
- C3dObject::Serialize(ar, iVersion);
- }
- }
- void C3dObjectLathe::Build(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- GLfloat Theta, DTheta, HalfDTheta;
- GLfloat CosThetaMinus, SinThetaMinus;
- GLfloat CosThetaPlus, SinThetaPlus;
- LatheFace Vertice, Normal;
- GLfloat s, t; // Texture coordinates
- int i, j, k;
- // Our angles used to define vertices
- DTheta=m_fSweepAngle/m_iSegments;
- HalfDTheta=DTheta/2;
- Theta=HalfDTheta;
- s = (1/(m_fSweepAngle/DTheta));
- t = (1/(GLfloat)(m_pPointArray->m_iNumPoints-1));
- j = 0;
- glNewList(m_iDisplayLists, GL_COMPILE_AND_EXECUTE);
- do {
- i=0;
- k=m_pPointArray->m_iNumPoints-1;
- // Calcualte arc angles used to calcualte our
- // planes vertices
- CosThetaMinus=Cosf(Theta-HalfDTheta);
- SinThetaMinus=Sinf(Theta-HalfDTheta);
- CosThetaPlus=Cosf(Theta+HalfDTheta);
- SinThetaPlus=Sinf(Theta+HalfDTheta);
- do {
- // Calculate the plane vertices rotated about the Y-axis
- Vertice[0][X]=CosThetaPlus*m_pPointArray->m_pPoints[i].m_fOrigin[0];
- Vertice[0][Y]=SinThetaPlus*m_pPointArray->m_pPoints[i].m_fOrigin[0];
- Vertice[0][Z]=m_pPointArray->m_pPoints[i].m_fOrigin[2];
- Vertice[1][X]=CosThetaMinus*m_pPointArray->m_pPoints[i].m_fOrigin[0];
- Vertice[1][Y]=SinThetaMinus*m_pPointArray->m_pPoints[i].m_fOrigin[0];
- Vertice[1][Z]=m_pPointArray->m_pPoints[i].m_fOrigin[2];
- Vertice[2][X]=CosThetaMinus*m_pPointArray->m_pPoints[i+1].m_fOrigin[0];
- Vertice[2][Y]=SinThetaMinus*m_pPointArray->m_pPoints[i+1].m_fOrigin[0];
- Vertice[2][Z]=m_pPointArray->m_pPoints[i+1].m_fOrigin[2];
- Vertice[3][X]=CosThetaPlus*m_pPointArray->m_pPoints[i+1].m_fOrigin[0];
- Vertice[3][Y]=SinThetaPlus*m_pPointArray->m_pPoints[i+1].m_fOrigin[0];
- Vertice[3][Z]=m_pPointArray->m_pPoints[i+1].m_fOrigin[2];
- // Calculate the surface normals for each
- // of our lathe face vertices.
- if(m_bSmooth)
- CalSmoothNormals(Vertice, i, Theta, Normal);
- else
- CalNormals(Vertice, Normal);
- glBegin(GL_TRIANGLES);
- glNormal3f(Normal[0][X], Normal[0][Y], Normal[0][Z]);
- glTexCoord2f(s*(j+1), t*(k));
- glVertex3d(Vertice[0][X], Vertice[0][Y], Vertice[0][Z]);
- glNormal3f(Normal[1][X], Normal[1][Y], Normal[1][Z]);
- glTexCoord2f(s*(j), t*(k));
- glVertex3d(Vertice[1][X], Vertice[1][Y], Vertice[1][Z]);
- glNormal3f(Normal[2][X], Normal[2][Y], Normal[2][Z]);
- glTexCoord2f(s*(j), t*(k-1));
- glVertex3d(Vertice[2][X], Vertice[2][Y], Vertice[2][Z]);
- glTexCoord2f(s*(j), t*(k-1));
- glVertex3d(Vertice[2][X], Vertice[2][Y], Vertice[2][Z]);
- glNormal3f(Normal[3][X], Normal[3][Y], Normal[3][Z]);
- glTexCoord2f(s*(j+1), t*(k-1));
- glVertex3d(Vertice[3][X], Vertice[3][Y], Vertice[3][Z]);
- glNormal3f(Normal[0][X], Normal[0][Y], Normal[0][Z]);
- glTexCoord2f(s*(j+1), t*(k));
- glVertex3d(Vertice[0][X], Vertice[0][Y], Vertice[0][Z]);
- glEnd();
- ++i; // Increment face
- --k; // Decrement face
- } while(i<m_pPointArray->m_iNumPoints-1);
- Theta+=DTheta;
- ++j; // Increment segment
- } while(Theta<(m_fSweepAngle));
- glEndList();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dObjectLathe function implimentation
- void C3dObjectLathe::CalNormals(LatheFace Vertice, LatheFace Normal)
- {
- VECTORF u, v;
- // Calculate the surface normals for the lathe face
- VecSubf(Vertice[3], Vertice[0], u);
- VecSubf(Vertice[1], Vertice[0], v);
- if(!VecLenf(v))
- VecSubf(Vertice[2], Vertice[0], v);
- VecCrossf(v, u, Normal[0]);
- VecNormalizef(Normal[0]);
- VecSubf(Vertice[0], Vertice[1], u);
- if(!VecLenf(u))
- VecSubf(Vertice[3], Vertice[1], u);
- VecSubf(Vertice[2], Vertice[1], v);
- VecCrossf(v, u, Normal[1]);
- VecNormalizef(Normal[1]);
- VecSubf(Vertice[1], Vertice[2], u);
- VecSubf(Vertice[3], Vertice[2], v);
- if(!VecLenf(v))
- VecSubf(Vertice[0], Vertice[2], v);
- VecCrossf(v, u, Normal[2]);
- VecNormalizef(Normal[2]);
- VecSubf(Vertice[2], Vertice[3], u);
- if(!VecLenf(u))
- VecSubf(Vertice[1], Vertice[3], u);
- VecSubf(Vertice[0], Vertice[3], v);
- VecCrossf(v, u, Normal[3]);
- VecNormalizef(Normal[3]);
- }
- void C3dObjectLathe::CalSmoothNormals(LatheFace Vertice, int iFace, float fTheta, LatheFace Normal)
- {
- // This routine calcualtes the surface normal as
- // an average of it's surrounding normals as long
- // as the angle between adjacent normals is
- // within the member variable m_fMaxAngle.
- //
- // <Padj 1> <Padj 0>
- //
- // _____________
- // <Padj 2> P1 | /| P0 <Padj 7>
- // | / |
- // | / |
- // | / |
- // | / |
- // | / |
- // <Padj 3> P2 |/____________| P3 <Padj 6>
- //
- //
- // <Padj 4> <Padj 5>
- //
- GLfloat DTheta, HalfDTheta;
- GLfloat CosThetaMinus, SinThetaMinus;
- GLfloat CosThetaPlus, SinThetaPlus;
- GLfloat CosThetaMinusMinus, SinThetaMinusMinus;
- GLfloat CosThetaPlusPlus, SinThetaPlusPlus;
- GLfloat fAngle;
- VECTORF AdjVertice[8];
- VECTORF u, v;
- VECTORF AdjNormal;
- VECTORF AveNormal;
- int iNumAveNormals;
- // Our angles used to define vertices
- DTheta=m_fSweepAngle/m_iSegments;
- HalfDTheta=DTheta/2;
- // Arc angles used to calcualte this
- // planes vertices
- CosThetaMinus=Cosf(fTheta-HalfDTheta);
- SinThetaMinus=Sinf(fTheta-HalfDTheta);
- CosThetaPlus=Cosf(fTheta+HalfDTheta);
- SinThetaPlus=Sinf(fTheta+HalfDTheta);
- // Arc angles used to calcualte our
- // planes adjacent vertices
- CosThetaMinusMinus=Cosf((fTheta-DTheta)-HalfDTheta);
- SinThetaMinusMinus=Sinf((fTheta-DTheta)-HalfDTheta);
- CosThetaPlusPlus=Cosf((fTheta+DTheta)+HalfDTheta);
- SinThetaPlusPlus=Sinf((fTheta+DTheta)+HalfDTheta);
- if(iFace) {
- // Calculate the previous planes vertices
- AdjVertice[0][X]=CosThetaPlus*m_pPointArray->m_pPoints[iFace-1].m_fOrigin[0];
- AdjVertice[0][Y]=SinThetaPlus*m_pPointArray->m_pPoints[iFace-1].m_fOrigin[0];
- AdjVertice[0][Z]=m_pPointArray->m_pPoints[iFace-1].m_fOrigin[2];
- AdjVertice[1][X]=CosThetaMinus*m_pPointArray->m_pPoints[iFace-1].m_fOrigin[0];
- AdjVertice[1][Y]=SinThetaMinus*m_pPointArray->m_pPoints[iFace-1].m_fOrigin[0];
- AdjVertice[1][Z]=m_pPointArray->m_pPoints[iFace-1].m_fOrigin[2];
- }
- // Calculate the left planes vertices
- AdjVertice[2][X]=CosThetaMinusMinus*m_pPointArray->m_pPoints[iFace].m_fOrigin[0];
- AdjVertice[2][Y]=SinThetaMinusMinus*m_pPointArray->m_pPoints[iFace].m_fOrigin[0];
- AdjVertice[2][Z]=m_pPointArray->m_pPoints[iFace].m_fOrigin[2];
- AdjVertice[3][X]=CosThetaMinusMinus*m_pPointArray->m_pPoints[iFace+1].m_fOrigin[0];
- AdjVertice[3][Y]=SinThetaMinusMinus*m_pPointArray->m_pPoints[iFace+1].m_fOrigin[0];
- AdjVertice[3][Z]=m_pPointArray->m_pPoints[iFace+1].m_fOrigin[2];
- if(iFace<(m_pPointArray->m_iNumPoints-1)) {
- // Calculate the next planes vertices
- AdjVertice[4][X]=CosThetaMinus*m_pPointArray->m_pPoints[iFace+2].m_fOrigin[0];
- AdjVertice[4][Y]=SinThetaMinus*m_pPointArray->m_pPoints[iFace+2].m_fOrigin[0];
- AdjVertice[4][Z]=m_pPointArray->m_pPoints[iFace+2].m_fOrigin[2];
- AdjVertice[5][X]=CosThetaPlus*m_pPointArray->m_pPoints[iFace+2].m_fOrigin[0];
- AdjVertice[5][Y]=SinThetaPlus*m_pPointArray->m_pPoints[iFace+2].m_fOrigin[0];
- AdjVertice[5][Z]=m_pPointArray->m_pPoints[iFace+2].m_fOrigin[2];
- }
- // Calculate the right planes vertices
- AdjVertice[6][X]=CosThetaPlusPlus*m_pPointArray->m_pPoints[iFace+1].m_fOrigin[0];
- AdjVertice[6][Y]=SinThetaPlusPlus*m_pPointArray->m_pPoints[iFace+1].m_fOrigin[0];
- AdjVertice[6][Z]=m_pPointArray->m_pPoints[iFace+1].m_fOrigin[2];
- AdjVertice[7][X]=CosThetaPlusPlus*m_pPointArray->m_pPoints[iFace].m_fOrigin[0];
- AdjVertice[7][Y]=SinThetaPlusPlus*m_pPointArray->m_pPoints[iFace].m_fOrigin[0];
- AdjVertice[7][Z]=m_pPointArray->m_pPoints[iFace].m_fOrigin[2];
- // Now that we have the adjacent face vertices,
- // calculate the average surface normal for each
- // face vertice..
- ///////////////////////////////////////////////////
- // Face Vertice '0' average surface normal:
- iNumAveNormals = 0;
- VecSubf(Vertice[3], Vertice[0], u);
- VecSubf(Vertice[1], Vertice[0], v);
- if(!VecLenf(v))
- VecSubf(Vertice[2], Vertice[0], v);
- VecCrossf(v, u, Normal[0]);
- VecCopy3f(Normal[0], AveNormal);
- ++iNumAveNormals;
- if(iFace) {
- VecSubf(Vertice[1], Vertice[0], u);
- VecSubf(AdjVertice[0], Vertice[0], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- // where:
- // u.v = u1v1 + u2v2 + u3v3
- // u.v = |u||v|cos theta
- // theta = inv cos(u.v/|u||v|)
- //
- fAngle = Degreesf( acos( (VecDotf(Normal[0], AdjNormal)) / (VecLenf(Normal[0])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- VecSubf(AdjVertice[0], Vertice[0], u);
- VecSubf(AdjVertice[7], Vertice[0], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- fAngle = Degreesf( acos( (VecDotf(Normal[0], AdjNormal)) / (VecLenf(Normal[0])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- }
- VecSubf(AdjVertice[7], Vertice[0], u);
- VecSubf(Vertice[3], Vertice[0], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- fAngle = Degreesf( acos( (VecDotf(Normal[0], AdjNormal)) / (VecLenf(Normal[0])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- // Calculate the average surface normal for face
- // vertice '0'
- Normal[0][X] = AveNormal[X]/iNumAveNormals;
- Normal[0][Y] = AveNormal[Y]/iNumAveNormals;
- Normal[0][Z] = AveNormal[Z]/iNumAveNormals;
- VecNormalizef(Normal[0]);
- ///////////////////////////////////////////////////
- // Face Vertice '1' average surface normal:
- iNumAveNormals = 0;
- VecSubf(Vertice[0], Vertice[1], u);
- if(!VecLenf(u))
- VecSubf(Vertice[3], Vertice[1], u);
- VecSubf(Vertice[2], Vertice[1], v);
- VecCrossf(v, u, Normal[1]);
- VecCopy3f(Normal[1], AveNormal);
- ++iNumAveNormals;
- if(iFace) {
- VecSubf(AdjVertice[1], Vertice[1], u);
- VecSubf(Vertice[0], Vertice[1], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- // where:
- // u.v = u1v1 + u2v2 + u3v3
- // u.v = |u||v|cos theta
- // theta = inv cos(u.v/|u||v|)
- //
- fAngle = Degreesf( acos( (VecDotf(Normal[1], AdjNormal)) / (VecLenf(Normal[1])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- VecSubf(AdjVertice[2], Vertice[1], u);
- VecSubf(AdjVertice[1], Vertice[1], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- fAngle = Degreesf( acos( (VecDotf(Normal[1], AdjNormal)) / (VecLenf(Normal[1])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- }
- VecSubf(Vertice[2], Vertice[1], u);
- VecSubf(AdjVertice[2], Vertice[1], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- fAngle = Degreesf( acos( (VecDotf(Normal[1], AdjNormal)) / (VecLenf(Normal[1])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- // Calculate the average surface normal for face
- // vertice '1'
- Normal[1][X] = AveNormal[X]/iNumAveNormals;
- Normal[1][Y] = AveNormal[Y]/iNumAveNormals;
- Normal[1][Z] = AveNormal[Z]/iNumAveNormals;
- VecNormalizef(Normal[1]);
- ///////////////////////////////////////////////////
- // Face Vertice '2' average surface normal:
- iNumAveNormals = 0;
- VecSubf(Vertice[1], Vertice[2], u);
- VecSubf(Vertice[3], Vertice[2], v);
- if(!VecLenf(v))
- VecSubf(Vertice[0], Vertice[2], v);
- VecCrossf(v, u, Normal[2]);
- VecCopy3f(Normal[2], AveNormal);
- ++iNumAveNormals;
- if(iFace<(m_pPointArray->m_iNumPoints-1)) {
- VecSubf(AdjVertice[4], Vertice[2], u);
- VecSubf(AdjVertice[3], Vertice[2], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- // where:
- // u.v = u1v1 + u2v2 + u3v3
- // u.v = |u||v|cos theta
- // theta = inv cos(u.v/|u||v|)
- //
- fAngle = Degreesf( acos( (VecDotf(Normal[2], AdjNormal)) / (VecLenf(Normal[2])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- VecSubf(Vertice[3], Vertice[2], u);
- VecSubf(AdjVertice[4], Vertice[2], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- fAngle = Degreesf( acos( (VecDotf(Normal[2], AdjNormal)) / (VecLenf(Normal[2])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- }
- VecSubf(AdjVertice[3], Vertice[2], u);
- VecSubf(Vertice[1], Vertice[2], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- fAngle = Degreesf( acos( (VecDotf(Normal[2], AdjNormal)) / (VecLenf(Normal[2])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- // Calculate the average surface normal for face
- // vertice '2'
- Normal[2][X] = AveNormal[X]/iNumAveNormals;
- Normal[2][Y] = AveNormal[Y]/iNumAveNormals;
- Normal[2][Z] = AveNormal[Z]/iNumAveNormals;
- VecNormalizef(Normal[2]);
- ///////////////////////////////////////////////////
- // Face Vertice '3' average surface normal:
- iNumAveNormals = 0;
- VecSubf(Vertice[2], Vertice[3], u);
- if(!VecLenf(u))
- VecSubf(Vertice[1], Vertice[3], u);
- VecSubf(Vertice[0], Vertice[3], v);
- VecCrossf(v, u, Normal[3]);
- VecCopy3f(Normal[3], AveNormal);
- ++iNumAveNormals;
- if(iFace<(m_pPointArray->m_iNumPoints-1)) {
- VecSubf(AdjVertice[6], Vertice[3], u);
- VecSubf(AdjVertice[5], Vertice[3], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- // where:
- // u.v = u1v1 + u2v2 + u3v3
- // u.v = |u||v|cos theta
- // theta = inv cos(u.v/|u||v|)
- //
- fAngle = Degreesf( acos( (VecDotf(Normal[3], AdjNormal)) / (VecLenf(Normal[3])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- VecSubf(AdjVertice[5], Vertice[3], u);
- VecSubf(Vertice[2], Vertice[3], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- fAngle = Degreesf( acos( (VecDotf(Normal[3], AdjNormal)) / (VecLenf(Normal[3])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- }
- VecSubf(Vertice[0], Vertice[3], u);
- VecSubf(AdjVertice[6], Vertice[3], v);
- VecCrossf(v, u, AdjNormal);
- // Calculate angle between the Normal vectors
- fAngle = Degreesf( acos( (VecDotf(Normal[3], AdjNormal)) / (VecLenf(Normal[3])*VecLenf(AdjNormal)) ));
- if(fAngle <= m_fMaxAngle) {
- VecAddf(AveNormal, AdjNormal, AveNormal);
- ++iNumAveNormals;
- }
- // Calculate the average surface normal for face
- // vertice '3'
- Normal[3][X] = AveNormal[X]/iNumAveNormals;
- Normal[3][Y] = AveNormal[Y]/iNumAveNormals;
- Normal[3][Z] = AveNormal[Z]/iNumAveNormals;
- VecNormalizef(Normal[3]);
- }