ShaderProgram* SpriteCache::createDefaultShader () { if (!Gdx::graphics->isGL20Available()) return NULL; std::string vertexShader = "attribute vec4 " + ShaderProgram::POSITION_ATTRIBUTE + ";\n" // "attribute vec4 " + ShaderProgram::COLOR_ATTRIBUTE + ";\n" // "attribute vec2 " + ShaderProgram::TEXCOORD_ATTRIBUTE + "0;\n" // "uniform mat4 u_projectionViewMatrix;\n" // "varying vec4 v_color;\n" // "varying vec2 v_texCoords;\n" // "\n" // "void main()\n" // "{\n" // " v_color = " + ShaderProgram::COLOR_ATTRIBUTE + ";\n" // " v_texCoords = " + ShaderProgram::TEXCOORD_ATTRIBUTE + "0;\n" // " gl_Position = u_projectionViewMatrix * " + ShaderProgram::POSITION_ATTRIBUTE + ";\n" // "}\n"; std::string fragmentShader = "#ifdef GL_ES\n" // "precision mediump float;\n" // "#endif\n" // "varying vec4 v_color;\n" // "varying vec2 v_texCoords;\n" // "uniform sampler2D u_texture;\n" // "void main()\n"// "{\n" // " gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" // "}"; ShaderProgram* shader = new ShaderProgram(vertexShader, fragmentShader); if (shader->isCompiled() == false) throw std::runtime_error("Error compiling shader: " + shader->getLog()); return shader; }
void GLShaderDev::buildCurrentProject() { ShaderProgram* prog = new ShaderProgram; bool success = true; int i = 1; OutputParser parser(_glInfo.getVendor()); const ShaderProject::Stages& stages = _projectManager.getCurrentProject()->getStages(); _editor->saveAll(); _output->getModel()->clear(); _buildOutputDock->setVisible(true); for (ShaderProject::Stages::const_iterator it = stages.begin(); it != stages.end(); ++it) { QFile file(it->second); QFileInfo fileInfo(file); ShaderObject* obj = new ShaderObject; QString str = QString(tr("[%1/%2] Compiling %3...")).arg(i).arg(stages.size()).arg(fileInfo.fileName()); _output->getModel()->addItem(OutputItem(str, OutputItem::InformationItem)); if (!file.open(QIODevice::ReadOnly)) throw (GlsdException(std::string("Could not open shader file") + it->second.toStdString())); // TODO proper exception handling if (!obj->compile(QString(file.readAll()).toStdString(), static_cast<ShaderObject::ShaderType>(it->first))) { _output->getModel()->addItems(parser.parse(obj->getErrorLog(), fileInfo.absoluteFilePath().toStdString())); success = false; } prog->attach(*obj); ++i; } _output->getModel()->addItem(OutputItem(tr("Linking shader..."), OutputItem::InformationItem)); if (!prog->link()) { _output->getModel()->addItem(OutputItem(prog->getLog().c_str(), OutputItem::ErrorItem)); success = false; } if (!success) { _output->getModel()->addItem(OutputItem(tr("*** Compilation failed ***"), OutputItem::StandardItem)); return; } _output->getModel()->addItem(OutputItem(tr("*** Compilation successful ***"), OutputItem::StandardItem)); // FIXME Set shader properly, with attributes correctly bound _glwidget->setShader(prog); prog->printDebug(); }