Advertisement
brightprogrammer

MouseEventProblem.cpp

Jun 13th, 2021
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. glm::vec3 camPos = glm::vec3(5.0f, 5.0f, 5.0f);
  2. glm::vec3 camUp = glm::vec3(0.0f, 1.0f, 0.0f);
  3. glm::vec3 camFront = glm::vec3(-1.0f, -1.0f, -1.0f);
  4.  
  5. glm::mat4 view;
  6. glm::vec3 direction;
  7.  
  8. constexpr float playerSpeed = 0.1f;
  9. constexpr float sensitivity = 0.2f;
  10. float fov = 45.f;
  11.  
  12. float yaw = -90.0f;
  13. float pitch = 89.f;
  14.  
  15. while(window.isOpen){
  16. // window.Hand1leEvents();
  17. static SDL_Event event;
  18.  
  19. SDL_PollEvent(&event);
  20. if( (event.type == SDL_WINDOWEVENT) && (event.window.windowID == window.windowID) ){
  21. // check if window is closed
  22. if(event.window.event == SDL_WINDOWEVENT_CLOSE) window.isOpen = false;
  23.  
  24. }
  25. // mouse motion event
  26. if(event.type == SDL_MOUSEMOTION){
  27. float xoffset = float(event.motion.xrel) * sensitivity;
  28. float yoffset = float(event.motion.yrel) * sensitivity;
  29.  
  30. LOG(DEBUG, "MOUSE MOTION [%lu]", renderer.frameNumber);
  31.  
  32. yaw += xoffset;
  33. pitch += yoffset;
  34.  
  35. if(pitch > 89.f) pitch = 89.f;
  36. if(pitch < -89.f) pitch = -89.f;
  37.  
  38. direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
  39. direction.y = sin(glm::radians(pitch));
  40. direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
  41. camFront = glm::normalize(direction);
  42. }
  43.  
  44. // key events
  45. if(event.type == SDL_KEYDOWN){
  46. if(event.key.keysym.sym == SDLK_UP){
  47. camPos += playerSpeed * camFront;
  48. // camPos.z += playerSpeed;
  49. }
  50.  
  51. if(event.key.keysym.sym == SDLK_DOWN){
  52. camPos -= playerSpeed * camFront;
  53. // camPos.z -= playerSpeed;
  54. }
  55.  
  56. if(event.key.keysym.sym == SDLK_RIGHT){
  57. // camPos += (glm::normalize(glm::cross(camFront, camUp)) * playerSpeed);
  58. camPos.x += playerSpeed;
  59. }
  60.  
  61. if(event.key.keysym.sym == SDLK_LEFT){
  62. // camPos -= glm::normalize(glm::cross(camFront, camUp)) * playerSpeed;
  63. camPos.x -= playerSpeed;
  64. }
  65.  
  66. if(event.key.keysym.sym == SDLK_w){
  67. // camPos += playerSpeed * camUp;
  68. camPos.y += playerSpeed;
  69. }
  70.  
  71. if(event.key.keysym.sym == SDLK_s){
  72. // camPos -= playerSpeed * camUp;
  73. camPos.y -= playerSpeed;
  74. }
  75. }
  76.  
  77. if(event.type == SDL_MOUSEWHEEL){
  78. if(event.wheel.y > 0){
  79. fov += 0.1f;
  80. }else if(event.wheel.y < 0){
  81. fov -= 0.1f;
  82. }
  83.  
  84. if(fov > 45.f) fov = 45.f;
  85. else if(fov < 1.f) fov = 1.f;
  86. }
  87.  
  88. renderer.cameraData.view = glm::lookAt(camPos, (camPos + camFront), camUp);
  89. renderer.cameraData.proj = glm::perspective(glm::radians(fov), float(window.size.x) / float(window.size.y), 0.1f, 100.f);
  90. renderer.cameraData.proj[1][1] *= -1;
  91.  
  92. renderer.Draw();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement