Exemplo n.º 1
0
    void createShaders()
    {
        vertexShader =
            "attribute vec4 position;\n"
            "attribute vec4 sourceColour;\n"
            "attribute vec2 texureCoordIn;\n"
            "\n"
            "uniform mat4 projectionMatrix;\n"
            "uniform mat4 viewMatrix;\n"
            "\n"
            "varying vec4 destinationColour;\n"
            "varying vec2 textureCoordOut;\n"
            "\n"
            "void main()\n"
            "{\n"
            "    destinationColour = sourceColour;\n"
            "    textureCoordOut = texureCoordIn;\n"
            "    gl_Position = projectionMatrix * viewMatrix * position;\n"
            "}\n";

        fragmentShader =
           #if JUCE_OPENGL_ES
            "varying lowp vec4 destinationColour;\n"
            "varying lowp vec2 textureCoordOut;\n"
           #else
            "varying vec4 destinationColour;\n"
            "varying vec2 textureCoordOut;\n"
           #endif
            "\n"
            "void main()\n"
            "{\n"
            "    vec4 colour = vec4(0.95, 0.57, 0.03, 0.7);\n"
            "    gl_FragColor = colour;\n"
            "}\n";

        ScopedPointer<OpenGLShaderProgram> newShader (new OpenGLShaderProgram (openGLContext));
        String statusText;

        if (newShader->addVertexShader (OpenGLHelpers::translateVertexShaderToV3 (vertexShader))
              && newShader->addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 (fragmentShader))
              && newShader->link())
        {
            shape = nullptr;
            attributes = nullptr;
            uniforms = nullptr;

            shader = newShader;
            shader->use();

            shape      = new Shape (openGLContext);
            attributes = new Attributes (openGLContext, *shader);
            uniforms   = new Uniforms (openGLContext, *shader);

            statusText = "GLSL: v" + String (OpenGLShaderProgram::getLanguageVersion(), 2);
        }
        else
        {
            statusText = newShader->getLastError();
        }
    }
Exemplo n.º 2
0
    void createShaders()
    {
        vertexShader =
            "attribute vec4 position;\n"
            "attribute vec4 sourceColour;\n"
            "attribute vec2 textureCoordIn;\n"
            "\n"
            "uniform mat4 projectionMatrix;\n"
            "uniform mat4 viewMatrix;\n"
            "\n"
            "varying vec4 destinationColour;\n"
            "varying vec2 textureCoordOut;\n"
            "\n"
            "void main()\n"
            "{\n"
            "    destinationColour = sourceColour;\n"
            "    textureCoordOut = textureCoordIn;\n"
            "    gl_Position = projectionMatrix * viewMatrix * position;\n"
            "}\n";

        fragmentShader =
#if JUCE_OPENGL_ES
            "varying lowp vec4 destinationColour;\n"
            "varying lowp vec2 textureCoordOut;\n"
#else
            "varying vec4 destinationColour;\n"
            "varying vec2 textureCoordOut;\n"
#endif
#if DEFAULT_TEXTURE == 2
            "\n"
            "uniform sampler2D demoTexture;\n"
            "\n"
#endif
            "void main()\n"
            "{\n"
#if JUCE_OPENGL_ES
            "    precision lowp float;\n"
#endif
        
#if !DEFAULT_TEXTURE
            "    gl_FragColor = destinationColour;\n"
#elif DEFAULT_TEXTURE == 1
            "    gl_FragColor = vec4(0.95, 0.57, 0.03, 0.7);\n"
#elif DEFAULT_TEXTURE == 2
            "    gl_FragColor = texture2D (demoTexture, textureCoordOut);\n"
#endif
            "}\n";

        // An OpenGLShaderProgram is a combination of shaders which are compiled and linked together
        ScopedPointer<OpenGLShaderProgram> newShaderProgram (new OpenGLShaderProgram (openGLContext));

        String statusText;  // Can be used to give a success or failure message after GLSL compilation

        // Create the program that combines Vertex and Fragment shaders
        if (newShaderProgram->addVertexShader (OpenGLHelpers::translateVertexShaderToV3 (vertexShader))
              && newShaderProgram->addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 (fragmentShader))
              && newShaderProgram->link())
        {
            // Do the following to be in a known state in case problems occur
            shape = nullptr;
            attributes = nullptr;
            uniforms = nullptr;

            // Tell the GPU that this program is the one to be used for next frame
            usedShaderProgram = newShaderProgram;
// ** Not needed **: will be called when render callback is run            shader->use();

            // Load the object that will be used to defined vertices and fragments
            shape      = new Shape (openGLContext);                             // The object itself
            attributes = new Attributes (openGLContext, *usedShaderProgram);    // The object attributes
            uniforms   = new Uniforms (openGLContext, *usedShaderProgram);      // The global data to be shared between
                                                                                // the CPU code and the GPU shaders

            statusText = "GLSL: v" + String (OpenGLShaderProgram::getLanguageVersion(), 2); // Not used in this demo
        }
        else
        {
            statusText = newShaderProgram->getLastError();                 // Not used in this demo
        }
    }