user1567896
user1567896

Reputation: 2398

Move Primitives (Triangles/Rectangles) with OpenGL ES 2.0

I'd like to create some kind of simple tetris clone with OpenGL ES 2.0 for education purposes. So far I managed to draw a simple rectangle made of two triangles on the screen.

I'd like to use those primitive rectangles as my tetris blocks.
Now, my problem is how to move those rectangles as they should fall down like tetris blocks.

This is how I define my rectangle:

...
public Rectangle()
{
    _vertices = new float[] 
    {
      // x, y, z
      // R, G, B, A

      -1.0f, 1.0f, 0.0f,
      1.0f, 0.0f, 0.0f, 1.0f,

      -1.0f, -1.0f, 0.0f,
      0.0f, 1.0f, 0.0f, 1.0f,

      1.0f, 1.0f, 0.0f,
      0.0f, 0.0f, 1.0f, 1.0f,

      -1.0f, -1.0f, 0.0f,
      1.0f, 0.0f, 0.0f, 1.0f,

      1.0f, -1.0f, 0.0f,
      0.0f, 1.0f, 0.0f, 1.0f,

      1.0f, 1.0f, 0.0f,
      0.0f, 0.0f, 1.0f, 1.0f
    };

    InitBuffer();
}
...

This is the code that draws the rectangle:

private void drawRectangle(final FloatBuffer aRectangleBuffer)
{
    aRectangleBuffer.position(mPositionOffset);

    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
     mStrideBytes, aRectangleBuffer);

    GLES20.glEnableVertexAttribArray(mPositionHandle);


    aRectangleBuffer.position(mColorOffset);
    GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false,
     mStrideBytes, aRectangleBuffer);

    GLES20.glEnableVertexAttribArray(mColorHandle);

    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
}

The code is basically copied from this tutorial:Learn OpenGL ES - Android Lesson One: Getting Started

The only way to move the rectangle I can think of is to change the vertices in my _vertices-array. But that would mean to create a new array, a new VertexBuffer etc. on every draw and I don't think that this is the way to go.

Perhaps this is a dump question but although I'm starting to understand how OpenGL ES works, this one I have not figured out yet.

Any help is really appreciated.

Upvotes: 0

Views: 569

Answers (1)

fadden
fadden

Reputation: 52313

There are a number of ways to handle this. Which way is best depends on your goals. Since the game you're writing is relatively undemanding GPU-wise, it makes sense to start with something simple.

In the example you cited, every vertex is being multiplied by u_MVPMatrix. If you update the matrix with the position before each draw call, you can use the same set of vertices to draw the shape anywhere on the screen (and change its scale, and rotate it, and all the other fancy stuff matrices let you do). This is the approach used by Android Breakout and parts of Grafika (see e.g. the "Hardware scaler exerciser").

If you get to the point where this approach isn't efficient enough, it's probably time to look into game engines (perhaps cocos2d-x?) rather than reinventing the texture-mapped wheel.

Upvotes: 1

Related Questions