paella/src/Animation/AnimatedMain.cpp

242 lines
6.6 KiB
C++

////////////////////////////////////////////////////////////////////////////////
//
// Paella
// Copyright (C) 2015 - Thomas FORGIONE, Emilie JALRAS, Marion LENFANT, Thierry MALON, Amandine PAILLOUX
// Authors :
// Thomas FORGIONE
// Emilie JALRAS
// Marion LENFANT
// Thierry MALON
// Amandine PAILLOUX
//
// This file is part of the project Paella
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <memory>
#include <cmath>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <GL/gl.h>
#include <GL/glu.h>
#include <SFMLTools/Camera.hpp>
#include <SFMLTools/FreeFlyCamera.hpp>
#include <SFMLTools/JoyCam.hpp>
#include <Geometry/Mesh.hpp>
#include <Geometry/Vector.hpp>
#include <Geometry/Point.hpp>
#include <Meshing/Skeleton3D.hpp>
#include <Animation/AnimatedMesh.hpp>
#include <Animation/Menu.hpp>
// Full hd : 1920x1080
constexpr auto WIDTH = 1920;
constexpr auto HEIGHT = 1080;
template<typename T>
void drawScene(T const& camera, pae::AnimatedMesh const& mesh);
int main(int argc, char *argv[])
{
std::srand(std::time(nullptr));
// Init mesh
pae::Skeleton3D skeleton;
if (argc <= 1)
{
if (!skeleton.loadFromFile("data/skl/dinoplat.skl"))
{
std::cerr << "Default file not found ! Try with a file..." << std::endl;
}
}
else
{
auto path = std::string{argv[1]};
if (!skeleton.loadFromFile(path))
{
std::cerr << "File " << path << " not found ! Try with another file..." << std::endl;
}
}
sf::Joystick::update();
int joy = -1;
for(int i=0; i< sf::Joystick::Count; ++i)
{
if(sf::Joystick::isConnected(i))
joy = i;
}
// Camera
std::unique_ptr<sft::Camera> camera;
if (joy >= 0)
{
std::cout << "Joystick detected ! You should now use your controller to move the camera" << std::endl;
camera = std::make_unique<sft::JoyCam>(joy);
}
else
{
std::cout << "No joystick detected ! You should use your keyboard to move the camera" << std::endl;
camera = std::make_unique<sft::FreeFlyCamera>();
}
auto mesh = std::make_shared<pae::AnimatedMesh>(skeleton);
auto menu = pae::Menu{mesh};
// Création de la fenêtre
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT),
"pae",
sf::Style::Fullscreen,
sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
window.setMouseCursorVisible(false);
//Initialisation du mode 3D
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,(double)WIDTH/HEIGHT,0.2,3000);
// Initialisation de Z-Buffer
glEnable(GL_DEPTH_TEST);
// Activer les textures
glEnable(GL_TEXTURE_2D);
// Autoriser la transparence
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Ces 2 lignes améliorent le rendu
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
// On efface le tampon d'affichage
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFlush();
// la boucle principale
auto running = true;
auto captureRequired = false;
while (running)
{
// gestion des évènements
sf::Event event;
while (window.pollEvent(event))
{
menu.manageEvent(event);
if (event.type == sf::Event::Closed)
{
// on stoppe le programme
running = false;
}
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
{
running = false;
}
else if (event.key.code == sf::Keyboard::P)
{
captureRequired = true;
}
}
else if (event.type == sf::Event::MouseMoved)
{
// Hum...
}
else if (event.type == sf::Event::Resized)
{
// on ajuste le viewport lorsque la fenêtre est redimensionnée
glViewport(0, 0, event.size.width, event.size.height);
}
}
// Les trucs qui sont pas des évènements
camera->nextStep(WIDTH, HEIGHT);
sf::Mouse::setPosition(sf::Vector2i{WIDTH/2, HEIGHT/2});
// Dessin de la scene
drawScene(camera, *mesh);
window.draw(menu);
if (captureRequired)
{
sf::Vector2u windowSize = window.getSize();
sf::Texture texture;
texture.create(windowSize.x, windowSize.y);
texture.update(window);
sf::Image screenshot = texture.copyToImage();
screenshot.saveToFile("capture.jpg");
captureRequired = false;
}
// Draw the texts
// termine la trame courante (en interne, échange les deux tampons de rendu)
window.display();
}
// libération des ressources...
return 0;
}
template<typename T>
void drawScene(T const& camera, pae::AnimatedMesh const& mesh)
{
// Initialisation
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Position Camera gluLookAt(positionCamera, positionCible, vecteurVertical)
camera->look();
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
// Dessin de carrés
mesh.draw();
// MAJ de l'écran
glFlush();
}