मेरे पास एक प्रस्तुतकर्ता है जो GLSurfaceView.Renderer इंटरफेस को कार्यान्वित करता है; GLSurfaceView का एक सबक्लास और मेरी ऑब्जेक्ट्स का प्रतिनिधित्व करने वाली कुछ कक्षाएं मैं आकर्षित करना चाहता हूं। मेरे पास http://developer.android.com/training/graphics/opengl/motion.html से कोड है, मैं इसे विस्तारित करना चाहता हूं और कुछ अक्षों के साथ आगे बढ़ना चाहता हूं और इसे प्रबंधित नहीं कर सकता। वस्तु केवल घूर्णन है। और यहाँ मेरे कोड है:ओपनजीएल ईएस एंड्रॉइड मैट्रिक्स ट्रांसफॉर्मेशन
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);
}
}
मेरे उम्मीदों कि प्रत्येक वस्तु redrawing पर वध तक घुमाया और 1f द्वारा y- अक्ष traslated से भरे जाएंगे। मैं केवल घूर्णन (थोड़ा अनुमानित) देख सकता हूं हालांकि। मुझे वास्तव में इसके बारे में कुछ प्रश्न हैं: मैं अपना अनुवाद मैट्रिक्स कैसे लागू करूं और ओपनजी कार्यक्षमता को विभाजित करने का सबसे अच्छा अभ्यास क्या है? मॉडल मैट्रिक्स को प्रस्तुतकर्ता के बजाए ऑब्जेक्ट में ही संग्रहीत नहीं किया जाना चाहिए? क्या मैट्रिक्स ऑपरेशंस को रेंडरर-क्लास में किया जाना चाहिए? मैंने उन्हें एक साथ समूहीकृत किया है क्योंकि मुझे लगता है कि वे सभी संबंधित हैं।
हम्म। अभी भी यह समझाने के लिए कुछ भी नहीं है कि mProjMatrix कैसे प्रारंभ किया गया है। –
यहां एक अंतिम संकेत दिया गया है: http://learningwebgl.com/blog/?page_id=1217 से पाठों को चरणबद्ध करके चरण दोहराने का प्रयास करें। इसमें केवल जावास्क्रिप्ट शामिल है, लेकिन अंतर न्यूनतम के बगल में हैं। एपीआई वही है और मैट्रिक्स गणित वही है। सौभाग्य! –
@AkiSuihkonen एक और [थ्रेड] में (http://stackoverflow.com/questions/11925647/is-googles-android-opengl-tutorial-teaching-incorrect-linear-algebra) लोगों ने सुझाव दिया (उसी ट्यूटोरियल के उसी कोड के लिए) 'gl_Position = uMVPMatrix * vPosition;' का उपयोग करके 'gl_Position = vPosition * uMVPMatrix; '। अगर मैं सुझाव का पालन करता हूं, तो मैं अपने त्रिकोण को सभी –