// @Override public void orbit(float horizAmount, float vertAmount) { xRotation += horizAmount; yRotation += vertAmount; Log.d("XROT", "X rotation: " + xRotation); Log.d("YROT", "Y rotation: " + yRotation); float[] newPos = Utilities.subtractVectors(position, target); float[] rotMatrix = new float[16]; float[] rotatedPos = new float[4]; if (newPos.length >= 4) newPos[3] = 1.0f; Matrix.setRotateM(rotMatrix, 0, horizAmount, 0.0f, 1.0f, 0.0f); Matrix.multiplyMV(rotatedPos, 0, rotMatrix, 0, newPos, 0); Matrix.multiplyMV(crossProd, 0, rotMatrix, 0, crossProd, 0); Matrix.multiplyMM(rotationMatrix, 0, rotMatrix, 0, rotationMatrix, 0); Matrix.setRotateM(rotMatrix, 0, vertAmount, crossProd[0], crossProd[1], crossProd[2]); Matrix.multiplyMV(rotatedPos, 0, rotMatrix, 0, rotatedPos, 0); Matrix.multiplyMM(rotationMatrix, 0, rotMatrix, 0, rotationMatrix, 0); position = Utilities.addVectors(rotatedPos, target); direction = Utilities.subtractVectors(target, position); }
@Override public void onDrawEye(Eye eye) { if (resetCameraFlag) { float[] invertedEye = new float[16]; Matrix.invertM(invertedEye, 0, eye.getEyeView(), 0); setUpCamera(); Matrix.multiplyMM(mCameraMatrix, 0, invertedEye, 0, mCameraMatrix, 0); resetCameraFlag = false; } GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); if (lockCameraFlag) { mViewMatrix = mCameraMatrix.clone(); } else { Matrix.multiplyMM(mViewMatrix, 0, eye.getEyeView(), 0, mCameraMatrix, 0); } Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0); Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0); mProjectionMatrix = eye.getPerspective(Z_NEAR, Z_FAR); GLES20.glUseProgram(mProgramHandle); currentContent.draw(eye); if (drawRedPoint) { drawTargetingPoint(); } Util.checkGLError("Error Draw Eye"); }
@Override public void onDrawFrame(GL10 gl) { super.onDrawFrame(gl); // GL Draw code onwards GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); // Do a complete rotation every 10 seconds. long time = SystemClock.uptimeMillis() % 10000L; float angleInDegrees = (360.0f / 10000.0f) * ((int) time); // Set our per-vertex lighting program. GLES20.glUseProgram(mProgramHandle); // Set program handles for cube drawing. mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix"); mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix"); mLightPosHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_LightPos"); mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture"); mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position"); mColorHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Color"); mNormalHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Normal"); mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate"); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, getGLSurfaceTexture()); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glUniform1i(mTextureUniformHandle, 0); // GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0); // GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, glSurfaceTex); // GLES20.glActiveTexture(GLES20.GL_TEXTURE0); // // Set the active texture unit to texture unit 0. // GLES20.glActiveTexture(GLES20.GL_TEXTURE0); // // // Bind the texture to this unit. // GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, glSurfaceTex); // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit // 0. // GLES20.glUniform1i(mTextureUniformHandle, 0); // Calculate position of the light. Rotate and then push into the distance. Matrix.setIdentityM(mLightModelMatrix, 0); Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, -4.0f); Matrix.rotateM(mLightModelMatrix, 0, angleInDegrees, 0.0f, 1.0f, 0.0f); Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, 2.0f); Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0); Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0); Matrix.setIdentityM(mModelMatrix, 0); Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -3.3f); Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 1.0f, 0.0f); drawCube(); // Draw a point to indicate the light. GLES20.glUseProgram(mPointProgramHandle); drawLight(); }
/** * Example. An object at position 5,5,5 is rotated by an rotation matrix (set via {@link * MeshComponent#setRotationMatrix(float[])} and we want to know there the point 0,0,1 (which * normaly without rotation would be at 5,5,6 ) is now. then we can use this method and pass 0,0,1 * and we will get the correct world coordinates * * @param modelSpaceCoords * @return the coordinates in the world system */ public Vec getWorldCoordsFromModelSpacePosition(Vec modelSpaceCoords) { float[] resultVec = new float[3]; float[] modelSpaceCoordsVec = {modelSpaceCoords.x, modelSpaceCoords.y, modelSpaceCoords.z}; Matrix.multiplyMV(resultVec, 0, markerRotationMatrix, 0, modelSpaceCoordsVec, 0); return new Vec( resultVec[0] + myPosition.x, resultVec[1] + myPosition.y, resultVec[2] + myPosition.z); }
public Vector3 getDirection() { Matrix.setIdentityM(matrix, 0); Matrix.rotateM(matrix, 0, yaw, 0, 1, 0); Matrix.rotateM(matrix, 0, pitch, 1, 0, 0); Matrix.multiplyMV(outVex, 0, matrix, 0, inVex, 0); direction.set(outVex[0], outVex[1], outVex[2]); return direction; }
public void updateEyeSpacePosition() { Matrix.multiplyMV( eyeSpacePosition, 0, GameState.getInstance().getCamera("MainCam").getViewMatrix(), 0, pos, 0); }
@Override public void onDrawEye(Eye eyeTransform) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); checkGLError("colorParam"); GLES20.glUseProgram(mProgram); GLES20.glDrawElements( GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer); checkGLError("befor activeTex"); GLES20.glActiveTexture(GLES20.GL_TEXTURE1); checkGLError("after activeTex"); GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture); checkGLError("bind Text"); checkGLError("mPostionHandle"); mPositionHandle = GLES20.glGetAttribLocation(mProgram, "position"); GLES20.glEnableVertexAttribArray(mPositionHandle); GLES20.glVertexAttribPointer( mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); checkGLError("mTexture Handle"); mTextureCoordHandle = GLES20.glGetAttribLocation(mProgram, "inputTextureCoordinate"); GLES20.glEnableVertexAttribArray(mTextureCoordHandle); GLES20.glVertexAttribPointer( mTextureCoordHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, textureVerticesBuffer); checkGLError("after mTexture Handle"); mColorHandle = GLES20.glGetAttribLocation(mProgram, "s_texture"); checkGLError("mColor Handel"); GLES20.glDrawElements( GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer); checkGLError("Disable"); GLES20.glDisableVertexAttribArray(mPositionHandle); GLES20.glDisableVertexAttribArray(mTextureCoordHandle); checkGLError("before Cube 2"); Matrix.multiplyMM(mView, 0, eyeTransform.getEyeView(), 0, mCamera, 0); Matrix.multiplyMV(lightPosInEyeSpace, 0, mView, 0, LIGHT_POS_IN_WORLD_SPACE, 0); checkGLError("before Cube"); float[] perspective = eyeTransform.getPerspective(Z_NEAR, Z_FAR); Matrix.multiplyMM(modelView, 0, mView, 0, mModelCube, 0); Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0); drawCube(); }
// XXX Find out whether or not using Matrix operations is better than a home-grown solution. // Perhaps run a test to see how long matrix ops really take. // TODO: Try to multiply multiple points in one M x V operation in Primitive. // This function belongs in Element, not Point. // XXX This rotates about the origin. public Float3 rotate(float a, float x, float y, float z) { float[] v = new float[] {this.x, this.y, this.z, 1}; float[] m = new float[16]; Matrix.setIdentityM(m, 0); Matrix.rotateM(m, 0, a, x, y, z); Matrix.multiplyMV(v, 0, m, 0, v, 0); return new Float3(v[0], v[1], v[2]); }
/** * Calculates a pick ray based on the given screen coordinates and the current projection matrix * and model view matrix. * * <p>The screen coordinates are expected to have (0,0) at the mLocalTranslationper left corner of * the screen and the y-axis is reversed compared to the OpenGL y-axis. * * @param pickX screen x coordinate * @param pickY screen y coordinate */ public Ray calculatePickRay(float pickX, float pickY) { // coordinates centered on the screen // -1 <= x <= 1 and -1 <= y <= 1 float unit_x = (pickX - mHalfWidth) / mHalfWidth; float unit_y = ((mHeight - pickY) - mHalfHeight) / mHalfHeight; float[] rayRawPos = {0.0f, 0.0f, 0.0f, 1.0f}; float[] rayRawDir = {unit_x * mNearHeight * mAspect, unit_y * mNearHeight, -mZNear, 0.0f}; float[] rayPos = new float[4]; float[] rayDir = new float[4]; // multiply the position and vector with the inverse model matrix // to get world coordinates synchronized (mViewMatrix) { Matrix.invertM(mInvModelMatrix, 0, mViewMatrix, 0); } Matrix.multiplyMV(rayPos, 0, mInvModelMatrix, 0, rayRawPos, 0); Matrix.multiplyMV(rayDir, 0, mInvModelMatrix, 0, rayRawDir, 0); return new Ray(rayPos[0], rayPos[1], rayPos[2], rayDir[0], rayDir[1], rayDir[2]); }
private NodeOrderUnit[] buildDrawOrder(float[] conversionMatrix) { NodeOrderUnit[] drawOrder = new NodeOrderUnit[nodes.length]; float[] tempPos = new float[4]; for (int i = 0; i < nodes.length; i++) { Matrix.multiplyMV(tempPos, 0, conversionMatrix, 0, nodes[i].getPos4f(), 0); drawOrder[i] = new NodeOrderUnit(i, tempPos[2]); } Arrays.sort(drawOrder); return drawOrder; }
// different version of glu.glunproject public static Vector3d unProject( float winx, float winy, float winz, float[] resultantMatrix, float width, float height) { winy = height - winy; float[] m = new float[16], in = new float[4], out = new float[4]; Matrix.invertM(m, 0, resultantMatrix, 0); in[0] = (winx / width) * 2 - 1; in[1] = (winy / height) * 2 - 1; in[2] = 2 * winz - 1; in[3] = 1; Matrix.multiplyMV(out, 0, m, 0, in, 0); if (out[3] == 0) return null; out[3] = 1 / out[3]; return new Vector3d(out[0] * out[3], out[1] * out[3], out[2] * out[3]); }
public void GetWorldCoords(float[] worldPos, float touchX, float touchY, float z) { // SCREEN height & width (ej: 320 x 480) float screenW = mViewPort[2]; float screenH = mViewPort[3]; // Invert y coordinate, as android uses // top-left, and ogl bottom-left. int oglTouchY = (int) (screenH - touchY); /* * Transform the screen point to clip space in ogl (-1,1) */ normalizedInPoint[0] = (float) (touchX * 2.0f / screenW - 1.0); normalizedInPoint[1] = (float) (oglTouchY * 2.0f / screenH - 1.0); normalizedInPoint[2] = z; normalizedInPoint[3] = 1.0f; /* * Obtain the transform matrix and then the inverse. */ // MatrixUtils.PrintMat("Proj", mProjection); // MatrixUtils.PrintMat("Model", mModelView); Matrix.multiplyMM(transformMatrix, 0, mProjection, 0, mModelView, 0); Matrix.invertM(invertedMatrix, 0, transformMatrix, 0); /* * Apply the inverse to the point in clip space */ Matrix.multiplyMV(outPoint, 0, invertedMatrix, 0, normalizedInPoint, 0); if (outPoint[3] == 0.0) { // Avoid /0 error. Log.e("World coords", "ERROR!"); return; } // Divide by the 3rd component to find // out the real position. worldPos[0] = outPoint[0] / outPoint[3]; worldPos[1] = outPoint[1] / outPoint[3]; worldPos[2] = outPoint[2] / outPoint[3]; }
public void apply(float[] mCoordsIn, float[] mCoordsOut) { Matrix.multiplyMV(mCoordsOut, 0, mMatrix, mTop, mCoordsIn, 0); }
/** * Calculates view position for light. * * @param viewM View matrix. */ public void updateMatrices(float[] viewM) { Matrix.multiplyMV(mViewPos, 0, viewM, 0, mPosition, 0); for (int i = 0; i < 3; ++i) { mViewPos[i] /= mViewPos[3]; } }
/* Modifications return new objects */ public Float3 transform(float[] transformation) { float[] v = new float[] {this.x, this.y, this.z, 1}; Matrix.multiplyMV(v, 0, transformation, 0, v, 0); return new Float3(v[0], v[1], v[2]); }