Exemplo n.º 1
0
bool Shader::compileSourceFile (CGcontext context,
                                const QString& file, const QString& entryPoint)
{
    QFile dev (file);
    if (!dev.open(QIODevice::ReadOnly)) {
        qWarning("failed to open file: %s", qPrintable(dev.errorString()));
        return false;
    }
    return compileSourceCode(context, dev.readAll(), entryPoint);
}
Exemplo n.º 2
0
bool Shader::compileSourceFile(const std::string& sourceFile)
{
    try {
        std::string sourceCode = g_resources.readFileContents(sourceFile);
        return compileSourceCode(sourceCode);
    } catch(stdext::exception& e) {
        g_logger.error(stdext::format("unable to load shader source form file '%s': %s", sourceFile, e.what()));
    }
    return false;
}
BasicFragmentShader::BasicFragmentShader(QObject *parent) :
    QGLShader(QGLShader::Fragment, parent)
{
    compileSourceCode(
                "uniform vec4 color;\n"
                "uniform bool hasTexture;\n"
                "uniform sampler2D texSampler;\n"
                "uniform float diffuseCoef;\n"
                "uniform float specularCoef;\n"
                "varying vec3 vNormal;\n"
                "varying vec3 vLight1Dir;\n"
                "varying vec3 vLight2Dir;\n"
                "varying vec3 vEyeDir;\n"
                "varying vec2 texCoords;\n"
                "void main() {\n"
                "vec4 newColor = color;\n"
                "if (hasTexture) {\n"
                "newColor = texture2D(texSampler, texCoords);\n"
                "}\n"
                "vec4 light1Color = vec4(0.8, 1.0, 1.0, 1.0);\n"
                "vec4 light2Color = vec4(1.0, 1.0, 0.8, 1.0);\n"
                "vec3 n = normalize(vNormal);\n"
                "vec3 l1 = normalize(vLight1Dir);\n"
                "vec3 l2 = normalize(vLight2Dir);\n"
                "vec3 e = normalize(vEyeDir);\n"
                "vec3 r1 = reflect(-l1, n);\n"
                "vec3 r2 = reflect(-l2, n);\n"
                "float cosAlpha1 = clamp(dot(e, r1), 0.0, 1.0);\n"
                "float light1Power = 0.5;\n"
                "if (dot(n, e) < 0) {\n"
                "n = n * -1;\n"
                "}\n"
                "float cosTheta1 = clamp(dot(n, l1), 0.0, 1.0);\n"
                "float cosAlpha2 = clamp(dot(e, r2), 0.0, 1.0);\n"
                "float light2Power = 0.5;\n"
                "float cosTheta2 = clamp(dot(n, l2), 0.0, 1.0);\n"
                "vec4 ambientColor = vec4(0.2, 0.2, 0.2, 1.0) * newColor;\n"
                "vec4 diffuseColor = newColor * light1Color * light1Power * cosTheta1 + newColor * light2Color * light2Power * cosTheta2;\n"
                "vec4 specularColor = light1Color * light1Power * pow(cosAlpha1, 200.0) + light2Color * light2Power * pow(cosAlpha2, 200.0);\n"
                "gl_FragColor = ambientColor + diffuseColor * diffuseCoef + specularColor * specularCoef;\n"
                "}"
                );
}
Exemplo n.º 4
0
bool Shader::compileSourceFile(const std::string& sourceFile)
{
    std::string sourceCode = g_resources.loadFile(sourceFile);
    return compileSourceCode(sourceCode);
}