0

I can use the below code to scale and translate a square using OpenGLES. But I'm not sure how to calculate the translation and scale factors. For example using the below picture of the OpenGL coordinate system and Matrix.translateM(mViewMatrix, 0, .5f, 0, 0); I would expect the square to be drawn halfway to the right of the screen, but instead it's drawn halfway to the left from the center. However Matrix.translateM(mViewMatrix, 0, 0, .5f, 0); does translate the square halfway up the screen from the center.

How would I translate and scale in order to programmatically draw N squares side by side horizontally filling the top of the screen?

OpenGLES coordinate system

@Override
public void onDrawFrame(GL10 unused) {

    // Draw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Translate by some amount
    // Matrix.translateM(mViewMatrix, 0, ?, ?, 0);

    // Scale by some amount
    // Matrix.scaleM(mViewMatrix, 0, ?, ?, 1);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

    // Draw square
    mSquare.draw(mMVPMatrix);
}
Community
  • 1
  • 1
GDanger
  • 1,641
  • 2
  • 22
  • 35
  • 1
    In eye space, +Z moves towards the viewer. You have eyeZ as -3 so you are actually on the other side of the viewing plane looking back. That is why your x axis translation appears to move in the wrong direction but the y translation does not. Have a look at http://stackoverflow.com/questions/4124041/is-opengl-coordinate-system-left-handed-or-right-handed. Yes, it can be somewhat confusing... – NigelK Dec 23 '13 at 21:44
  • Ahh, that explains that part of the question. Thank you. – GDanger Dec 23 '13 at 21:46

1 Answers1

0

I am not sure why you would need translate and scale to fill up a row of squares. To get rows of squares programmatically in openGL ES I would just make a bunch of squares initialized where you want them. An edited snippet from one of my projects went something like this:

public void onSurfaceCreated(GL10 unused, EGLConfig config){
   GLES20.glClearColor(bgr, bgg, bgb, bga);
   float z=0.0f;
   float y=0.0f;
   for(float i=(-worldwidth);i<worldwidth;i+=(cellwidth)){
      square=new Square(i,y,z);     
      cellvec.addElement(square);
   }
}
public void onDrawFrame(GL10 unused) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
    for(int i=0;i<cellvec.size();i++){
        cellvec.elementAt(i).draw(mMVPMatrix);
    }
}

I am not entirely sure if something like this is what your looking for but it it seems to get the result of a row of squares like you wanted.

ebeilmann
  • 171
  • 2
  • 6