android - Using Matrix. Rotate in OpenGL ES 2.0 -


edit - added more code

having lot of problems attempting correctly rotate quad using opengl es 2.0.

it seems rotate around centre of screen co-ordinates. i'm trying rotate around it's own centre (for 2d, z axis only).

i've been experimenting matrix.translate show below. however, changing x or y pos here draws quad @ different place on screen, when rotates, again rotates around centre of screen. please explain how spin around it's own z axis (like wheel)?

thanks, here relevant lines of code - if more needed, please ask , post. (please note, i've looked @ lot of similar questions on , wider internet i've not managed find answer far).

thanks.

//set rotation matrix.setrotatem(mrotationmatrix, 0, -angle, 0, 0, 1.0f);  //testing translation matrix.translatem(mrotationmatrix, 0, -.5f, .5f, 0f);  // combine rotation matrix projection , camera view matrix.multiplymm(mvpmatrix, 0, mrotationmatrix, 0, mvpmatrix, 0); 

my shaders (declared @ class level)

private final string vertexshadercode = "uniform mat4 umvpmatrix;" +  "attribute vec4 vposition;" + "void main() {" + " gl_position = umvpmatrix * vposition;" + "}";  private final string fragmentshadercode = "precision mediump float;" + "uniform vec4 vcolor;" + "void main() {" + " gl_fragcolor = vcolor;" + "}"; 

from onsurfacechanged

float ratio = (float) width / height; matrix.frustumm(mprojmatrix, 0, -ratio, ratio, -1, 1, 3, 7); 

in ondrawframe method

// set camera position (view matrix) matrix.setlookatm(mvmatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);  //calculate projection , view transformation matrix.multiplymm(mmvpmatrix, 0, mprojmatrix, 0, mvmatrix, 0); 

i´ve encountered same problems (seen weird distortions , else), solution based on android training > displaying graphics opengl es > adding motion below.

(head on detailed post @ if needed: opengl es android matrix transformations.)

  1. set mmodelmatrix identity matrix

    matrix.setidentitym(mmodelmatrix, 0); // initialize identity matrix 
  2. apply translation mmodelmatrix

    matrix.translatem(mmodelmatrix, 0, -0.5f, 0, 0); // translation left 
  3. apply rotation mrotationmatrix (angles in degrees)

    matrix.setrotatem(mrotationmatrix, 0, mangle, 0, 0, -1.0f); 
  4. combine rotation , translation via matrix.multiplymm

    mtempmatrix = mmodelmatrix.clone(); matrix.multiplymm(mmodelmatrix, 0, mtempmatrix, 0, mrotationmatrix, 0); 
  5. combine model matrix projection , camera view

    mtempmatrix = mmvpmatrix.clone(); matrix.multiplymm(mmvpmatrix, 0, mtempmatrix, 0, mmodelmatrix, 0); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -