Ejemplo n.º 1
0
bool RenderMethod_PhongPoint_CG::isSupported() const {
	return areShadersAvailable();
}
void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
                                 const Rectangle<int>& anchorPosAndTextureSize,
                                 const int contextWidth, const int contextHeight)
{
    if (contextWidth <= 0 || contextHeight <= 0)
        return;

    JUCE_CHECK_OPENGL_ERROR
    glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable (GL_BLEND);

   #if JUCE_USE_OPENGL_SHADERS
    if (areShadersAvailable())
    {
        struct OverlayShaderProgram  : public ReferenceCountedObject
        {
            OverlayShaderProgram (OpenGLContext& context)
                : program (context), builder (program), params (program)
            {}

            static const OverlayShaderProgram& select (OpenGLContext& context)
            {
                static const char programValueID[] = "juceGLComponentOverlayShader";
                OverlayShaderProgram* program = static_cast <OverlayShaderProgram*> (context.getAssociatedObject (programValueID));

                if (program == nullptr)
                {
                    program = new OverlayShaderProgram (context);
                    context.setAssociatedObject (programValueID, program);
                }

                program->program.use();
                return *program;
            }

            struct ProgramBuilder
            {
                ProgramBuilder (OpenGLShaderProgram& prog)
                {
                    prog.addShader ("attribute " JUCE_HIGHP " vec2 position;"
                                    "uniform " JUCE_HIGHP " vec2 screenSize;"
                                    "varying " JUCE_HIGHP " vec2 pixelPos;"
                                    "void main()"
                                    "{"
                                    "pixelPos = position;"
                                    JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);"
                                    "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
                                    "}",
                                    GL_VERTEX_SHADER);

                    prog.addShader ("uniform sampler2D imageTexture;"
                                    "uniform " JUCE_HIGHP " float textureBounds[4];"
                                    "varying " JUCE_HIGHP " vec2 pixelPos;"
                                    "void main()"
                                    "{"
                                     JUCE_HIGHP " vec2 texturePos = (pixelPos - vec2 (textureBounds[0], textureBounds[1]))"
                                                                      "/ vec2 (textureBounds[2], textureBounds[3]);"
                                     "gl_FragColor = texture2D (imageTexture, vec2 (texturePos.x, 1.0 - texturePos.y));"
                                    "}",
                                    GL_FRAGMENT_SHADER);
                    prog.link();
                }
            };

            struct Params
            {
                Params (OpenGLShaderProgram& prog)
                    : positionAttribute (prog, "position"),
                      screenSize (prog, "screenSize"),
                      imageTexture (prog, "imageTexture"),
                      textureBounds (prog, "textureBounds")
                {}

                void set (const float targetWidth, const float targetHeight, const Rectangle<float>& bounds) const
                {
                    const GLfloat m[] = { bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() };
                    textureBounds.set (m, 4);
                    imageTexture.set (0);
                    screenSize.set (targetWidth, targetHeight);
                }

                OpenGLShaderProgram::Attribute positionAttribute;
                OpenGLShaderProgram::Uniform screenSize, imageTexture, textureBounds;
            };

            OpenGLShaderProgram program;
            ProgramBuilder builder;
            Params params;
        };

        const GLshort left   = (GLshort) targetClipArea.getX();
        const GLshort top    = (GLshort) targetClipArea.getY();
        const GLshort right  = (GLshort) targetClipArea.getRight();
        const GLshort bottom = (GLshort) targetClipArea.getBottom();
        const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };

        const OverlayShaderProgram& program = OverlayShaderProgram::select (*this);
        program.params.set ((float) contextWidth, (float) contextHeight, anchorPosAndTextureSize.toFloat());

        extensions.glVertexAttribPointer (program.params.positionAttribute.attributeID, 2, GL_SHORT, GL_FALSE, 4, vertices);
        extensions.glEnableVertexAttribArray (program.params.positionAttribute.attributeID);
        JUCE_CHECK_OPENGL_ERROR

        glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);

        extensions.glUseProgram (0);
        extensions.glDisableVertexAttribArray (program.params.positionAttribute.attributeID);
    }
    #if JUCE_USE_OPENGL_FIXED_FUNCTION
    else
    #endif
   #endif

   #if JUCE_USE_OPENGL_FIXED_FUNCTION
    {
        glEnable (GL_SCISSOR_TEST);
        glScissor (targetClipArea.getX(), contextHeight - targetClipArea.getBottom(),
                   targetClipArea.getWidth(), targetClipArea.getHeight());

        JUCE_CHECK_OPENGL_ERROR
        glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
        glDisableClientState (GL_COLOR_ARRAY);
        glDisableClientState (GL_NORMAL_ARRAY);
        glEnableClientState (GL_VERTEX_ARRAY);
        glEnableClientState (GL_TEXTURE_COORD_ARRAY);
        OpenGLHelpers::prepareFor2D (contextWidth, contextHeight);
        JUCE_CHECK_OPENGL_ERROR

        const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
        glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);

        const GLshort left   = (GLshort) anchorPosAndTextureSize.getX();
        const GLshort right  = (GLshort) anchorPosAndTextureSize.getRight();
        const GLshort top    = (GLshort) (contextHeight - anchorPosAndTextureSize.getY());
        const GLshort bottom = (GLshort) (contextHeight - anchorPosAndTextureSize.getBottom());
        const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
        glVertexPointer (2, GL_SHORT, 0, vertices);

        glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
        glDisable (GL_SCISSOR_TEST);
    }
   #endif

    JUCE_CHECK_OPENGL_ERROR
}
Ejemplo n.º 3
0
bool RenderMethod_TerrainPhong_CG::isSupported() const {
	return areShadersAvailable();
}
Ejemplo n.º 4
0
bool RenderMethod_Grass_CG::isSupported() const {
	return areShadersAvailable();
}