Impostor.cpp
上传用户:ghyvgy
上传日期:2009-05-26
资源大小:547k
文件大小:4k
源码类别:

其他游戏

开发平台:

Python

  1. /*
  2. s_p_oneil@hotmail.com
  3. Copyright (c) 2000, Sean O'Neil
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice,
  8.   this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10.   this list of conditions and the following disclaimer in the documentation
  11.   and/or other materials provided with the distribution.
  12. * Neither the name of this project nor the names of its contributors
  13.   may be used to endorse or promote products derived from this software
  14.   without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "Master.h"
  28. #include "Impostor.h"
  29. int CImpostor::m_nViewport[4];
  30. void CImpostor::InitImpostorRender(C3DObject *pCamera)
  31. {
  32. // Calculate and store the camera orientation and distance for calculating future errors
  33. CVector vView = m_vPosition - pCamera->GetPosition();
  34. m_vOrientation = UnitInverse().RotateVector(-vView);
  35. m_fDistance = m_vOrientation.Magnitude();
  36. m_vOrientation /= m_fDistance;
  37. // Get the suggested resolution for this impostor and see if it's changed since last time
  38. short nOldResolution = m_nResolution;
  39. m_nResolution = GetImpostorResolution(GetImpostorScreenSpace(m_fDistance));
  40. if(nOldResolution != m_nResolution)
  41. SetFlags(NewTexture);
  42. // Calculate the model-view matrix for rendering the impostor, and get the up vector for rendering the billboard
  43. CMatrix mModel, mView;
  44. mModel.ModelMatrix(*this, vView);
  45. GetImpostorViewMatrix(mView, vView);
  46. m_vUp = CVector(mView.f12, mView.f22, mView.f32);
  47. // Set up the necessary viewport, modelview matrix, and projection matrix for rendering to the impostor texture
  48. glGetIntegerv(GL_VIEWPORT, m_nViewport);
  49. glViewport(0, 0, m_nResolution, m_nResolution);
  50. glMatrixMode(GL_PROJECTION);
  51. glPushMatrix();
  52. glLoadIdentity();
  53. // Time for some fancy geometry
  54. float fAltitude = m_fDistance - m_fBoundingRadius; // Height above the surface of the bounding radius
  55. float fHorizon2 = fAltitude*fAltitude + 2.0f*fAltitude*m_fBoundingRadius; // Distance to horizon squared
  56. float fHorizon = sqrtf(fHorizon2); // Distance to horizon
  57. float fCos = fHorizon / m_fDistance; // Cosine of angle between ray to center of sphere and ray to horizon (part of a right triangle)
  58. float fTemp = fAltitude / fCos; // Horizon distance cut off at the near clipping plane
  59. float fBound = sqrtf(fTemp*fTemp - fAltitude*fAltitude); // Distance from center to sides at the near clipping plane (boundaries of the projection frustum) (part of another right triangle)
  60. float fTemp2 = m_fDistance / fCos;
  61. m_fBillboardRadius = sqrtf(fTemp2*fTemp2 - m_fDistance*m_fDistance); // Distance from center to sides at the center of the object (size of the billboard)
  62. glFrustum(-fBound, fBound, -fBound, fBound, m_fDistance-m_fBoundingRadius, m_fDistance+m_fBoundingRadius);
  63. glMatrixMode(GL_MODELVIEW);
  64. glPushMatrix();
  65. glLoadMatrixf(mView);
  66. glPushMatrix();
  67. glMultMatrixf(mModel);
  68. }
  69. void CImpostor::FinishImpostorRender()
  70. {
  71. // Copy the currently rendered viewport into the impostor texture
  72. if(GetFlags(NewTexture) != 0)
  73. m_tBillboard.InitCopy(0, 0, m_nResolution, m_nResolution);
  74. else
  75. m_tBillboard.UpdateCopy(0, 0, m_nResolution, m_nResolution);
  76. ClearFlags(NeedsUpdate | NewTexture);
  77. // Clean up the OpenGL matrix stack and viewport
  78. glViewport(m_nViewport[0], m_nViewport[1], m_nViewport[2], m_nViewport[3]);
  79. glMatrixMode(GL_PROJECTION);
  80. glPopMatrix();
  81. glMatrixMode(GL_MODELVIEW);
  82. glPopMatrix();
  83. glPopMatrix();
  84. }