aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/wallpapers/photophase/shapes/ColorShape.java
blob: ceb7ec4a31188fde9b8bfc5f1388d1d275326550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cyanogenmod.wallpapers.photophase.shapes;

import android.content.Context;
import android.opengl.GLES20;

import org.cyanogenmod.wallpapers.photophase.utils.GLESUtil;
import org.cyanogenmod.wallpapers.photophase.utils.GLESUtil.GLColor;
import org.cyanogenmod.wallpapers.photophase.R;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

/**
 * A shape plus color.
 */
public class ColorShape implements DrawableShape {

    private int mProgramHandler;
    private int mPositionHandler;
    private int mColorHandler;
    private int mMatrixHandler;
    private FloatBuffer mVertexBuffer;

    private final GLColor mColor;

    /**
     * Constructor of <code>ColorShape</code>.
     *
     * @param ctx The current context
     * @param vertex The vertext data
     * @param color The color
     */
    public ColorShape(Context ctx, float[] vertex, GLColor color) {
        super();
        mColor = color;

        mProgramHandler = GLESUtil.createProgram(
                                                ctx.getResources(),
                                                R.raw.color_vertex_shader,
                                                R.raw.color_fragment_shader);
        mPositionHandler = GLES20.glGetAttribLocation(mProgramHandler, "aPosition");
        GLESUtil.glesCheckError("glGetAttribLocation");
        mColorHandler = GLES20.glGetAttribLocation(mProgramHandler, "aColor");
        GLESUtil.glesCheckError("glGetAttribLocation");
        mMatrixHandler = GLES20.glGetUniformLocation(mProgramHandler, "uMVPMatrix");
        GLESUtil.glesCheckError("glGetUniformLocation");

        // Initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(vertex.length * 4); // (# of coordinate values * 4 bytes per float)
        bb.order(ByteOrder.nativeOrder());
        mVertexBuffer = bb.asFloatBuffer();
        mVertexBuffer.put(vertex);
        mVertexBuffer.position(0);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void draw(float[] matrix) {
        // Bind default FBO
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
        GLESUtil.glesCheckError("glBindFramebuffer");

        // Enable properties
        if (mColor.a != 1.0f) {
            GLES20.glEnable(GLES20.GL_BLEND);
            GLESUtil.glesCheckError("glEnable");
            GLES20.glBlendFunc(GLES20.GL_SRC_COLOR, GLES20.GL_ONE_MINUS_SRC_ALPHA);
            GLESUtil.glesCheckError("glBlendFunc");
        }

        // Set the program and its attributes
        GLES20.glUseProgram(mProgramHandler);
        GLESUtil.glesCheckError("glUseProgram");

        // Position
        mVertexBuffer.position(0);
        GLES20.glVertexAttribPointer(mPositionHandler, 2, GLES20.GL_FLOAT, false, 0, mVertexBuffer);
        GLESUtil.glesCheckError("glVertexAttribPointer");
        GLES20.glEnableVertexAttribArray(mPositionHandler);
        GLESUtil.glesCheckError("glEnableVertexAttribArray");

        // Color
        GLES20.glVertexAttrib4f(mColorHandler, mColor.r, mColor.g, mColor.b, mColor.a);
        GLESUtil.glesCheckError("glVertexAttrib4f");

        // Apply the projection and view transformation
        GLES20.glUniformMatrix4fv(mMatrixHandler, 1, false, matrix, 0);
        GLESUtil.glesCheckError("glUniformMatrix4fv");

        // Draw the photo frame
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        GLESUtil.glesCheckError("glDrawElements");

        // Disable attributes
        GLES20.glDisableVertexAttribArray(mPositionHandler);
        GLESUtil.glesCheckError("glDisableVertexAttribArray");
        GLES20.glDisableVertexAttribArray(mColorHandler);
        GLESUtil.glesCheckError("glDisableVertexAttribArray");

        // Disable properties
        if (mColor.a != 1.0f) {
            GLES20.glDisable(GLES20.GL_BLEND);
            GLESUtil.glesCheckError("glDisable");
        }
    }

    /**
     * Method that sets the alpha color of the shape
     *
     * @param value The new alpha color of the shape
     */
    public void setAlpha(float value) {
        mColor.a = value;
    }

    /**
     * Method that destroy all the internal references
     */
    public void recycle() {
        if (GLES20.glIsProgram(mProgramHandler)) {
            GLES20.glDeleteProgram(mProgramHandler);
            GLESUtil.glesCheckError("glDeleteProgram");
        }
        mProgramHandler = 0;
        mPositionHandler = 0;
        mColorHandler = 0;
        mMatrixHandler = 0;
        mVertexBuffer.clear();
        mVertexBuffer = null;
    }
}