void VideoViewer::makeShaders() { _shaderProgram = new QGLShaderProgram(this); QGLShader *vertexShader = new QGLShader(QGLShader::Vertex, this); QGLShader *fragmentShader = new QGLShader(QGLShader::Fragment, this); QString vertexShaderSource; QString fragmentShaderSource; vertexShaderSource.append("#version 110\n"); vertexShaderSource.append("attribute vec2 position;\n"); vertexShaderSource.append("uniform mat2 matrix;\n"); vertexShaderSource.append("varying vec2 texcoord;\n"); vertexShaderSource.append("void main()\n"); vertexShaderSource.append("{\n"); vertexShaderSource.append( "gl_Position = vec4(matrix * position, 0.0, 1.0);\n"); vertexShaderSource.append("texcoord = vec2(position.x, -position.y) " "* vec2(0.5) + vec2(0.5);\n"); vertexShaderSource.append("}\n"); fragmentShaderSource.append("#version 110\n"); fragmentShaderSource.append("uniform sampler2D texture;\n"); fragmentShaderSource.append("varying vec2 texcoord;\n"); fragmentShaderSource.append("void main()\n"); fragmentShaderSource.append("{\n"); fragmentShaderSource.append( "gl_FragColor = texture2D(texture, texcoord);\n"); fragmentShaderSource.append("}\n"); vertexShader->compileSourceCode(vertexShaderSource); fragmentShader->compileSourceCode(fragmentShaderSource); if (!vertexShader->isCompiled()) { qDebug() << "Could not compile vertex shader:\n" << vertexShader->log(); } if (!fragmentShader->isCompiled()) { qDebug() << "Could not compile fragment shader:\n" << fragmentShader->log(); } _shaderProgram->addShader(fragmentShader); _shaderProgram->addShader(vertexShader); _shaderProgram->link(); _shaderProgram->bind(); }
void Widget::initializeGL() { qDebug() << "isValid:" << this->context()->isValid(); qDebug() << "isSharing:" << this->context()->isSharing(); qDebug() << "hasOpenGL:" << this->context()->format().hasOpenGL(); qDebug() << "Format:" << this->context()->format(); glEnable(GL_DEPTH_TEST); QString vertexShaderSource( "uniform mat4 modelViewMatrix;\n" "uniform mat4 projectionMatrix;\n" "attribute vec4 vertexPosition;\n" "\n" "void main(void)\n" "{\n" " gl_Position = projectionMatrix * modelViewMatrix * vertexPosition;\n" "}\n"); QString fragmentShaderSource( "precision mediump float;\n" "\n" "void main(void)\n" "{\n" "gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n" "}\n"); QGLShader *vertexShader = new QGLShader(QGLShader::Vertex, this); QGLShader *fragmentShader = new QGLShader(QGLShader::Fragment, this); qDebug() << "After constuctor Shaders."; qDebug() << vertexShader->log(); vertexShader->compileSourceCode(vertexShaderSource); fragmentShader->compileSourceCode(fragmentShaderSource); _shaderProgram = new QGLShaderProgram(this); _shaderProgram->addShader(vertexShader); _shaderProgram->addShader(fragmentShader); _shaderProgram->link(); _shaderProgram->bind(); qglClearColor(Qt::gray); _timer->start(10); qDebug() << vertexShader->log(); }
void GLWidget::loadShaders(QGLShaderProgram* program, QString vShaderFileName, QString fShaderFileName) { QGLShader* vertexShader = NULL; QGLShader* fragmentShader = NULL; QFileInfo vsh(vShaderFileName); if(vsh.exists()) { vertexShader = new QGLShader(QGLShader::Vertex); if(vertexShader->compileSourceFile(vShaderFileName)) { program->addShader(vertexShader); qDebug() << "Vertex shader compiled successfully."; } else { qWarning() << "Vertex Shader Error" << vertexShader->log(); } } else { qWarning() << "Vertex Shader source file " << fShaderFileName << " not found."; } QFileInfo fsh(fShaderFileName); if(fsh.exists()) { fragmentShader = new QGLShader(QGLShader::Fragment); if(fragmentShader->compileSourceFile(fShaderFileName)) { program->addShader(fragmentShader); qDebug() << "Fragment shader compiled successfully."; } else { qWarning() << "Fragment Shader Error" << fragmentShader->log(); } } else { qWarning() << "Fragment Shader source file " << fShaderFileName << " not found."; } if(!program->link()){ qWarning() << "Shader Program Linker Error" << program->log(); } }
QGLShader* StelQGLGLSLShader::createVertexShader(const QString& source) { Q_ASSERT_X(state == State_Unlocked || state == State_Modified, Q_FUNC_INFO, "Can't add a vertex shader to a built shader program"); state = State_Modified; QGLShader* shader = new QGLShader(QGLShader::Vertex, renderer->getGLContext()); aggregatedLog += shader->log(); if(shader->compileSourceCode(source)) { return shader; } else { delete shader; return NULL; } }
void RenderViewWidget::initShaders(){ Assert(QGLContext::currentContext() != 0); //First, check if we can even use OpenGL shader programs const bool canUseShaders = QGLShaderProgram::hasOpenGLShaderPrograms(); Assert(canUseShaders); //Fragment shader(s) QGLShader* shader = new QGLShader(QGLShader::Fragment, this); const bool ok1 = shader->compileSourceCode( "//Simple Vertex Shader" \ "\n"\ "uniform sampler2D sampler;\n"\ "\n"\ "void main(void)\n" \ "{\n" \ "// gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" \ "\n"\ " vec4 c = texture2D(sampler, gl_TexCoord[0].st);\n" \ " gl_FragColor = vec4(c.x, c.y, c.z, 1.0);\n"\ "}\n" \ ); std::cout << shader->log().toStdString() << std::endl; Assert(ok1); hdrFragShaders.push_back(shader); //Vertex shader hdrVertShader = new QGLShader(QGLShader::Vertex, this); const bool ok2 = hdrVertShader->compileSourceCode( "//Simple fragment shader\n"\ "\n"\ "\n"\ "void main(void){\n"\ " gl_TexCoord[0] = gl_MultiTexCoord0;\n"\ " gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;\n"\ "}\n"\ "\n"\ ); std::cout << hdrVertShader->log().toStdString() << std::endl; Assert(ok2); hdrFragShaders.push_back(hdrVertShader); }
bool StelQGLGLSLShader::addFragmentShader(const QString& source) { Q_ASSERT_X(state == State_Unlocked || state == State_Modified, Q_FUNC_INFO, "Can't add a fragment shader to a built shader program"); state = State_Modified; QGLShader* shader = new QGLShader(QGLShader::Fragment, renderer->getGLContext()); const bool success = shader->compileSourceCode(source); aggregatedLog += shader->log(); if(success) { defaultFragmentShaders.append(shader); } else { delete shader; } return success; }
QGLShader *SceneParser::loadShader(KeyValues *data) { const char *type = data->getString("type"); if(!type) { fprintf(stderr, "Key 'type' not found on Shader\n"); return NULL; } const char *file = data->getString("file"); if(!file) { fprintf(stderr, "Key 'file' not found on Shader\n"); return NULL; } char *filename = resolvePath(file); QGLShader::ShaderType shaderType; if(strcmp(type, "vertex") == 0) { shaderType = QGLShader::Vertex; } else if(strcmp(type, "fragment") == 0) { shaderType = QGLShader::Fragment; } else { fprintf(stderr, "Invalid shader type: %s\n", type); delete[] filename; return NULL; } QGLShader *shader = new QGLShader(shaderType); if(!shader->compileSourceFile(filename)) { fprintf(stderr, "Failed to load shader %s\n", filename); fprintf(stdout, shader->log().toStdString().c_str()); delete shader; delete[] filename; return NULL; } delete[] filename; return shader; }
const bool CCDeviceRenderer::loadShader(CCShader *shader) { // Create shader program shader->program = new QGLShaderProgram(); // Create and compile vertex shader CCText shaderFilename( "Resources/Shaders/" ); shaderFilename += shader->name; shaderFilename += ".fx"; const char *qtDefines = "#define QT\r\n"; QGLShader *vertexShader = new QGLShader( QGLShader::Vertex ); QGLShader *fragmentShader = new QGLShader( QGLShader::Fragment ); { char *fileData = NULL; CCFileManager::getPackagedFile( shaderFilename.buffer, &fileData ); { QString shaderData = QString( qtDefines ); shaderData.append( "#define VERTEX_SHADER\r\n" ); shaderData.append( fileData ); if( vertexShader->compileSourceCode( shaderData ) == false ) { QString error( "CCDeviceRenderer::Failed to compile vertex shader.\n" ); error.append( vertexShader->log() ); ASSERT_MESSAGE( false, error.toUtf8().constData() ); delete vertexShader; vertexShader = NULL; } } { QString shaderData = QString( qtDefines ); shaderData.append( "#define PIXEL_SHADER\r\n" ); shaderData.append( fileData ); if( fragmentShader->compileSourceCode( shaderData ) == false ) { QString error( "CCDeviceRenderer::Failed to compile pixel shader.\n" ); error.append( fragmentShader->log() ); ASSERT_MESSAGE( false, error.toUtf8().constData() ); delete fragmentShader; fragmentShader = NULL; } } FREE_POINTER( fileData ); } // Attach vertex shader to program shader->program->addShader( vertexShader ); // Attach fragment shader to program shader->program->addShader( fragmentShader ); // Bind attribute locations // this needs to be done prior to linking shader->program->bindAttributeLocation( "vs_position", ATTRIB_VERTEX ); shader->program->bindAttributeLocation( "vs_color", ATTRIB_COLOR ); shader->program->bindAttributeLocation( "vs_texCoord", ATTRIB_TEXCOORD ); // Link program if( !shader->program->link() ) { // Should log: "Failed to link program"; return false; } return true; }