2018-09-12 18:53:34 +02:00
# include "mainwindow.h"
# include "ui_mainwindow.h"
# include <QMessageBox>
# include <QDir>
# include <iostream>
# include <sstream>
# include <iomanip>
MainWindow : : MainWindow ( QWidget * parent ) : QMainWindow ( parent ) , ui ( new Ui : : MainWindow ) {
QSurfaceFormat format ;
format . setVersion ( 4 , 1 ) ;
format . setProfile ( QSurfaceFormat : : CoreProfile ) ;
format . setDepthBufferSize ( 24 ) ;
QSurfaceFormat : : setDefaultFormat ( format ) ;
ui - > setupUi ( this ) ;
// Add shaders
addShaders ( ) ;
ui - > openglWidget - > setFocus ( ) ;
}
MainWindow : : ~ MainWindow ( ) {
delete ui ;
}
void MainWindow : : on_action_Version_OpenGL_triggered ( ) {
std : : stringstream message ;
message < < " Renderer : " < < glGetString ( GL_RENDERER ) < < std : : endl ;
message < < " Vendor : " < < glGetString ( GL_VENDOR ) < < std : : endl ;
message < < " Version : " < < glGetString ( GL_VERSION ) < < std : : endl ;
message < < " GLSL Version : " < < glGetString ( GL_SHADING_LANGUAGE_VERSION ) < < std : : endl ;
QMessageBox : : information ( this , " OpenGL Information " , message . str ( ) . c_str ( ) ) ;
}
void MainWindow : : on_actionHello_clear_triggered ( ) {
ui - > openglWidget - > activatedemo ( 0 ) ;
}
void MainWindow : : on_actionHello_triangle_triggered ( ) {
ui - > openglWidget - > activatedemo ( 1 ) ;
}
void MainWindow : : on_actionHello_camera_triggered ( ) {
ui - > openglWidget - > activatedemo ( 2 ) ;
}
void MainWindow : : on_actionHello_spheres_triggered ( ) {
ui - > openglWidget - > activatedemo ( 3 ) ;
}
2018-10-08 10:12:21 +02:00
void MainWindow : : on_actionHello_spline_triggered ( ) {
ui - > openglWidget - > activatedemo ( 4 ) ;
}
2018-09-12 18:53:34 +02:00
void MainWindow : : on_actionToggle_Back_Face_Culling_triggered ( bool checked )
{
culling = checked ;
}
void MainWindow : : addShaders ( ) {
// Compile shaders Maps
QDir shadersDir = QDir ( " ../src/shader/ " ) ;
std : : cout < < " Loading shaders from: " < < shadersDir . absolutePath ( ) . toStdString ( ) < < std : : endl ;
QStringList shadersPath = shadersDir . entryList ( QDir : : Files ) ;
for ( auto it = shadersPath . cbegin ( ) ; it ! = shadersPath . cend ( ) ; + + it ) {
QString name ( * it ) ;
name . remove ( " .glsl " ) ;
QStringList shader = name . split ( " _ " ) ;
//std::cout << shadersDir.absoluteFilePath(*it).toStdString() << std::endl;
QFile shaderFile ( shadersDir . absoluteFilePath ( * it ) ) ;
shaderFile . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ;
if ( shader . at ( 0 ) = = " vs " )
vertexShaders . insert ( shader . at ( 1 ) , shaderFile . readAll ( ) . data ( ) ) ;
else
fragmentShaders . insert ( shader . at ( 1 ) , shaderFile . readAll ( ) . data ( ) ) ;
shaderFile . close ( ) ;
std : : cout < < " Added shader: " < < shader . at ( 1 ) . toStdString ( ) < < std : : endl ;
}
if ( vertexShaders . empty ( ) | | fragmentShaders . empty ( ) )
std : : cerr < < " Not enough shaders found. Please provide vertex AND fragment shaders. " < < std : : endl ;
// Add Shaders Maps to menu
ui - > menuShaders - > addSection ( " Vertex " ) ;
auto it = vertexShaders . constBegin ( ) ;
while ( it ! = vertexShaders . constEnd ( ) ) {
ui - > menuShaders - > addAction ( it + + . key ( ) , [ = ] ( ) { this - > vertexShader = it . key ( ) ; std : : cout < < " Changed vertex shader to " < < it . key ( ) . toStdString ( ) < < std : : endl ; } ) ;
}
ui - > menuShaders - > addSeparator ( ) ;
ui - > menuShaders - > addSection ( " Fragment " ) ;
it = fragmentShaders . constBegin ( ) ;
while ( it ! = fragmentShaders . constEnd ( ) ) {
ui - > menuShaders - > addAction ( it + + . key ( ) , [ = ] ( ) { this - > fragmentShader = it . key ( ) ; std : : cout < < " Changed fragment shader to " < < it . key ( ) . toStdString ( ) < < std : : endl ; } ) ;
}
// Set default shaders
2018-10-09 23:29:30 +02:00
vertexShader = vertexShaders . firstKey ( ) ;
fragmentShader = fragmentShaders . lastKey ( ) ;
2018-09-12 18:53:34 +02:00
}