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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/*
* Copyright (C) 2011 The Android Open Source 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 com.example.cyclopus.nativecpp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
/**
* A two-dimensional square for use as a drawn object in OpenGL ES 2.0.
*/
public class TexturedSquare {
private static int[] textureHandle;
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;\n" +
"attribute vec2 aPosition;\n" +
"attribute vec2 aTexPos;\n" +
"varying vec2 vTexPos;\n" +
"void main() {\n" +
" vTexPos = aTexPos;\n" +
" gl_Position = uMVPMatrix * vec4(aPosition.xy, 0.0, 1.0);\n" +
"}";
private final String fragmentShaderCode =
"precision mediump float;\n"+
"uniform sampler2D uTexture;\n" +
"varying vec2 vTexPos;\n" +
"void main(void)\n" +
"{\n" +
" gl_FragColor = texture2D(uTexture, vTexPos);\n" +
"}";
private final FloatBuffer vertexBuffer;
private final ShortBuffer drawListBuffer;
private final int mProgram;
private int mPositionHandle;
private int mTexturePosHandle;
private int mColorHandle;
private int mMVPMatrixHandle;
// number of coordinates per vertex in this array
final int COORDS_PER_VERTEX = 4;
float squareCoords[] = {
-1.0f,-1.0f, 0.0f, 0.0f, // vertex 3
-1.0f, 1.0f, 0.0f, 1.0f, // vertex 1
1.0f,-1.0f, 1.0f, 0.0f, // vertex 2
1.0f, 1.0f, 1.0f, 1.0f, // vertex 0
}; // top right
private final short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
float color[] = { 0.2f, 0.709803922f, 0.898039216f, 1.0f };
/**
* Sets up the drawing object data for use in an OpenGL ES context.
*/
public TexturedSquare() {
MyGLRenderer.checkGlError("glGetUniformLocation");
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
squareCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareCoords);
vertexBuffer.position(0);
// initialize byte buffer for the draw list
ByteBuffer dlb = ByteBuffer.allocateDirect(
// (# of coordinate values * 2 bytes per short)
drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
MyGLRenderer.checkGlError("glGetUniformLocation");
// prepare shaders and OpenGL program
int vertexShader = MyGLRenderer.loadShader(
GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = MyGLRenderer.loadShader(
GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(mProgram); // create OpenGL program executables
String LogInfo = GLES20.glGetProgramInfoLog(mProgram);
/* //Link the program.
int isLinked = 0;
int []i = GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS);
if(isLinked == GL_FALSE)
{
//The maxLength includes the NULL character
String LogInfo GLES20.glGetProgramInfoLog(mProgram);
//The program is useless now. So delete it.
GLES20.glDeleteProgram(program);
//Provide the infolog in whatever manner you deem best.
//Exit with failure.
return;
}*/
MyGLRenderer.checkGlError("glGetUniformLocation");
}
public int loadTexture()
{
int uTexture = GLES20.glGetUniformLocation(mProgram, "uTexture");
textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);
MyGLRenderer.checkGlError("glGetUniformLocation");
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false; // No pre-scaling
// Read in the resource
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bitmap = Bitmap.createBitmap(640, 480, conf); // this creates a MUTABLE bitmap
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
// Set filtering
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
GLES20.glUniform1i(uTexture, 0);
MyGLRenderer.checkGlError("glGetUniformLocation");
//
//
// // Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
return textureHandle[0];
}
/**
* Encapsulates the OpenGL ES instructions for drawing this shape.
*
* @param mvpMatrix - The Model View Project matrix in which to draw
* this shape.
*/
public void draw(float[] mvpMatrix) {
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
MyGLRenderer.checkGlError("glGetUniformLocation");
mTexturePosHandle = GLES20.glGetAttribLocation(mProgram, "aTexPos");
MyGLRenderer.checkGlError("glGetUniformLocation");
// Enable a handle to the triangle vertices
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(
mPositionHandle, 2,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
MyGLRenderer.checkGlError("glGetUniformLocation");
GLES20.glVertexAttribPointer(
mTexturePosHandle, 2,
GLES20.GL_FLOAT, false,
vertexStride, (vertexBuffer.position(2)));
GLES20.glEnableVertexAttribArray(mTexturePosHandle);
MyGLRenderer.checkGlError("glGetUniformLocation");
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
MyGLRenderer.checkGlError("glGetUniformLocation");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
MyGLRenderer.checkGlError("glUniformMatrix4fv");
// Draw the square
// GLES20.glDrawElements(
// GLES20.GL_TRIANGLES, drawOrder.length,
// GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
GLES20.glDrawElements(
GLES20.GL_TRIANGLES, 0,
GLES20.GL_UNSIGNED_SHORT, 4);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
} |
Partager