Browse Source

Added (bad) audio, debug display, window resize support

Horde3D
Macoy Madson 1 year ago
parent
commit
c0c595f268
30 changed files with 349 additions and 18 deletions
  1. BIN
      Content/textures/TestGrid.png
  2. +2
    -0
      Jamfile
  3. BIN
      audio/MouthFX_VehicleAcceleration.ogg
  4. BIN
      audio/MouthFX_VehicleBodySlide.ogg
  5. BIN
      audio/MouthFX_VehicleChokeOut.ogg
  6. BIN
      audio/MouthFX_VehicleCollision.ogg
  7. BIN
      audio/MouthFX_VehicleIdle.ogg
  8. BIN
      audio/MouthFX_VehicleLanding.ogg
  9. BIN
      audio/MouthFX_VehicleRoll.ogg
  10. BIN
      audio/MouthFX_VehicleShifting.ogg
  11. BIN
      audio/MouthFX_VehicleShutDown.ogg
  12. BIN
      audio/MouthFX_VehicleSkid.ogg
  13. BIN
      audio/MouthFX_VehicleSkidDirt.ogg
  14. BIN
      audio/MouthFX_VehicleStartup.ogg
  15. BIN
      audio/MouthFX_VehicleSuspension.ogg
  16. BIN
      audio/MouthFX_WorldSpaciousness.ogg
  17. BIN
      audio/MouthFX_WorldWindAtSpeed.ogg
  18. BIN
      audio/MouthFX_WorldWindIdle.ogg
  19. BIN
      audioAssets/MouthFX_M.wav
  20. BIN
      audioAssets/MouthFX_V.wav
  21. BIN
      data/fonts/UbuntuMono-R.ttf
  22. +188
    -0
      src/Audio.cpp
  23. +9
    -0
      src/Audio.hpp
  24. +58
    -0
      src/DebugDisplay.cpp
  25. +12
    -0
      src/DebugDisplay.hpp
  26. +11
    -3
      src/Joystick.cpp
  27. +32
    -9
      src/Main_Horde3D.cpp
  28. +5
    -0
      src/Math.hpp
  29. +31
    -6
      src/Render_Horde3D.cpp
  30. +1
    -0
      src/Render_Horde3D.hpp

BIN
Content/textures/TestGrid.png View File

Before After
Width: 1024  |  Height: 1024  |  Size: 280 KiB

+ 2
- 0
Jamfile View File

@ -14,7 +14,9 @@ SubDir . ;
Main spargus_vehicle_prototype_horde3D : src/Main_Horde3D.cpp
src/Render_Horde3D.cpp
src/DebugDisplay.cpp
src/Camera.cpp
src/Audio.cpp
src/Math.cpp
src/Joystick.cpp
src/ModelUtilities/ModelLoader.cpp


BIN
audio/MouthFX_VehicleAcceleration.ogg View File


BIN
audio/MouthFX_VehicleBodySlide.ogg View File


BIN
audio/MouthFX_VehicleChokeOut.ogg View File


BIN
audio/MouthFX_VehicleCollision.ogg View File


BIN
audio/MouthFX_VehicleIdle.ogg View File


BIN
audio/MouthFX_VehicleLanding.ogg View File


BIN
audio/MouthFX_VehicleRoll.ogg View File


BIN
audio/MouthFX_VehicleShifting.ogg View File


BIN
audio/MouthFX_VehicleShutDown.ogg View File


BIN
audio/MouthFX_VehicleSkid.ogg View File


BIN
audio/MouthFX_VehicleSkidDirt.ogg View File


BIN
audio/MouthFX_VehicleStartup.ogg View File


BIN
audio/MouthFX_VehicleSuspension.ogg View File


BIN
audio/MouthFX_WorldSpaciousness.ogg View File


BIN
audio/MouthFX_WorldWindAtSpeed.ogg View File


BIN
audio/MouthFX_WorldWindIdle.ogg View File


