Shader::Shader(QString vshaderName, QString fshaderName, QObject *parent) :QOpenGLShaderProgram(parent) { QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, parent); { QFile file(":/res/Shaders/"+vshaderName+".vert"); if (file.open(QIODevice::ReadOnly)) { QString vsrc = file.readAll(); if (!vshader->compileSourceCode(vsrc)){ printf("%s", vshader->log().data()); exit(1); } } } QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, parent); { QFile file(":/res/Shaders/"+fshaderName+".frag"); if (file.open(QIODevice::ReadOnly)) { QString fsrc = file.readAll(); if (!fshader->compileSourceCode(fsrc)){ printf("%s", fshader->log().data()); exit(1); } } } this->addShader(vshader); this->addShader(fshader); this->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE); this->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE); this->bindAttributeLocation("lightPosition", PROGRAM_LIGHT_POSITION_ATTRIBUTE); this->bindAttributeLocation("lightColour", PROGRAM_LIGHT_COLOUR_ATTRIBUTE); this->bindAttributeLocation("useTexture", PROGRAM_USE_TEXTURE); if (!this->link()) { printf("%s", this->log().data()); exit(1); } this->bind(); this->setUniformValue("texture", 0); }
void GLWidget::initializeGL() { initializeOpenGLFunctions(); glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); makeCurrent(); qglClearColor(QColor::fromCmykF(0.79, 0.79, 0.79, 0.0).dark()); glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); glEnable(GL_DEPTH_TEST); qDebug() << "Loading quad (vertex shader)"; QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); vshader->compileSourceFile(":/resources/plane.vert"); if (!vshader->log().isEmpty()) qDebug() << vshader->log(); else qDebug() << "done"; qDebug() << "Loading quad (fragment shader)"; QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); fshader->compileSourceFile(":/resources/plane.frag"); if (!fshader->log().isEmpty()) qDebug() << fshader->log(); else qDebug() << "done"; qDebug() << "Loading quad (tessellation control shader)"; QOpenGLShader *tcshader = new QOpenGLShader(QOpenGLShader::TessellationControl, this); tcshader->compileSourceFile(":/resources/plane.tcs.vert"); if (!tcshader->log().isEmpty()) qDebug() << tcshader->log(); else qDebug() << "done"; qDebug() << "Loading quad (tessellation evaluation shader)"; QOpenGLShader *teshader = new QOpenGLShader(QOpenGLShader::TessellationEvaluation, this); teshader->compileSourceFile(":/resources/plane.tes.vert"); if (!teshader->log().isEmpty()) qDebug() << teshader->log(); else qDebug() << "done"; qDebug() << "Loading quad (geometry shader)"; QOpenGLShader *gshader = new QOpenGLShader(QOpenGLShader::Geometry, this); QFile gFile(":/resources/plane.geom"); gFile.open(QFile::ReadOnly); qDebug() << gFile.isOpen() << " File"; QTextStream in(&gFile); QString preambule = "#version 400 core\n" "layout(triangle_strip, max_vertices = 3) out;\n" ; QString shaderCode = in.readAll(); gshader->compileSourceCode(preambule+shaderCode); //gshader->compileSourceFile(in.readAll()); if (!gshader->log().isEmpty()) qDebug() << gshader->log(); else qDebug() << "done"; program = new QOpenGLShaderProgram(this); program->addShader(vshader); program->addShader(fshader); program->addShader(tcshader); program->addShader(teshader); program->addShader(gshader); program->bindAttributeLocation("FragColor",0); program->bindAttributeLocation("FragNormal",1); program->bindAttributeLocation("FragGlowColor",2); program->bindAttributeLocation("FragPosition",3); GLCHK(program->link()); GLCHK(program->bind()); program->setUniformValue("texDiffuse" , 0); program->setUniformValue("texNormal" , 1); program->setUniformValue("texSpecular" , 2); program->setUniformValue("texHeight" , 3); program->setUniformValue("texSSAO" , 4); program->setUniformValue("texRoughness", 5); program->setUniformValue("texMetallic", 6); program->setUniformValue("texMaterial", 7); program->setUniformValue("texDiffuseEnvMap", 8); program->setUniformValue("texEnvMap" , 9); // lines shader qDebug() << "Compiling lines program..."; preambule = QString("#version 400 core\n")+ "layout(line_strip, max_vertices = 3) out;\n" ; gshader->compileSourceCode(preambule+shaderCode); line_program = new QOpenGLShaderProgram(this); line_program->addShader(vshader); line_program->addShader(fshader); line_program->addShader(tcshader); line_program->addShader(teshader); line_program->addShader(gshader); line_program->bindAttributeLocation("FragColor",0); line_program->bindAttributeLocation("FragNormal",1); line_program->bindAttributeLocation("FragGlowColor",2); line_program->bindAttributeLocation("FragPosition",3); GLCHK(line_program->link()); GLCHK(line_program->bind()); line_program->setUniformValue("texDiffuse" , 0); line_program->setUniformValue("texNormal" , 1); line_program->setUniformValue("texSpecular" , 2); line_program->setUniformValue("texHeight" , 3); line_program->setUniformValue("texSSAO" , 4); line_program->setUniformValue("texRoughness", 5); line_program->setUniformValue("texMetallic", 6); line_program->setUniformValue("texMaterial", 7); line_program->setUniformValue("texDiffuseEnvMap", 8); line_program->setUniformValue("texEnvMap" , 9); delete vshader; delete fshader; delete tcshader; delete teshader; delete gshader; // loading sky box shader qDebug() << "Loading skybox shader (vertex shader)"; vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); vshader->compileSourceFile(":/resources/skybox.vert.glsl"); if (!vshader->log().isEmpty()) qDebug() << vshader->log(); else qDebug() << "done"; qDebug() << "Loading skybox shader (fragment shader)"; fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); fshader->compileSourceFile(":/resources/skybox.frag.glsl"); if (!fshader->log().isEmpty()) qDebug() << fshader->log(); else qDebug() << "done"; skybox_program = new QOpenGLShaderProgram(this); skybox_program->addShader(vshader); skybox_program->addShader(fshader); skybox_program->bindAttributeLocation("FragColor",0); skybox_program->bindAttributeLocation("FragNormal",1); skybox_program->bindAttributeLocation("FragGlowColor",2); skybox_program->bindAttributeLocation("FragPosition",3); GLCHK(skybox_program->link()); GLCHK(skybox_program->bind()); skybox_program->setUniformValue("texEnv" , 0); // loading enviromental shader qDebug() << "Loading enviromental shader (vertex shader)"; vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); vshader->compileSourceFile(":/resources/env.vert"); if (!vshader->log().isEmpty()) qDebug() << vshader->log(); else qDebug() << "done"; qDebug() << "Loading enviromental shader (geometry shader)"; gshader = new QOpenGLShader(QOpenGLShader::Geometry, this); gshader->compileSourceFile(":/resources/env.geom"); if (!gshader->log().isEmpty()) qDebug() << gshader->log(); else qDebug() << "done"; qDebug() << "Loading enviromental shader (fragment shader)"; fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); fshader->compileSourceFile(":/resources/env.frag"); if (!fshader->log().isEmpty()) qDebug() << fshader->log(); else qDebug() << "done"; env_program = new QOpenGLShaderProgram(this); env_program->addShader(vshader); env_program->addShader(gshader); env_program->addShader(fshader); GLCHK(env_program->link()); GLCHK(env_program->bind()); env_program->setUniformValue("texEnv" , 0); delete vshader; delete fshader; delete gshader; // ------------------------------------------------- // Loading post processing filters // ------------------------------------------------- qDebug() << "Loading post-processing shader (vertex shader)"; vshader = new QOpenGLShader(QOpenGLShader::Vertex, this); vshader->compileSourceFile(":/resources/filters_3d.vert"); if (!vshader->log().isEmpty()) qDebug() << vshader->log(); else qDebug() << "done"; qDebug() << "Loading post-processing shader (fragment shader)"; fshader = new QOpenGLShader(QOpenGLShader::Fragment, this); fshader->compileSourceFile(":/resources/filters_3d.frag"); if (!fshader->log().isEmpty()) qDebug() << fshader->log(); else qDebug() << "done"; filters_program = new QOpenGLShaderProgram(this); filters_program->addShader(vshader); filters_program->addShader(fshader); filters_program->bindAttributeLocation("positionIn", 0); GLCHK( filters_program->link() ); GLCHK( filters_program->bind() ); GLCHK( filters_program->setUniformValue("layerA" , 0) ); GLCHK( filters_program->setUniformValue("layerB" , 1) ); GLCHK( filters_program->setUniformValue("layerC" , 2) ); GLCHK( filters_program->setUniformValue("layerD" , 3) ); GLCHK( filters_program->setUniformValue("layerE" , 4) ); GLCHK( filters_program->setUniformValue("layerF" , 5) ); GLCHK( filters_program->setUniformValue("layerG" , 6) ); GLCHK( subroutines["mode_normal_filter"] = glGetSubroutineIndex(filters_program->programId(),GL_FRAGMENT_SHADER,"mode_normal_filter") ); GLCHK( subroutines["mode_gauss_filter"] = glGetSubroutineIndex(filters_program->programId(),GL_FRAGMENT_SHADER,"mode_gauss_filter") ); GLCHK( subroutines["mode_bloom_filter"] = glGetSubroutineIndex(filters_program->programId(),GL_FRAGMENT_SHADER,"mode_bloom_filter") ); GLCHK( subroutines["mode_dof_filter"] = glGetSubroutineIndex(filters_program->programId(),GL_FRAGMENT_SHADER,"mode_dof_filter") ); GLCHK( filters_program->release()); delete vshader; delete fshader; camera.position.setZ( -0 ); camera.toggleFreeCamera(false); newCamera.toggleFreeCamera(false); lightDirection.position.setZ(0); lightDirection.toggleFreeCamera(false); lightDirection.radius = 1; mesh = new Mesh("Core/3D/","Cube.obj"); skybox_mesh = new Mesh("Core/3D/","sky_cube.obj"); env_mesh = new Mesh("Core/3D/","sky_cube_env.obj"); quad_mesh = new Mesh("Core/3D/","quad.obj"); m_prefiltered_env_map = new GLTextureCube(512); resizeFBOs(); emit readyGL(); }