2016-12-07 19:42:58 +01:00

252 lines
7.9 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/cameranode.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 <glm/ext.hpp>
class myKeysMap : public IKeysMap{
public:
enum{ROTATE_CAMERA, MOVE_CAMERA, TOGGLE_CONSOLE = 15};
myKeysMap(){
Binding b;
b.action = TOGGLE_CONSOLE;
b.key = sf::Keyboard::F3;
b.type = IKeysMap::PRESSED;
keys.push_back(b);
b.action = ROTATE_CAMERA;
b.type = IKeysMap::HOLD;
b.key = sf::Keyboard::KeyCount + sf::Mouse::Left;
keys.push_back(b);
b.action = MOVE_CAMERA;
b.type = IKeysMap::HOLD;
b.key = sf::Keyboard::KeyCount + sf::Mouse::Right;
keys.push_back(b);
}
static std::vector<int> getMap()
{
return {ROTATE_CAMERA, MOVE_CAMERA, 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)
{
GraphicalContainerNode* terrainContainer = new GraphicalContainerNode();
scene->getRootObject()->addChild(terrainContainer);
TestGen gen;
PhongMaterial *mat = new PhongMaterial();
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);
}
}
}
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("test");
SceneTree scene;
CameraNode *cam = new CameraNode(engine.getInput());
cam->setInputs(myKeysMap::MOVE_CAMERA, myKeysMap::ROTATE_CAMERA);
scene.getRootObject()->addChild(cam);
scene.setMainCamera(cam);
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);
/* Exemple creation mesh 2D
*
Mesh* mesh = new Mesh();
mesh->addRectangle2D(180, 400,10,64);
PhongMaterial *mat = new PhongMaterial();
mat->diffuse = glm::vec3(1, 0.5, 0);
mesh->setMaterial(mat);
mesh->setDepth(1);
mesh->initGL();
scene.getRootObject()->addChild(new MeshNode(mesh));
*/
/* Exemple ajout d'un objet a la scene
*
SparrowShell *shell = new SparrowShell(engine.getWindow(),engine.getInput());
scene.getRootObject()->addChild(shell);
*/
// the pipeline needs to updates his shaders because the scene changed
// this should be handled somewhere else in the future
//DeferredPipeline* pipeline = (DeferredPipeline*)scene.getPipeline();
//pipeline->refreshScene(&scene);
Font* fonte_des_neiges = Loader::loadFont("../data/consolas.fnt","../data/consolas.png");
RESOURCE_ADD(fonte_des_neiges,Font,"shellfont");
TextNode* tnode/* = new MeshNode(fonte_des_neiges->getTextMesh("Hello World!"));
scene.getRootObject()->addChild(mnode)*/;
// mnode = new MeshNode(fonte_des_neiges->getTextMesh("Portez ce vieux whisky au juge blond qui fume.", glm::vec3(0.5, 0.7, 0.2)));
// mnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(70, 30, 0)), 0.4f, glm::vec3(0, 0, 1)));
// utils::setPosition2D(mnode,glm::vec2(0, 400));
// utils::rotate2D(mnode, glm::vec2(10,10),0.5);
// scene.getRootObject()->addChild(mnode);
tnode = fonte_des_neiges->getTextNode("Such Text", glm::vec3(0.7, 0.4, 0.2), 32.f);
utils::setPosition2D((MeshNode*)tnode,glm::vec2(200, 170));
utils::rotate2D((MeshNode*)tnode, glm::vec2(0),-0.5);
tnode->m_movement = glm::translate(glm::rotate(glm::translate(glm::mat4(), glm::vec3(240, 180, 0)), 0.03f, glm::vec3(0, 0, 1)), glm::vec3(-240, -180, 0));
// mnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(200, 170, 0)), -0.5f, glm::vec3(0, 0, 1)));
scene.getRootObject()->addChild((SceneNode*)tnode);
//tnode = fonte_des_neiges->getTextNode("Such Text", glm::vec3(0.7, 0.4, 0.2));
//tnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(200, 170, 0)), -0.5f, glm::vec3(0, 0, 1)));
//scene.getRootObject()->addChild(tnode);
// mnode = new MeshNode(fonte_des_neiges->getTextMesh("Very font", glm::vec3(0.7, 0.2, 0.8)));
// mnode->setTransform(glm::rotate(glm::translate(glm::mat4(), glm::vec3(180, 400, 0)), 0.1f, glm::vec3(0, 0, 1)));
// scene.getRootObject()->addChild(mnode);
// mnode = new MeshNode(fonte_des_neiges->getTextMesh("Much animation", glm::vec3(0.3, 0.3, 0.8)));
// mnode->setTransform(glm::translate(glm::mat4(), glm::vec3(400, 250, 0)));
// mnode->m_movement = glm::translate(glm::rotate(glm::translate(glm::mat4(), glm::vec3(580, 280, 0)), 0.03f, glm::vec3(0, 0, 1)), glm::vec3(-580, -280, 0));
// scene.getRootObject()->addChild(mnode);
generateTerrain(&scene);
engine.setScene(&scene);
engine.outputShell("Hello World!");
engine.outputShell("Starting test :");
for(int i = 0; i < 17; i++){
engine.outputShell(std::to_string(i));
}
Input* input = engine.getInput();
input->setKeysMap(myKeysMap());
input->addContext(Context("default", myKeysMap::getMap()));
input->setCurrentContext("default");
input->updateKeyBindings();
engine.start();
/* 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;
}
Loader::setObjDirectory("../data/");
Loader::setMtlDirectory("../data/");
Loader::setTexDirectory("../data/");
std::vector<Mesh*> meshes = Loader::loadMesh("sword.obj");
*/
}