BIN
audioAssets/MouthFX_M.wav View File


BIN
audioAssets/MouthFX_V.wav View File


BIN
data/fonts/UbuntuMono-R.ttf View File


+ 188
- 0
src/Audio.cpp View File

@ -0,0 +1,188 @@
#include "Audio.hpp"
#include "PhysicsVehicle.hpp"
#include "DebugDisplay.hpp"
#include "btBulletDynamicsCommon.h"
#include "sound/sound.hpp"
#include "graphics/graphics.hpp"
// #include <SFML/Sound.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <sstream>
sound sfxVehicleStartup;
sound sfxVehicleIdle;
sound sfxVehicleAccelerate;
sound sfxVehicleShifting;
sound sfxVehicleShutDown;
sound sfxVehicleChokeOut;
sound sfxVehicleSuspensionLanding;
sound sfxVehicleSuspensionJump;
sound sfxVehicleRoadNoiseDirt;
sound sfxVehicleSkidDirt;
sound sfxVehicleLanding;
sound sfxVehicleGasUp;
sound sfxVehicleCollision;
sound sfxVehicleBottomOut;
sound sfxVehicleRoll;
sound sfxVehicleBodySlide;
sound sfxWorldWindAtSpeed;
sound sfxWorldWindStationary;
sound sfxWorldSpaciousness;
listener audioListener;
enum class SoundLocation : int
{
None = 0,
Vehicle,
World
};
struct SoundEffect
{
const char* filename;
sound* soundFx;
SoundLocation location;
};
static SoundEffect soundFxFilePairs[] = {
{"audio/MouthFX_VehicleStartup.ogg", &sfxVehicleStartup, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleIdle.ogg", &sfxVehicleIdle, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleAcceleration.ogg", &sfxVehicleAccelerate, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleShifting.ogg", &sfxVehicleShifting, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleShutDown.ogg", &sfxVehicleShutDown, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleChokeOut.ogg", &sfxVehicleChokeOut, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleSuspension.ogg", &sfxVehicleSuspensionLanding, SoundLocation::Vehicle},
// {"audio/MouthFX_VehicleSuspensionJump.ogg", &sfxVehicleSuspensionJump,
// SoundLocation::Vehicle},
// {"audio/MouthFX_VehicleRoadNoiseDirt.ogg", &sfxVehicleRoadNoiseDirt, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleSkidDirt.ogg", &sfxVehicleSkidDirt, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleLanding.ogg", &sfxVehicleLanding, SoundLocation::Vehicle},
// {"audio/MouthFX_VehicleGasUp.ogg", &sfxVehicleGasUp, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleCollision.ogg", &sfxVehicleCollision, SoundLocation::Vehicle},
// {"audio/MouthFX_VehicleBottomOut.ogg", &sfxVehicleBottomOut, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleRoll.ogg", &sfxVehicleRoll, SoundLocation::Vehicle},
{"audio/MouthFX_VehicleBodySlide.ogg", &sfxVehicleBodySlide, SoundLocation::Vehicle},
{"audio/MouthFX_WorldWindAtSpeed.ogg", &sfxWorldWindAtSpeed, SoundLocation::World},
{"audio/MouthFX_WorldWindIdle.ogg", &sfxWorldWindStationary, SoundLocation::World},
{"audio/MouthFX_WorldSpaciousness.ogg", &sfxWorldSpaciousness, SoundLocation::World},
};
void loadAudio()
{
for (size_t i = 0; i < sizeof(soundFxFilePairs) / sizeof(SoundEffect); ++i)
{
if (!soundFxFilePairs[i].soundFx->load(soundFxFilePairs[i].filename))
std::cout << "Failed to load " << soundFxFilePairs[i].filename << "\n";
}
}
template <typename T,unsigned Length>
inline unsigned ArraySize(const T (&v)[Length]) { return Length; }
void debugPrintAudio()
{
for (size_t i = 0; i < ArraySize(soundFxFilePairs); ++i)
{
std::ostringstream outputString;
sf::Sound* baseSound = soundFxFilePairs[i].soundFx->getBase();
switch (baseSound->getStatus())
{
case sf::Sound::Status::Stopped:
break;
case sf::Sound::Status::Paused:
break;
case sf::Sound::Status::Playing:
outputString << soundFxFilePairs[i].filename << " Playing at volume "
<< baseSound->getVolume();
DebugDisplay::print(outputString.str());
break;
}
}
}
void updateAudio(PhysicsVehicle& vehicle)
{
const btTransform& vehicleTransform = vehicle.vehicle->getChassisWorldTransform();
const btVector3 vehiclePosition = vehicleTransform.getOrigin();
audioListener.setPosition(vehiclePosition.getX(), vehiclePosition.getY(),
vehiclePosition.getZ());
static bool worldAudioStarted = false;
if (!worldAudioStarted)
{
sfxWorldSpaciousness.loop(true);
sfxWorldSpaciousness.play();
sfxWorldWindStationary.loop(true);
sfxWorldWindStationary.play();
// Always startup car at the beginning
sfxVehicleStartup.play();
// sfxWorldWorldWind.loop(true);
// sfxWorldWorldWind.play();
worldAudioStarted = true;
}
// Update sound positions
for (size_t i = 0; i < sizeof(soundFxFilePairs) / sizeof(SoundEffect); ++i)
{
switch (soundFxFilePairs[i].location)
{
case SoundLocation::World:
soundFxFilePairs[i].soundFx->setPosition(
vehiclePosition.getX(), vehiclePosition.getY(), vehiclePosition.getZ());
break;
case SoundLocation::Vehicle:
soundFxFilePairs[i].soundFx->setPosition(
vehiclePosition.getX(), vehiclePosition.getY(), vehiclePosition.getZ());
break;
default:
break;
}
}
// Vehicle
{
// Wheels
for (int i = 0; i < vehicle.vehicle->getNumWheels(); i++)
{
const btWheelInfo& wheelInfo = vehicle.vehicle->getWheelInfo(i);
// std::cout << "Wheel " << i << " skid " << wheelInfo.m_skidInfo << "\n";
if (wheelInfo.m_skidInfo < 1.f)
sfxVehicleSkidDirt.play();
std::ostringstream outputSuspension;
outputSuspension << "Wheel [" << i << "] skid " << wheelInfo.m_skidInfo << " suspension " << wheelInfo.m_wheelsSuspensionForce;
DebugDisplay::print(outputSuspension.str());
}
// Engine noise
if (vehicle.EngineForce > 5.f)
{
sfxVehicleIdle.stop();
sfxVehicleAccelerate.loop(true);
sfxVehicleAccelerate.play();
}
else
{
sfxVehicleAccelerate.stop();
sfxVehicleIdle.loop(true);
sfxVehicleIdle.play();
}
// const btRigidBody* getRigidBody
}
}

