3dAxis.cpp
Upload User: shxiangxiu
Upload Date: 2007-01-03
Package Size: 1101k
Code Size: 6k
Category:

OpenGL program

Development Platform:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // 3dAxis.cpp : Implementation file of the 3dAxis class
  3. //
  4. // glOOP (OpenGL Object Oriented Programming library)
  5. // Copyright (c) Craig Fahrnbach 1997, 1998
  6. //
  7. // OpenGL is a registered trademark of Silicon Graphics
  8. //
  9. //
  10. // This program is provided for educational and personal use only and
  11. // is provided without guarantee or warrantee expressed or implied.
  12. //
  13. // Commercial use is strickly prohibited without written permission
  14. // from ImageWare Development.
  15. //
  16. // This program is -not- in the public domain.
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #include "stdafx.h"
  20. #include "glOOP.h"
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. //////////////////////////////////////////////////////////////////
  27. // C3dAxis
  28. IMPLEMENT_DYNAMIC(C3dAxis, CObject)
  29. /////////////////////////////////////////////////////////////////////////////
  30. // C3dAxis construction
  31. C3dAxis::C3dAxis()
  32. {
  33. // Assign Default values to member attributes
  34. m_fXAxisColor[0] = 1.00f; // Red
  35. m_fXAxisColor[1] = 0.00f;
  36. m_fXAxisColor[2] = 0.00f;
  37. m_fXAxisColor[3] = 1.00f;
  38. m_fYAxisColor[0] = 0.00f; // Green
  39. m_fYAxisColor[1] = 1.00f;
  40. m_fYAxisColor[2] = 0.00f;
  41. m_fYAxisColor[3] = 1.00f;
  42. m_fZAxisColor[0] = 0.00f; // Blue
  43. m_fZAxisColor[1] = 0.00f;
  44. m_fZAxisColor[2] = 1.00f;
  45. m_fZAxisColor[3] = 1.00f;
  46. }
  47. /////////////////////////////////////////////////////////////////////////////
  48. // C3dAxis Destructor
  49. C3dAxis::~C3dAxis()
  50. {
  51. }
  52. /////////////////////////////////////////////////////////////////////////////
  53. // C3dAxis virtual overrides
  54. void C3dAxis::Serialize(CArchive& ar)
  55. {
  56. CString szBuffer;
  57. if (ar.IsStoring())
  58. {
  59. // Save the C3dAxis class header
  60. szBuffer.Format("C3dAxis {n");
  61. ar.WriteString(szBuffer);
  62. // Save the axis default color information
  63. szBuffer.Format("tX-AxisColor   < %f %f %f %f >n", m_fXAxisColor[0], m_fXAxisColor[1], m_fXAxisColor[2], m_fXAxisColor[3]);
  64. ar.WriteString(szBuffer);
  65. szBuffer.Format("tY-AxisColor   < %f %f %f %f >n", m_fYAxisColor[0], m_fYAxisColor[1], m_fYAxisColor[2], m_fYAxisColor[3]);
  66. ar.WriteString(szBuffer);
  67. szBuffer.Format("tZ-AxisColor   < %f %f %f %f >n", m_fZAxisColor[0], m_fZAxisColor[1], m_fZAxisColor[2], m_fZAxisColor[3]);
  68. ar.WriteString(szBuffer);
  69. szBuffer.Format("}nn"); // end of material def
  70. ar.WriteString(szBuffer);
  71. }
  72. else
  73. {
  74. // Read the axis color data...
  75. ar.ReadString(szBuffer);
  76. szBuffer.TrimLeft(); // Remove leading white spaces
  77. sscanf(szBuffer, "X-AxisColor   < %f %f %f %f >n", &m_fXAxisColor[0], &m_fXAxisColor[1], &m_fXAxisColor[2], &m_fXAxisColor[3]);
  78. ar.ReadString(szBuffer);
  79. szBuffer.TrimLeft();
  80. sscanf(szBuffer, "Y-AxisColor   < %f %f %f %f >n", &m_fYAxisColor[0], &m_fYAxisColor[1], &m_fYAxisColor[2], &m_fYAxisColor[3]);
  81. ar.ReadString(szBuffer);
  82. szBuffer.TrimLeft();
  83. sscanf(szBuffer, "Z-AxisColor   < %f %f %f %f >n", &m_fZAxisColor[0], &m_fZAxisColor[1], &m_fZAxisColor[2], &m_fZAxisColor[3]);
  84. }
  85. }
  86. /////////////////////////////////////////////////////////////////////////////
  87. // C3dAxis Procedures
  88. C3dAxis* C3dAxis::Create()
  89. {
  90. C3dAxis* pAxis = new C3dAxis;
  91. if (pAxis) {
  92. return pAxis;
  93. }
  94. return NULL;
  95. }
  96. void C3dAxis::Display(GLdouble dAxisLength,
  97.   GLdouble dAxisRadius,
  98.   GLdouble dArrowLength,
  99.   GLdouble dArrowRadius,
  100.   GLint iSlices,
  101.   GLint iStacks,
  102.   GLfloat fColorX[4],
  103.   GLfloat fColorY[4],
  104.   GLfloat fColorZ[4],
  105.   BOOL bSolid)
  106. {
  107. GLdouble dArrowPosn = dAxisLength - (dArrowLength/2);
  108. // Save the current transformation matrix..
  109. glPushMatrix();
  110. // Reset the material properties to default
  111. // settings and track the current color
  112. C3dMaterial::ResetMaterialAttrib();
  113. // Disable textsure maping
  114. glDisable(GL_TEXTURE_2D);
  115. // Create a quadratic object used to draw our axis
  116. // cylinders and cones
  117. GLUquadricObj* m_pQuadric = gluNewQuadric();
  118. if(bSolid) {
  119. glPolygonMode(GL_FRONT, GL_FILL);
  120. gluQuadricDrawStyle(m_pQuadric, GLU_FILL);
  121. }
  122. else {
  123. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  124. gluQuadricDrawStyle(m_pQuadric, GLU_LINE);
  125. }
  126. // Display the Z-Axis and arrow
  127. // Set the color
  128. if(fColorZ)
  129. glColor4fv(fColorZ);
  130. else
  131. glColor4fv(m_fZAxisColor);
  132. gluCylinder(m_pQuadric, dAxisRadius, dAxisRadius, dAxisLength, iSlices, iStacks);
  133. glTranslated(0.0f, 0.0f, dArrowPosn); // Move to arrow position
  134. gluCylinder(m_pQuadric, dArrowRadius, 0.0f, dArrowLength, iSlices, iStacks);
  135.  
  136. // Display the X-Axis and arrow
  137. // Set the color
  138. if(fColorX)
  139. glColor4fv(fColorX);
  140. else
  141. glColor4fv(m_fXAxisColor);
  142. glTranslated(0.0f, 0.0f, -dArrowPosn);// Move to 0, 0, 0
  143. glRotated(90, 0.0, 1.0, 0.0); // Rotate for X
  144. gluCylinder(m_pQuadric, dAxisRadius, dAxisRadius, dAxisLength, iSlices, iStacks);
  145. glTranslated(0.0f, 0.0f, dArrowPosn); // Move to arrow position
  146. gluCylinder(m_pQuadric, dArrowRadius, 0.0f, dArrowLength, iSlices, iStacks);
  147. // Display the Y-Axis and arrow
  148. // Set the color
  149. if(fColorY)
  150. glColor4fv(fColorY);
  151. else
  152. glColor4fv(m_fYAxisColor);
  153. glTranslated(0.0f, 0.0f, -dArrowPosn);// Move to 0, 0, 0
  154. glRotated(-90, 1.0, 0.0, 0.0); // Rotate for X
  155. gluCylinder(m_pQuadric, dAxisRadius, dAxisRadius, dAxisLength, iSlices, iStacks);
  156. glTranslated(0.0f, 0.0f, dArrowPosn); // Move to arrow position
  157. gluCylinder(m_pQuadric, dArrowRadius, 0.0f, dArrowLength, iSlices, iStacks);
  158. // Delete the quadric
  159. gluDeleteQuadric(m_pQuadric);
  160. // Restore the current transformation matrix..
  161. glPopMatrix();
  162. }