| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chromoting.cardboard; | |
| 6 | |
| 7 import static org.chromium.chromoting.cardboard.CardboardUtil.makeFloatBuffer; | |
| 8 import static org.chromium.chromoting.cardboard.CardboardUtil.makeRectangularTex
tureBuffer; | |
| 9 | |
| 10 import android.content.Context; | |
| 11 import android.graphics.Bitmap; | |
| 12 import android.graphics.BitmapFactory; | |
| 13 import android.graphics.PointF; | |
| 14 import android.graphics.RectF; | |
| 15 import android.opengl.GLES20; | |
| 16 | |
| 17 import org.chromium.chromoting.cardboard.MenuBar.MenuItemType; | |
| 18 | |
| 19 import java.nio.FloatBuffer; | |
| 20 | |
| 21 /** | |
| 22 * Cardboard activity menu item representing a corresponding function. | |
| 23 */ | |
| 24 public class MenuItem { | |
| 25 private static final String VERTEX_SHADER = | |
| 26 "uniform mat4 u_CombinedMatrix;" | |
| 27 + "attribute vec4 a_Position;" | |
| 28 + "attribute vec2 a_TexCoordinate;" | |
| 29 + "varying vec2 v_TexCoordinate;" | |
| 30 + "attribute float a_selected;" | |
| 31 + "varying float v_selected;" | |
| 32 + "void main() {" | |
| 33 + " v_selected = a_selected;" | |
| 34 + " v_TexCoordinate = a_TexCoordinate;" | |
| 35 + " gl_Position = u_CombinedMatrix * a_Position;" | |
| 36 + "}"; | |
| 37 | |
| 38 private static final String FRAGMENT_SHADER = | |
| 39 "precision mediump float;" | |
| 40 + "uniform sampler2D u_Texture;" | |
| 41 + "varying vec2 v_TexCoordinate;" | |
| 42 + "varying float v_selected;" | |
| 43 + "void main() {" | |
| 44 + " vec4 texture = texture2D(u_Texture, v_TexCoordinate);" | |
| 45 + " if (v_selected > 0.5) {" | |
| 46 + " gl_FragColor = vec4(texture.r + 0.5, texture.g + 0.5," | |
| 47 + " texture.b + 0.5, texture.a);" | |
| 48 + " } else {" | |
| 49 + " gl_FragColor = texture;" | |
| 50 + " }" | |
| 51 + "}"; | |
| 52 | |
| 53 private static final FloatBuffer TEXTURE_COORDINATES = makeRectangularTextur
eBuffer( | |
| 54 0.0f, 1.0f, 0.0f, 1.0f); | |
| 55 | |
| 56 private static final int POSITION_COORDINATE_DATA_SIZE = 3; | |
| 57 private static final int TEXTURE_COORDINATE_DATA_SIZE = 2; | |
| 58 | |
| 59 // Number of vertices passed to glDrawArrays(). | |
| 60 private static final int VERTICES_NUMBER = 6; | |
| 61 | |
| 62 private final FloatBuffer mPositionCoordinates; | |
| 63 | |
| 64 private int mVertexShaderHandle; | |
| 65 private int mFragmentShaderHandle; | |
| 66 private int mProgramHandle; | |
| 67 private int mCombinedMatrixHandle; | |
| 68 private int mTextureUniformHandle; | |
| 69 private int mPositionHandle; | |
| 70 private int mTextureDataHandle; | |
| 71 private int mTextureCoordinateHandle; | |
| 72 private int mItemSelectedHandle; | |
| 73 | |
| 74 private MenuItemType mType; | |
| 75 | |
| 76 private RectF mRect; | |
| 77 | |
| 78 public MenuItem(Context context, MenuItemType type, RectF rect) { | |
| 79 mType = type; | |
| 80 mRect = new RectF(rect); | |
| 81 float halfHeight = mRect.height() / 2; | |
| 82 float halfWidth = mRect.width() / 2; | |
| 83 mPositionCoordinates = makeFloatBuffer(new float[] { | |
| 84 // Desktop model coordinates. | |
| 85 -halfWidth, halfHeight, 0.0f, | |
| 86 -halfWidth, -halfHeight, 0.0f, | |
| 87 halfWidth, halfHeight, 0.0f, | |
| 88 -halfWidth, -halfHeight, 0.0f, | |
| 89 halfWidth, -halfHeight, 0.0f, | |
| 90 halfWidth, halfHeight, 0.0f | |
| 91 }); | |
| 92 | |
| 93 mVertexShaderHandle = | |
| 94 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE
R); | |
| 95 mFragmentShaderHandle = | |
| 96 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S
HADER); | |
| 97 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle, | |
| 98 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin
ate", | |
| 99 "a_selected", "u_CombinedMatrix", "u_Texture"}); | |
| 100 | |
| 101 mCombinedMatrixHandle = | |
| 102 GLES20.glGetUniformLocation(mProgramHandle, "u_CombinedMatrix"); | |
| 103 mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_T
exture"); | |
| 104 mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position
"); | |
| 105 mItemSelectedHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_sele
cted"); | |
| 106 mTextureDataHandle = TextureHelper.createTextureHandle(); | |
| 107 mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a
_TexCoordinate"); | |
| 108 | |
| 109 Bitmap texture = BitmapFactory.decodeResource(context.getResources(), | |
| 110 type.resourceId()); | |
| 111 TextureHelper.linkTexture(mTextureDataHandle, texture); | |
| 112 texture.recycle(); | |
| 113 } | |
| 114 | |
| 115 /** | |
| 116 * Return the type of this menu item. | |
| 117 */ | |
| 118 public MenuItemType getType() { | |
| 119 return mType; | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * Return the position of the center of this menu item. | |
| 124 */ | |
| 125 public PointF getPosition() { | |
| 126 return new PointF(mRect.centerX(), mRect.centerY()); | |
| 127 } | |
| 128 | |
| 129 /** | |
| 130 * Return true if the point is inside this menu item. | |
| 131 */ | |
| 132 public boolean contains(PointF point) { | |
| 133 return mRect.contains(point.x, point.y); | |
| 134 } | |
| 135 | |
| 136 /** | |
| 137 * Draw menu item according to the given model view projection matrix. | |
| 138 */ | |
| 139 public void draw(float[] combinedMatrix, boolean selected) { | |
| 140 GLES20.glUseProgram(mProgramHandle); | |
| 141 | |
| 142 // Pass in model view project matrix. | |
| 143 GLES20.glUniformMatrix4fv(mCombinedMatrixHandle, 1, false, combinedMatri
x, 0); | |
| 144 | |
| 145 // Pass in whether the item is selected | |
| 146 GLES20.glVertexAttrib1f(mItemSelectedHandle, selected ? 1.0f : 0.0f); | |
| 147 | |
| 148 // Pass in model position. | |
| 149 GLES20.glVertexAttribPointer(mPositionHandle, POSITION_COORDINATE_DATA_S
IZE, | |
| 150 GLES20.GL_FLOAT, false, 0, mPositionCoordinates); | |
| 151 GLES20.glEnableVertexAttribArray(mPositionHandle); | |
| 152 | |
| 153 // Pass in texture coordinate. | |
| 154 GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORDINAT
E_DATA_SIZE, | |
| 155 GLES20.GL_FLOAT, false, 0, TEXTURE_COORDINATES); | |
| 156 GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); | |
| 157 | |
| 158 // Enable the transparent background. | |
| 159 GLES20.glEnable(GLES20.GL_BLEND); | |
| 160 GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); | |
| 161 | |
| 162 GLES20.glActiveTexture(GLES20.GL_TEXTURE0); | |
| 163 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle); | |
| 164 GLES20.glUniform1i(mTextureUniformHandle, 0); | |
| 165 | |
| 166 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, VERTICES_NUMBER); | |
| 167 | |
| 168 GLES20.glDisable(GLES20.GL_BLEND); | |
| 169 GLES20.glDisableVertexAttribArray(mPositionHandle); | |
| 170 GLES20.glDisableVertexAttribArray(mTextureCoordinateHandle); | |
| 171 } | |
| 172 | |
| 173 /* | |
| 174 * Clean menu item related opengl data. | |
| 175 */ | |
| 176 public void clean() { | |
| 177 GLES20.glDeleteShader(mVertexShaderHandle); | |
| 178 GLES20.glDeleteShader(mFragmentShaderHandle); | |
| 179 GLES20.glDeleteTextures(1, new int[]{mTextureDataHandle}, 0); | |
| 180 } | |
| 181 } | |
| OLD | NEW |