+ 9
- 0
src/Audio.hpp View File

@ -0,0 +1,9 @@
#pragma once
void loadAudio();
void debugPrintAudio();
struct PhysicsVehicle;
void updateAudio(PhysicsVehicle& vehicle);

+ 58
- 0
src/DebugDisplay.cpp View File

@ -0,0 +1,58 @@
#include "DebugDisplay.hpp"
#include <iostream>
namespace DebugDisplay
{
window* outputWindow = nullptr;
text displayText;
const float displayTextSize = 32.f;
const float lineSpacing = 2.f;
const float startOffset = 5.f;
float displayYOffset = startOffset;
std::vector<std::string> outputBuffer;
void initialize(window* newOutputWindow)
{
if (!displayText.loadFont("data/fonts/UbuntuMono-R.ttf"))
{
std::cout << "Error: Cannot load default text font. Nothing will work\n";
return;
}
displayText.setSize(32);
displayText.setColor(232, 30, 34, 255);
outputWindow = newOutputWindow;
}
void print(std::string output)
{
outputBuffer.push_back(output);
}
void endFrame()
{
for (const std::string& str : outputBuffer)
{
if (!outputWindow)
{
std::cout
<< "ERROR: DebugDisplay::print() called before output window was set!\n Message:"
<< "\t" << str << "\n";
return;
}
DebugDisplay::displayText.setText(str);
DebugDisplay::displayText.setPosition(5.f, displayYOffset);
outputWindow->draw(&DebugDisplay::displayText);
displayYOffset += displayTextSize + lineSpacing;
}
outputBuffer.clear();
displayYOffset = startOffset;
}
} // namespace DebugDisplay

