11#include " opegl.hpp"
22#include " GLFW/glfw3.h"
33#include " constants.hpp"
4+ #include " glm/common.hpp"
45#include " glm/ext/matrix_clip_space.hpp"
56#include " glm/ext/vector_float2.hpp"
67#include " glm/trigonometric.hpp"
78#include " level.hpp"
89#include " light.hpp"
910#include " shader.hpp"
1011#include " vertex.hpp"
12+ #include < cfloat>
1113#include < cstddef>
1214#include < cstdint>
15+ #include < cstdlib>
1316#include < iostream>
17+ #include < memory>
18+ #include < print>
1419#include < stdexcept>
1520
1621OpeGL::OpeGL () { init (); }
@@ -37,11 +42,11 @@ void OpeGL::mainLoop() {
3742 }
3843
3944 while (!glfwWindowShouldClose (window)) {
45+ processInput (window);
4046 update ();
4147 int width, height;
4248 glfwGetFramebufferSize (window, &width, &height);
4349 glViewport (0 , 0 , width, height);
44- processInput (window);
4550
4651 glClearColor (0 .0f , 0 .0f , 0 .0f , 1 .0f );
4752 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -54,6 +59,7 @@ void OpeGL::mainLoop() {
5459 shader.setMat4 (" projection" , projection);
5560
5661 shader.setMat4 (" view" , camera.getView ());
62+ shader.setVec3 (" viewPos" , camera.getPosition ());
5763
5864 glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 2 , perInstanceDataBuffer);
5965
@@ -65,6 +71,7 @@ void OpeGL::mainLoop() {
6571 modelShader.setMat4 (" projection" , projection);
6672
6773 modelShader.setMat4 (" view" , camera.getView ());
74+ modelShader.setVec3 (" viewPos" , camera.getPosition ());
6875
6976 for (auto &model : models) {
7077 modelShader.setMat4 (" modelMatrix" , model.modelMatrix ());
@@ -85,6 +92,7 @@ void OpeGL::init() {
8592 glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4 );
8693 glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 6 );
8794 glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
95+
8896 if (debug)
8997 glfwWindowHint (GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
9098 window =
@@ -173,14 +181,48 @@ void OpeGL::framebuffer_size_callback(GLFWwindow *window, int width,
173181}
174182
175183void OpeGL::processInput (GLFWwindow *window) {
176- if (glfwGetKey (window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
177- glfwSetWindowShouldClose (window, true );
184+ if (glfwJoystickPresent (GLFW_JOYSTICK_1)) {
185+ if (glfwJoystickIsGamepad (GLFW_JOYSTICK_1)) {
186+ GLFWgamepadstate state;
187+ if (glfwGetGamepadState (GLFW_JOYSTICK_1, &state)) {
188+ float left_stick_x = state.axes [GLFW_GAMEPAD_AXIS_LEFT_X];
189+ float left_stick_y = state.axes [GLFW_GAMEPAD_AXIS_LEFT_Y];
190+ float right_stick_x = state.axes [GLFW_GAMEPAD_AXIS_RIGHT_X];
191+ float right_stick_y = state.axes [GLFW_GAMEPAD_AXIS_RIGHT_Y];
192+
193+ camera.movement .forward = left_stick_y < -0 .5f ;
194+ camera.movement .backward = left_stick_y > 0 .5f ;
195+ camera.movement .left = left_stick_x < -0 .5f ;
196+ camera.movement .right = left_stick_x > 0 .5f ;
197+
198+ camera.updateRightAxes (deltaTime, right_stick_x, right_stick_y);
199+ }
200+ } else {
201+ int count;
202+ const float *axes = glfwGetJoystickAxes (GLFW_JOYSTICK_1, &count);
203+
204+ for (int i = 0 ; i < count; i++)
205+ std::println (" axes {} == {}" , i, axes[i]);
206+
207+ if (count >= 4 ) {
208+ camera.movement .forward = axes[1 ] < -0 .5f ;
209+ camera.movement .backward = axes[1 ] > 0 .5f ;
210+ camera.movement .left = axes[0 ] < -0 .5f ;
211+ camera.movement .right = axes[0 ] > 0 .5f ;
212+
213+ camera.updateRightAxes (deltaTime, axes[4 ], axes[3 ]);
214+ }
215+ }
216+ }
178217}
179218
180219void OpeGL::key_callback (GLFWwindow *window, int key, int scancode, int action,
181220 int mods) {
182221 auto app = reinterpret_cast <OpeGL *>(glfwGetWindowUserPointer (window));
183222 const bool press = action != GLFW_RELEASE;
223+
224+ if (glfwGetKey (window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
225+ glfwSetWindowShouldClose (window, true );
184226 if (key == GLFW_KEY_ESCAPE)
185227 glfwSetWindowShouldClose (window, GLFW_TRUE);
186228 if (key == GLFW_KEY_W)
@@ -309,9 +351,9 @@ void OpeGL::addPointLight(PointLight &light) { pointLights.push_back(light); }
309351void OpeGL::loadLevel (std::string path, uint32_t wallTexture,
310352 uint32_t floorTexture, uint32_t ceilingTexture,
311353 size_t maxHeight) {
312- auto newLevel =
313- OpeLevel (path, wallTexture, floorTexture, ceilingTexture, maxHeight);
314- currentLevel = & newLevel;
354+ auto newLevel = std::make_unique<OpeLevel>(
355+ OpeLevel (path, wallTexture, floorTexture, ceilingTexture, maxHeight)) ;
356+ currentLevel = std::move ( newLevel) ;
315357 currentLevel->loadLevel (*this );
316358}
317359
0 commit comments