added config file loader, changed mouse sensitivity, changed player speed, and objects throwing force

This commit is contained in:
Anselme 2016-12-21 16:53:03 +01:00
parent f7c2609da4
commit 1262f6417d
11 changed files with 141 additions and 2165 deletions

6
config.ini Normal file
View File

@ -0,0 +1,6 @@
# this is the demo's config file
width=800
height=600
mode=windowed
scene=none
vsync=false

View File

@ -1,9 +0,0 @@
#
# generated by kHED
#
newmtl steel
d 1.0
map_Kd steel.jpg
newmtl leather
d 1.0
map_Kd leather.jpg

File diff suppressed because it is too large Load Diff

View File

@ -45,11 +45,16 @@ Engine::~Engine()
void Engine::createWindow(std::string title, void Engine::createWindow(std::string title,
unsigned int w, unsigned int w,
unsigned int h, unsigned int h,
bool isWindowed) const std::string &mode)
{ {
sf::Uint32 style = sf::Style::Close;
if(mode == "fullscreen")
style = sf::Style::Fullscreen;
else if(mode == "borderless")
style = sf::Style::None;
m_window = new sf::Window(sf::VideoMode(w, h), m_window = new sf::Window(sf::VideoMode(w, h),
title, title,
isWindowed ? sf::Style::Close : sf::Style::Fullscreen, style,
sf::ContextSettings(24, 8, 0, 3, 3, sf::ContextSettings::Attribute::Core)); sf::ContextSettings(24, 8, 0, 3, 3, sf::ContextSettings::Attribute::Core));
m_window->setFramerateLimit(60); m_window->setFramerateLimit(60);
m_input = new Input(m_window); m_input = new Input(m_window);
@ -164,6 +169,7 @@ void Engine::toggleMouseVisibility()
{ {
m_mouseVisible = !m_mouseVisible; m_mouseVisible = !m_mouseVisible;
m_window->setMouseCursorVisible(m_mouseVisible); m_window->setMouseCursorVisible(m_mouseVisible);
m_input->setMouseGrabbed(!m_mouseVisible);
} }
void Engine::setTogglePhysicsDebugAction(int action) void Engine::setTogglePhysicsDebugAction(int action)
@ -195,6 +201,9 @@ void Engine::checkSpecialInputs()
{ {
for(int action : m_input->getActions()) for(int action : m_input->getActions())
{ {
if(action == -1)
continue;
if(action == m_togglePhysicsDebugAction) if(action == m_togglePhysicsDebugAction)
{ {
if(m_physicsDebugNode == nullptr) if(m_physicsDebugNode == nullptr)

View File

@ -25,7 +25,7 @@ public:
void createWindow(std::string title = "SparrowEngine", void createWindow(std::string title = "SparrowEngine",
unsigned int w = 800, unsigned int w = 800,
unsigned int h = 600, unsigned int h = 600,
bool isWindowed = true); const std::string &mode = "windowed");
void setScene(SceneTree *scene); void setScene(SceneTree *scene);
void initPhysics(); void initPhysics();

View File

@ -7,8 +7,10 @@
#include <glm/ext.hpp> #include <glm/ext.hpp>
#include "scenetree.h" #include "scenetree.h"
#include "lightnode.h"
#include <SFML/System.hpp>
#define DEFAULT_ROTATION_SPEED 0.01f #define DEFAULT_ROTATION_SPEED 0.001f
void FirstPersonCamera::computeView() void FirstPersonCamera::computeView()
{ {
@ -52,7 +54,7 @@ void FirstPersonCamera::setUpVector(const glm::vec3 &up)
computeView(); computeView();
} }
const float WALK_SPEED = 8.f; const float WALK_SPEED = 5.f;
const float PLAYER_RADIUS = 0.30f; const float PLAYER_RADIUS = 0.30f;
const float PLAYER_HEIGHT = 1.75f; const float PLAYER_HEIGHT = 1.75f;
const float EYES_OFFSET = 0.775f; const float EYES_OFFSET = 0.775f;
@ -63,6 +65,10 @@ PlayerCharacterNode::PlayerCharacterNode(bool noClip) :
m_noclipMode(noClip), m_noclipMode(noClip),
m_inputActions({NO_ACTION, NO_ACTION, NO_ACTION, NO_ACTION, NO_ACTION}) m_inputActions({NO_ACTION, NO_ACTION, NO_ACTION, NO_ACTION, NO_ACTION})
{ {
m_playerLight = new PointLight(glm::vec3(150, 10, 30), 10, glm::vec3(0.18f, 0.16f, 0.096f)*2.f);
m_playerLightNode = new LightNode(m_playerLight);
m_playerLightNode->m_parent = this;
// Create the shape // Create the shape
btCollisionShape *shape = new btCapsuleShape(PLAYER_RADIUS, PLAYER_HEIGHT); btCollisionShape *shape = new btCapsuleShape(PLAYER_RADIUS, PLAYER_HEIGHT);
@ -78,6 +84,13 @@ PlayerCharacterNode::PlayerCharacterNode(bool noClip) :
m_rigidBody->setAngularFactor(0.0); m_rigidBody->setAngularFactor(0.0);
} }
PlayerCharacterNode::~PlayerCharacterNode()
{
m_playerLightNode->setSceneTree(nullptr);
delete m_playerLightNode;
delete m_playerLight;
}
void PlayerCharacterNode::setInputs(int forward, int backward, int strafeLeft, int strafeRight, int jump, int toggleNoClip) void PlayerCharacterNode::setInputs(int forward, int backward, int strafeLeft, int strafeRight, int jump, int toggleNoClip)
{ {
m_inputActions[FORWARD] = forward; m_inputActions[FORWARD] = forward;
@ -127,6 +140,7 @@ void PlayerCharacterNode::update()
// update camera position // update camera position
btVector3 pos = m_rigidBody->getCenterOfMassPosition(); btVector3 pos = m_rigidBody->getCenterOfMassPosition();
m_playerLight->setPos(glm::vec3(pos.x(), pos.y()+PLAYER_HEIGHT/2.f, pos.z()));
m_fpsCamera.moveTo(glm::vec3(pos.x(), pos.y()+EYES_OFFSET, pos.z())); m_fpsCamera.moveTo(glm::vec3(pos.x(), pos.y()+EYES_OFFSET, pos.z()));
// update body movement // update body movement
@ -179,6 +193,7 @@ void PlayerCharacterNode::update()
} }
m_rigidBody->setLinearVelocity(newVelocity); m_rigidBody->setLinearVelocity(newVelocity);
} }
m_playerLightNode->update();
} }
void PlayerCharacterNode::toggleNoClip() void PlayerCharacterNode::toggleNoClip()
@ -186,3 +201,9 @@ void PlayerCharacterNode::toggleNoClip()
m_noclipMode = !m_noclipMode; m_noclipMode = !m_noclipMode;
m_noclip_pos = m_rigidBody->getCenterOfMassPosition(); m_noclip_pos = m_rigidBody->getCenterOfMassPosition();
} }
void PlayerCharacterNode::setSceneTree(SceneTree* tree)
{
m_scene = tree;
m_playerLightNode->setSceneTree(tree);
}

View File

@ -7,6 +7,8 @@
class btRigidBody; class btRigidBody;
class btDefaultMotionState; class btDefaultMotionState;
class PointLight;
class LightNode;
class FirstPersonCamera : public BasicCamera class FirstPersonCamera : public BasicCamera
{ {
@ -39,12 +41,16 @@ class PlayerCharacterNode : public CameraNode
bool m_noclipMode; bool m_noclipMode;
btVector3 m_noclip_pos; btVector3 m_noclip_pos;
PointLight* m_playerLight;
LightNode* m_playerLightNode;
std::vector<int> m_inputActions; std::vector<int> m_inputActions;
enum PlayerAction {FORWARD, BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, JUMP, TOGGLE_NOCLIP}; enum PlayerAction {FORWARD, BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, JUMP, TOGGLE_NOCLIP};
public: public:
PlayerCharacterNode(bool noClip = true); PlayerCharacterNode(bool noClip = true);
~PlayerCharacterNode();
void setInputs(int forward, int backward, int strafe_left, int strafe_right, int jump = NO_ACTION, int toggleNoClip = NO_ACTION); void setInputs(int forward, int backward, int strafe_left, int strafe_right, int jump = NO_ACTION, int toggleNoClip = NO_ACTION);
@ -60,6 +66,8 @@ public:
btRigidBody* getRigidbody() { return m_rigidBody; } btRigidBody* getRigidbody() { return m_rigidBody; }
virtual Camera *getCamera() { return &m_fpsCamera; } virtual Camera *getCamera() { return &m_fpsCamera; }
virtual void setSceneTree(SceneTree* tree);
}; };
#endif // PLAYERCHARACTERNODE_H #endif // PLAYERCHARACTERNODE_H

View File

@ -124,24 +124,43 @@ void generateSponza(SceneTree *scene, btDiscreteDynamicsWorld *world)
} }
} }
int main(int argc, char** argv){ struct Config
enum Mode { SIMPLEST_TEST, TERRAIN_TEST, SPONZA_TEST, FULLSCREEN_DEMO }; {
Mode mode = SIMPLEST_TEST; std::string mode; // fullscreen / windowed / borderless
if(argc > 1) std::string scene; // terrain / sponza / none
bool vsync;
int width;
int height;
Config() :
mode("windowed"),
scene("none"),
vsync(false),
width(800),
height(600)
{}
void loadFromMap(std::unordered_map<std::string, std::string>* configMap)
{ {
std::string modeStr(argv[1]); std::unordered_map<std::string, std::string>& conf = *configMap;
if(modeStr == "terrain") if(conf.count("width"))
mode = TERRAIN_TEST; width = std::stoi(conf["width"]);
else if(modeStr == "sponza") if(conf.count("height"))
mode = SPONZA_TEST; height = std::stoi(conf["height"]);
else if(modeStr == "demo") if(conf.count("vsync"))
mode = FULLSCREEN_DEMO; vsync = (conf["vsync"] == "true");
else if(modeStr == "simple") if(conf.count("mode"))
mode = SIMPLEST_TEST; mode = conf["mode"];
else if(conf.count("scene"))
std::cout << "AVAILABLE MODES : simple(default) / terrain / sponza / demo" << std::endl; scene = conf["scene"];
} }
};
int main(){
Config config;
config.loadFromMap(Loader::loadConfigFile("../config.ini"));
Engine engine; Engine engine;
Loader::setObjDirectory("../data/"); Loader::setObjDirectory("../data/");
@ -150,13 +169,10 @@ int main(int argc, char** argv){
// this creates the opengl context // this creates the opengl context
// the opengl context must exist before any opengl class is used (texture, pipeline, etc..) // the opengl context must exist before any opengl class is used (texture, pipeline, etc..)
if(mode == FULLSCREEN_DEMO || mode == SPONZA_TEST) engine.createWindow("Sparrow Engine Demo", config.width, config.height, config.mode);
{ engine.getWindow()->setVerticalSyncEnabled(config.vsync);
engine.createWindow("Sparrow Engine Testing Environment", 1920, 1080, true);
engine.toggleMouseVisibility(); engine.toggleMouseVisibility();
}
else
engine.createWindow("Sparrow Engine Testing Environment");
// setting up SparrowEngine // setting up SparrowEngine
engine.initPhysics(); engine.initPhysics();
@ -201,34 +217,31 @@ int main(int argc, char** argv){
scene->getRootObject()->addChild(potator); scene->getRootObject()->addChild(potator);
// lighting // lighting
LightNode *ambientLight = new LightNode(new AmbientLight()); LightNode *ambientLight = new LightNode(new AmbientLight(glm::vec3(0.05f)));
DirectionnalLight* sun = new DirectionnalLight(glm::vec3(5, 8, -2), glm::vec3(0.9f)); DirectionnalLight* sun = new DirectionnalLight(glm::vec3(5, 8, -2), glm::vec3(0.9f));
LightNode *sunLight = new LightNode(sun); LightNode *sunLight = new LightNode(sun);
LightNode *fillLight = new LightNode(new DirectionnalLight(glm::vec3(8, -4, 1), glm::vec3(0.1f)));
scene->getRootObject()->addChild(ambientLight); scene->getRootObject()->addChild(ambientLight);
scene->getRootObject()->addChild(sunLight); scene->getRootObject()->addChild(sunLight);
scene->getRootObject()->addChild(fillLight);
// terrain // scene
if(mode != SIMPLEST_TEST) if(config.scene == "sponza")
{ {
sun->initShadowMap(4096); sun->initShadowMap(4096);
if(mode == SPONZA_TEST)
{
generateSponza(scene, engine.getPhysics()); generateSponza(scene, engine.getPhysics());
scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-3.5, 2, 1.8), 15, glm::vec3(0.35f))));
scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-5, 6, 2), 15, glm::vec3(0.35f))));
player->setPosition(0.f, 2.f, 0.f); player->setPosition(0.f, 2.f, 0.f);
sun->setShadowView(glm::vec3(30, 30, 50)); sun->setShadowView(glm::vec3(30, 30, 50));
} }
else else if(config.scene == "terrain")
{ {
sun->initShadowMap(4096);
generateTerrain(scene, engine.getPhysics()); generateTerrain(scene, engine.getPhysics());
player->setPosition(0.f, 15.f, 0.f); player->setPosition(0.f, 15.f, 0.f);
sun->setShadowView(glm::vec3(130, 130, 70)); sun->setShadowView(glm::vec3(130, 130, 70));
} }
}
// shell output tests // shell output tests
engine.outputShell("Hello World!"); engine.outputShell("Hello World!");

