paella/src/Meshing/MainDisplay.cpp

218 lines
6.1 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/FreeFlyCamera.hpp"
#include "Geometry/Mesh.hpp"
#include "Geometry/Vector.hpp"
#include "Geometry/Point.hpp"
#include "Meshing/Skeleton3D.hpp"
// Full hd : 1920x1080
constexpr auto WIDTH = 1920;
constexpr auto HEIGHT = 1080;
void drawScene(sft::FreeFlyCamera const& camera, geo::Mesh const& mesh);
int main(int argc, char *argv[])
{
// Init mesh
pae::Skeleton3D skeleton;
if (argc <= 1)
{
if (!skeleton.loadFromFile("data/skl/skel.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;
}
}
geo::Mesh mesh;
mesh = skeleton.computeMesh();
mesh.prepareEdges();
// 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(70,(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();
// Camera
auto camera = sft::FreeFlyCamera{};
// la boucle principale
auto running = true;
auto captureRequired = false;
while (running)
{
// gestion des évènements
sf::Event event;
while (window.pollEvent(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);
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;
}
// termine la trame courante (en interne, échange les deux tampons de rendu)
window.display();
}
// libération des ressources...
return 0;
}
void drawScene(sft::FreeFlyCamera const& camera, geo::Mesh 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.drawEdges();
glTranslatef(mesh.vertices[0].x(), mesh.vertices[0].y(), mesh.vertices[0].z());
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();
// MAJ de l'écran
glFlush();
}