+ 12
- 0
src/DebugDisplay.hpp View File

@ -0,0 +1,12 @@
#pragma once
#include "graphics/graphics.hpp"
namespace DebugDisplay
{
extern text displayText;
void initialize(window* newOutputWindow);
void print(std::string output);
void endFrame();
} // namespace DebugDisplay

+ 11
- 3
src/Joystick.cpp View File

@ -5,10 +5,14 @@
#include "PhysicsVehicle.hpp"
#include "DebugDisplay.hpp"
#include "Math.hpp"
// For abs
#include "glm/common.hpp"
#include <iostream>
#include <sstream>
const float joystickRange = 100.f;
@ -103,9 +107,13 @@ void processVehicleInputJoystick(PhysicsVehicle& vehicle)
btScalar speedKmHour = vehicle.vehicle->getCurrentSpeedKmHour();
if (debugPrint)
std::cout << "speedKmHour = " << speedKmHour << " throttle = " << vehicle.EngineForce
<< " brake = " << vehicle.BrakingForce << "\n";
{
std::ostringstream output;
output << "speedKmHour = " << speedKmHour << " (mph = " << KilometersToMiles(speedKmHour)
<< ") throttle = " << vehicle.EngineForce << " brake = " << vehicle.BrakingForce
<< "\n";
DebugDisplay::print(output.str());
}
// Auto reverse on brake, if going slow enough (and throttle isn't pressed)
const float autoReverseThresholdKmHour = 1.f;


+ 32
- 9
src/Main_Horde3D.cpp View File

@ -13,12 +13,14 @@
#include "Camera.hpp"
#include "Joystick.hpp"
#include "Audio.hpp"
#include "Math.hpp"
#include "ModelUtilities/ModelLoader.hpp"
#include "ModelUtilities/ModelToBullet.hpp"
#include "ModelUtilities/ObjLoader.hpp"
#include "PhysicsVehicle.hpp"
#include "PhysicsWorld.hpp"
#include "DebugDisplay.hpp"
#include <Horde3D.h>
#include "Render_Horde3D.hpp"
@ -58,7 +60,9 @@ void windowResizeCB(float width, float height)
WindowHeight = height;
// Is this necessary?
glViewport(0, 0, width, height);
// glViewport(0, 0, width, height);
hordeResize(width, height);
}
void processVehicleInput(inputManager& input, PhysicsVehicle& vehicle)
@ -216,12 +220,15 @@ void handleConfigurationInput(inputManager& input)
{
bool enableKeyRepeat = false;
if (input.WasTapped(inputCode::Num1, enableKeyRepeat))
debugPhysicsDraw = !debugPhysicsDraw;
if (input.WasTapped(inputCode::Num2, enableKeyRepeat))
if (input.WasTapped(inputCode::F1, enableKeyRepeat))
std::cout << "TODO: Reset vehicle\n";
// vehicle.
if (input.WasTapped(inputCode::F2, enableKeyRepeat))
useChaseCam = !useChaseCam;
if (input.WasTapped(inputCode::Num3, enableKeyRepeat))
if (input.WasTapped(inputCode::F3, enableKeyRepeat))
useJoystick = !useJoystick;
if (input.WasTapped(inputCode::F4, enableKeyRepeat))
debugPhysicsDraw = !debugPhysicsDraw;
}
int main()
@ -257,12 +264,15 @@ int main()
window mainWindow(WindowWidth, WindowHeight, "Spargus Vehicle Prototype", &windowResizeCB);
initializeWindow(mainWindow);
DebugDisplay::initialize(&mainWindow);
{
WindowScopedContextActivate activate(mainWindow);
hordeInitialize(WindowWidth, WindowHeight);
}
loadAudio();
inputManager input(&mainWindow);
// Ground mesh from GLTF
@ -311,6 +321,9 @@ int main()
vehicle.Update(previousFrameTime);
physicsWorld.Update(previousFrameTime);
// Audio
updateAudio(vehicle);
// Camera
{
if (!useChaseCam)
@ -346,7 +359,7 @@ int main()
}
// glCallList(groundCallList);
hordeUpdate(previousFrameTime);
// Draw debug things (must happen AFTER h3dFinalizeFrame() but BEFORE swapping buffers)
@ -384,14 +397,24 @@ int main()
// ... draw code
physicsWorld.DebugRender();
// glPopMatrix();
}
}
// 2D overlays
{
// Required for 2D drawing
mainWindow.getBase()->resetGLStates();
debugPrintAudio();
DebugDisplay::endFrame();
}
// Finished physics update and drawing; send it on its way
mainWindow.update();
mainWindow.getBase()->setActive(false);
}


