Commit 620c9fa1 authored by Thibault Soucarre's avatar Thibault Soucarre

New class Shader. Render_alternate can draw containers, states and links....

New class Shader. Render_alternate can draw containers, states and links. Scaling and position of the camera aren't correct. Files vertexshader and fragmentshader should be copied to the directory of the binary file. No changes when using Render_opengl
parent c6475b04
......@@ -249,12 +249,14 @@ IF(VITE_ENABLE_VBO)
SET(VITE_HDRS
${VITE_HDRS}
render/vbo.hpp
render/Shader.hpp
render/Render_alternate.hpp
)
SET(VITE_SRCS
${VITE_SRCS}
render/vbo.cpp
render/Shader.cpp
render/Render_alternate.cpp
)
ENDIF(VITE_ENABLE_VBO)
......
......@@ -45,7 +45,7 @@
*\brief This is the console interface C source code.
*/
#include <GL/glew.h>
#include <queue>
#include <string>
#include <iostream>
......
This diff is collapsed.
......@@ -55,6 +55,10 @@
#include <QGLWidget>
#include <QLabel>
#include <QPainter>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
/* -- */
#include "common/common.hpp"
#include "common/Info.hpp"
......@@ -65,39 +69,12 @@
#include "render/Minimap.hpp"
#include "render/Ruler.hpp"
#include "render/GanttDiagram.hpp"
#include "render/vbo.hpp"
#include "render/Shader.hpp"
class Render_alternate;
class Interface_console;
struct Container_text_;
class Vbo;
/*!
* \brief Structure used to store arrow information.
*/
struct Arrow_{
/*!
* \brief Coordinates.
*/
Element_pos start_time, end_time, start_height, end_height;
/*!
* \brief Colors.
*/
Element_col red, green, blue;
};
/*!
* \brief Structure used to store event information.
*/
struct Event_{
/*!
* \brief Coordinates and radius.
*/
Element_pos time, height, container_height;
/*!
* \brief Colors.
*/
Element_col red, green, blue;
};
/*!
......@@ -106,15 +83,21 @@ struct Event_{
class Render_alternate : /*public QGLWidget,*/ public Hook_event, public Render
{
Q_OBJECT
private:
Shader _shader;
Element_col _r;
Element_col _g;
Element_col _b;
bool _draw_container;
bool _draw_states;
bool _draw_arrow;
bool _draw_event;
bool _draw_counter;
std::vector<Element_pos> _links;
glm::mat4 _modelview;
protected:
/*!
* \brief Contains the parent instance.
*/
//Core* _core;
/*!
/*!
* \brief The waiting screen display list.
*/
GLuint _wait_list;
......@@ -141,52 +124,15 @@ protected:
*/
// GLuint _wait_list;
float _red, _green, _blue;
std::vector<Container_text_> _texts;
std::vector<Arrow_> _arrows;
std::vector<Event_> _circles;
/***********************************
*
* The wait screen drawing.
*
**********************************/
/***********************************
* The wait list Attributes.
**********************************/
/*!
* \brief Rotation angle for the wait.
*/
float _wait_angle;
/*!
* \brief Y rotation angle for the wait.
*/
float _wait_angle_y;
/*!
* \brief Time in ms between two frames for the waiting screen.
*/
static const int DRAWING_TIMER_DEFAULT;
/*!
* \brief Wait animation seconds per frame.
*/
int _wait_spf;
//std::vector<Container_text_> _texts;
//std::vector<Arrow_> _arrows;
//std::vector<Event_> _circles;
Vbo _containers;
Vbo _states;
Vbo _events;
Vbo _arrows;
Vbo _counters;
Vbo* _current_vbo;
std::vector<Vbo*> _vbos;
/*!
* \brief Timer to animate the wait.
*/
QTimer* _wait_timer;
public:
......
#include <GL/glew.h>
#include <iostream>
#include "Shader.hpp"
// Constructeurs et Destructeur
Shader::Shader() : m_vertexID(0), m_fragmentID(0), m_programID(0), m_vertexSource(), m_fragmentSource()
{
}
Shader::Shader(Shader const &shaderACopier)
{
// Copie des fichiers sources
m_vertexSource = shaderACopier.m_vertexSource;
m_fragmentSource = shaderACopier.m_fragmentSource;
// Chargement du nouveau shader
charger();
}
Shader::Shader(std::string vertexSource, std::string fragmentSource) : m_vertexID(0), m_fragmentID(0), m_programID(0),
m_vertexSource(vertexSource), m_fragmentSource(fragmentSource)
{
}
Shader::~Shader()
{
// Destruction du shader
glDeleteShader(m_vertexID);
glDeleteShader(m_fragmentID);
glDeleteProgram(m_programID);
}
// Méthodes
Shader& Shader::operator=(Shader const &shaderACopier)
{
// Copie des fichiers sources
m_vertexSource = shaderACopier.m_vertexSource;
m_fragmentSource = shaderACopier.m_fragmentSource;
// Chargement du nouveau shader
charger();
// Retour du pointeur this
return *this;
}
bool Shader::charger()
{
// Destruction d'un éventuel ancien Shader
/*if(glIsShader(m_vertexID) == GL_TRUE)
glDeleteShader(m_vertexID);
if(glIsShader(m_fragmentID) == GL_TRUE)
glDeleteShader(m_fragmentID);
if(glIsProgram(m_programID) == GL_TRUE)
glDeleteProgram(m_programID);
*/
// Compilation des shaders
if(!compilerShader(m_vertexID, GL_VERTEX_SHADER, m_vertexSource))
return false;
if(!compilerShader(m_fragmentID, GL_FRAGMENT_SHADER, m_fragmentSource))
return false;
// Création du programme
m_programID = glCreateProgram();
// Association des shaders
glAttachShader(m_programID, m_vertexID);
glAttachShader(m_programID, m_fragmentID);
// Verrouillage des entrées shader
glBindAttribLocation(m_programID, 0, "in_Vertex");
glBindAttribLocation(m_programID, 1, "in_Color");
glBindAttribLocation(m_programID, 2, "in_TexCoord0");
// Linkage du programme
glLinkProgram(m_programID);
// Vérification du linkage
GLint erreurLink(0);
glGetProgramiv(m_programID, GL_LINK_STATUS, &erreurLink);
// S'il y a eu une erreur
if(erreurLink != GL_TRUE)
{
// Récupération de la taille de l'erreur
GLint tailleErreur(0);
glGetProgramiv(m_programID, GL_INFO_LOG_LENGTH, &tailleErreur);
// Allocation de mémoire
char *erreur = new char[tailleErreur + 1];
// Récupération de l'erreur
glGetShaderInfoLog(m_programID, tailleErreur, &tailleErreur, erreur);
erreur[tailleErreur] = '\0';
// Affichage de l'erreur
std::cout << erreur << std::endl;
// Libération de la mémoire et retour du booléen false
delete[] erreur;
glDeleteProgram(m_programID);
return false;
}
// Sinon c'est que tout s'est bien passé
else
return true;
}
bool Shader::compilerShader(GLuint &shader, GLenum type, std::string const &fichierSource)
{
// Création du shader
shader = glCreateShader(type);
// Vérification du shader
if(shader == 0)
{
std::cout << "Erreur, le type de shader (" << type << ") n'existe pas" << std::endl;
return false;
}
// Flux de lecture
std::ifstream fichier(fichierSource.c_str());
// Test d'ouverture
if(!fichier)
{
std::cout << "Erreur le fichier " << fichierSource << " est introuvable" << std::endl;
glDeleteShader(shader);
return false;
}
// Strings permettant de lire le code source
std::string ligne;
std::string codeSource;
// Lecture
while(getline(fichier, ligne))
codeSource += ligne + '\n';
// Fermeture du fichier
fichier.close();
// Récupération de la chaine C du code source
const GLchar* chaineCodeSource = codeSource.c_str();
// Envoi du code source au shader
glShaderSource(shader, 1, &chaineCodeSource, 0);
// Compilation du shader
glCompileShader(shader);
// Vérification de la compilation
GLint erreurCompilation(0);
glGetShaderiv(shader, GL_COMPILE_STATUS, &erreurCompilation);
// S'il y a eu une erreur
if(erreurCompilation != GL_TRUE)
{
// Récupération de la taille de l'erreur
GLint tailleErreur(0);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &tailleErreur);
// Allocation de mémoire
char *erreur = new char[tailleErreur + 1];
// Récupération de l'erreur
glGetShaderInfoLog(shader, tailleErreur, &tailleErreur, erreur);
erreur[tailleErreur] = '\0';
// Affichage de l'erreur
std::cout << erreur << std::endl;
// Libération de la mémoire et retour du booléen false
delete[] erreur;
glDeleteShader(shader);
return false;
}
// Sinon c'est que tout s'est bien passé
else
return true;
}
// Getter
GLuint Shader::getProgramID() const
{
return m_programID;
}
#ifndef DEF_SHADER
#define DEF_SHADER
//#include <GL/glew.h>
// Includes communs
#include <iostream>
#include <string>
#include <fstream>
// Classe Shader
class Shader
{
public:
Shader();
Shader(Shader const &shaderACopier);
Shader(std::string vertexSource, std::string fragmentSource);
~Shader();
Shader& operator=(Shader const &shaderACopier);
bool charger();
bool compilerShader(GLuint &shader, GLenum type, std::string const &fichierSource);
GLuint getProgramID() const;
private:
GLuint m_vertexID;
GLuint m_fragmentID;
GLuint m_programID;
std::string m_vertexSource;
std::string m_fragmentSource;
};
#endif
#version 330 core
in vec3 color;
out vec4 out_Color;
void main(){
out_Color = vec4(color, 1.0);
}
\ No newline at end of file
......@@ -46,6 +46,10 @@
#include <GL/glew.h>
#include <stdlib.h>
#include <iostream>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "vbo.hpp"
......@@ -70,6 +74,10 @@ const int Vbo::VBO_UNDEFINED_ERROR = 2;
Vbo::Vbo(){
}
Vbo::Vbo(GLenum mode){
_mode = mode;
_nbVertex = 0;
}
Vbo::Vbo(int nb_object, Type object_type){
......@@ -110,6 +118,9 @@ Vbo::Vbo(int nb_object, Type object_type){
glBufferData(GL_ARRAY_BUFFER, _size*sizeof(Data_type), NULL, GL_STREAM_DRAW);/* allocate memory space inside graphic card */
glBindBuffer(GL_ARRAY_BUFFER, 0);/* deselect our vbo */
_vertex.clear();
_colors.clear();
_nbVertex = 0;
}
......@@ -132,39 +143,66 @@ Vbo::~Vbo(){
*
**********************************/
int Vbo::add(const unsigned int data_size, const Data_type* const data){
glBindBuffer(GL_ARRAY_BUFFER, _id);/* select our vbo */
/* Take care:
*
* For performance, please use glMapBuffer() if data amount is more than 32Kbits,
* else use glBufferSubData().
*
*/
if ( (_cur+data_size) <= _size){
glBufferSubData(GL_ARRAY_BUFFER, _cur*sizeof(Data_type), data_size*sizeof(Data_type), data);
_cur += data_size;
}else{
glBindBuffer(GL_ARRAY_BUFFER, 0);/* deselect our vbo */
return Vbo::VBO_OUT_OF_MEMORY;
}
glBindBuffer(GL_ARRAY_BUFFER, 0);/* deselect our vbo */
return Vbo::VBO_OK;
int Vbo::add(Element_pos x, Element_pos y, Element_col r, Element_col g, Element_col b){
_vertex.push_back(x);
_vertex.push_back(y);
_colors.push_back(r);
_colors.push_back(g);
_colors.push_back(b);
_nbVertex++;
}
int Vbo::get_remaining_size() const{
return (_size-_cur)/(_nb_verteces_per_object*_nb_coordinates_per_vertex);
}
void Vbo::config(){
std::cout << "Configuration du vbo, " << _nbVertex << " vertices" << std::endl;
int vertex_size = _vertex.size() * sizeof(Element_pos);
std::cout << _vertex.size() << std::endl;
std::cout << _colors.size() << std::endl;
int colors_size = _colors.size() * sizeof(Element_col);
Element_pos * vertex = &_vertex[0];
Element_col * colors = &_colors[0];
//Delete old vbo if necessary
if(glIsBuffer(_vboID) == GL_TRUE)
glDeleteBuffers(1, &_vboID);
//Generate vbo
glGenBuffers(1, &_vboID);
//Select our VBO
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
//Allocate memory inside graphic card
glBufferData(GL_ARRAY_BUFFER, vertex_size+colors_size, 0, GL_STATIC_DRAW);
//Send datas
glBufferSubData(GL_ARRAY_BUFFER, 0 , vertex_size, vertex);
glBufferSubData(GL_ARRAY_BUFFER, vertex_size, colors_size, colors);
//Deselect our VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
//Clear RAM
_vertex.clear();
_colors.clear();
//Delete old vao if necessary
if(glIsVertexArray(_vaoID))
glDeleteVertexArrays(1, &_vaoID);
//Generate VAO
glGenVertexArrays(1, &_vaoID);
//Select our VAO
glBindVertexArray(_vaoID);
//The following instructions are stocked inside the VAO
{
//Select VBO
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
//Send vertex
glVertexAttribPointer(0, 2, GL_DOUBLE, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
//Send colors
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertex_size));
glEnableVertexAttribArray(1);
//Deselect VBO
glBindBuffer(GL_ARRAY_BUFFER,0);
}
//Deselect VAO
glBindVertexArray(0);
}
/***********************************
......@@ -177,39 +215,30 @@ int Vbo::get_remaining_size() const{
*
**********************************/
void Vbo::display() const{
glBindBuffer(GL_ARRAY_BUFFER, _id);/* select our vbo */
/* glVertexPointer(A, B, C, D)
*
* A: coordinate number to identify a vertex (2 in 2 dimension space, 3 in 3 dimension space).
* B: the coordinate type (GL_INT, GL_FLOAT, GL_DOUBLE, etc.)
* C: stride -> the case offset between two vertex group (and not coordinate !) . Use to interleaves to kind of objects (for example, verteces and colors).
* D: the vertex buffer address in the VRAM. Use 0 with the VBO_BUFFER_OFFSET macro to convert a number into an address.
*/
glVertexPointer(_nb_coordinates_per_vertex, GL_DOUBLE, 0, VBO_BUFFER_OFFSET(0));/* give to OpenGL the VBO address */
glEnableClientState(_client_state);
/* glDrawArrays(A, B, C)
*
* A: an OpenGL primitive (GL_LINES, GL_TRIANGLES, etc.)
* B: the first vertex index of the vertex buffer which will be printed.
* C: the whole number of verteces which will be drawn.
*
* Please take care that a vertex is represented by _nb_coordinates_per_vertex cases of the _cur array.
*/
glDrawArrays(_object_type, 0, _cur/_nb_coordinates_per_vertex);/* RENDERING */
glDisableClientState(_client_state);
void Vbo::display(glm::mat4 modelview) const{
//select vao
glBindVertexArray(_vaoID);
//Matrix
// glUniformMatrix4fv(glGetUniformLocation(_shader.getProgramID(), "modelview"), 1, GL_FALSE, glm::value_ptr(modelview));
//Drawing
std::cout << "display" << std::endl;
glDrawArrays(_mode, 0, _nbVertex);
//deselect vao
glBindVertexArray(0);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);/* deselect our vbo */
void Vbo::lock(){
glBindVertexArray(_vaoID);
}
void Vbo::unlock(){
glBindVertexArray(0);
}
int Vbo::getNbVertex(){
return _nbVertex;
}
/***********************************
......
......@@ -46,7 +46,9 @@
#ifndef VBO_HPP
#define VBO_HPP
#include <vector>
#include "common/common.hpp"
using namespace std;
typedef double Data_type;
......@@ -57,9 +59,7 @@ typedef double Data_type;
\def VBO_BUFFER_OFFSET(a)
OpenGL special macro
*/
#define VBO_BUFFER_OFFSET(a) ((char*)NULL + (a))
class Vbo;
#define BUFFER_OFFSET(a) ((char*)NULL + (a))
/*!
* \brief Manage the Vertex Buffer Object.
......@@ -113,6 +113,12 @@ protected:
*/
unsigned int _nb_coordinates_per_vertex;
GLuint _vboID;
GLuint _vaoID;
vector<Element_pos> _vertex;
vector<Element_col> _colors;
GLenum _mode;
int _nbVertex;
private:
/*!
......@@ -144,6 +150,8 @@ public:
*/
Vbo(int nb_object, Type object_type);
Vbo(GLenum mode);
/*!
* \brief The destructor.
*/
......@@ -186,7 +194,7 @@ public:
* <li><b>VBO_UNDEFINED_ERROR</b>: an error occurs but has not been identified.
* </ul>
*/
int add(const unsigned int data_size, const Data_type* const data);
int add(Element_pos x, Element_pos y, Element_col r, Element_col g, Element_col b);
/*!
......@@ -207,9 +215,10 @@ public:
/*!
* \brief Display the VBO list inside the OpenGL area.
*/
void display() const;
void display(glm::mat4 modelview) const;
void lock();
void unlock();
int getNbVertex();
/***********************************
*
......@@ -222,6 +231,8 @@ public:
*/
void display_info();
void config();
void *operator new(size_t s){
std::cout << "Vbo: allocation de "<< s << " octets." << std::endl;
......
#version 330 core
in vec2 in_Vertex;
in vec3 in_Color;
uniform mat4 modelview;
out vec3 color;
void main(){
gl_Position = modelview * vec4(in_Vertex, 1.0, 1.0);