camera.cpp
Upload User: szxjsk
Upload Date: 2022-06-16
Package Size: 35k
Code Size: 3k
Category:

OpenGL program

Development Platform:

C/C++

  1. #include "camera.h"
  2. #include "math.h"
  3. #include <iostream>
  4. #include "windows.h"
  5. #define SQR(x) (x*x)
  6. #define NULL_VECTOR F3dVector(0.0f,0.0f,0.0f)
  7. SF3dVector F3dVector ( GLfloat x, GLfloat y, GLfloat z )
  8. {
  9. SF3dVector tmp;
  10. tmp.x = x;
  11. tmp.y = y;
  12. tmp.z = z;
  13. return tmp;
  14. }
  15. GLfloat GetF3dVectorLength( SF3dVector * v)
  16. {
  17. return (GLfloat)(sqrt(SQR(v->x)+SQR(v->y)+SQR(v->z)));
  18. }
  19. SF3dVector Normalize3dVector( SF3dVector v)
  20. {
  21. SF3dVector res;
  22. float l = GetF3dVectorLength(&v);
  23. if (l == 0.0f) return NULL_VECTOR;
  24. res.x = v.x / l;
  25. res.y = v.y / l;
  26. res.z = v.z / l;
  27. return res;
  28. }
  29. SF3dVector operator+ (SF3dVector v, SF3dVector u)
  30. {
  31. SF3dVector res;
  32. res.x = v.x+u.x;
  33. res.y = v.y+u.y;
  34. res.z = v.z+u.z;
  35. return res;
  36. }
  37. SF3dVector operator- (SF3dVector v, SF3dVector u)
  38. {
  39. SF3dVector res;
  40. res.x = v.x-u.x;
  41. res.y = v.y-u.y;
  42. res.z = v.z-u.z;
  43. return res;
  44. }
  45. SF3dVector operator* (SF3dVector v, float r)
  46. {
  47. SF3dVector res;
  48. res.x = v.x*r;
  49. res.y = v.y*r;
  50. res.z = v.z*r;
  51. return res;
  52. }
  53. SF3dVector CrossProduct (SF3dVector * u, SF3dVector * v)
  54. {
  55. SF3dVector resVector;
  56. resVector.x = u->y*v->z - u->z*v->y;
  57. resVector.y = u->z*v->x - u->x*v->z;
  58. resVector.z = u->x*v->y - u->y*v->x;
  59. return resVector;
  60. }
  61. float operator* (SF3dVector v, SF3dVector u) //dot product
  62. {
  63. return v.x*u.x+v.y*u.y+v.z*u.z;
  64. }
  65. /***************************************************************************************/
  66. CCamera::CCamera()
  67. {
  68. //Init with standard OGL values:
  69. Position = F3dVector (0.0, 0.0, 0.0);
  70. ViewDir = F3dVector( 0.0, 0.0, -1.0);
  71. RightVector = F3dVector (1.0, 0.0, 0.0);
  72. UpVector = F3dVector (0.0, 1.0, 0.0);
  73. //Only to be sure:
  74. RotatedX = RotatedY = RotatedZ = 0.0;
  75. }
  76. void CCamera::Move (SF3dVector Direction)
  77. {
  78. Position = Position + Direction;
  79. }
  80. void CCamera::RotateX (GLfloat Angle)
  81. {
  82. RotatedX += Angle;
  83. //Rotate viewdir around the right vector:
  84. ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)
  85. + UpVector*sin(Angle*PIdiv180));
  86. //now compute the new UpVector (by cross product)
  87. UpVector = CrossProduct(&ViewDir, &RightVector)*-1;
  88. }
  89. void CCamera::RotateY (GLfloat Angle)
  90. {
  91. RotatedY += Angle;
  92. //Rotate viewdir around the up vector:
  93. ViewDir = Normalize3dVector(ViewDir*cos(Angle*PIdiv180)
  94. - RightVector*sin(Angle*PIdiv180));
  95. //now compute the new RightVector (by cross product)
  96. RightVector = CrossProduct(&ViewDir, &UpVector);
  97. }
  98. void CCamera::RotateZ (GLfloat Angle)
  99. {
  100. RotatedZ += Angle;
  101. //Rotate viewdir around the right vector:
  102. RightVector = Normalize3dVector(RightVector*cos(Angle*PIdiv180)
  103. + UpVector*sin(Angle*PIdiv180));
  104. //now compute the new UpVector (by cross product)
  105. UpVector = CrossProduct(&ViewDir, &RightVector)*-1;
  106. }
  107. void CCamera::Render( void )
  108. {
  109. //The point at which the camera looks:
  110. SF3dVector ViewPoint = Position+ViewDir;
  111. //as we know the up vector, we can easily use gluLookAt:
  112. gluLookAt( Position.x,Position.y,Position.z,
  113. ViewPoint.x,ViewPoint.y,ViewPoint.z,
  114. UpVector.x,UpVector.y,UpVector.z);
  115. }
  116. void CCamera::MoveForward( GLfloat Distance )
  117. {
  118. Position = Position + (ViewDir*-Distance);
  119. }
  120. void CCamera::StrafeRight ( GLfloat Distance )
  121. {
  122. Position = Position + (RightVector*Distance);
  123. }
  124. void CCamera::MoveUpward( GLfloat Distance )
  125. {
  126. Position = Position + (UpVector*Distance);
  127. }