From 73eb0d20ab3bba0d0cca8dde00a8d9f83096a5d0 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Mon, 4 Sep 2017 12:23:12 +0200 Subject: [PATCH 1/4] basic interfacing of sfml sound/music and integration with scenenode --- src/scene/musicnode.cpp | 28 +++++++++++++++++++++++ src/scene/musicnode.h | 22 ++++++++++++++++++ src/scene/soundnode.cpp | 40 +++++++++++++++++++++++++++++++++ src/scene/soundnode.h | 20 +++++++++++++++++ src/sparrowshell/sparrowshell.h | 1 + 5 files changed, 111 insertions(+) create mode 100644 src/scene/musicnode.cpp create mode 100644 src/scene/musicnode.h create mode 100644 src/scene/soundnode.cpp create mode 100644 src/scene/soundnode.h diff --git a/src/scene/musicnode.cpp b/src/scene/musicnode.cpp new file mode 100644 index 0000000..d6670bc --- /dev/null +++ b/src/scene/musicnode.cpp @@ -0,0 +1,28 @@ +#include "musicnode.h" + +MusicNode::MusicNode(std::string musicFileName): + m_music_file(musicFileName) +{ +} + +void MusicNode::init(){ + m_valid = m_music.openFromFile(m_music_file); +} + +void MusicNode::play() +{ + if (m_valid) + m_music.play(); +} + +void MusicNode::pause() +{ + if(m_valid) + m_music.pause(); +} + +void MusicNode::stop() +{ + if(m_valid) + m_music.stop(); +} diff --git a/src/scene/musicnode.h b/src/scene/musicnode.h new file mode 100644 index 0000000..3b542ed --- /dev/null +++ b/src/scene/musicnode.h @@ -0,0 +1,22 @@ +#ifndef MUSICNODE_H +#define MUSICNODE_H + +#include "scenenode.h" +#include "SFML/Audio.hpp" + +class MusicNode : public SceneNode +{ + sf::Music m_music; + std::string m_music_file; + bool m_valid; +public: + MusicNode(std::string musicFileName); + void init(); + void play(); + void pause(); + void stop(); + void setPlayingOffset(int offset); + +}; + +#endif // MUSICNODE_H diff --git a/src/scene/soundnode.cpp b/src/scene/soundnode.cpp new file mode 100644 index 0000000..aa8debf --- /dev/null +++ b/src/scene/soundnode.cpp @@ -0,0 +1,40 @@ +#include "soundnode.h" +#include "resourcemanager.h" + +SoundNode::SoundNode(std::string soundFileName): + m_sound_file(soundFileName) +{ + +} + +void SoundNode::init() +{ + if ((m_valid = RESOURCE_CHECK(sf::SoundBuffer,m_sound_file))) + m_sound.setBuffer(*RESOURCE_GET(sf::SoundBuffer, m_sound_file)); + else{ + sf::SoundBuffer* buffer; + m_valid = buffer->loadFromFile(m_sound_file); + if(m_valid){ + RESOURCE_ADD(buffer,sf::SoundBuffer,m_sound_file); + m_sound.setBuffer(*buffer); + } + } +} + +void SoundNode::play() +{ + if (m_valid) + m_sound.play(); +} + +void SoundNode::pause() +{ + if(m_valid) + m_sound.pause(); +} + +void SoundNode::stop() +{ + if(m_valid) + m_sound.stop(); +} diff --git a/src/scene/soundnode.h b/src/scene/soundnode.h new file mode 100644 index 0000000..ff3c279 --- /dev/null +++ b/src/scene/soundnode.h @@ -0,0 +1,20 @@ +#ifndef SOUNDNODE_H +#define SOUNDNODE_H + +#include "scene/scenenode.h" +#include "SFML/Audio.hpp" + +class SoundNode : public SceneNode +{ + sf::Sound m_sound; + std::string m_sound_file; + bool m_valid; +public: + SoundNode(std::string soundFileName); + void init(); + void play(); + void pause(); + void stop(); +}; + +#endif // SOUNDNODE_H diff --git a/src/sparrowshell/sparrowshell.h b/src/sparrowshell/sparrowshell.h index cce3c9a..835df79 100644 --- a/src/sparrowshell/sparrowshell.h +++ b/src/sparrowshell/sparrowshell.h @@ -85,6 +85,7 @@ public: unsigned int getIndex(){return m_buffer->getIndex();} ShellBuffer* getBuffer(){return m_buffer;} + ScriptNode* getScript(){return m_script;} void setInputs(int cursor_left, int cursor_right, int history_up, int history_down); From 73010a52e181bda116f2a41eb90bb041121b3783 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Mon, 4 Sep 2017 19:54:29 +0200 Subject: [PATCH 2/4] spatialization integrated in soundnode --- src/scene/musicnode.cpp | 5 +++++ src/scene/musicnode.h | 1 - src/scene/playercharacternode.cpp | 3 +++ src/scene/soundnode.cpp | 7 +++++++ src/scene/soundnode.h | 2 ++ 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/scene/musicnode.cpp b/src/scene/musicnode.cpp index d6670bc..0e47ad5 100644 --- a/src/scene/musicnode.cpp +++ b/src/scene/musicnode.cpp @@ -26,3 +26,8 @@ void MusicNode::stop() if(m_valid) m_music.stop(); } + +void MusicNode::setPlayingOffset(int offset) +{ + m_music.setPlayingOffset(sf::seconds(offset)); +} diff --git a/src/scene/musicnode.h b/src/scene/musicnode.h index 3b542ed..c01ffa3 100644 --- a/src/scene/musicnode.h +++ b/src/scene/musicnode.h @@ -16,7 +16,6 @@ public: void pause(); void stop(); void setPlayingOffset(int offset); - }; #endif // MUSICNODE_H diff --git a/src/scene/playercharacternode.cpp b/src/scene/playercharacternode.cpp index f6176c8..03aa293 100644 --- a/src/scene/playercharacternode.cpp +++ b/src/scene/playercharacternode.cpp @@ -11,6 +11,7 @@ #include "scenetree.h" #include "lightnode.h" #include +#include #define DEFAULT_ROTATION_SPEED 0.001f #define TRIGGER_VALUE 15 @@ -297,6 +298,8 @@ void PlayerCharacterNode::update() m_rigidBody->setLinearVelocity(newVelocity); } m_playerLightNode->update(); + glm::vec3 p = this->getEyePosition(); + sf::Listener::setPosition(p.x,p.y,p.z); } void PlayerCharacterNode::toggleNoClip() diff --git a/src/scene/soundnode.cpp b/src/scene/soundnode.cpp index aa8debf..cda21aa 100644 --- a/src/scene/soundnode.cpp +++ b/src/scene/soundnode.cpp @@ -38,3 +38,10 @@ void SoundNode::stop() if(m_valid) m_sound.stop(); } + +void SoundNode::setPosition(glm::vec3 pos,bool relative) +{ + m_sound.setPosition(pos.x,pos.y,pos.z); + m_sound.setRelativeToListener(relative); +} + diff --git a/src/scene/soundnode.h b/src/scene/soundnode.h index ff3c279..6283be5 100644 --- a/src/scene/soundnode.h +++ b/src/scene/soundnode.h @@ -3,6 +3,7 @@ #include "scene/scenenode.h" #include "SFML/Audio.hpp" +#include "glm/vec3.hpp" class SoundNode : public SceneNode { @@ -15,6 +16,7 @@ public: void play(); void pause(); void stop(); + void setPosition(glm::vec3 pos, bool relative); }; #endif // SOUNDNODE_H From aaee7d6762049686d7662e3646837733485748c3 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Mon, 4 Sep 2017 20:15:07 +0200 Subject: [PATCH 3/4] added direction to listener for sound spatialization --- src/scene/playercharacternode.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/scene/playercharacternode.cpp b/src/scene/playercharacternode.cpp index 03aa293..1ec24eb 100644 --- a/src/scene/playercharacternode.cpp +++ b/src/scene/playercharacternode.cpp @@ -300,6 +300,8 @@ void PlayerCharacterNode::update() m_playerLightNode->update(); glm::vec3 p = this->getEyePosition(); sf::Listener::setPosition(p.x,p.y,p.z); + glm::vec3 d = getDirection(); + sf::Listener::setDirection(d.x,d.y,d.z); } void PlayerCharacterNode::toggleNoClip() From 0f763ae51f5048216b8ebffddb609c0164f9e6ed Mon Sep 17 00:00:00 2001 From: Anselme Date: Tue, 5 Sep 2017 11:40:43 +0200 Subject: [PATCH 4/4] compatibility with the new naming of mesh's members --- src/engine.cpp | 1 + src/scene/meshnode.cpp | 8 ++++---- src/scene/physicsdebugnode.cpp | 6 ++++-- src/test/main.cpp | 2 +- src/test/potator.cpp | 7 ++++--- src/tools/loader.cpp | 6 +++--- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index c672f2d..87120fd 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -194,6 +194,7 @@ void Engine::enablePhysicsDebug() { m_physicsDebugNode = new PhysicsDebugNode(); getScene()->addToIndex(m_physicsDebugNode); + getScene()->registerMeshType(m_physicsDebugNode->getGeometryNode()->mesh->getFlags()); getScene()->getPhysics()->setDebugDrawer(m_physicsDebugNode); getScene()->getPhysics()->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe); } diff --git a/src/scene/meshnode.cpp b/src/scene/meshnode.cpp index d0dca2a..cd40531 100644 --- a/src/scene/meshnode.cpp +++ b/src/scene/meshnode.cpp @@ -28,13 +28,13 @@ btRigidBody* MeshNode::buildStaticCollider() btIndexedMesh *bulletMesh = new btIndexedMesh(); Mesh *m = m_geometry.mesh; // vertices - bulletMesh->m_numVertices = m->positions3D.size(); - bulletMesh->m_vertexBase = (unsigned char*)(m->positions3D.data()); + bulletMesh->m_numVertices = m->m_positions3D.size(); + bulletMesh->m_vertexBase = (unsigned char*)(m->m_positions3D.data()); bulletMesh->m_vertexStride = sizeof(glm::vec3); bulletMesh->m_vertexType = PHY_FLOAT; // indices - bulletMesh->m_numTriangles = m->indices.size()/3; - bulletMesh->m_triangleIndexBase = (unsigned char*)(m->indices.data()); + bulletMesh->m_numTriangles = m->m_indices.size()/3; + bulletMesh->m_triangleIndexBase = (unsigned char*)(m->m_indices.data()); bulletMesh->m_triangleIndexStride = 3*sizeof(GLuint); bulletMesh->m_indexType = PHY_INTEGER; diff --git a/src/scene/physicsdebugnode.cpp b/src/scene/physicsdebugnode.cpp index e13b50b..707a93b 100644 --- a/src/scene/physicsdebugnode.cpp +++ b/src/scene/physicsdebugnode.cpp @@ -2,6 +2,8 @@ #include "SparrowRenderer/mesh.h" #include "SparrowRenderer/pbrmaterial.h" +#include "scene/scenetree.h" + PhysicsDebugNode::PhysicsDebugNode() : MeshNode(new Mesh()) { @@ -37,6 +39,6 @@ void PhysicsDebugNode::flushLines() void PhysicsDebugNode::clearBuffers() { - m_geometry.mesh->positions3D.clear(); - m_geometry.mesh->normals.clear(); + m_geometry.mesh->m_positions3D.clear(); + m_geometry.mesh->m_normals.clear(); } diff --git a/src/test/main.cpp b/src/test/main.cpp index 252776e..825a376 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -100,7 +100,7 @@ void generateTerrain(SceneTree *scene, btDiscreteDynamicsWorld *world) Chunk *chunk = new Chunk(&gen); // ! WARNING ! : chunk pointer is lost and never deleted glm::vec3 pos(x, y, z); chunk->generate(pos); - if(chunk->mesh->positions3D.empty()) + if(chunk->mesh->m_positions3D.empty()) delete chunk; else { diff --git a/src/test/potator.cpp b/src/test/potator.cpp index 416508a..b85b5c7 100644 --- a/src/test/potator.cpp +++ b/src/test/potator.cpp @@ -68,7 +68,7 @@ Potator::Potator(PlayerCharacterNode * player, m_cubeMesh->addTriangle(id+7, id+5, id+6); } - PBRMaterial *mat = new PBRMaterial(); /* + PBRMaterial *mat = new PBRMaterial(); Image* img = Loader::loadImage("woodframe_albedo.png", 24); mat->setTexture(PBRMaterial::ALBEDO_SLOT, new Texture(img)); img = Loader::loadImage("woodframe_metallic.png", 8); @@ -76,7 +76,7 @@ Potator::Potator(PlayerCharacterNode * player, img = Loader::loadImage("woodframe_roughness.png", 8); mat->setTexture(PBRMaterial::ROUGHNESS_SLOT, new Texture(img)); img = Loader::loadImage("woodframe_normal.png", 24); - mat->setTexture(PBRMaterial::NORMALS_SLOT, new Texture(img)); */ + mat->setTexture(PBRMaterial::NORMALS_SLOT, new Texture(img)); m_cubeMesh->setMaterial(mat); m_cubeMesh->computeTangents(); @@ -183,7 +183,7 @@ void Potator::update() throwSword(); } } - +/* ImGui::Begin("Potator"); if(ImGui::Button("Load pack")) LoadingThread::get()->loadResourcePack("woodbox"); @@ -197,4 +197,5 @@ void Potator::update() m_scene->registerMeshType(m_cubeMesh->getFlags()); } ImGui::End(); + */ } diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index 6eedfe9..c4271fb 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -216,7 +216,7 @@ std::vector Loader::loadMesh(const std::string &filename){ std::sscanf(line.c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d",tab,tab+1,tab+2,tab+3,tab+4,tab+5,tab+6,tab+7,tab+8); //TODO: check sscanf success - int nb_vertices = currentMesh->positions3D.size(); + int nb_vertices = currentMesh->m_positions3D.size(); currentMesh->addTriangle(nb_vertices, nb_vertices+1, nb_vertices+2); for(int i=0; i<3; ++i) @@ -278,7 +278,7 @@ std::vector Loader::loadMesh(const std::string &filename){ for(std::size_t i=0; iindices.size() == 0) + if(meshes[i]->m_indices.size() == 0) { meshes[i] = meshes.back(); meshes.pop_back(); @@ -287,7 +287,7 @@ std::vector Loader::loadMesh(const std::string &filename){ else { Mesh* m = meshes[i]; - if(m->normals.empty()) + if(m->m_normals.empty()) m->computeNormals(); if(m->getFlags() & (1 << Mesh::MATERIAL_PBR_NORMAL_MAP)) m->computeTangents();