android.opengl.Matrix Java Examples

The following examples show how to use android.opengl.Matrix. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelRenderer.java    From react-native-3d-model-view with MIT License 6 votes vote down vote up
@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);
}
 
Example #2
Source File: GLES20Canvas.java    From android-openGL-canvas with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #3
Source File: CN1Matrix4f.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
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);
   

}
 
Example #4
Source File: GLSurface.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: GLRenderer.java    From bombsquad-remote-android with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example #6
Source File: Math3DUtils.java    From react-native-3d-model-view with MIT License 6 votes vote down vote up
/**
 * 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};
}
 
Example #7
Source File: GLTexture.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
	 * 指定したビットマップをテクスチャに読み込む
 	 * @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();
	}
 
Example #8
Source File: GLES20Canvas.java    From LB-Launcher with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #9
Source File: Object3DData.java    From android-3D-model-viewer with GNU Lesser General Public License v3.0 6 votes vote down vote up
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);
       }
}
 
Example #10
Source File: SurfaceTextureRenderer.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #11
Source File: Trajectory.java    From ParaViewTangoRecorder with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #12
Source File: GLDrawer.java    From Building-Android-UIs-with-Custom-Views with MIT License 6 votes vote down vote up
@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");
    }
}
 
Example #13
Source File: DisplayTransformManager.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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];
}
 
Example #14
Source File: PointCloudRenderer.java    From react-native-arcore with MIT License 6 votes vote down vote up
/**
 * 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");
  }
 
Example #15
Source File: DetailFilterActivity.java    From GSYVideoPlayer with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #16
Source File: EPlayerRenderer.java    From SimpleVideoEdit with Apache License 2.0 5 votes vote down vote up
EPlayerRenderer(EPlayerView glPreview) {
    super();
    //设置stmatrix为单位矩阵
    //stmatrix stand for scale and translation缩放和平移
    Matrix.setIdentityM(STMatrix, 0);
    this.glPreview = glPreview;
}
 
Example #17
Source File: TextureManager.java    From spydroid-ipcamera with GNU General Public License v3.0 5 votes vote down vote up
public TextureManager() {
	mTriangleVertices = ByteBuffer.allocateDirect(
			mTriangleVerticesData.length * FLOAT_SIZE_BYTES)
			.order(ByteOrder.nativeOrder()).asFloatBuffer();
	mTriangleVertices.put(mTriangleVerticesData).position(0);

	Matrix.setIdentityM(mSTMatrix, 0);
}
 
Example #18
Source File: NoFilterRender.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
public NoFilterRender() {
  squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.length * FLOAT_SIZE_BYTES)
      .order(ByteOrder.nativeOrder())
      .asFloatBuffer();
  squareVertex.put(squareVertexDataFilter).position(0);
  Matrix.setIdentityM(MVPMatrix, 0);
  Matrix.setIdentityM(STMatrix, 0);
}
 
Example #19
Source File: CardboardMotionStrategy.java    From MD360Player4Android with Apache License 2.0 5 votes vote down vote up
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    if (getParams().mSensorListener != null){
        getParams().mSensorListener.onAccuracyChanged(sensor,accuracy);
    }

    synchronized (matrixLock){
        Matrix.setIdentityM(mTmpMatrix, 0);
        headTracker.getLastHeadView(mTmpMatrix, 0);
    }

    getParams().glHandler.post(updateSensorRunnable);
}
 
Example #20
Source File: Sprite2d.java    From AndroidPlayground with MIT License 5 votes vote down vote up
/**
 * Draws the rectangle with the supplied program and projection matrix.
 */
public void draw(FlatShadedProgram program, float[] projectionMatrix) {
    // Compute model/view/projection matrix.
    Matrix.multiplyMM(mScratchMatrix, 0, projectionMatrix, 0, getModelViewMatrix(), 0);

    program.draw(mScratchMatrix, mColor, mDrawable.getVertexArray(), 0,
            mDrawable.getVertexCount(), mDrawable.getCoordsPerVertex(),
            mDrawable.getVertexStride());
}
 
