224 lines
7.4 KiB
C++

#include <engine.h>
#include <input.h>
#include <scene.h>
#include <mesh.h>
#include <deferredpipeline.h>
#include <phongmaterial.h>
#include <resourcemanager.h>
#include <sparrowrenderer.h>
#include <texture.h>
#include <chunk.h>
#include <scene/scenetree.h>
#include <scene/textnode.h>
#include <scene/trackballcameranode.h>
#include <scene/playercharacternode.h>
#include <scene/lightnode.h>
#include <scene/graphicalcontainernode.h>
#include <tools/graph.h>
#include <tools/pathfinder.h>
#include <tools/loader.h>
#include <tools/font.h>
#include <tools/utils.h>
#include <sparrowshell/sparrowshell.h>
#include <btBulletDynamicsCommon.h>
#include <glm/ext.hpp>
#include "potator.h"
class myKeysMap : public IKeysMap{
public:
enum{MAIN_ACTION, SECONDARY_ACTION, MOVE_FORWARD, MOVE_BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, TOGGLE_NOCLIP, TOGGLE_PHYSICS_DEBUG, TOGGLE_CONSOLE = 15};
myKeysMap(){
keys.push_back( {MAIN_ACTION, sf::Keyboard::KeyCount + sf::Mouse::Left, IKeysMap::PRESSED} );
keys.push_back( {SECONDARY_ACTION, sf::Keyboard::KeyCount + sf::Mouse::Right, IKeysMap::PRESSED} );
keys.push_back( {MOVE_FORWARD, sf::Keyboard::Z, IKeysMap::HOLD} );
keys.push_back( {MOVE_BACKWARD, sf::Keyboard::S, IKeysMap::HOLD} );
keys.push_back( {STRAFE_LEFT, sf::Keyboard::Q, IKeysMap::HOLD} );
keys.push_back( {STRAFE_RIGHT, sf::Keyboard::D, IKeysMap::HOLD} );
keys.push_back( {TOGGLE_NOCLIP, sf::Keyboard::G, IKeysMap::PRESSED} );
keys.push_back( {TOGGLE_PHYSICS_DEBUG, sf::Keyboard::P, IKeysMap::PRESSED} );
keys.push_back( {TOGGLE_CONSOLE, sf::Keyboard::F5, IKeysMap::PRESSED} );
}
static std::vector<int> getMap()
{
return {MAIN_ACTION, SECONDARY_ACTION, MOVE_FORWARD, MOVE_BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, TOGGLE_NOCLIP, TOGGLE_PHYSICS_DEBUG, TOGGLE_CONSOLE};
}
};
class TestGen : public TerrainGenerator
{
float map[64*64];
public:
TestGen()
{
for(int i=0; i<64*64; ++i)
map[i] = (rand()%64)/64.f;
}
float getHeight(float x, float z, float zoom)
{
x /= zoom;
z /= zoom;
int i1 = int(floor(x)+64)%64;
int j1 = int(floor(z)+64)%64;
int i2 = (i1+1)%64;
int j2 = (j1+1)%64;
float v1 = map[i1*64+j1];
float v2 = map[i2*64+j1];
float v3 = map[i1*64+j2];
float v4 = map[i2*64+j2];
float ph = x - floor(x);
float pv = z - floor(z);
ph = 0.5f-cos(3.1416*ph)/2;
pv = 0.5f-cos(3.1416*pv)/2;
float v5 = v1*(1-ph) + v2*ph;
float v6 = v3*(1-ph) + v4*ph;
return (v5*(1-pv) + v6*pv)*zoom;
}
virtual float func(float x, float y, float z)
{
float height = getHeight(x+6.7, z+15.7, 2.5f);
height += getHeight(x+5.76, z+2.14, 5.4f);
height += getHeight(x, z, 10.f);
return y+8.f - height;
}
};
void generateTerrain(SceneTree *scene, btDiscreteDynamicsWorld *world)
{
GraphicalContainerNode* terrainContainer = new GraphicalContainerNode();
scene->getRootObject()->addChild(terrainContainer);
TestGen gen;
PhongMaterial *mat = new PhongMaterial();
mat->shininess = 5.f;
mat->diffuse = glm::vec3(0.1f, 0.4f, 0.2f);
mat->specular = glm::vec3(0.1f);
mat->emission = glm::vec3(0.5f, 0.1f, 0.1f);
for(int x=-6; x<6; ++x)
for(int y=-3; y<3; ++y)
for(int z=-6; z<6; ++z)
{
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())
delete chunk;
else
{
chunk->mesh->setMaterial(mat);
chunk->mesh->initGL();
MeshNode *node = new MeshNode(chunk->mesh);
node->setTransform(glm::translate(glm::scale(glm::mat4(), glm::vec3(2.f)), pos*8.f));
terrainContainer->addChild(node);
world->addRigidBody(node->buildStaticCollider());
}
}
}
int main(){
Engine engine;
// this creates the opengl context
// the opengl context must exist before any opengl class is used (texture, pipeline, etc..)
engine.createWindow("Sparrow Engine Testing Environment");
// setting up SparrowEngine
engine.initPhysics();
SceneTree *scene = engine.createScene();
engine.setScene(scene);
// settin gup SparrowInput
Input* input = engine.getInput();
input->setKeysMap(myKeysMap());
input->addContext(Context("default", myKeysMap::getMap()));
input->setCurrentContext("default");
input->updateKeyBindings();
engine.setTogglePhysicsDebugAction(myKeysMap::TOGGLE_PHYSICS_DEBUG);
/*
// trackball camera
TrackBallCameraNode *trackBallCam = new TrackBallCameraNode(engine.getInput());
trackBallCam->setInputs(myKeysMap::SECONDARY_HOLD, myKeysMap::MAIN_HOLD);
scene.getRootObject()->addChild(trackBallCam);
scene.setMainCamera(trackBallCam);
*/
// first person player controller
PlayerCharacterNode *player = new PlayerCharacterNode();
player->setInputs(myKeysMap::MOVE_FORWARD, myKeysMap::MOVE_BACKWARD, myKeysMap::STRAFE_LEFT, myKeysMap::STRAFE_RIGHT, myKeysMap::TOGGLE_NOCLIP);
scene->getRootObject()->addChild(player);
scene->setMainCamera(player);
player->setPosition(0.f, 10.f, 0.f);
engine.getPhysics()->addRigidBody(player->getRigidbody());
// throw cubes and spheres with mouse clicks
Potator *potator = new Potator(player, myKeysMap::MAIN_ACTION, myKeysMap::SECONDARY_ACTION);
scene->getRootObject()->addChild(potator);
// lighting
LightNode *sunLight = new LightNode(new DirectionnalLight(glm::vec3(5, 8, -2), glm::vec3(1.f)));
LightNode *ambientLight = new LightNode(new AmbientLight());
scene->getRootObject()->addChild(ambientLight);
scene->getRootObject()->addChild(sunLight);
// TODO : set this elsewhere, since the shell needs it
Font* fonte_des_neiges = Loader::loadFont("../data/consolas.fnt","../data/consolas.png");
RESOURCE_ADD(fonte_des_neiges,Font,"shellfont");
// terrain
generateTerrain(scene, engine.getPhysics());
// shell output tests
engine.outputShell("Hello World!");
engine.outputShell("Starting test :");
for(int i = 0; i < 17; i++){
engine.outputShell(std::to_string(i));
}
// preparing shaders and launching the engine
scene->updateShaders();
engine.start();
// pathfinding tests
/* GraphNode n1 = GraphNode();
n1.setValue(1);
GraphNode n2 = GraphNode();
n2.setValue(2);
GraphNode n3 = GraphNode();
n3.setValue(3);
GraphNode n4 = GraphNode();
n4.setValue(4);
GraphNode n5 = GraphNode();
n5.setValue(5);
n1.addNeighbours(&n2);
n1.addNeighbours(&n3);
n2.addNeighbours(&n4);
n3.addNeighbours(&n4);
n3.addNeighbours(&n2);
n3.addNeighbours(&n5);
std::vector<GraphNode*> path = PathFinder::a_star(&n1,&n4,true);
std::cout << "Path Size: " << path.size() << std::endl;
for(GraphNode* gn: path){
std::cout << gn->getValue() << std::endl;
}
// loaders tests
Loader::setObjDirectory("../data/");
Loader::setMtlDirectory("../data/");
Loader::setTexDirectory("../data/");
std::vector<Mesh*> meshes = Loader::loadMesh("sword.obj");
*/
}