View File

@ -127,7 +127,7 @@ void Potator::throwCube()
dir = m_player->getDirection(); dir = m_player->getDirection();
pos = m_player->getEyePosition() + dir*2.f; pos = m_player->getEyePosition() + dir*2.f;
} }
float throwForce = 10.f; float throwForce = 5.f;
createGib(new MeshNode(m_cubeMesh), m_cubeShape, m_cubeMass, pos, dir*throwForce, 30000); createGib(new MeshNode(m_cubeMesh), m_cubeShape, m_cubeMass, pos, dir*throwForce, 30000);
} }
@ -141,7 +141,7 @@ void Potator::throwSphere()
dir = m_player->getDirection(); dir = m_player->getDirection();
pos = m_player->getEyePosition() + dir*2.f; pos = m_player->getEyePosition() + dir*2.f;
} }
float throwForce = 10.f; float throwForce = 5.f;
createGib(new MeshNode(m_sphereMesh), m_sphereShape, m_sphereMass, pos, dir*throwForce, 30000); createGib(new MeshNode(m_sphereMesh), m_sphereShape, m_sphereMass, pos, dir*throwForce, 30000);
} }
@ -155,12 +155,12 @@ void Potator::throwSword()
dir = m_player->getDirection(); dir = m_player->getDirection();
pos = m_player->getEyePosition() + dir*2.f; pos = m_player->getEyePosition() + dir*2.f;
} }
float throwForce = 10.f; float throwForce = 5.f;
GraphicalContainerNode *node = new GraphicalContainerNode(); GraphicalContainerNode *node = new GraphicalContainerNode();
for(Mesh * m : m_swordMeshes) for(Mesh * m : m_swordMeshes)
node->addChild(new MeshNode(m)); node->addChild(new MeshNode(m));
createGib(node, m_swordShape, m_swordMass, pos, dir*throwForce, 300000); createGib(node, m_swordShape, m_swordMass, pos, dir*throwForce, 30000);
} }
void Potator::update() void Potator::update()