Example #21
Source File: CartoonFilterRender.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
public CartoonFilterRender() {
  squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.length * FLOAT_SIZE_BYTES)
      .order(ByteOrder.nativeOrder())
      .asFloatBuffer();
  squareVertex.put(squareVertexDataFilter).position(0);
  Matrix.setIdentityM(MVPMatrix, 0);
  Matrix.setIdentityM(STMatrix, 0);
}
 
Example #22
Source File: ImmersiveSensorNavigation.java    From Spectaculum with Apache License 2.0 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    if(mEffect != null && mActive) {
        // TODO understand those sensor coordinate spaces
        // TODO find out how the sensor rotation can be mapped to the sphere shader correctly
        // TODO should we store the initial rotation value to set the zero rotation point to the current phone rotation?

        // Get the rotation matrix from the sensor
        SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);

        // When the first sensor data comes in, we set the initial rotation matrix as
        // "zero rotation point" to be able to calculate the relative rotation from the initial
        // device rotation, instead of the absolute rotation from true north.
        // Later, we subtract the initial rotation from the rotation matrix to get the relative rotation
        if(mInitialRotationMatrix == null) {
            mInitialRotationMatrix = new float[16];
            // Matrix subtraction works by multiplying the inverse (Mb - Ma == inv(Ma) * Mb),
            // so we directly store the inverse
            Matrix.invertM(mInitialRotationMatrix, 0, mRotationMatrix, 0);
        }

        // Remove initial rotation
        Matrix.multiplyMM(mRotationMatrix, 0, mInitialRotationMatrix, 0, mRotationMatrix, 0);

        // Some axes seem like they need to be exchanged
        Matrix.invertM(mRemappedRotationMatrix, 0, mRotationMatrix, 0);
        // FIXME this does not seem to remap axes at all!?
        //SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, mRemappedRotationMatrix);

        // Debug output
        //float[] orientation = new float[3];
        //SensorManager.getOrientation(mRemappedRotationMatrix, orientation);
        //debugOutputOrientationInDegree(orientation);

        // Update effect and thus the viewport too
        mEffect.setRotationMatrix(mRemappedRotationMatrix);
    }
}
 
Example #23
Source File: OrientationHelper.java    From Pano360 with MIT License 5 votes vote down vote up
public void recordRotation(float []rotationMatrix){
    // we need to Transpose it to match model
    // or just rotate in the reverse orientation
    // and reversed order
    if (!rotationRecorded){
        Matrix.transposeM(tmp,0,rotationMatrix,0);
        SensorUtils.getOrientationFromRotationMatrix(tmp,initialRotation);
        convertToDegrees(initialRotation);
        rotationRecorded=true;
    }else{
        Matrix.transposeM(tmp,0,rotationMatrix,0);
        SensorUtils.getOrientationFromRotationMatrix(tmp,currentRotation);
        convertToDegrees(currentRotation);
    }
}
 
Example #24
Source File: BezierRenderer.java    From Muzesto with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
    GLES20.glDisable(GLES20.GL_CULL_FACE);

    // Position the eye in front of the origin.
    final float eyeX = 0.0f;
    final float eyeY = 0.0f;
    final float eyeZ = 0.0f;

    // We are looking toward the distance
    final float lookX = 0.0f;
    final float lookY = 0.0f;
    final float lookZ = 1.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;

    Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);

    final String vertexShader = RawResourceReader.readTextFileFromRawResource(mGlSurfaceView.getContext(), R.raw.bz_vert);
    final String fragmentShader = RawResourceReader.readTextFileFromRawResource(mGlSurfaceView.getContext(), R.raw.bz_frag);

    final int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
    final int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);

    programHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
            new String[]{"a_BzData", "a_BzDataCtrl", "a_TData"});

    // Initialize the accumulated rotation matrix
    Matrix.setIdentityM(mAccumulatedRotation, 0);
}
 
