diff options
Diffstat (limited to 'src/org/cyanogenmod/wallpapers/photophase/shapes/ColorShape.java')
-rw-r--r-- | src/org/cyanogenmod/wallpapers/photophase/shapes/ColorShape.java | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/src/org/cyanogenmod/wallpapers/photophase/shapes/ColorShape.java b/src/org/cyanogenmod/wallpapers/photophase/shapes/ColorShape.java new file mode 100644 index 0000000..027d089 --- /dev/null +++ b/src/org/cyanogenmod/wallpapers/photophase/shapes/ColorShape.java @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2013 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.GLESUtil; +import org.cyanogenmod.wallpapers.photophase.GLESUtil.GLColor; +import org.cyanogenmod.wallpapers.photophase.R; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.FloatBuffer; +import java.nio.ShortBuffer; + +/** + * 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 ShortBuffer mVertexOrderBuffer; + + 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); + + // Initialize vertex byte buffer for shape coordinates order + final short[] order = { 0, 1, 2, 0, 2, 3 }; + ByteBuffer dlb = ByteBuffer.allocateDirect(order.length * 2); // (# of coordinate values * 2 bytes per short) + dlb.order(ByteOrder.nativeOrder()); + mVertexOrderBuffer = dlb.asShortBuffer(); + mVertexOrderBuffer.put(order); + mVertexOrderBuffer.position(0); + } + + /** + * {@inheritDoc} + */ + @Override + public void draw(float[] matrix) { + // Enable properties + if (mColor.a != 1.0f) { + GLES20.glEnable(GLES20.GL_BLEND); + GLES20.glBlendFunc(GLES20.GL_SRC_COLOR, GLES20.GL_ONE_MINUS_SRC_ALPHA); + } + + // Set the program and its attributes + GLES20.glUseProgram(mProgramHandler); + GLESUtil.glesCheckError("glUseProgram"); + + // Position + mVertexBuffer.position(0); + GLES20.glVertexAttribPointer( + mPositionHandler, + 3, + GLES20.GL_FLOAT, + false, + 3 * 4, + 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 + mVertexOrderBuffer.position(0); + GLES20.glDrawElements( + GLES20.GL_TRIANGLE_FAN, + 6, + GLES20.GL_UNSIGNED_SHORT, + mVertexOrderBuffer); + 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(); + mVertexOrderBuffer.clear(); + mVertexBuffer = null; + mVertexOrderBuffer = null; + } +} |