View File

@ -31,6 +31,39 @@ std::string* Loader::loadTextFile(const std::string &filename)
return str; return str;
} }
std::unordered_map<std::string, std::string>* Loader::loadConfigFile(const std::string &filename)
{
std::unordered_map<std::string, std::string>* configPtr = new std::unordered_map<std::string, std::string>();
std::unordered_map<std::string, std::string>& config = *configPtr;
std::ifstream t(filename);
if(!t.is_open())
return configPtr;
char line[256];
std::string currentLine;
while(!t.eof())
{
currentLine = "";
t.getline(line, 256);
currentLine += std::string(line);
if(t.eof() || t.fail() || currentLine[0] == '#')
continue;
unsigned int pos = currentLine.find_first_of('=');
if(pos == std::string::npos)
continue;
std::string key = currentLine.substr(0, pos);
std::string val = currentLine.substr(pos+1);
config[key] = val;
}
return configPtr;
}
Image* Loader::loadImage(const std::string &filename, bool hasAlpha, bool reversed) Image* Loader::loadImage(const std::string &filename, bool hasAlpha, bool reversed)
{ {
sf::Image sfImg; sf::Image sfImg;

View File

@ -3,6 +3,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
class Image; class Image;
@ -17,6 +18,7 @@ class Loader
public: public:
static std::string* loadTextFile(const std::string &filename); static std::string* loadTextFile(const std::string &filename);
static std::unordered_map<std::string, std::string>* loadConfigFile(const std::string &filename);
static Image* loadImage(const std::string &filename, bool hasAlpha = true, bool reversed = true); static Image* loadImage(const std::string &filename, bool hasAlpha = true, bool reversed = true);
static std::vector<Mesh*> loadMesh(const std::string &filename); static std::vector<Mesh*> loadMesh(const std::string &filename);
static Font* loadFont(const std::string &texture, const std::string &description); static Font* loadFont(const std::string &texture, const std::string &description);