3dPoint.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:9k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dPoint.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 <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////
- // C3dPoint
- IMPLEMENT_DYNAMIC(C3dPoint, CObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dPoint Construction
- C3dPoint::C3dPoint()
- {
- // Set the attributes to default values..
- m_fOrigin[0] = 0.0f;
- m_fOrigin[1] = 0.0f;
- m_fOrigin[2] = 0.0f;
- m_fOrigin[3] = 0.0f;
- m_Color.SetColor4f(.5f, .5f, .5f, .0f); // Default gray color
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dPoint Destructor
- C3dPoint::~C3dPoint()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dPoint static functions
- void C3dPoint::Display(float x,
- float y,
- float z,
- float fRadius,
- float fColor[4],
- BOOL bSolid)
- {
- GLint iSlices = 5;
- GLint iStacks = 5;
- // Save the current transformation matrix..
- glPushMatrix();
- // Disable textsure maping
- glDisable(GL_TEXTURE_2D);
- // Create a quadratic object used to draw our cylinders
- GLUquadricObj* pSphere = gluNewQuadric();
- ASSERT(pSphere);
- if(bSolid) {
- glPolygonMode(GL_FRONT, GL_FILL);
- gluQuadricDrawStyle(pSphere, GLU_FILL);
- }
- else {
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- gluQuadricDrawStyle(pSphere, GLU_LINE);
- }
- // Set the Cylinder color
- if(fColor)
- glColor4fv(fColor);
- else {
- // Define default color..
- GLfloat fDefColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
- glColor4fv(fDefColor);
- }
- // translate the points origin
- glTranslated( x, y, z);
- gluSphere(pSphere, (GLdouble)fRadius, iSlices, iStacks);
- gluDeleteQuadric(pSphere);
- // Restore the current transformation matrix..
- glPopMatrix();
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dPoint virtual function implimentation
- void C3dPoint::Serialize(CArchive& ar)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- // Save the Object Class header...
- // szBuffer.Format("n%sC3dPoint {n", szIndent);
- // ar.WriteString(szBuffer);
- // Save the base class object data...
- // C3dObject::Serialize(ar);
- // Save the this objects' specific data...
- // szBuffer.Format("%stDepth < %f >n", szIndent, m_fDepth);
- // ar.WriteString(szBuffer);
- // szBuffer.Format("%s}n", szIndent); // end of object def
- // ar.WriteString(szBuffer);
- }
- else
- {
- // Read the derived class data..
- // ar.ReadString(szBuffer);
- // szBuffer.TrimLeft(); // Remove leading white spaces
- // sscanf(szBuffer, "Depth < %f >n", &m_fDepth);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dPoint function implimentation
- void C3dPoint::Display(float fRadius,
- float fColor[4],
- BOOL bSolid)
- {
- GLint iSlices = 5;
- GLint iStacks = 5;
- // Create a quadratic object used to draw our point
- GLUquadricObj* pQuad = gluNewQuadric();
- ASSERT(pQuad);
- // Save the current transformation matrix..
- glPushMatrix();
- // Disable textsure maping
- glDisable(GL_TEXTURE_2D);
- if(bSolid) {
- glPolygonMode(GL_FRONT, GL_FILL);
- gluQuadricDrawStyle(pQuad, GLU_FILL);
- }
- else {
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- gluQuadricDrawStyle(pQuad, GLU_LINE);
- }
- // Set the Cylinder color
- if(fColor)
- glColor4fv(fColor);
- else {
- // Define default color..
- GLfloat fDefColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
- glColor4fv(fDefColor);
- }
- // translate the points origin
- glTranslated( m_fOrigin[0], m_fOrigin[1], m_fOrigin[2]);
- gluSphere(pQuad, (GLdouble)fRadius, iSlices, iStacks);
- // Restore the current transformation matrix..
- glPopMatrix();
- gluDeleteQuadric(pQuad);
- }
- BOOL C3dPoint::HitTest(float x, float y, float z, float test)
- {
- float dif;
- dif = m_fOrigin[0]-x;
- if(fabs(dif)>test)
- return FALSE;
- dif = m_fOrigin[1]-y;
- if(fabs(dif)>test)
- return FALSE;
- dif = m_fOrigin[2]-z;
- if(fabs(dif)>test)
- return FALSE;
- return TRUE;
- }
- void C3dPoint::SetOrigin(GLfloat x, GLfloat y, GLfloat z)
- {
- // Set the point's origin
- m_fOrigin[X] = x;
- m_fOrigin[Y] = y;
- m_fOrigin[Z] = z;
- }
- void C3dPoint::GetOrigin(GLfloat *x, GLfloat *y, GLfloat *z)
- {
- // Get the points origin
- *x = m_fOrigin[X];
- *y = m_fOrigin[Y];
- *z = m_fOrigin[Z];
- }
- BOOL C3dPoint::IsPoint(C3dCamera* pCamera, C3dObject* pObject,
- float x, float y, float z)
- {
- // IsPoint checks to see if the given x,y,z coordinate passes through this points
- // coordinates.
- // We perform the following steps to 'check' a point:
- // 1. Get the direction vector of the given x,y,z coordinate relative to the
- // camera position.
- // 2. Get the direction vector of a transformed point in the array relative
- // to the camera position.
- // 3. Calcualte the angle between these two vectors.
- // 4. If the angle is small, this is the point!
- //
- Matx4x4 ObjXformMatrix;
- VECTORF CameraPosn, MousePosn, temp;
- VECTORF CameraToMouse, CameraToPoint;
- VECTORF PointPosn;
- float cosTheta;
- if(!pCamera || !pObject)
- return NULL;
- // Convert the mouse position to a vector
- Vec3f(x, y, z, MousePosn);
- // Get the camera origin
- Vec3f(pCamera->m_fOrigin[X],
- pCamera->m_fOrigin[Y],
- pCamera->m_fOrigin[Z],
- temp);
- // Transform the camera's origin to world coordinates
- Transformf(temp, CameraPosn, pCamera->m_dModelViewMatrix);
- // Subtract the camera coordinate from the mouse coordinate
- // and normalize. This is the direction vector from the Camera
- // to the mouse.
- VecSubf(CameraPosn, MousePosn, CameraToMouse);
- VecNormalizef(CameraToMouse);
- // Get the objects transformation matrix. We will use this to
- // transform each point in the array to world coordinates.
- pObject->GetTransformMatrix(ObjXformMatrix);
- // Convert this point to a vector and multiply by the
- // transformation matrix
- Vec3f(m_fOrigin[X], m_fOrigin[Y], m_fOrigin[Z], temp);
- VecTransformf(temp, PointPosn, ObjXformMatrix);
- // Subtract the camera coordinate from the array point and
- // normalize. This is the direction vector from the Camera
- // to the array point.
- VecSubf(CameraPosn, PointPosn, CameraToPoint);
- VecNormalizef(CameraToPoint);
- // Calcualte the angle between vectors
- cosTheta = Degreesf(acos(VecDotf(CameraToMouse, CameraToPoint)));
- if(cosTheta < 0.5f) // 1/2 degree tolerence
- return TRUE; // Found a match
- return FALSE;
- }
- BOOL C3dPoint::IsPoint(C3dCamera* pCamera, Matx4x4 XformMatrix,
- float x, float y, float z)
- {
- // IsPoint checks to see if the given x,y,z coordinate passes through this points
- // coordinates.
- // We perform the following steps to 'check' a point:
- // 1. Get the direction vector of the given x,y,z coordinate relative to the
- // camera position.
- // 2. Get the direction vector of a transformed point in the array relative
- // to the camera position.
- // 3. Calcualte the angle between these two vectors.
- // 4. If the angle is small, this is the point!
- //
- VECTORF CameraPosn, MousePosn, temp;
- VECTORF CameraToMouse, CameraToPoint;
- VECTORF PointPosn;
- float cosTheta;
- if(!pCamera)
- return FALSE;
- // Convert the mouse position to a vector
- Vec3f(x, y, z, MousePosn);
- // Get the camera origin
- Vec3f(pCamera->m_fOrigin[X],
- pCamera->m_fOrigin[Y],
- pCamera->m_fOrigin[Z],
- temp);
- // Transform the camera's origin to world coordinates
- Transformf(temp, CameraPosn, pCamera->m_dModelViewMatrix);
- // Subtract the camera coordinate from the mouse coordinate
- // and normalize. This is the direction vector from the Camera
- // to the mouse.
- VecSubf(CameraPosn, MousePosn, CameraToMouse);
- VecNormalizef(CameraToMouse);
- // Convert this point to a vector and multiply by the
- // transformation matrix
- Vec3f(m_fOrigin[X], m_fOrigin[Y], m_fOrigin[Z], temp);
- VecTransformf(temp, PointPosn, XformMatrix);
- // Subtract the camera coordinate from the array point and
- // normalize. This is the direction vector from the Camera
- // to the array point.
- VecSubf(CameraPosn, PointPosn, CameraToPoint);
- VecNormalizef(CameraToPoint);
- // Calcualte the angle between vectors
- cosTheta = Degreesf(acos(VecDotf(CameraToMouse, CameraToPoint)));
- if(cosTheta < 0.5f) // 1/2 degree tolerence
- return TRUE; // Found a match
- return FALSE;
- }