1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.vr.cts; 17 18 import android.opengl.GLES20; 19 import android.opengl.GLSurfaceView; 20 import android.opengl.GLSurfaceView.Renderer; 21 22 import java.nio.ByteBuffer; 23 import java.nio.ByteOrder; 24 import java.nio.FloatBuffer; 25 import java.util.concurrent.CountDownLatch; 26 27 import javax.microedition.khronos.egl.EGLConfig; 28 import javax.microedition.khronos.opengles.GL10; 29 30 public class RendererBasicTest implements GLSurfaceView.Renderer { 31 private String vertexShaderCode = 32 "attribute vec4 vPosition; \n" 33 + "void main(){ \n" + 34 "gl_Position = vPosition; \n" 35 + "} \n"; 36 37 private String fragmentShaderCode = "precision mediump float; \n" 38 + "void main(){ \n" 39 + " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n" 40 + "} \n"; 41 42 43 FloatBuffer floatBuffer; 44 int mProgram; 45 int maPositionHandle; 46 float[] mColorOne = new float[4]; 47 48 int[] mShaderCount = null; 49 int mError; 50 51 // child may need to manipulate them directly 52 protected CountDownLatch mLatch; 53 protected boolean mDrawn = false; 54 RendererBasicTest(CountDownLatch latch)55 public RendererBasicTest(CountDownLatch latch) { 56 mLatch = latch; 57 } 58 59 @Override onSurfaceChanged(GL10 gl, int width, int height)60 public void onSurfaceChanged(GL10 gl, int width, int height) { 61 } 62 loadShader(int type, String shaderCode)63 public int loadShader(int type, String shaderCode) { 64 int shader = GLES20.glCreateShader(type); 65 GLES20.glShaderSource(shader, shaderCode); 66 GLES20.glCompileShader(shader); 67 return shader; 68 } 69 70 @Override onDrawFrame(GL10 gl)71 public void onDrawFrame(GL10 gl) { 72 if (mDrawn) { 73 return; 74 } 75 mDrawn = true; 76 doOnDrawFrame(gl); 77 mLatch.countDown(); 78 } 79 doOnDrawFrame(GL10 gl)80 public void doOnDrawFrame(GL10 gl) { 81 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 82 GLES20.glUseProgram(mProgram); 83 84 GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, 85 false, 12, floatBuffer); 86 GLES20.glEnableVertexAttribArray(maPositionHandle); 87 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); 88 mShaderCount = new int[1]; 89 int[] shaders = new int[10]; 90 GLES20.glGetAttachedShaders(mProgram, 10, mShaderCount, 0, shaders, 0); 91 } 92 93 @Override onSurfaceCreated(GL10 gl, EGLConfig config)94 public void onSurfaceCreated(GL10 gl, EGLConfig config) { 95 GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); 96 initShapes(); 97 int vertexShaderOne = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); 98 int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); 99 mProgram = GLES20.glCreateProgram(); 100 GLES20.glAttachShader(mProgram, vertexShaderOne); 101 GLES20.glAttachShader(mProgram, fragmentShader); 102 GLES20.glLinkProgram(mProgram); 103 int[] linkStatus = new int[1]; 104 GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, linkStatus, 0); 105 if (linkStatus[0] != GLES20.GL_TRUE) { 106 //do nothing 107 } 108 109 } 110 initShapes()111 public void initShapes(){ 112 float triangleCoords[] = { -0.5f, -0.25f, 0, 113 0.5f, -0.25f, 0, 114 0.0f, 0.559016994f, 0}; 115 ByteBuffer byteBuffer = ByteBuffer.allocateDirect(triangleCoords.length * 4); 116 byteBuffer.order(ByteOrder.nativeOrder()); 117 floatBuffer = byteBuffer.asFloatBuffer(); 118 floatBuffer.put(triangleCoords); 119 floatBuffer.position(0); 120 } 121 } 122