3dBoundingBox.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:7k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dBoundingBox.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"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////
- // C3dBoundingBox
- IMPLEMENT_DYNAMIC(C3dBoundingBox, CObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dBoundingBox construction
- C3dBoundingBox::C3dBoundingBox()
- {
- // Assign Default values to member attributes
- m_fHighlightColor[0] = 0.90f; // Bright Red
- m_fHighlightColor[1] = 0.10f;
- m_fHighlightColor[2] = 0.10f;
- m_fHighlightColor[3] = 1.00f;
- m_fHighlightOffset = 0.50f;
- m_fHighlightLineWidth = 4.00f;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dBoundingBox Destructor
- C3dBoundingBox::~C3dBoundingBox()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dBoundingBox Implementation/Methods
- void C3dBoundingBox::TransformBounds(Matx4x4 XformMatrix)
- {
- // Our bounding box is defined my two (2) member variables; m_fMin & m_fMax
- // with these two points, we can derive the remaining points of our bounding
- // box, which is defined as follows:
- /*
- p7=m_fMin[X],m_fMax[Y],m_fMax[Z] * ____________ * p1=m_fMax
- / /
- / / |
- p6=m_fMin[X],m_fMin[Y],m_fMax[Z] * /------------/ * p2=m_fMax[X],m_fMin[Y],m_fMax[Z]
- | Z Y | |
- | | / | |
- | -- X | |
- p8=m_fMin[X],m_fMax[Y],m_fMin[Z] * | / * p4=m_fMax[X],m_fMax[Y],m_fMin[Z]
- | | /
- p5=m_fMin * |____________|/ * p3=m_fMax[X],m_fMin[Y],m_fMin[Z]
- */
- // Calculate bounding box remaining six(6) points, multiply by the transformation
- // matrix, then determine the new transformed bounding box m_fMin & m_fMax
- // coordinates.
- VECTORF p1, p2, p3, p4, p5, p6, p7, p8;
- if(!XformMatrix)
- return; // nothing to do!
- VecCopy3f(m_fMax, p1);
- Vec3f(m_fMax[X], m_fMin[Y], m_fMax[Z], p2);
- Vec3f(m_fMax[X], m_fMin[Y], m_fMin[Z], p3);
- Vec3f(m_fMax[X], m_fMax[Y], m_fMin[Z], p4);
- VecCopy3f(m_fMin, p5);
- Vec3f(m_fMin[X], m_fMin[Y], m_fMax[Z], p6);
- Vec3f(m_fMin[X], m_fMax[Y], m_fMax[Z], p7);
- Vec3f(m_fMin[X], m_fMax[Y], m_fMin[Z], p8);
- // Transform the bounding box points
- VecTransformf(p1, p1, XformMatrix);
- VecTransformf(p2, p2, XformMatrix);
- VecTransformf(p3, p3, XformMatrix);
- VecTransformf(p4, p4, XformMatrix);
- VecTransformf(p5, p5, XformMatrix);
- VecTransformf(p6, p6, XformMatrix);
- VecTransformf(p7, p7, XformMatrix);
- VecTransformf(p8, p8, XformMatrix);
- // Find the Min/Max bounds of the newly transformed bounding box
- SetMinMax(p1, p2);
- SetMinMax(p3);
- SetMinMax(p4);
- SetMinMax(p5);
- SetMinMax(p6);
- SetMinMax(p7);
- SetMinMax(p8);
- }
- void C3dBoundingBox::SetMinMax(VECTORF A)
- {
- if(A[X] < m_fMin[X])
- m_fMin[X] = A[X];
- if(A[X] > m_fMax[X])
- m_fMax[X] = A[X];
- if(A[Y] < m_fMin[Y])
- m_fMin[Y] = A[Y];
- if(A[Y] > m_fMax[Y])
- m_fMax[Y] = A[Y];
- if(A[Z] < m_fMin[Z])
- m_fMin[Z] = A[Z];
- if(A[Z] > m_fMax[Z])
- m_fMax[Z] = A[Z];
- }
- void C3dBoundingBox::SetMinMax(VECTORF A, VECTORF B)
- {
- if(A[X] < B[X]) {
- m_fMin[X] = A[X];
- m_fMax[X] = B[X];
- }
- else {
- m_fMin[X] = B[X];
- m_fMax[X] = A[X];
- }
- if(A[Y] < B[Y]) {
- m_fMin[Y] = A[Y];
- m_fMax[Y] = B[Y];
- }
- else {
- m_fMin[Y] = B[Y];
- m_fMax[Y] = A[Y];
- }
- if(A[Z] < B[Z]) {
- m_fMin[Z] = A[Z];
- m_fMax[Z] = B[Z];
- }
- else {
- m_fMin[Z] = B[Z];
- m_fMax[Z] = A[Z];
- }
- }
- void C3dBoundingBox::SetMinMax(C3dBoundingBox* pBox)
- {
- if(pBox->m_fMin[X] < m_fMin[X])
- m_fMin[X] = pBox->m_fMin[X];
- if(pBox->m_fMax[X] > m_fMax[X])
- m_fMax[X] = pBox->m_fMax[X];
- if(pBox->m_fMin[Y] < m_fMin[Y])
- m_fMin[Y] = pBox->m_fMin[Y];
- if(pBox->m_fMax[Y] > m_fMax[Y])
- m_fMax[Y] = pBox->m_fMax[Y];
- if(pBox->m_fMin[Z] < m_fMin[Z])
- m_fMin[Z] = pBox->m_fMin[Z];
- if(pBox->m_fMax[Z] > m_fMax[Z])
- m_fMax[Z] = pBox->m_fMax[Z];
- }
- void C3dBoundingBox::Display(float fOffset,
- float fLineSize,
- int iSlices,
- int iStacks,
- float fColor[3],
- BOOL bSolid)
- {
- VECTORF p1, p2, p3, p4, p5, p6, p7, p8;
- VecCopy3f(m_fMax, p1);
- Vec3f(m_fMax[X], m_fMin[Y], m_fMax[Z], p2);
- Vec3f(m_fMax[X], m_fMin[Y], m_fMin[Z], p3);
- Vec3f(m_fMax[X], m_fMax[Y], m_fMin[Z], p4);
- VecCopy3f(m_fMin, p5);
- Vec3f(m_fMin[X], m_fMin[Y], m_fMax[Z], p6);
- Vec3f(m_fMin[X], m_fMax[Y], m_fMax[Z], p7);
- Vec3f(m_fMin[X], m_fMax[Y], m_fMin[Z], p8);
- // Save our current state..
- glPushAttrib(GL_ALL_ATTRIB_BITS);
- // Disable texture maping
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_LIGHTING);
- // Display bounding box as wireframe..
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- // Set the Box's color
- if(fColor)
- glColor4fv(fColor);
- else
- glColor4fv(m_fHighlightColor);
- glLineWidth(3.0f);
- glBegin(GL_POLYGON); // front CCW order...
- glVertex3fv(p2);
- glVertex3fv(p6);
- glVertex3fv(p5);
- glVertex3fv(p3);
- glEnd();
- glBegin(GL_POLYGON); // back
- glVertex3fv(p1);
- glVertex3fv(p4);
- glVertex3fv(p8);
- glVertex3fv(p7);
- glEnd();
- glBegin(GL_POLYGON); // left
- glVertex3fv(p5);
- glVertex3fv(p6);
- glVertex3fv(p7);
- glVertex3fv(p8);
- glEnd();
- glBegin(GL_POLYGON); // right
- glVertex3fv(p1);
- glVertex3fv(p2);
- glVertex3fv(p3);
- glVertex3fv(p4);
- glEnd();
- glBegin(GL_POLYGON); // top
- glVertex3fv(p1);
- glVertex3fv(p2);
- glVertex3fv(p6);
- glVertex3fv(p7);
- glEnd();
- glBegin(GL_POLYGON); // bottom
- glVertex3fv(p4);
- glVertex3fv(p3);
- glVertex3fv(p5);
- glVertex3fv(p8);
- glEnd();
- // Restore our states
- glPopAttrib();
- }