3dGrid.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:4k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dGrid.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
- /////////////////////////////////////////////////////////////////////////////
- // C3dGrid
- IMPLEMENT_DYNAMIC(C3dGrid, CObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dGrid construction
- C3dGrid::C3dGrid()
- {
- // Assign Default values to member attributes
- m_fGridWidth = 20.0f;
- m_fGridDepth = 20.0f;
- m_fSpacing = 1.0f;
- m_fLineWidth = 0.05f;
- m_fGridColor[0] = 0.50f; // light gray
- m_fGridColor[1] = 0.50f;
- m_fGridColor[2] = 0.50f;
- m_fGridColor[3] = 1.00f;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dGrid Destructor
- C3dGrid::~C3dGrid()
- {
- }
- C3dGrid* C3dGrid::Create(float fSpacing)
- {
- C3dGrid* pGrid = new C3dGrid;
- ASSERT_VALID(pGrid);
- if(fSpacing)
- pGrid->m_fSpacing = (GLfloat)fSpacing;
- return pGrid;
- }
- void C3dGrid::SetSize(float fSpacing)
- {
- m_fSpacing = (GLfloat)fSpacing;
- }
- void C3dGrid::PointToGrid(float *x, float *y, float *z)
- {
- *x=Roundf(*x/m_fSpacing)*m_fSpacing;
- *y=Roundf(*y/m_fSpacing)*m_fSpacing;
- *z=Roundf(*z/m_fSpacing)*m_fSpacing;
- }
- void C3dGrid::Display(C3dWorld* pWorld, C3dCamera* pCamera)
- {
- if(!pCamera)
- return;
- // Save our current state
- glPushAttrib(GL_ALL_ATTRIB_BITS);
- // Save our modelview matrix
- glPushMatrix();
- // Disable lighting calculations..
- glDisable(GL_LIGHTING);
- // Reset color parameters to default values
- glDisable(GL_TEXTURE_2D);
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glColor4fv(m_fGridColor);
- glLineWidth(m_fLineWidth);
- if(pCamera->m_iViewType == VIEW_ISO)
- // Grid3D(pCamera);
- GridXY(pCamera);
- else
- {
- if(pCamera->m_iViewType == VIEW_TOP)
- {
- GridXY(pCamera);
- }
- else if(pCamera->m_iViewType == VIEW_FRONT)
- {
- glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
- GridXY(pCamera);
- }
- else if(pCamera->m_iViewType == VIEW_SIDE)
- {
- glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
- glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
- GridXY(pCamera);
- }
- }
- // Restore our modelview matrix
- glPopMatrix();
- // Restore our state
- glPopAttrib();
- }
- void C3dGrid::Grid3D(C3dCamera* pCamera)
- {
- // Draw the bottom XY grid
- GridXY(pCamera);
- // Draw the back XZ grid
- glPushMatrix(); // Save our modelview matrix
- glTranslatef(0.0f, m_fGridDepth/2, m_fGridDepth/2);
- glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
- GridXY(pCamera);
- glPopMatrix(); // Restore our modelview matrix
- // Draw the side YZ grid
- glPushMatrix(); // Save our modelview matrix
- glTranslatef(-m_fGridWidth/2, 0.0f, m_fGridWidth/2);
- glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
- GridXY(pCamera);
- glPopMatrix(); // Restore our modelview matrix
- }
- void C3dGrid::GridXY(C3dCamera* pCamera)
- {
- GLfloat x, y;
- // Draw the Vertical lines
- for(x=-(m_fGridWidth/2); x<=(m_fGridWidth/2); x+=m_fSpacing)
- {
- glBegin(GL_LINES);
- glVertex3f(x*m_fSpacing, m_fGridDepth/2, 0.0f); // top
- glVertex3f(x*m_fSpacing, -m_fGridDepth/2, 0.0f); // bottom
- glEnd();
- }
- // Draw the Horizontal lines
- for(y=-(m_fGridDepth/2); y<=(m_fGridDepth/2); y+=m_fSpacing)
- {
- glBegin(GL_LINES);
- glVertex3f(-m_fGridWidth/2, y*m_fSpacing, 0.0f); // left
- glVertex3f( m_fGridWidth/2, y*m_fSpacing, 0.0f); // right
- glEnd();
- }
- }