Ho un renderer che implementa l'interfaccia GLSurfaceView.Renderer; una sottoclasse di GLSurfaceView e alcune classi che rappresentano i miei oggetti che voglio disegnare. Ho il codice da http://developer.android.com/training/graphics/opengl/motion.html Voglio espandere questo e aggiungere alcuni spostamenti lungo gli assi e non riesco a gestirlo. L'oggetto è solo ruotato. e qui è il mio codice:Trasformazioni della matrice Android OpenGL ES
public class NotMyCoolRenderer implements GLSurfaceView.Renderer {
public GLShip mTriangle;
private GLBackgroundStar mSquare;
private final float[] mMVPMatrix = new float[16];
private final float[] mProjMatrix = new float[16];
private final float[] mVMatrix = new float[16];
private final float[] mModelMatrix = new float[16];
private final float[] tempMatrix = new float[16];
public void onDrawFrame(GL10 unused) {
// Draw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
// Set the camera position (View matrix)
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
// Draw square
mSquare.draw(mMVPMatrix);
// Now moving on to triangle aka ship
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, 0.1f, 0f, 0);
Matrix.rotateM(mModelMatrix, 0, mTriangle.mAngle, 0, 0, -1.0f);
Matrix.multiplyMM(tempMatrix, 0, mVMatrix, 0, mProjMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mModelMatrix , 0, tempMatrix , 0);
// Draw triangle
mTriangle.draw(mMVPMatrix);
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
// Adjust the viewport based on geometry changes,
// such as screen rotation
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width/height;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
public class GLShip {
public volatile float mAngle;
private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
// the matrix must be included as a modifier of gl_Position
" gl_Position = uMVPMatrix * vPosition;" +
"}";
public void draw(float[] mvpMatrix) {
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
NotMyCoolRenderer.checkGlError("glGetUniformLocation");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
NotMyCoolRenderer.checkGlError("glUniformMatrix4fv");
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
Le mie aspettative sono che su ogni ridisegnare l'oggetto sarebbe essere ruotata di storpiare e traslare lungo l'asse Y da 1f. Posso vedere solo ruotando (anche se un po 'troppo). Ho effettivamente ricevuto alcune domande a riguardo: come applicare la mia matrice di traduzione e qual è la migliore pratica per dividere la funzionalità di opengl? Non dovrebbe il modelMatrix essere memorizzato nell'oggetto stesso piuttosto che nel renderer? Le operazioni della matrice dovrebbero essere eseguite nella classe di rendering? Li ho raggruppati insieme perché immagino che siano tutti correlati.
Hmm. ancora nulla per spiegare come mProjMatrix è inizializzato. –
Ecco un suggerimento finale: prova a replicare passo dopo passo le lezioni da http://learningwebgl.com/blog/?page_id=1217. Copre solo javascript, ma le differenze sono prossime al minimo. L'API è la stessa e la matematica a matrice è la stessa. In bocca al lupo! –
@AkiSuihkonen In un altro [thread] (http://stackoverflow.com/questions/11925647/is-googles-android-opengl-tutorial-teaching-incorrect-linear-algebra) persone suggerite (per lo stesso codice dello stesso tutorial) usando 'gl_Position = uMVPMatrix * vPosition;' invece di 'gl_Position = vPosition * uMVPMatrix;'. Se seguo il suggerimento, non vedo affatto il mio triangolo –