private void initViewMatrix() { // Position the eye in front of the origin final float eyeX = 0.0f; final float eyeY = 0.0f; final float eyeZ = 0f; // We are looking toward the distance. final float lookX = 0.0f; final float lookY = 0.0f; final float lookZ = -5.0f; // Set our up vector. // This is where our head would be pointing // were we holding the camera. final float upX = 0.0f; final float upY = 1.0f; final float upZ = 0.0f; // Set the view matrix. // This matrix can be said to represent the camera position, Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); }
@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"); } }
/** * Draws a point representing the position of the light. */ private void drawLight() { final int pointMVPMatrixHandle = GLES20.glGetUniformLocation(mPointProgramHandle, "u_MVPMatrix"); final int pointPositionHandle = GLES20.glGetAttribLocation(mPointProgramHandle, "a_Position"); // Pass in the position. GLES20.glVertexAttrib3f(pointPositionHandle, mLightPosInModelSpace[0], mLightPosInModelSpace[1], mLightPosInModelSpace[2]); // Since we are not using a buffer object, disable vertex arrays for this attribute. GLES20.glDisableVertexAttribArray(pointPositionHandle); // Pass in the transformation matrix. Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mLightModelMatrix, 0); Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); GLES20.glUniformMatrix4fv(pointMVPMatrixHandle, 1, false, mMVPMatrix, 0); // Draw the point. GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1); }
@Override public void onSurfaceChanged(GL10 glUnused, int width, int height) { // Set the OpenGL viewport to the same size as the surface. GLES20.glViewport(0, 0, width, height); // Create a new perspective projection matrix. The height will stay the same // while the width will vary as per aspect ratio. final float ratio = (float) width / height; final float left = -ratio; final float right = ratio; final float bottom = -1.0f; final float top = 1.0f; final float near = 1.0f; final float far = 10.0f; Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); }
private void _drawBox(float x, float y, float sx, float sy, float r, float g, float b, float a, 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); //Log.v(TAG,"RATIO IS "+_ratio); float[] m2 = new float[16]; Matrix.multiplyMM(m2, 0, m, 0, mMVPMatrix, 0); if (tex == -1) mSquare.draw(m2, r, g, b, a); else mSquareTex.draw(m2,r,g,b,a,tex); checkGlError("draw"); }
public final void draw(final int tex_id, final float[] tex_matrix, final float[] mvp_matrix) { synchronized (mSync) { if (mRequestRelease) return; mTexId = tex_id; if ((tex_matrix != null) && (tex_matrix.length >= 16)) { System.arraycopy(tex_matrix, 0, mMatrix, 0, 16); } else { Matrix.setIdentityM(mMatrix, 0); } if ((mvp_matrix != null) && (mvp_matrix.length >= 16)) { System.arraycopy(mvp_matrix, 0, mMatrix, 16, 16); } else { Matrix.setIdentityM(mMatrix, 16); } mRequestDraw++; mSync.notifyAll(); /* try { mSync.wait(); } catch (final InterruptedException e) { } */ } }
@Test public void testGetTranslationMatrix() throws Exception { Bitmap3DString o = new Bitmap3DString(font, string); o.setPositionX(2.0f); PowerMockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { float[] matrix = (float[])invocation.getArguments()[0]; for(int counter = 0; counter < matrix.length; counter++) { matrix[counter] = 2.0f; } return null; } }).when(Matrix.class, "translateM", o.translationMatrix, 0, o.getPositionX(), o.getPositionY(), o.getPositionZ()); float[] expected = { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f }; assertArrayEquals(expected, o.getTranslationMatrix(), 0); }
@Override public void onSurfaceChanged(GL10 glUnused, int width, int height) { // Set the OpenGL viewport to the same size as the surface. GLES20.glViewport(0, 0, width, height); // Create a new perspective projection matrix. The height will stay the // same while the width will vary as per aspect ratio. final float ratio = (float) width / height; final float left = -ratio; final float right = ratio; final float bottom = -1.0f; final float top = 1.0f; final float near = 1.0f; final float far = 1000.0f; Matrix.frustumM(projectionMatrix, 0, left, right, bottom, top, near, far); }
@Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { Log.d(TAG, "onSurfaceCreated"); final CameraView cameraView = mCameraViewRef.get(); if (cameraView != null) { cameraView.mGLSurfaceFilter = new GLSurfaceFilter(); Matrix.setIdentityM(mMvpMatrix, 0); mTextureId = cameraView.mGLSurfaceFilter.createTexture(); cameraView.mSurfaceTexture = new SurfaceTexture(mTextureId); cameraView.mSurfaceTexture.setOnFrameAvailableListener(cameraView); if (cameraView.mCameraSurfaceListener != null) { cameraView.mCameraSurfaceListener.onCameraSurfaceCreate(cameraView.mSurfaceTexture); } } }
@Override public void onSurfaceChanged(GL10 glUnused, int width, int height) { // Set the OpenGL viewport to the same size as the surface. GLES20.glViewport(0, 0, width, height); // Create a new perspective projection matrix. The height will stay the // same // while the width will vary as per aspect ratio. final float ratio = (float) width / height; final float left = -ratio; final float right = ratio; final float bottom = -1.0f; final float top = 1.0f; final float near = 1.0f; final float far = 10.0f; Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); }
/** * Draws a point representing the position of the light. */ private void drawLight() { final int pointMVPMatrixHandle = GLES20.glGetUniformLocation(mPointProgramHandle, "u_MVPMatrix"); final int pointPositionHandle = GLES20.glGetAttribLocation(mPointProgramHandle, "a_Position"); // Pass in the position. GLES20.glVertexAttrib3f(pointPositionHandle, mLightPosInModelSpace[0], mLightPosInModelSpace[1], mLightPosInModelSpace[2]); // Since we are not using a buffer object, disable vertex arrays for this attribute. GLES20.glDisableVertexAttribArray(pointPositionHandle); // Pass in the transformation matrix. Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mLightModelMatrix, 0); Matrix.multiplyMM(mTemporaryMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); System.arraycopy(mTemporaryMatrix, 0, mMVPMatrix, 0, 16); GLES20.glUniformMatrix4fv(pointMVPMatrixHandle, 1, false, mMVPMatrix, 0); // Draw the point. GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1); }
public StereoDiveEffect(Context context) { mDpm = new DisplayMetrics(); mActivity = context; getWindowManager(context).getDefaultDisplay().getMetrics(mDpm); generateVideoVertices(); generateVideoTextures(); generateUITextures(); mCamera = new float[16]; mPersp = new float[16]; mMVP = new float[16]; mHeadTransform = new float[16]; mAngleOrigin = new float[3]; Matrix.setIdentityM(mHeadTransform, 0); Matrix.perspectiveM(mPersp, 0, EYE_FOV, 1.0f, 0.0f, -(1.5f*SCREEN_FAR)); mRightEyePos = new float[] { 0.0f, -IPD/2.0f, 0.0f }; mLeftEyePos = new float[] { 0.0f, IPD/2.0f, 0.0f }; }
@Test public void testGetScaleMatrix() throws Exception { Bitmap3DString o = new Bitmap3DString(font, string); PowerMockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); float[] matrix = (float[])args[0]; for(int counter = 0; counter < matrix.length; counter++) { matrix[counter] = 2.0f; } return null; } }).when(Matrix.class, "scaleM", o.scaleMatrix, 0, o.getScaleX(), o.getScaleY(), o.getScaleZ()); float[] expected = { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f }; assertArrayEquals(expected, o.getScaleMatrix(), 0); }
private void setupRightEye() { Matrix.setLookAtM(mCamera, 0, mRightEyePos[0], mRightEyePos[1], mRightEyePos[2], 0.0f, 0.0f, -SCREEN_FAR, 0.0f, 1.0f, 0.0f); if (HEAD_TRACKING) Matrix.rotateM(mCamera, 0, (float)mOrientation*90.0f, 0.0f, 0.0f, -1.0f); Matrix.multiplyMM(mMVP, 0, mCamera, 0, mHeadTransform, 0); Matrix.multiplyMM(mMVP, 0, mPersp, 0, mMVP, 0); GLES20.glDisable(GLES20.GL_BLEND); if (getEffectMode() == VideoEffect.ANAGLYPH_MODE) { mColorFilter = RED_COLOR_FILTER; } GLES20.glScissor(mViewWidth/2, 0, mViewWidth/2, mViewHeight); GLES20.glViewport(mViewWidth / 2, 0, mViewWidth / 2, mViewHeight); if (CHECK_GL_ERRORS) OpenGLUtils.checkGlError("glViewport"); GLES20.glVertexAttribPointer(mTexCoordHandle, 2, GLES20.GL_FLOAT, false, 0, mVideoTextureCoordRight); if (CHECK_GL_ERRORS) OpenGLUtils.checkGlError("glVertexAttribPointer"); }
/** * Get a matrix usable for zero calibration (only position and compass direction) */ public float[] getCalibrationMatrix() { float[] t = new float[3]; float[] m = new float[16]; mFrame.getPose().getTranslation(t, 0); float[] z = mFrame.getPose().getZAxis(); Vector3f zAxis = new Vector3f(z[0], z[1], z[2]); zAxis.y = 0; zAxis.normalize(); double rotate = Math.atan2(zAxis.x, zAxis.z); Matrix.setIdentityM(m, 0); Matrix.translateM(m, 0, t[0], t[1], t[2]); Matrix.rotateM(m, 0, (float) Math.toDegrees(rotate), 0, 1, 0); return m; }
/** * 在surface被创建后,每次surface尺寸变化时,这个方法都会被GLSurfaceView调用到。eg.横竖屏切换 * @param gl * @param width * @param height */ @Override public void onSurfaceChanged(GL10 gl, int width, int height) { //设置视口大小,告诉OpenGL可以用来渲染的surface的大小 GLES20.glViewport(0,0,width,height); //得到一个正交投影矩阵 float ratio; if (width>height) { ratio =(float)width / (float)height; Matrix.orthoM(projectionMatrix, 0, -ratio, ratio, -1, 1, -1, 1); }else { ratio = (float)height/(float)width; Matrix.orthoM(projectionMatrix, 0, -1, 1, -ratio, ratio, -1, 1); } }
@Override public void onSurfaceChanged(GL10 glUnused, int width, int height) { // Set the OpenGL viewport to the same size as the surface. GLES20.glViewport(0, 0, width, height); // Create a new perspective projection matrix. The height will stay the same // while the width will vary as per aspect ratio. final float ratio = (float) width / height; final float left = -ratio; final float right = ratio; final float bottom = -1.0f; final float top = 1.0f; final float near = 1.0f; final float far = 1000.0f; Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); }
public void onSurfaceChanged(GL10 unused, int width, int height) { GLES20.glViewport(0, 0, width, height); final float ratio = (float) width / height; final float left = -ratio; final float right = ratio; final float bottom = -1.0f; final float top = 1.0f; final float near = 2.0f; final float far = 10.0f; // this projection matrix is applied to object coordinates // in the onDrawFrame() method // Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7); Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); }
public void initProjectionMatrix(int width, int height) { // Set the OpenGL viewport to the same size as the surface. GLES20.glViewport(0, 0, width, height); // Create a new perspective projection matrix. // The height will stay the same. // while the width will vary as per aspect ratio. final float ratio = (float) width / height; final float left = -ratio; final float right = ratio; final float bottom = -1.0f; final float top = 1.0f; final float near = 1.0f; final float far = 10.0f; Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); }
/** * Gets the matrix that will be used to rotate this 3D char * @return float[] object */ public float[] getRotationMatrix() { Matrix.setIdentityM(this.rotationMatrix, 0); //Painful Android bug. Could be done with: //Matrix.setRotateEulerM(rotationMatrix, 0, rotation[0], rotation[1], rotation[2]); //But returns a wrong matrix on Y axis Matrix.rotateM(this.rotationMatrix, 0, this.getRotationX(), 1.0f, 0.0f, 0.0f); Matrix.rotateM(this.rotationMatrix, 0, this.getRotationY(), 0.0f, 1.0f, 0.0f); Matrix.rotateM(this.rotationMatrix, 0, this.getRotationZ(), 0.0f, 0.0f, 1.0f); return this.rotationMatrix; }
public static void gluPerspective(float[] m, float angle,float aspect,float near,float far) { float top=near*(float)Math.tan(angle*(Math.PI/360.0)); float bottom=-top; float left=bottom*aspect; float right=top*aspect; float[] frustumM=new float[16]; float[] resultM=new float[16]; Matrix.frustumM(frustumM,0,left,right,bottom,top,near,far); Matrix.multiplyMM(resultM,0,m,0,frustumM,0); System.arraycopy(resultM,0,m,0,16); }
public static void gluLookAt(float[] m, float eyeX,float eyeY,float eyeZ, float focusX,float focusY,float focusZ, float upX,float upY,float upZ) { float[] lookAtM=new float[16]; float[] resultM=new float[16]; Matrix.setLookAtM(lookAtM,0, eyeX,eyeY,eyeZ,focusX,focusY,focusZ,upX,upY,upZ); Matrix.multiplyMM(resultM,0,m,0,lookAtM,0); System.arraycopy(resultM,0,m,0,16); }
@Override public void onDrawFrame(GL10 gl10) { // 画面をglClearColorで指定した色で初期化 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT| GLES20.GL_DEPTH_BUFFER_BIT); //射影変換 Matrix.setIdentityM(GLES.pMatrix,0); GLES.gluPerspective(GLES.pMatrix, 60.0f, //Y方向の画角 aspect, //アスペクト比 0.1f, //ニアクリップ 1000.0f);//ファークリップ //光源位置の指定 GLES20.glUniform4f(GLES.lightPosHandle,0f,0f,0f,1.0f); Matrix.setIdentityM(GLES.mMatrix,0); //ビュー変換 GLES.gluLookAt(GLES.mMatrix, 0.0f,0.0f,0.0f, //カメラの視点 0.0f,0.0f,-0.01f, //カメラの焦点 0.0f,1.0f,0.0f);//カメラの上方向 // ARのターゲット描画 targetObject.draw(); // レーダー描画 raderObject.draw(); }
@Override public void onSurfaceChanged(GL10 gl, int width, int height) { mScreenWidth = width; mScreenHeight = height; spriteKit.setBoardRatio(width, height); GLES20.glViewport(0, 0, (int) mScreenWidth, (int) mScreenHeight); for (int i = 0; i < 16; i++) { mtrxProjection[i] = 0.0f; mtrxView[i] = 0.0f; mtrxProjectionAndView[i] = 0.0f; } Matrix.orthoM(mtrxProjection, 0, 0f, mScreenWidth, 0.0f, mScreenHeight, 0, 50); Matrix.setLookAtM(mtrxView, 0, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1.0f, 0.0f); Matrix.multiplyMM(mtrxProjectionAndView, 0, mtrxProjection, 0, mtrxView, 0); }
/** * 产生最终变换矩阵的方法 * @param spec * @return */ public static float[] getFinalMatrix(float[] spec){ //初始化总变换矩阵 mMVPMatrix = new float[16]; /** * Multiplies two 4x4 matrices together and stores the result in a third 4x4 * matrix. In matrix notation: result = lhs x rhs. Due to the way * matrix multiplication works, the result matrix will have the same * effect as first multiplying by the rhs matrix, then multiplying by * the lhs matrix. This is the opposite of what you might expect. * <p> * The same float array may be passed for result, lhs, and/or rhs. However, * the result element values are undefined if the result elements overlap * either the lhs or rhs elements. * * @param result The float array that holds the result. * @param resultOffset The offset into the result array where the result is * stored. * @param lhs The float array that holds the left-hand-side matrix. * @param lhsOffset The offset into the lhs array where the lhs is stored * @param rhs The float array that holds the right-hand-side matrix. * @param rhsOffset The offset into the rhs array where the rhs is stored. * * @throws IllegalArgumentException if result, lhs, or rhs are null, or if * resultOffset + 16 > result.length or lhsOffset + 16 > lhs.length or * rhsOffset + 16 > rhs.length. */ Matrix.multiplyMM(mMVPMatrix , 0 , mVMatrix , 0 , spec , 0); Matrix.multiplyMM(mMVPMatrix , 0 , mProjMatrix , 0 , mMVPMatrix , 0); return mMVPMatrix; }
/** * Updates the object model matrix and applies scaling. * * @param modelMatrix A 4x4 model-to-world transformation matrix, stored in column-major order. * @param scaleFactor A separate scaling factor to apply before the {@code modelMatrix}. * @see android.opengl.Matrix */ public void updateModelMatrix(float[] modelMatrix, float scaleFactor) { float[] scaleMatrix = new float[16]; Matrix.setIdentityM(scaleMatrix, 0); scaleMatrix[0] = scaleFactor; scaleMatrix[5] = scaleFactor; scaleMatrix[10] = scaleFactor; Matrix.multiplyMM(mModelMatrix, 0, modelMatrix, 0, scaleMatrix, 0); }
@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); }
@Override public void onSurfaceChanged(GL10 unused, int width, int height) { GLES20.glViewport(0, 0, width, height); float ratio = (float) width / height; Matrix.perspectiveM(mProjectionMatrix, 0, 90, ratio, 0.1f, 7.f); }
/** * Draws a cube. */ private void drawCube() { // Pass in the position information mCubePositions.position(0); GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, mCubePositions); GLES20.glEnableVertexAttribArray(mPositionHandle); // Pass in the color information mCubeColors.position(0); GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 0, mCubeColors); GLES20.glEnableVertexAttribArray(mColorHandle); // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix // (which currently contains model * view). Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix // (which now contains model * view * projection). Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); // Pass in the combined matrix. GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); // Draw the cube. GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36); }
public TextureRender() { mTriangleVertices = ByteBuffer.allocateDirect( mTriangleVerticesData.length * FLOAT_SIZE_BYTES) .order(ByteOrder.nativeOrder()).asFloatBuffer(); mTriangleVertices.put(mTriangleVerticesData).position(0); Matrix.setIdentityM(mSTMatrix, 0); }
private void initMatrix() { Matrix.setIdentityM(modelMatrix,0); Matrix.rotateM(modelMatrix,0,90.0f,0f,1f,0f); Matrix.setIdentityM(projectionMatrix,0); Matrix.setIdentityM(viewMatrix, 0); Matrix.setLookAtM(viewMatrix, 0, 0f,10f,10f, 0.0f, 0.0f,-1.0f, 0.0f, 1.0f, 0.0f); }
public void flip(float[] matrix, boolean flipHorizontal, boolean flipVertical) { if (matrix.length < 16) { throw new IllegalArgumentException("bad matrix length: " + matrix.length); } if (flipHorizontal && flipVertical) { Matrix.multiplyMM(mTemp, 0, mFlipHorizontal, 0, matrix, 0); Matrix.multiplyMM(matrix, 0, mFlipVertical, 0, mTemp, 0); } else if (flipHorizontal) { Matrix.multiplyMM(mTemp, 0, mFlipHorizontal, 0, matrix, 0); System.arraycopy(mTemp, 0, matrix, 0, 16); } else if (flipVertical) { Matrix.multiplyMM(mTemp, 0, mFlipVertical, 0, matrix, 0); System.arraycopy(mTemp, 0, matrix, 0, 16); } }
/** * 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; }
/** * Draws the rectangle with the supplied program and projection matrix. */ public void draw(Texture2dProgram program, float[] projectionMatrix) { // Compute model/view/projection matrix. Matrix.multiplyMM(mScratchMatrix, 0, projectionMatrix, 0, getModelViewMatrix(), 0); program.draw(mScratchMatrix, mDrawable.getVertexArray(), 0, mDrawable.getVertexCount(), mDrawable.getCoordsPerVertex(), mDrawable.getVertexStride(), GlUtil.IDENTITY_MATRIX, mDrawable.getTexCoordArray(), mTextureId, mDrawable.getTexCoordStride()); }