Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glcamera.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* libsegl - Sebas Extended GL Library
  2. * Collection of Opengl/3D-Math helpers
  3. *
  4. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  5. * http://www.seba-geek.de
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #include "glcamera.h"
  23. namespace segl {
  24. GLCamera::GLCamera() {
  25. std_norm.set(0.0f, 1.0f, 0.0f);
  26. std_dir.set(0.0f, 0.0f, -1.0f);
  27. mpersec = 10.0f;
  28. apersec = 90.0f;
  29. doupdate = true;
  30. norm.set(0.0f, 1.0f, 0.0f);
  31. dir.set(0.0f, 0.0f, -1.0f);
  32. rotx = roty = 0.0f;
  33. rotmatX.set(x_axis, 0.0f);
  34. rotmatY.set(y_axis, 0.0f);
  35. }
  36. void GLCamera::updateVectors() {
  37. dir = rotmatY * rotmatX * Punkt3D(0.0f, 0.0f, -1.0f);
  38. norm = rotmatY * rotmatX * std_norm;
  39. dir.normalize();
  40. norm.normalize();
  41. }
  42. void GLCamera::setCamera() {
  43. if(doupdate) {
  44. updateVectors();
  45. }
  46. Punkt3D port = pos + dir;
  47. gluLookAt( pos.x, pos.y, pos.z,
  48. port.x, port.y, port.z,
  49. norm.x, norm.y, norm.z);
  50. // rotFrom2VecTo2Vec(std_dir, dir, std_norm, norm);
  51. // glTranslateP3D(-pos);
  52. }
  53. void GLCamera::setPosDirNorm(Punkt3D p, Punkt3D d, Punkt3D n) {
  54. pos = p;
  55. dir = d.getNormalized();
  56. norm = n.getNormalized();
  57. }
  58. // Move-Funktionen
  59. void GLCamera::moveForward(float sec) {
  60. if(doupdate) {
  61. updateVectors();
  62. }
  63. pos += (mpersec*sec) * dir;
  64. // std::cout << "move..";
  65. // pos.print("pos");
  66. }
  67. void GLCamera::moveBackward(float sec) {
  68. if(doupdate) {
  69. updateVectors();
  70. }
  71. pos -= (mpersec*sec) * dir;
  72. }
  73. void GLCamera::moveLeft(float sec) {
  74. if(doupdate) {
  75. updateVectors();
  76. }
  77. Punkt3D right = (dir.kreuzprodukt(norm));
  78. right.normalize();
  79. pos -= right * sec * mpersec;
  80. }
  81. void GLCamera::moveRight(float sec) {
  82. if(doupdate) {
  83. updateVectors();
  84. }
  85. Punkt3D right = (dir.kreuzprodukt(norm));
  86. right.normalize();
  87. pos += right * sec * mpersec;
  88. }
  89. void GLCamera::rotateLeft(float sec) {
  90. roty += apersec * sec;
  91. rotmatY.set(roty);
  92. doupdate = true;
  93. // rotmat.set(Punkt3D(0.0f, 1.0f, 0.0f), sec*apersec);
  94. // dir = rotmat * dir;
  95. // dir.normalize();
  96. }
  97. void GLCamera::rotateRight(float sec) {
  98. roty -= apersec * sec;
  99. rotmatY.set(roty);
  100. doupdate = true;
  101. // statt norm
  102. // rotmat.set(Punkt3D(0.0f, 1.0f, 0.0f), -apersec*sec);
  103. // dir = rotmat * dir;
  104. // dir.normalize();
  105. }
  106. void GLCamera::rotateUp(float sec) {
  107. if(rotx+sec*apersec<=90.0f && rotx+sec*apersec>=-90.0f) {
  108. rotx += sec*apersec;
  109. rotmatX.set(rotx);
  110. doupdate = true;
  111. } else {
  112. //std::cout << "zu gro�" << std::endl;
  113. }
  114. // rotmat.set(dir.kreuzprodukt(norm), -apersec*sec);
  115. // norm = rotmat * norm;
  116. // dir = rotmat * dir;
  117. // norm.normalize();
  118. // dir.normalize();
  119. }
  120. void GLCamera::rotateDown(float sec) {
  121. if( (rotx-sec*apersec) >= -90.0f) {
  122. rotx -= sec*apersec;
  123. rotmatX.set(rotx);
  124. doupdate = true;
  125. }
  126. // rotmat.set(dir.kreuzprodukt(norm), apersec*sec);
  127. // norm = rotmat * norm;
  128. // dir = rotmat * dir;
  129. // norm.normalize();
  130. // dir.normalize();
  131. }
  132. void GLCamera::handleKeys(float sec) {
  133. Uint8 *keys = SDL_GetKeyState(NULL);
  134. if(keys[SDLK_w])
  135. moveForward(sec);
  136. if(keys[SDLK_s])
  137. moveBackward(sec);
  138. if(keys[SDLK_UP])
  139. rotateUp(sec);
  140. if(keys[SDLK_DOWN])
  141. rotateDown(sec);
  142. if(keys[SDLK_LEFT])
  143. rotateLeft(sec);
  144. if(keys[SDLK_RIGHT])
  145. rotateRight(sec);
  146. if(keys[SDLK_a])
  147. moveLeft(sec);
  148. if(keys[SDLK_d])
  149. moveRight(sec);
  150. }
  151. float GLCamera::getXrot() {
  152. return rotx;
  153. }
  154. float GLCamera::getYrot() {
  155. return roty;
  156. }
  157. void GLCamera::print() {
  158. std::cout << " --- GL Camera --- " << std::endl;
  159. pos.print("Position");
  160. norm.print("Normale");
  161. dir.print("Direction");
  162. dir.kreuzprodukt(norm).print("Kreuz");
  163. std::cout << "Rotation X: " << rotx << " Y: " << roty << std::endl;
  164. rotmatY.print();
  165. std::cout << " --- End Camera --- " << std::endl;
  166. }
  167. void GLCamera::renderCoord() {
  168. glBegin(GL_LINES);
  169. glColor3f(0.0f, 1.0f, 0.0f);
  170. glVertex3f(0.0f, -1.0f, 0.0f);
  171. glVertex3f(0.0f, -1.0f, -5.0f);
  172. glEnd();
  173. }
  174. Punkt3D GLCamera::getPos() {
  175. return pos;
  176. }
  177. GLCamera::~GLCamera() {
  178. }
  179. } // namespace segl