Loading src/application.cpp +43 −59 Original line number Diff line number Diff line Loading @@ -4,6 +4,13 @@ Application::Application(size_t initialWidth, size_t initialHeight) { this->width = initialWidth; this->height = initialHeight; pioneer = std::make_unique<Probe>(Probe("objects/pioneer/pioneer.obj", { 0.0f, 4.0f, 15.0f }, 0.2f, texturedObjectProgram, normalMappedObjectProgram)); voyager = std::make_unique<Probe>(Probe("objects/voyager/voyager.obj", { 20.0f, 2.0f, 0.0f }, -0.1f, texturedObjectProgram, normalMappedObjectProgram)); ulysses = std::make_unique<Probe>(Probe("objects/ulysses/ulysses.obj", { 20.0f, -3.0f, 20.0f }, 0.15f, texturedObjectProgram, normalMappedObjectProgram)); galileo = std::make_unique<Probe>(Probe("objects/galileo/galileo.obj", { 8.0f, 6.0f, 8.0f }, -0.4f, texturedObjectProgram, normalMappedObjectProgram)); cassini = std::make_unique<Probe>(Probe("objects/cassini/cassini.obj", { -10.0f, -4.0f, -10.0f }, -0.2f, texturedObjectProgram, normalMappedObjectProgram)); juno = std::make_unique<Probe>(Probe("objects/juno/Juno.obj", { 25.0f, 0.5f, 25.0f }, -0.05f, texturedObjectProgram, normalMappedObjectProgram)); cameraUbo.position = glm::vec4(camera.eyePosition(), 1.0f); cameraUbo.projection = glm::perspective(glm::radians(45.0f), float(width) / float(height), 0.01f, 1000.0f); cameraUbo.view = glm::lookAt(camera.eyePosition(), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); Loading @@ -25,26 +32,26 @@ Application::Application(size_t initialWidth, size_t initialHeight) { beltMaterial.diffuseColor(glm::vec4(1.0f)); beltMaterial.specularColor(glm::vec4(1.0f, 1.0f, 1.0f, 2048.0f)); pioneer.modelMatrix() = glm::scale(pioneer.modelMatrix(), glm::vec3(0.2f)); pioneer.modelMatrix() = glm::rotate(pioneer.modelMatrix(), -3.14159f / 8.0f, glm::vec3(1.0, 0.0, 0.0)); pioneer->modelMatrix() = glm::scale(pioneer->modelMatrix(), glm::vec3(0.2f)); pioneer->modelMatrix() = glm::rotate(pioneer->modelMatrix(), -3.14159f / 8.0f, glm::vec3(1.0, 0.0, 0.0)); voyager.modelMatrix() = glm::scale(voyager.modelMatrix(), glm::vec3(0.1f)); voyager.modelMatrix() = glm::rotate(voyager.modelMatrix(), 3.14159f / 2.0f, glm::vec3(1.0, 0.0, 0.0)); voyager.modelMatrix() = glm::rotate(voyager.modelMatrix(), 3.14159f / 2.0f, glm::vec3(0.0, 0.0, 1.0)); voyager->modelMatrix() = glm::scale(voyager->modelMatrix(), glm::vec3(0.1f)); voyager->modelMatrix() = glm::rotate(voyager->modelMatrix(), 3.14159f / 2.0f, glm::vec3(1.0, 0.0, 0.0)); voyager->modelMatrix() = glm::rotate(voyager->modelMatrix(), 3.14159f / 2.0f, glm::vec3(0.0, 0.0, 1.0)); ulysses.modelMatrix() = glm::scale(ulysses.modelMatrix(), glm::vec3(0.5f)); ulysses.modelMatrix() = glm::rotate(ulysses.modelMatrix(), 3.14159f / 4.0f, glm::vec3(0.0, 1.0, 0.0)); ulysses.modelMatrix() = glm::rotate(ulysses.modelMatrix(), -3.14159f / 2.5f, glm::vec3(1.0, 0.0, 0.0)); ulysses->modelMatrix() = glm::scale(ulysses->modelMatrix(), glm::vec3(0.5f)); ulysses->modelMatrix() = glm::rotate(ulysses->modelMatrix(), 3.14159f / 4.0f, glm::vec3(0.0, 1.0, 0.0)); ulysses->modelMatrix() = glm::rotate(ulysses->modelMatrix(), -3.14159f / 2.5f, glm::vec3(1.0, 0.0, 0.0)); galileo.modelMatrix() = glm::scale(galileo.modelMatrix(), glm::vec3(0.1f)); galileo.modelMatrix() = glm::rotate(galileo.modelMatrix(), 3.14159f / 16.0f, glm::vec3(1.0, 0.0, 0.0)); galileo.modelMatrix() = glm::rotate(galileo.modelMatrix(), -3.14159f / 16.0f, glm::vec3(0.0, 0.0, 1.0)); galileo->modelMatrix() = glm::scale(galileo->modelMatrix(), glm::vec3(0.1f)); galileo->modelMatrix() = glm::rotate(galileo->modelMatrix(), 3.14159f / 16.0f, glm::vec3(1.0, 0.0, 0.0)); galileo->modelMatrix() = glm::rotate(galileo->modelMatrix(), -3.14159f / 16.0f, glm::vec3(0.0, 0.0, 1.0)); cassini.modelMatrix() = glm::scale(cassini.modelMatrix(), glm::vec3(0.2f)); cassini->modelMatrix() = glm::scale(cassini->modelMatrix(), glm::vec3(0.2f)); juno.modelMatrix() = glm::scale(juno.modelMatrix(), glm::vec3(0.1f)); juno.modelMatrix() = glm::rotate(juno.modelMatrix(), -3.14159f / 4.0f, glm::vec3(0.0, 1.0, 0.0)); juno.modelMatrix() = glm::rotate(juno.modelMatrix(), 3.14159f / 2.0f, glm::vec3(1.0, 0.0, 0.0)); juno->modelMatrix() = glm::scale(juno->modelMatrix(), glm::vec3(0.1f)); juno->modelMatrix() = glm::rotate(juno->modelMatrix(), -3.14159f / 4.0f, glm::vec3(0.0, 1.0, 0.0)); juno->modelMatrix() = glm::rotate(juno->modelMatrix(), 3.14159f / 2.0f, glm::vec3(1.0, 0.0, 0.0)); glCreateBuffers(1, &cameraBuffer); glNamedBufferStorage(cameraBuffer, sizeof(CameraUBO), &cameraUbo, GL_DYNAMIC_STORAGE_BIT); Loading @@ -53,7 +60,7 @@ Application::Application(size_t initialWidth, size_t initialHeight) { glNamedBufferStorage(lightsBuffer, lights.size() * sizeof(LightUBO), lights.data(), GL_DYNAMIC_STORAGE_BIT); glCreateBuffers(1, &beltBuffer); glNamedBufferStorage(beltBuffer, sizeof(music), &music, GL_DYNAMIC_STORAGE_BIT); glNamedBufferStorage(beltBuffer, sizeof(double) * Playback::magicCount, music.data(), GL_DYNAMIC_STORAGE_BIT); } Application::~Application() { Loading @@ -65,37 +72,22 @@ Application::~Application() { glDeleteBuffers(1, &cameraBuffer); } void Application::rotateProbeMatrix(glm::mat4 &modelMatrix, float velocity) { modelMatrix = glm::rotate(glm::mat4(1.0f), float((time - lastTime) * velocity), glm::vec3(0.0f, 1.0f, 0.0f)) * modelMatrix; } void Application::drawProbe(Scene &scene, const glm::mat4 &modelMatrix) { for (auto &mesh : scene.meshes()) { GLuint program = mesh->material() && mesh->material()->normalTexture() ? normalMappedObjectProgram.id() : texturedObjectProgram.id(); glUseProgram(program); glProgramUniformMatrix4fv(program, 0, 1, GL_FALSE, glm::value_ptr(modelMatrix)); mesh->draw(); } } void Application::render() { time = glfwGetTime(); // -------------------------------------------------------------------------- // Update data // -------------------------------------------------------------------------- pioneer.update(); voyager.update(); ulysses.update(); galileo.update(); cassini.update(); juno.update(); pioneer->update(); voyager->update(); ulysses->update(); galileo->update(); cassini->update(); juno->update(); // Camera if (followedProbe) { camera.center(followedProbe->position()); camera.angleDirection(camera.angleDirection() - followedProbe->deltaAngle()); camera.angleDirection(camera.angleDirection() - float(followedProbe->deltaAngle())); } cameraUbo.position = glm::vec4(camera.eyePosition(), 1.0f); cameraUbo.view = glm::lookAt(camera.eyePosition(), camera.center(), glm::vec3(0.0f, 1.0f, 0.0f)); Loading @@ -109,7 +101,7 @@ void Application::render() { music[i] += Application::musicStepDown; } } glNamedBufferSubData(beltBuffer, 0, sizeof(music), &music); glNamedBufferSubData(beltBuffer, 0, sizeof(double) * Playback::magicCount, music.data()); // -------------------------------------------------------------------------- // Draw the scene Loading Loading @@ -149,20 +141,12 @@ void Application::render() { glDrawElementsInstanced(teapot.mode(), teapot.indicesCount(), GL_UNSIGNED_INT, nullptr, 2048); // Draw the probes pioneer.draw(); voyager.draw(); ulysses.draw(); galileo.draw(); cassini.draw(); juno.draw(); glUseProgram(drawLightsProgram.id()); glBindBufferBase(GL_UNIFORM_BUFFER, 0, cameraBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, lightsBuffer); glBindVertexArray(sphere.vao()); glDrawElementsInstanced(sphere.mode(), sphere.indicesCount(), GL_UNSIGNED_INT, nullptr, lights.size()); pioneer->draw(); voyager->draw(); ulysses->draw(); galileo->draw(); cassini->draw(); juno->draw(); lastTime = time; } Loading Loading @@ -199,22 +183,22 @@ void Application::onKeyPressed(GLFWwindow *window, int key, int scancode, int ac camera.center(glm::vec3(0.0f)); break; case GLFW_KEY_1: followedProbe = &pioneer; followedProbe = pioneer.get(); break; case GLFW_KEY_2: followedProbe = &voyager; followedProbe = voyager.get(); break; case GLFW_KEY_3: followedProbe = &ulysses; followedProbe = ulysses.get(); break; case GLFW_KEY_4: followedProbe = &galileo; followedProbe = galileo.get(); break; case GLFW_KEY_5: followedProbe = &cassini; followedProbe = cassini.get(); break; case GLFW_KEY_6: followedProbe = &juno; followedProbe = juno.get(); break; } } No newline at end of file src/application.hpp +9 −11 Original line number Diff line number Diff line Loading @@ -13,6 +13,8 @@ #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> #include <memory> #include <vector> // Used for clearing custom framebuffers (glClearNamedFramebufferfv) const float clearColor[4] = { 0.0, 0.0, 0.0, 1.0 }; Loading Loading @@ -60,16 +62,15 @@ private: Program normalMappedObjectProgram = Program("shaders/normalMappedObject.vert", "shaders/normalMappedObject.frag"); Mesh cube = Mesh::cube(); Mesh sphere = Mesh::sphere(); Mesh teapot = Mesh::teapot(); Mesh polarTeapot = Mesh::polarTeapot(); Probe pioneer = Probe("objects/pioneer/pioneer.obj", { 0.0f, 4.0f, 15.0f }, 0.2f, texturedObjectProgram, normalMappedObjectProgram); Probe voyager = Probe("objects/voyager/voyager.obj", { 20.0f, 2.0f, 0.0f }, -0.1f, texturedObjectProgram, normalMappedObjectProgram); Probe ulysses = Probe("objects/ulysses/ulysses.obj", { 20.0f, -3.0f, 20.0f }, 0.15f, texturedObjectProgram, normalMappedObjectProgram); Probe galileo = Probe("objects/galileo/galileo.obj", { 8.0f, 6.0f, 8.0f }, -0.4f, texturedObjectProgram, normalMappedObjectProgram); Probe cassini = Probe("objects/cassini/cassini.obj", { -10.0f, -4.0f, -10.0f }, -0.2f, texturedObjectProgram, normalMappedObjectProgram); Probe juno = Probe("objects/juno/Juno.obj", { 25.0f, 0.5f, 25.0f }, -0.05f, texturedObjectProgram, normalMappedObjectProgram); std::unique_ptr<Probe> pioneer; std::unique_ptr<Probe> voyager; std::unique_ptr<Probe> ulysses; std::unique_ptr<Probe> galileo; std::unique_ptr<Probe> cassini; std::unique_ptr<Probe> juno; Material jupiterMaterial; glm::mat4 jupiterModelMatrix; Loading @@ -96,10 +97,7 @@ private: double time = 0.0f; double lastTime = 0.0f; double music[Playback::magicCount] = { 0 }; std::vector<double> music = std::vector<double>(Playback::magicCount); const double musicStepDown = -0.03; Probe *followedProbe = nullptr; void rotateProbeMatrix(glm::mat4 &modelMatrix, float velocity); void drawProbe(Scene &scene, const glm::mat4 &modelMatrix); }; No newline at end of file src/main.cpp +25 −32 Original line number Diff line number Diff line #include <glad/glad.h> #include "application.hpp" #include <GLFW/glfw3.h> #include <glad/glad.h> #include <iostream> #include "application.hpp" #define SUCCESS 0 #define ERR_GLFW_INIT 1 Loading @@ -24,8 +24,7 @@ void onMouseMove(GLFWwindow *window, double x, double y); void onMousePressed(GLFWwindow *window, int button, int action, int mods); void onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods); int main(void) { int main(void) { // GLFW if (!glfwInit()) { std::cerr << "Could not initialize GLFW!" << std::endl; Loading Loading @@ -73,8 +72,7 @@ int main(void) return SUCCESS; } void onGlfwError(int errorCode, const char *description) { void onGlfwError(int errorCode, const char *description) { std::cerr << "\033[31m[GLFW ERROR]\033[37m " << description << std::endl; } Loading @@ -84,8 +82,7 @@ void APIENTRY onGlDebugMessage(GLenum source, GLenum severity, GLsizei length, const char *message, const void *user_parameter) { const void *user_parameter) { const char *tag; switch (type) { case GL_DEBUG_TYPE_ERROR: Loading @@ -106,26 +103,22 @@ void APIENTRY onGlDebugMessage(GLenum source, std::cerr << tag << message << std::endl; } void onResize(GLFWwindow *window, int width, int height) { void onResize(GLFWwindow *window, int width, int height) { Application *application = (Application *)glfwGetWindowUserPointer(window); application->onResize(window, width, height); } void onMouseMove(GLFWwindow *window, double x, double y) { void onMouseMove(GLFWwindow *window, double x, double y) { Application *application = (Application *)glfwGetWindowUserPointer(window); application->onMouseMove(window, x, y); } void onMousePressed(GLFWwindow *window, int button, int action, int mods) { void onMousePressed(GLFWwindow *window, int button, int action, int mods) { Application *application = (Application *)glfwGetWindowUserPointer(window); application->onMousePressed(window, button, action, mods); } void onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods) { void onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods) { Application *application = (Application *)glfwGetWindowUserPointer(window); application->onKeyPressed(window, key, scancode, action, mods); } src/playback.cpp +12 −12 Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ static void dataCallback(ma_device* pDevice, void* pOutput, const void* pInput, if (playback == nullptr) { return; } ma_uint64 readCount = ma_decoder_read_pcm_frames(&playback->_decoder, pOutput, frameCount); ma_uint64 readCount = ma_decoder_read_pcm_frames(playback->_decoder.get(), pOutput, frameCount); double input[Playback::magicCount * 2]; double realOutput[Playback::magicCount * 2]; double imaginaryOutput[Playback::magicCount * 2]; Loading @@ -29,30 +29,30 @@ static void dataCallback(ma_device* pDevice, void* pOutput, const void* pInput, } Playback::Playback(const std::string& flacFile) { if (ma_decoder_init_file(flacFile.c_str(), nullptr, &_decoder) != MA_SUCCESS) { if (ma_decoder_init_file(flacFile.c_str(), nullptr, _decoder.get()) != MA_SUCCESS) { throw std::logic_error("File could not be played."); } _config = ma_device_config_init(ma_device_type_playback); _config.playback.format = _decoder.outputFormat; _config.playback.channels = _decoder.outputChannels; _config.sampleRate = _decoder.outputSampleRate; _config.playback.format = _decoder->outputFormat; _config.playback.channels = _decoder->outputChannels; _config.sampleRate = _decoder->outputSampleRate; _config.dataCallback = dataCallback; _config.pUserData = this; if (ma_device_init(nullptr, &_config, &_device) != MA_SUCCESS) { ma_decoder_uninit(&_decoder); if (ma_device_init(nullptr, &_config, _device.get()) != MA_SUCCESS) { ma_decoder_uninit(_decoder.get()); throw std::logic_error("Failed to initialize a playback device."); } if (ma_device_start(&_device) != MA_SUCCESS) { ma_device_uninit(&_device); ma_decoder_uninit(&_decoder); if (ma_device_start(_device.get()) != MA_SUCCESS) { ma_device_uninit(_device.get()); ma_decoder_uninit(_decoder.get()); throw std::logic_error("Failed to start the playback device."); } } Playback::~Playback() { ma_device_uninit(&_device); ma_decoder_uninit(&_decoder); ma_device_uninit(_device.get()); ma_decoder_uninit(_decoder.get()); } src/playback.hpp +3 −2 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ #include <miniaudio.h> #include <string> #include <vector> #include <memory> class Playback { public: Loading @@ -14,8 +15,8 @@ public: ~Playback(); private: ma_decoder _decoder; ma_device _device; std::unique_ptr<ma_decoder> _decoder = std::make_unique<ma_decoder>(); std::unique_ptr<ma_device> _device = std::make_unique<ma_device>(); ma_device_config _config; friend void dataCallback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount); Loading Loading
src/application.cpp +43 −59 Original line number Diff line number Diff line Loading @@ -4,6 +4,13 @@ Application::Application(size_t initialWidth, size_t initialHeight) { this->width = initialWidth; this->height = initialHeight; pioneer = std::make_unique<Probe>(Probe("objects/pioneer/pioneer.obj", { 0.0f, 4.0f, 15.0f }, 0.2f, texturedObjectProgram, normalMappedObjectProgram)); voyager = std::make_unique<Probe>(Probe("objects/voyager/voyager.obj", { 20.0f, 2.0f, 0.0f }, -0.1f, texturedObjectProgram, normalMappedObjectProgram)); ulysses = std::make_unique<Probe>(Probe("objects/ulysses/ulysses.obj", { 20.0f, -3.0f, 20.0f }, 0.15f, texturedObjectProgram, normalMappedObjectProgram)); galileo = std::make_unique<Probe>(Probe("objects/galileo/galileo.obj", { 8.0f, 6.0f, 8.0f }, -0.4f, texturedObjectProgram, normalMappedObjectProgram)); cassini = std::make_unique<Probe>(Probe("objects/cassini/cassini.obj", { -10.0f, -4.0f, -10.0f }, -0.2f, texturedObjectProgram, normalMappedObjectProgram)); juno = std::make_unique<Probe>(Probe("objects/juno/Juno.obj", { 25.0f, 0.5f, 25.0f }, -0.05f, texturedObjectProgram, normalMappedObjectProgram)); cameraUbo.position = glm::vec4(camera.eyePosition(), 1.0f); cameraUbo.projection = glm::perspective(glm::radians(45.0f), float(width) / float(height), 0.01f, 1000.0f); cameraUbo.view = glm::lookAt(camera.eyePosition(), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); Loading @@ -25,26 +32,26 @@ Application::Application(size_t initialWidth, size_t initialHeight) { beltMaterial.diffuseColor(glm::vec4(1.0f)); beltMaterial.specularColor(glm::vec4(1.0f, 1.0f, 1.0f, 2048.0f)); pioneer.modelMatrix() = glm::scale(pioneer.modelMatrix(), glm::vec3(0.2f)); pioneer.modelMatrix() = glm::rotate(pioneer.modelMatrix(), -3.14159f / 8.0f, glm::vec3(1.0, 0.0, 0.0)); pioneer->modelMatrix() = glm::scale(pioneer->modelMatrix(), glm::vec3(0.2f)); pioneer->modelMatrix() = glm::rotate(pioneer->modelMatrix(), -3.14159f / 8.0f, glm::vec3(1.0, 0.0, 0.0)); voyager.modelMatrix() = glm::scale(voyager.modelMatrix(), glm::vec3(0.1f)); voyager.modelMatrix() = glm::rotate(voyager.modelMatrix(), 3.14159f / 2.0f, glm::vec3(1.0, 0.0, 0.0)); voyager.modelMatrix() = glm::rotate(voyager.modelMatrix(), 3.14159f / 2.0f, glm::vec3(0.0, 0.0, 1.0)); voyager->modelMatrix() = glm::scale(voyager->modelMatrix(), glm::vec3(0.1f)); voyager->modelMatrix() = glm::rotate(voyager->modelMatrix(), 3.14159f / 2.0f, glm::vec3(1.0, 0.0, 0.0)); voyager->modelMatrix() = glm::rotate(voyager->modelMatrix(), 3.14159f / 2.0f, glm::vec3(0.0, 0.0, 1.0)); ulysses.modelMatrix() = glm::scale(ulysses.modelMatrix(), glm::vec3(0.5f)); ulysses.modelMatrix() = glm::rotate(ulysses.modelMatrix(), 3.14159f / 4.0f, glm::vec3(0.0, 1.0, 0.0)); ulysses.modelMatrix() = glm::rotate(ulysses.modelMatrix(), -3.14159f / 2.5f, glm::vec3(1.0, 0.0, 0.0)); ulysses->modelMatrix() = glm::scale(ulysses->modelMatrix(), glm::vec3(0.5f)); ulysses->modelMatrix() = glm::rotate(ulysses->modelMatrix(), 3.14159f / 4.0f, glm::vec3(0.0, 1.0, 0.0)); ulysses->modelMatrix() = glm::rotate(ulysses->modelMatrix(), -3.14159f / 2.5f, glm::vec3(1.0, 0.0, 0.0)); galileo.modelMatrix() = glm::scale(galileo.modelMatrix(), glm::vec3(0.1f)); galileo.modelMatrix() = glm::rotate(galileo.modelMatrix(), 3.14159f / 16.0f, glm::vec3(1.0, 0.0, 0.0)); galileo.modelMatrix() = glm::rotate(galileo.modelMatrix(), -3.14159f / 16.0f, glm::vec3(0.0, 0.0, 1.0)); galileo->modelMatrix() = glm::scale(galileo->modelMatrix(), glm::vec3(0.1f)); galileo->modelMatrix() = glm::rotate(galileo->modelMatrix(), 3.14159f / 16.0f, glm::vec3(1.0, 0.0, 0.0)); galileo->modelMatrix() = glm::rotate(galileo->modelMatrix(), -3.14159f / 16.0f, glm::vec3(0.0, 0.0, 1.0)); cassini.modelMatrix() = glm::scale(cassini.modelMatrix(), glm::vec3(0.2f)); cassini->modelMatrix() = glm::scale(cassini->modelMatrix(), glm::vec3(0.2f)); juno.modelMatrix() = glm::scale(juno.modelMatrix(), glm::vec3(0.1f)); juno.modelMatrix() = glm::rotate(juno.modelMatrix(), -3.14159f / 4.0f, glm::vec3(0.0, 1.0, 0.0)); juno.modelMatrix() = glm::rotate(juno.modelMatrix(), 3.14159f / 2.0f, glm::vec3(1.0, 0.0, 0.0)); juno->modelMatrix() = glm::scale(juno->modelMatrix(), glm::vec3(0.1f)); juno->modelMatrix() = glm::rotate(juno->modelMatrix(), -3.14159f / 4.0f, glm::vec3(0.0, 1.0, 0.0)); juno->modelMatrix() = glm::rotate(juno->modelMatrix(), 3.14159f / 2.0f, glm::vec3(1.0, 0.0, 0.0)); glCreateBuffers(1, &cameraBuffer); glNamedBufferStorage(cameraBuffer, sizeof(CameraUBO), &cameraUbo, GL_DYNAMIC_STORAGE_BIT); Loading @@ -53,7 +60,7 @@ Application::Application(size_t initialWidth, size_t initialHeight) { glNamedBufferStorage(lightsBuffer, lights.size() * sizeof(LightUBO), lights.data(), GL_DYNAMIC_STORAGE_BIT); glCreateBuffers(1, &beltBuffer); glNamedBufferStorage(beltBuffer, sizeof(music), &music, GL_DYNAMIC_STORAGE_BIT); glNamedBufferStorage(beltBuffer, sizeof(double) * Playback::magicCount, music.data(), GL_DYNAMIC_STORAGE_BIT); } Application::~Application() { Loading @@ -65,37 +72,22 @@ Application::~Application() { glDeleteBuffers(1, &cameraBuffer); } void Application::rotateProbeMatrix(glm::mat4 &modelMatrix, float velocity) { modelMatrix = glm::rotate(glm::mat4(1.0f), float((time - lastTime) * velocity), glm::vec3(0.0f, 1.0f, 0.0f)) * modelMatrix; } void Application::drawProbe(Scene &scene, const glm::mat4 &modelMatrix) { for (auto &mesh : scene.meshes()) { GLuint program = mesh->material() && mesh->material()->normalTexture() ? normalMappedObjectProgram.id() : texturedObjectProgram.id(); glUseProgram(program); glProgramUniformMatrix4fv(program, 0, 1, GL_FALSE, glm::value_ptr(modelMatrix)); mesh->draw(); } } void Application::render() { time = glfwGetTime(); // -------------------------------------------------------------------------- // Update data // -------------------------------------------------------------------------- pioneer.update(); voyager.update(); ulysses.update(); galileo.update(); cassini.update(); juno.update(); pioneer->update(); voyager->update(); ulysses->update(); galileo->update(); cassini->update(); juno->update(); // Camera if (followedProbe) { camera.center(followedProbe->position()); camera.angleDirection(camera.angleDirection() - followedProbe->deltaAngle()); camera.angleDirection(camera.angleDirection() - float(followedProbe->deltaAngle())); } cameraUbo.position = glm::vec4(camera.eyePosition(), 1.0f); cameraUbo.view = glm::lookAt(camera.eyePosition(), camera.center(), glm::vec3(0.0f, 1.0f, 0.0f)); Loading @@ -109,7 +101,7 @@ void Application::render() { music[i] += Application::musicStepDown; } } glNamedBufferSubData(beltBuffer, 0, sizeof(music), &music); glNamedBufferSubData(beltBuffer, 0, sizeof(double) * Playback::magicCount, music.data()); // -------------------------------------------------------------------------- // Draw the scene Loading Loading @@ -149,20 +141,12 @@ void Application::render() { glDrawElementsInstanced(teapot.mode(), teapot.indicesCount(), GL_UNSIGNED_INT, nullptr, 2048); // Draw the probes pioneer.draw(); voyager.draw(); ulysses.draw(); galileo.draw(); cassini.draw(); juno.draw(); glUseProgram(drawLightsProgram.id()); glBindBufferBase(GL_UNIFORM_BUFFER, 0, cameraBuffer); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, lightsBuffer); glBindVertexArray(sphere.vao()); glDrawElementsInstanced(sphere.mode(), sphere.indicesCount(), GL_UNSIGNED_INT, nullptr, lights.size()); pioneer->draw(); voyager->draw(); ulysses->draw(); galileo->draw(); cassini->draw(); juno->draw(); lastTime = time; } Loading Loading @@ -199,22 +183,22 @@ void Application::onKeyPressed(GLFWwindow *window, int key, int scancode, int ac camera.center(glm::vec3(0.0f)); break; case GLFW_KEY_1: followedProbe = &pioneer; followedProbe = pioneer.get(); break; case GLFW_KEY_2: followedProbe = &voyager; followedProbe = voyager.get(); break; case GLFW_KEY_3: followedProbe = &ulysses; followedProbe = ulysses.get(); break; case GLFW_KEY_4: followedProbe = &galileo; followedProbe = galileo.get(); break; case GLFW_KEY_5: followedProbe = &cassini; followedProbe = cassini.get(); break; case GLFW_KEY_6: followedProbe = &juno; followedProbe = juno.get(); break; } } No newline at end of file
src/application.hpp +9 −11 Original line number Diff line number Diff line Loading @@ -13,6 +13,8 @@ #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> #include <memory> #include <vector> // Used for clearing custom framebuffers (glClearNamedFramebufferfv) const float clearColor[4] = { 0.0, 0.0, 0.0, 1.0 }; Loading Loading @@ -60,16 +62,15 @@ private: Program normalMappedObjectProgram = Program("shaders/normalMappedObject.vert", "shaders/normalMappedObject.frag"); Mesh cube = Mesh::cube(); Mesh sphere = Mesh::sphere(); Mesh teapot = Mesh::teapot(); Mesh polarTeapot = Mesh::polarTeapot(); Probe pioneer = Probe("objects/pioneer/pioneer.obj", { 0.0f, 4.0f, 15.0f }, 0.2f, texturedObjectProgram, normalMappedObjectProgram); Probe voyager = Probe("objects/voyager/voyager.obj", { 20.0f, 2.0f, 0.0f }, -0.1f, texturedObjectProgram, normalMappedObjectProgram); Probe ulysses = Probe("objects/ulysses/ulysses.obj", { 20.0f, -3.0f, 20.0f }, 0.15f, texturedObjectProgram, normalMappedObjectProgram); Probe galileo = Probe("objects/galileo/galileo.obj", { 8.0f, 6.0f, 8.0f }, -0.4f, texturedObjectProgram, normalMappedObjectProgram); Probe cassini = Probe("objects/cassini/cassini.obj", { -10.0f, -4.0f, -10.0f }, -0.2f, texturedObjectProgram, normalMappedObjectProgram); Probe juno = Probe("objects/juno/Juno.obj", { 25.0f, 0.5f, 25.0f }, -0.05f, texturedObjectProgram, normalMappedObjectProgram); std::unique_ptr<Probe> pioneer; std::unique_ptr<Probe> voyager; std::unique_ptr<Probe> ulysses; std::unique_ptr<Probe> galileo; std::unique_ptr<Probe> cassini; std::unique_ptr<Probe> juno; Material jupiterMaterial; glm::mat4 jupiterModelMatrix; Loading @@ -96,10 +97,7 @@ private: double time = 0.0f; double lastTime = 0.0f; double music[Playback::magicCount] = { 0 }; std::vector<double> music = std::vector<double>(Playback::magicCount); const double musicStepDown = -0.03; Probe *followedProbe = nullptr; void rotateProbeMatrix(glm::mat4 &modelMatrix, float velocity); void drawProbe(Scene &scene, const glm::mat4 &modelMatrix); }; No newline at end of file
src/main.cpp +25 −32 Original line number Diff line number Diff line #include <glad/glad.h> #include "application.hpp" #include <GLFW/glfw3.h> #include <glad/glad.h> #include <iostream> #include "application.hpp" #define SUCCESS 0 #define ERR_GLFW_INIT 1 Loading @@ -24,8 +24,7 @@ void onMouseMove(GLFWwindow *window, double x, double y); void onMousePressed(GLFWwindow *window, int button, int action, int mods); void onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods); int main(void) { int main(void) { // GLFW if (!glfwInit()) { std::cerr << "Could not initialize GLFW!" << std::endl; Loading Loading @@ -73,8 +72,7 @@ int main(void) return SUCCESS; } void onGlfwError(int errorCode, const char *description) { void onGlfwError(int errorCode, const char *description) { std::cerr << "\033[31m[GLFW ERROR]\033[37m " << description << std::endl; } Loading @@ -84,8 +82,7 @@ void APIENTRY onGlDebugMessage(GLenum source, GLenum severity, GLsizei length, const char *message, const void *user_parameter) { const void *user_parameter) { const char *tag; switch (type) { case GL_DEBUG_TYPE_ERROR: Loading @@ -106,26 +103,22 @@ void APIENTRY onGlDebugMessage(GLenum source, std::cerr << tag << message << std::endl; } void onResize(GLFWwindow *window, int width, int height) { void onResize(GLFWwindow *window, int width, int height) { Application *application = (Application *)glfwGetWindowUserPointer(window); application->onResize(window, width, height); } void onMouseMove(GLFWwindow *window, double x, double y) { void onMouseMove(GLFWwindow *window, double x, double y) { Application *application = (Application *)glfwGetWindowUserPointer(window); application->onMouseMove(window, x, y); } void onMousePressed(GLFWwindow *window, int button, int action, int mods) { void onMousePressed(GLFWwindow *window, int button, int action, int mods) { Application *application = (Application *)glfwGetWindowUserPointer(window); application->onMousePressed(window, button, action, mods); } void onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods) { void onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods) { Application *application = (Application *)glfwGetWindowUserPointer(window); application->onKeyPressed(window, key, scancode, action, mods); }
src/playback.cpp +12 −12 Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ static void dataCallback(ma_device* pDevice, void* pOutput, const void* pInput, if (playback == nullptr) { return; } ma_uint64 readCount = ma_decoder_read_pcm_frames(&playback->_decoder, pOutput, frameCount); ma_uint64 readCount = ma_decoder_read_pcm_frames(playback->_decoder.get(), pOutput, frameCount); double input[Playback::magicCount * 2]; double realOutput[Playback::magicCount * 2]; double imaginaryOutput[Playback::magicCount * 2]; Loading @@ -29,30 +29,30 @@ static void dataCallback(ma_device* pDevice, void* pOutput, const void* pInput, } Playback::Playback(const std::string& flacFile) { if (ma_decoder_init_file(flacFile.c_str(), nullptr, &_decoder) != MA_SUCCESS) { if (ma_decoder_init_file(flacFile.c_str(), nullptr, _decoder.get()) != MA_SUCCESS) { throw std::logic_error("File could not be played."); } _config = ma_device_config_init(ma_device_type_playback); _config.playback.format = _decoder.outputFormat; _config.playback.channels = _decoder.outputChannels; _config.sampleRate = _decoder.outputSampleRate; _config.playback.format = _decoder->outputFormat; _config.playback.channels = _decoder->outputChannels; _config.sampleRate = _decoder->outputSampleRate; _config.dataCallback = dataCallback; _config.pUserData = this; if (ma_device_init(nullptr, &_config, &_device) != MA_SUCCESS) { ma_decoder_uninit(&_decoder); if (ma_device_init(nullptr, &_config, _device.get()) != MA_SUCCESS) { ma_decoder_uninit(_decoder.get()); throw std::logic_error("Failed to initialize a playback device."); } if (ma_device_start(&_device) != MA_SUCCESS) { ma_device_uninit(&_device); ma_decoder_uninit(&_decoder); if (ma_device_start(_device.get()) != MA_SUCCESS) { ma_device_uninit(_device.get()); ma_decoder_uninit(_decoder.get()); throw std::logic_error("Failed to start the playback device."); } } Playback::~Playback() { ma_device_uninit(&_device); ma_decoder_uninit(&_decoder); ma_device_uninit(_device.get()); ma_decoder_uninit(_decoder.get()); }
src/playback.hpp +3 −2 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ #include <miniaudio.h> #include <string> #include <vector> #include <memory> class Playback { public: Loading @@ -14,8 +15,8 @@ public: ~Playback(); private: ma_decoder _decoder; ma_device _device; std::unique_ptr<ma_decoder> _decoder = std::make_unique<ma_decoder>(); std::unique_ptr<ma_device> _device = std::make_unique<ma_device>(); ma_device_config _config; friend void dataCallback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount); Loading