下面列出了android.opengl.EGLContext#android.opengl.Matrix 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onDrawFrame(GL10 unused) {
angle = ((float) SystemClock.elapsedRealtime() - startTime) * 0.02f;
GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
if (scene != null) {
Matrix.setLookAtM(mViewMatrix, 0,
0, 0, -4,
0f, 0f, 0f,
0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
Matrix.rotateM(mMVPMatrix, 0, angle, 0.8f, 2.f, 1.f);
GLES20.glUseProgram(shaderProgram);
int mMVPMatrixHandle = GLES20.glGetUniformLocation(shaderProgram, "uMVPMatrix");
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
scene.render(shaderProgram, "vPosition", "aColor");
}
}
public void transformCoord(float[] pIn, float[] pOut) {
//Log.p("Transforming "+pIn[0]+","+pIn[1]);
//Log.p("Transform is "+this);
int len = Math.min(pIn.length, 3);
factory.sTemp[2] = 0;
System.arraycopy(pIn, 0, factory.sTemp, 0, len);
factory.sTemp[3] = 1f;
Matrix.multiplyMV(factory.sTemp, 4, data, 0, factory.sTemp, 0);
float w = factory.sTemp[7];
if ( w != 1 && w != 0 ){
for ( int i=4; i<7; i++){
factory.sTemp[i] = factory.sTemp[i]/w;
}
}
//len = pOut.length;
System.arraycopy(factory.sTemp, 4, pOut, 0, len);
}
public GLES20Canvas() {
Matrix.setIdentityM(mTempTextureMatrix, 0);
Matrix.setIdentityM(mMatrices, mCurrentMatrixIndex);
mAlphas[mCurrentAlphaIndex] = 1f;
mTargetTextures.add(null);
FloatBuffer boxBuffer = createBuffer(BOX_COORDINATES);
mBoxCoordinates = uploadBuffer(boxBuffer);
mDrawProgram = assembleProgram(loadShader(GLES20.GL_VERTEX_SHADER, BasicDrawShapeFilter.DRAW_VERTEX_SHADER), loadShader(GLES20.GL_FRAGMENT_SHADER, BasicDrawShapeFilter.DRAW_FRAGMENT_SHADER), mDrawParameters, mTempIntArray);
int textureFragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, BasicTextureFilter.TEXTURE_FRAGMENT_SHADER);
int meshVertexShader = loadShader(GLES20.GL_VERTEX_SHADER, MESH_VERTEX_SHADER);
setupMeshProgram(meshVertexShader, textureFragmentShader);
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
checkError();
}
private void _drawBox(float x, float y, float sx, float sy, float r, float g,
float b, int tex) {
// scale and translate
float[] m = new float[16];
Matrix.setIdentityM(m, 0);
Matrix.scaleM(m, 0, 2.0f * sx, 2.0f * sy, 1.0f);
m[3] += (1.0 - 2.0 * x);
m[7] += (1.0 / _ratio - 2.0 * y);
float[] m2 = new float[16];
Matrix.multiplyMM(m2, 0, m, 0, mMVPMatrix, 0);
if (tex == -1) {
mSquare.draw(m2, r, g, b, 1.0f);
} else {
mSquareTex.draw(m2, r, g, b, 1.0f, tex);
}
checkGlError("draw");
}
/**
* 指定したビットマップをテクスチャに読み込む
* @param bitmap
*/
@Override
public void loadBitmap(@NonNull final Bitmap bitmap) {
mImageWidth = bitmap.getWidth(); // 読み込んだイメージのサイズを取得
mImageHeight = bitmap.getHeight();
Bitmap texture = Bitmap.createBitmap(mTexWidth, mTexHeight, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(texture);
canvas.drawBitmap(bitmap, 0, 0, null);
bitmap.recycle();
// テクスチャ座標変換行列を設定(読み込んだイメージサイズがテクスチャサイズにフィットするようにスケール変換)
Matrix.setIdentityM(mTexMatrix, 0);
mTexMatrix[0] = mImageWidth / (float)mTexWidth;
mTexMatrix[5] = mImageHeight / (float)mTexHeight;
// if (DEBUG) Log.v(TAG, String.format("image(%d,%d),scale(%f,%f)",
// mImageWidth, mImageHeight, mMvpMatrix[0], mMvpMatrix[5]));
makeCurrent();
GLUtils.texImage2D(mTextureTarget, 0, texture, 0);
swap();
texture.recycle();
}
/**
* Returns the composition of all current color matrices, or {@code null} if there are none.
*/
@GuardedBy("mColorMatrix")
private float[] computeColorMatrixLocked() {
final int count = mColorMatrix.size();
if (count == 0) {
return null;
}
final float[][] result = mTempColorMatrix;
Matrix.setIdentityM(result[0], 0);
for (int i = 0; i < count; i++) {
float[] rhs = mColorMatrix.valueAt(i);
Matrix.multiplyMM(result[(i + 1) % 2], 0, result[i % 2], 0, rhs, 0);
}
return result[count % 2];
}
public SurfaceTextureRenderer(int facing) {
mFacing = facing;
mRegularTriangleVertices = ByteBuffer.allocateDirect(sRegularTriangleVertices.length *
FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
mRegularTriangleVertices.put(sRegularTriangleVertices).position(0);
mHorizontalFlipTriangleVertices = ByteBuffer.allocateDirect(
sHorizontalFlipTriangleVertices.length * FLOAT_SIZE_BYTES).
order(ByteOrder.nativeOrder()).asFloatBuffer();
mHorizontalFlipTriangleVertices.put(sHorizontalFlipTriangleVertices).position(0);
mVerticalFlipTriangleVertices = ByteBuffer.allocateDirect(
sVerticalFlipTriangleVertices.length * FLOAT_SIZE_BYTES).
order(ByteOrder.nativeOrder()).asFloatBuffer();
mVerticalFlipTriangleVertices.put(sVerticalFlipTriangleVertices).position(0);
mBothFlipTriangleVertices = ByteBuffer.allocateDirect(
sBothFlipTriangleVertices.length * FLOAT_SIZE_BYTES).
order(ByteOrder.nativeOrder()).asFloatBuffer();
mBothFlipTriangleVertices.put(sBothFlipTriangleVertices).position(0);
Matrix.setIdentityM(mSTMatrix, 0);
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
this.width = width;
this.height = height;
// Adjust the viewport based on geometry changes, such as screen rotation
GLES20.glViewport(0, 0, width, height);
// INFO: Set the camera position (View matrix)
// The camera has 3 vectors (the position, the vector where we are looking at, and the up position (sky)
Matrix.setLookAtM(modelViewMatrix, 0, camera.xPos, camera.yPos, camera.zPos, camera.xView, camera.yView,
camera.zView, camera.xUp, camera.yUp, camera.zUp);
// the projection matrix is the 3D virtual space (cube) that we want to project
float ratio = (float) width / height;
Log.d(TAG, "projection: [" + -ratio + "," + ratio + ",-1,1]-near/far[1,10]");
Matrix.frustumM(modelProjectionMatrix, 0, -ratio, ratio, -1, 1, getNear(), getFar());
// Calculate the projection and view transformation
Matrix.multiplyMM(mvpMatrix, 0, modelProjectionMatrix, 0, modelViewMatrix, 0);
}
/**
* Calculate the 2 vectors, that is a line (x1,y1,z1-x2,y2,z2} corresponding to the normal of the specified face.
* The calculated line will be positioned exactly in the middle of the face
*
* @param v0 the first vector of the face
* @param v1 the second vector of the face
* @param v2 the third vector of the face
* @return the 2 vectors (line) corresponding to the face normal
*/
public static float[][] calculateFaceNormal(float[] v0, float[] v1, float[] v2) {
// calculate perpendicular vector to the face. That is to calculate the cross product of v1-v0 x v2-v0
float[] va = new float[]{v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]};
float[] vb = new float[]{v2[0] - v0[0], v2[1] - v0[1], v2[2] - v0[2]};
float[] n = new float[]{va[1] * vb[2] - va[2] * vb[1], va[2] * vb[0] - va[0] * vb[2],
va[0] * vb[1] - va[1] * vb[0]};
float modul = Matrix.length(n[0], n[1], n[2]);
float[] vn = new float[]{n[0] / modul, n[1] / modul, n[2] / modul};
// calculate center of the face
float[] faceCenter = calculateFaceCenter(v0, v1, v2);
float[] vn2 = new float[]{faceCenter[0] + vn[0], faceCenter[1] + vn[1], faceCenter[2] + vn[2]};
@SuppressWarnings("unused")
String msg = "fNormal(" + v0[0] + "," + v0[1] + "," + v0[2] + "#" + v1[0] + "," + v1[1] + "," + v1[2] + "#"
+ v2[0] + "," + v2[1] + "," + v2[2] + ")#normal(" + vn[0] + "," + vn[1] + "," + vn[2] + ") center("
+ faceCenter[0] + "," + faceCenter[1] + "," + faceCenter[2] + ") to(" + vn2[0] + "," + vn2[1] + ","
+ vn2[2] + ")";
// Log.d("ObjectV4", msg);
return new float[][]{faceCenter, vn2};
}
/**
* Bitmapから画像をテクスチャに読み込む
* @param bitmap
*/
@Override
public void loadBitmap(@NonNull final Bitmap bitmap) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
if ((width > mTexWidth) || (height > mTexHeight)) {
mWidth = width;
mHeight = height;
releaseFrameBuffer();
createFrameBuffer(width, height);
}
GLES30.glActiveTexture(TEX_UNIT);
GLES30.glBindTexture(TEX_TARGET, mFBOTexId);
GLUtils.texImage2D(TEX_TARGET, 0, bitmap, 0);
GLES30.glBindTexture(TEX_TARGET, 0);
// initialize texture matrix
Matrix.setIdentityM(mTexMatrix, 0);
mTexMatrix[0] = width / (float)mTexWidth;
mTexMatrix[5] = height / (float)mTexHeight;
}
public GLES20Canvas() {
Matrix.setIdentityM(mTempTextureMatrix, 0);
Matrix.setIdentityM(mMatrices, mCurrentMatrixIndex);
mAlphas[mCurrentAlphaIndex] = 1f;
mTargetTextures.add(null);
FloatBuffer boxBuffer = createBuffer(BOX_COORDINATES);
mBoxCoordinates = uploadBuffer(boxBuffer);
int drawVertexShader = loadShader(GLES20.GL_VERTEX_SHADER, DRAW_VERTEX_SHADER);
int textureVertexShader = loadShader(GLES20.GL_VERTEX_SHADER, TEXTURE_VERTEX_SHADER);
int meshVertexShader = loadShader(GLES20.GL_VERTEX_SHADER, MESH_VERTEX_SHADER);
int drawFragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, DRAW_FRAGMENT_SHADER);
int textureFragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, TEXTURE_FRAGMENT_SHADER);
int oesTextureFragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
OES_TEXTURE_FRAGMENT_SHADER);
mDrawProgram = assembleProgram(drawVertexShader, drawFragmentShader, mDrawParameters);
mTextureProgram = assembleProgram(textureVertexShader, textureFragmentShader,
mTextureParameters);
mOesTextureProgram = assembleProgram(textureVertexShader, oesTextureFragmentShader,
mOesTextureParameters);
mMeshProgram = assembleProgram(meshVertexShader, textureFragmentShader, mMeshParameters);
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
checkError();
}
protected void updateModelMatrix(){
Matrix.setIdentityM(modelMatrix, 0);
if (getRotation() != null) {
Matrix.rotateM(modelMatrix, 0, getRotation()[0], 1f, 0f, 0f);
Matrix.rotateM(modelMatrix, 0, getRotation()[1], 0, 1f, 0f);
Matrix.rotateM(modelMatrix, 0, getRotationZ(), 0, 0, 1f);
}
if (getScale() != null) {
Matrix.scaleM(modelMatrix, 0, getScaleX(), getScaleY(), getScaleZ());
}
if (getPosition() != null) {
Matrix.translateM(modelMatrix, 0, getPositionX(), getPositionY(), getPositionZ());
}
if (this.bindShapeMatrix == null){
// geometries not linked to any joint does not have bind shape transform
System.arraycopy(this.modelMatrix,0,this.newModelMatrix,0,16);
} else {
Matrix.multiplyMM(newModelMatrix, 0, this.modelMatrix, 0, this.bindShapeMatrix, 0);
}
}
public Trajectory(int lineWidth) {
mLineWidth = lineWidth;
// Reset the model matrix to the identity
Matrix.setIdentityM(getModelMatrix(), 0);
// Allocate a vertex buffer
ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(MAX_VERTICES
* BYTES_PER_FLOAT);
vertexByteBuffer.order(ByteOrder.nativeOrder());
mVertexBuffer = vertexByteBuffer.asFloatBuffer();
// Load the vertex and fragment shaders, then link the program
int vertexShader = RenderUtils.loadShader(GLES20.GL_VERTEX_SHADER,
mVertexShaderCode);
int fragShader = RenderUtils.loadShader(GLES20.GL_FRAGMENT_SHADER,
mFragmentShaderCode);
mProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glAttachShader(mProgram, fragShader);
GLES20.glLinkProgram(mProgram);
}
/**
* Renders the point cloud. ArCore point cloud is given in world space.
*
* @param cameraView the camera view matrix for this frame, typically from {@link
* com.google.ar.core.Camera#getViewMatrix(float[], int)}.
* @param cameraPerspective the camera projection matrix for this frame, typically from {@link
* com.google.ar.core.Camera#getProjectionMatrix(float[], int, float, float)}.
*/
public void draw(float[] cameraView, float[] cameraPerspective) {
float[] modelViewProjection = new float[16];
Matrix.multiplyMM(modelViewProjection, 0, cameraPerspective, 0, cameraView, 0);
ShaderUtil.checkGLError(TAG, "Before draw");
GLES20.glUseProgram(mProgramName);
GLES20.glEnableVertexAttribArray(mPositionAttribute);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo);
GLES20.glVertexAttribPointer(
mPositionAttribute, 4, GLES20.GL_FLOAT, false, BYTES_PER_POINT, 0);
GLES20.glUniform4f(mColorUniform, 31.0f / 255.0f, 188.0f / 255.0f, 210.0f / 255.0f, 1.0f);
GLES20.glUniformMatrix4fv(mModelViewProjectionUniform, 1, false, modelViewProjection, 0);
GLES20.glUniform1f(mPointSizeUniform, 5.0f);
GLES20.glDrawArrays(GLES20.GL_POINTS, 0, mNumPoints);
GLES20.glDisableVertexAttribArray(mPositionAttribute);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
ShaderUtil.checkGLError(TAG, "Draw");
}
@Override
public void run() {
float[] transform = new float[16];
//旋转到正常角度
Matrix.setRotateM(transform, 0, 180f, 0.0f, 0, 1.0f);
//调整大小比例
Matrix.scaleM(transform, 0, mCustomBitmapIconEffect.getScaleW(), mCustomBitmapIconEffect.getScaleH(), 1);
if (moveBitmap) {
//调整位置
Matrix.translateM(transform, 0, mCustomBitmapIconEffect.getPositionX(), mCustomBitmapIconEffect.getPositionY(), 0f);
} else {
float maxX = mCustomBitmapIconEffect.getMaxPositionX();
float minX = mCustomBitmapIconEffect.getMinPositionX();
float maxY = mCustomBitmapIconEffect.getMaxPositionY();
float minY = mCustomBitmapIconEffect.getMinPositionY();
float x = (float) Math.random() * (maxX - minX) + minX;
float y = (float) Math.random() * (maxY - minY) + minY;
//调整位置
Matrix.translateM(transform, 0, x, y, 0f);
mGSYVideoGLViewCustomRender.setCurrentMVPMatrix(transform);
}
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
Matrix.frustumM(mProjectionMatrix, 0, -ratio * 2, ratio * 2, -2, 2, 2, 7);
}
/**
* Re-computes mModelViewMatrix, based on the current values for rotation, scale, and
* translation.
*/
private void recomputeMatrix() {
float[] modelView = mModelViewMatrix;
Matrix.setIdentityM(modelView, 0);
Matrix.translateM(modelView, 0, mPosX, mPosY, 0.0f);
if (mAngle != 0.0f) {
Matrix.rotateM(modelView, 0, mAngle, 0.0f, 0.0f, 1.0f);
}
Matrix.scaleM(modelView, 0, mScaleX, mScaleY, 1.0f);
mMatrixReady = true;
}
/**
* Constructor
* this should be called in GL context
*/
public GLDrawer2D() {
pVertex = ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
pVertex.put(VERTICES);
pVertex.flip();
pTexCoord = ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
pTexCoord.put(TEXCOORD);
pTexCoord.flip();
hProgram = loadShader(vss, fss);
GLES20.glUseProgram(hProgram);
maPositionLoc = GLES20.glGetAttribLocation(hProgram, "aPosition");
maTextureCoordLoc = GLES20.glGetAttribLocation(hProgram, "aTextureCoord");
muMVPMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uMVPMatrix");
muTexMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uTexMatrix");
Matrix.setIdentityM(mMvpMatrix, 0);
GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mMvpMatrix, 0);
GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, mMvpMatrix, 0);
GLES20.glVertexAttribPointer(maPositionLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ, pVertex);
GLES20.glVertexAttribPointer(maTextureCoordLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ,
pTexCoord);
GLES20.glEnableVertexAttribArray(maPositionLoc);
GLES20.glEnableVertexAttribArray(maTextureCoordLoc);
}
/**
* Set model/view/projection transform matrix
*/
public void setMatrix(final float[] matrix, final int offset) {
if ((matrix != null) && (matrix.length >= offset + 16)) {
System.arraycopy(matrix, offset, mMvpMatrix, 0, 16);
} else {
Matrix.setIdentityM(mMvpMatrix, 0);
}
}
public void drawFrame(SurfaceTexture st) {
checkGlError("onDrawFrame start");
st.getTransformMatrix(mSTMatrix);
GLES20.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glUseProgram(mProgram);
checkGlError("glUseProgram");
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maPosition");
GLES20.glEnableVertexAttribArray(maPositionHandle);
checkGlError("glEnableVertexAttribArray maPositionHandle");
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
GLES20.glVertexAttribPointer(maTextureHandle, 2, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maTextureHandle");
GLES20.glEnableVertexAttribArray(maTextureHandle);
checkGlError("glEnableVertexAttribArray maTextureHandle");
Matrix.setIdentityM(mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
checkGlError("glDrawArrays");
GLES20.glFinish();
}
@Override
public void onDrawFrame(int textureId) {
onPreDrawElements();
glPassThroughProgram.use();
Matrix.setIdentityM(projectionMatrix,0);
plane.uploadTexCoordinateBuffer(glPassThroughProgram.getTextureCoordinateHandle());
plane.uploadVerticesBuffer(glPassThroughProgram.getPositionHandle());
GLES20.glUniformMatrix4fv(glPassThroughProgram.getMVPMatrixHandle(), 1, false, projectionMatrix, 0);
TextureUtils.bindTexture2D(textureId, GLES20.GL_TEXTURE0,glPassThroughProgram.getTextureSamplerHandle(),0);
GLES20.glViewport(0,0,surfaceWidth,surfaceHeight);
plane.draw();
}
private void setMatrix(ShaderParameter[] params, float x, float y, float width, float height, ICustomMVPMatrix customMVPMatrix) {
if (customMVPMatrix != null) {
GLES20.glUniformMatrix4fv(params[INDEX_MATRIX].handle, 1, false, customMVPMatrix.getMVPMatrix(mScreenWidth, mScreenHeight, x, y, width, height), 0);
checkError();
return;
}
GLES20.glViewport(0, 0, mScreenWidth, mScreenHeight);
Matrix.translateM(mTempMatrix, 0, mMatrices, mCurrentMatrixIndex, x, y, 0f);
Matrix.scaleM(mTempMatrix, 0, width, height, 1f);
Matrix.multiplyMM(mTempMatrix, MATRIX_SIZE, mProjectionMatrix, 0, mTempMatrix, 0);
printMatrix("translate matrix:", mTempMatrix, MATRIX_SIZE);
GLES20.glUniformMatrix4fv(params[INDEX_MATRIX].handle, 1, false, mTempMatrix, MATRIX_SIZE);
checkError();
}
@Override
public void multiplyMatrix(float[] matrix, int offset) {
float[] temp = mTempMatrix;
float[] currentMatrix = mMatrices;
int index = mCurrentMatrixIndex;
Matrix.multiplyMM(temp, 0, currentMatrix, index, matrix, offset);
System.arraycopy(temp, 0, currentMatrix, index, 16);
}
public CameraViewModel()
{
mPosition = new float [X_SLICE * Y_SLICE * 3];
mTextureCoordinate = new float [X_SLICE * Y_SLICE * 2];
mIndex = new short[(Y_SLICE -1) * (X_SLICE -1) * 2 * 3];
int cc = 0;
for(int dy = 0 ; dy < Y_SLICE - 1; dy++) {
int ii = dy * X_SLICE;
for(int dx = 0 ; dx < X_SLICE - 1; dx++) {
mIndex[cc++] = (short)ii;
mIndex[cc++] = (short)(ii +1);
mIndex[cc++] = (short)(ii + Y_SLICE);
mIndex[cc++] = (short)(ii +1);
mIndex[cc++] = (short)(ii + Y_SLICE + 1);
mIndex[cc++] = (short)(ii + Y_SLICE);
ii++;
}
}
final int BYTES_PER_SHORT = 2;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(mIndex.length * BYTES_PER_SHORT);
byteBuffer.order(ByteOrder.nativeOrder());
mIndexBuffer = byteBuffer.asShortBuffer();
mIndexBuffer.put(mIndex);
mIndexBuffer.position(0);
Matrix.orthoM(mProjectMatrix, 0, -1, 1, -1, 1, -1, 1);
Matrix.setIdentityM(mViewMatrix, 0);
Matrix.multiplyMM(mMvpMatrix, 0, mProjectMatrix, 0, mViewMatrix, 0);
}
private void MoveCameraZImpl(float direction) {
// Moving the camera requires a little more then adding 1 to the z or
// subracting 1.
// First we need to get the direction at which we are looking.
float xLookDirection, yLookDirection, zLookDirection;
// The look direction is the view minus the position (where we are).
xLookDirection = xView - xPos;
yLookDirection = yView - yPos;
zLookDirection = zView - zPos;
// Normalize the direction.
float dp = Matrix.length(xLookDirection, yLookDirection, zLookDirection);
xLookDirection /= dp;
yLookDirection /= dp;
zLookDirection /= dp;
float x = xPos + xLookDirection * direction;
float y = yPos + yLookDirection * direction;
float z = zPos + zLookDirection * direction;
if (isOutOfBounds(x, y , z)) return;
xPos = x;
yPos = y;
zPos = z;
setChanged(true);
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
// Adjust the viewport based on geometry changes,
// such as screen rotation
GLES30.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
// Adjust the viewport based on geometry changes,
// such as screen rotation
GLES31.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
public STextureRender() {
mTriangleVertices = ByteBuffer.allocateDirect(
mTriangleVerticesData.length * FLOAT_SIZE_BYTES)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mTriangleVertices.put(mTriangleVerticesData).position(0);
Matrix.setIdentityM(mSTMatrix, 0);
}
public void setIgnoreAspectRatio(boolean ignoreAspectRatio) {
this.ignoreAspectRatio = ignoreAspectRatio;
if (ignoreAspectRatio) {
Matrix.orthoM(orthographicMatrix, 0, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
setUniformMatrix4f(orthographicMatrixUniform, orthographicMatrix);
} else {
onOutputSizeChanged(getOutputWidth(), getOutputHeight());
}
}
/**
* Re-computes mModelViewMatrix, based on the current values for rotation, scale, and
* translation.
*/
private void recomputeMatrix() {
float[] modelView = mModelViewMatrix;
Matrix.setIdentityM(modelView, 0);
Matrix.translateM(modelView, 0, mPosX, mPosY, 0.0f);
if (mAngle != 0.0f) {
Matrix.rotateM(modelView, 0, mAngle, 0.0f, 0.0f, 1.0f);
}
Matrix.scaleM(modelView, 0, mScaleX, mScaleY, 1.0f);
mMatrixReady = true;
}