+ 5
- 0
src/Math.hpp View File

@ -9,3 +9,8 @@ void openGlMatrixToGlmMat4(const float* openGlMatrix, glm::mat4& matOut);
void openGlMatrixToGlmMat4(const double* openGlMatrix, glm::mat4& matOut);
class btTransform;
void BulletTransformToHordeMatrix(const btTransform& transform, float* hordeMatrixOut);
inline float KilometersToMiles(float kilometers)
{
return kilometers / 1.609344f;
}

+ 31
- 6
src/Render_Horde3D.cpp View File

@ -10,6 +10,11 @@ H3DNode buggyNode = 0;
H3DNode buggyWheelNodes[4];
H3DRes buggyWheelRes = 0;
H3DRes fontMaterialRes = 0;
H3DRes pipeRes = 0;
const float cameraFov = 60.f;
const float cameraNearPlane = 0.5f;
const float cameraFarPlane = 2048.0f;
void hordeInitialize(int winWidth, int winHeight)
{
@ -21,7 +26,8 @@ void hordeInitialize(int winWidth, int winHeight)
H3DRes envRes = h3dAddResource(H3DResTypes::SceneGraph, "assets/World.scene.xml", 0);
// H3DRes envRes = h3dAddResource(H3DResTypes::SceneGraph, "assets/Plane.scene.xml", 0);
// H3DRes buggyRes = h3dAddResource(H3DResTypes::SceneGraph, "assets/BasicBuggy.scene.xml", 0);
H3DRes buggyRes = h3dAddResource(H3DResTypes::SceneGraph, "assets/BasicBuggy_Chassis.scene.xml", 0);
H3DRes buggyRes =
h3dAddResource(H3DResTypes::SceneGraph, "assets/BasicBuggy_Chassis.scene.xml", 0);
buggyWheelRes = h3dAddResource(H3DResTypes::SceneGraph, "assets/Wheel_Rear.scene.xml", 0);
// Scale reference
@ -44,7 +50,7 @@ void hordeInitialize(int winWidth, int winHeight)
// H3DRes animRes = h3dAddResource(H3DResTypes::Animation, "animations/knight_order.anim", 0);
// Add pipeline resource
H3DRes pipeRes = h3dAddResource(H3DResTypes::Pipeline, "pipelines/hdr.pipeline.xml", 0);
pipeRes = h3dAddResource(H3DResTypes::Pipeline, "pipelines/hdr.pipeline.xml", 0);
// Add font
fontMaterialRes = h3dAddResource(H3DResTypes::Material, "overlays/font.material.xml", 0);
// Load added resources
@ -58,7 +64,7 @@ void hordeInitialize(int winWidth, int winHeight)
h3dSetNodeTransform(buggyNode, 0, 0, 0, 0, 0, 0, 1.f, 1.f, 1.f);
for (int i = 0; i < sizeof(buggyWheelNodes) / sizeof(buggyWheelNodes[0]); ++i)
buggyWheelNodes[i] = h3dAddNodes(H3DRootNode, buggyWheelRes);
buggyWheelNodes[i] = h3dAddNodes(H3DRootNode, buggyWheelRes);
// Add model to scene
// model = h3dAddNodes(H3DRootNode, modelRes);
@ -86,7 +92,8 @@ void hordeInitialize(int winWidth, int winHeight)
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportYI, 0);
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportWidthI, winWidth);
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportHeightI, winHeight);
h3dSetupCameraView(hordeCamera, 60.0f, (float)winWidth / winHeight, 0.5f, 2048.0f);
h3dSetupCameraView(hordeCamera, 60.0f, (float)winWidth / winHeight, cameraNearPlane,
cameraFarPlane);
h3dResizePipelineBuffers(pipeRes, winWidth, winHeight);
// h3dSetOption(H3DOptions::DebugViewMode, 1.0f);
@ -130,7 +137,7 @@ void hordeTestInitialize(int winWidth, int winHeight)
H3DRes envRes = h3dAddResource(H3DResTypes::SceneGraph, "assets/BasicBuggy.scene.xml", 0);
// Add pipeline resource
H3DRes pipeRes = h3dAddResource(H3DResTypes::Pipeline, "pipelines/hdr.pipeline.xml", 0);
pipeRes = h3dAddResource(H3DResTypes::Pipeline, "pipelines/hdr.pipeline.xml", 0);
// H3DRes pipeRes = h3dAddResource(H3DResTypes::Pipeline, "pipelines/forward.pipeline.xml", 0);
// Add model resource
H3DRes modelRes = h3dAddResource(H3DResTypes::SceneGraph, "models/knight/knight.scene.xml", 0);
@ -170,7 +177,7 @@ void hordeTestInitialize(int winWidth, int winHeight)
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportYI, 0);
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportWidthI, winWidth);
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportHeightI, winHeight);
h3dSetupCameraView(hordeCamera, 60.0f, (float)winWidth / winHeight, 0.5f, 2048.0f);
h3dSetupCameraView(hordeCamera, cameraFov, (float)winWidth / winHeight, 0.5f, 2048.0f);
h3dResizePipelineBuffers(pipeRes, winWidth, winHeight);
// h3dSetOption(H3DOptions::DebugViewMode, 1.0f);
@ -203,6 +210,24 @@ void hordeTestInitialize(int winWidth, int winHeight)
}
}
void hordeResize(int winWidth, int winHeight)
{
if (!hordeCamera || !pipeRes)
return;
// Resize viewport
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportXI, 0);
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportYI, 0);
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportWidthI, winWidth);
h3dSetNodeParamI(hordeCamera, H3DCamera::ViewportHeightI, winHeight);
// Set virtual camera parameters
h3dSetupCameraView(hordeCamera, cameraFov, (float)winWidth / winHeight, cameraNearPlane,
cameraFarPlane);
// TODO: Fix buffer resize
// h3dResizePipelineBuffers(pipeRes, winWidth, winHeight);
}
void hordeUpdate(float fps)
{
if (model)


+ 1
- 0
src/Render_Horde3D.hpp View File

@ -9,5 +9,6 @@ extern H3DNode buggyWheelNodes[4];
void hordeInitialize(int winWidth, int winHeight);
void hordeTestInitialize(int winWidth, int winHeight);
void hordeResize(int winWidth, int winHeight);
void hordeUpdate(float fps);
void hordeRelease();

Loading…
Cancel
Save