Example #25
Source File: myRenderer.java    From opengl with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawFrame(GL10 glUnused) {
    // Clear the color buffer  set above by glClearColor.
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT);

    //need this otherwise, it will over right stuff and the cube will look wrong!
    GLES30.glEnable(GLES30.GL_DEPTH_TEST);

    // Set the camera position (View matrix)  note Matrix is an include, not a declared method.
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Create a rotation and translation for the cube
    Matrix.setIdentityM(mRotationMatrix, 0);

    //move the cube up/down and left/right
    Matrix.translateM(mRotationMatrix, 0, mTransX, mTransY, 0);

    //mangle is how fast, x,y,z which directions it rotates.
    Matrix.rotateM(mRotationMatrix, 0, mAngle, 0.4f, 1.0f, 0.6f);

    // combine the model with the view matrix
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mRotationMatrix, 0);

    // combine the model-view with the projection matrix
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

    mPyramid.draw(mMVPMatrix);

    //change the angle, so the cube will spin.
    mAngle+=.4;
}
 
Example #26
Source File: GLRenderable.java    From Tanks with MIT License 5 votes vote down vote up
private void setModelMatrix(TData data)
{
  if (prevData != null && prevData.equals(data))
    return;
  else
  {
    prevData = new Data();
    prevData.setFrom(data);
  }

  // reset matrix
  Matrix.setIdentityM(modelMatrix, 0);

  Vector3 localPosition = data.localPosition.getSum(getLocalPosition());
  Vector3 localAngles = data.localAngles.getSum(getLocalAngles());

  // set global
  Matrix.translateM(modelMatrix, 0, data.position.getX(), data.position.getY(), data.position.getZ());
  Matrix.rotateM(modelMatrix, 0, data.angles.getX(), 1, 0, 0);
  Matrix.rotateM(modelMatrix, 0, data.angles.getY(), 0, 1, 0);
  Matrix.rotateM(modelMatrix, 0, data.angles.getZ(), 0, 0, 1);

  // set local
  Matrix.translateM(modelMatrix, 0, localPosition.getX(), localPosition.getY(), localPosition.getZ());
  Matrix.rotateM(modelMatrix, 0, localAngles.getX(), 1, 0, 0);
  Matrix.rotateM(modelMatrix, 0, localAngles.getY(), 0, 1, 0);
  Matrix.rotateM(modelMatrix, 0, localAngles.getZ(), 0, 0, 1);

  // set scale
  Matrix.scaleM(modelMatrix, 0, data.scale, data.scale, data.scale);

  Vector3.release(localPosition);
  Vector3.release(localAngles);
}
 
Example #27
Source File: MainActivity.java    From Android-9-Development-Cookbook with MIT License 5 votes vote down vote up
public void onDrawFrame(GL10 unused) {
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f,
            1.0f, 0.0f);    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0,
            mViewMatrix, 0);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    mTriangle.draw(mMVPMatrix);
}
 
Example #28
Source File: Image70sFilterRender.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
public Image70sFilterRender() {
  squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.length * FLOAT_SIZE_BYTES)
      .order(ByteOrder.nativeOrder())
      .asFloatBuffer();
  squareVertex.put(squareVertexDataFilter).position(0);
  Matrix.setIdentityM(MVPMatrix, 0);
  Matrix.setIdentityM(STMatrix, 0);
}
 
Example #29
Source File: GLES20Canvas.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
@Override
public void rotate(float angle, float x, float y, float z) {
    if (angle == 0f) {
        return;
    }
    float[] temp = mTempMatrix;
    Matrix.setRotateM(temp, 0, angle, x, y, z);
    float[] matrix = mMatrices;
    int index = mCurrentMatrixIndex;
    Matrix.multiplyMM(temp, MATRIX_SIZE, matrix, index, temp, 0);
    System.arraycopy(temp, MATRIX_SIZE, matrix, index, MATRIX_SIZE);
}
 
Example #30
Source File: Sprite2d.java    From AndroidPlayground with MIT License 5 votes vote down vote up
/**
 * 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;
}