0
votes

After two days of bashing my head and trying to figure this stuff out I have been woefully unsuccessful, hopefully someone can point me in the right direction.

I am trying to make a tile based game in GLES 2.0, but I cant get anything to show up the way I want. Basically I have an array of vertices that make up pairs of triangles that would form a square grid. I want to use GLES20.glDrawArrays() to draw subsections of this grid at a time.

I have figured out how to "view" from a different perspective using a combination of Matrix.orthoM() and Matrix.setLookAtM() but for the life of me I can figure out how to have my triangles not fill the entire screen.

I really need some guidance on setting up a projection so that if the triangle is defined as (0,0,0) (0,20,0) (20,0,0) it shows up on the screen as 20 pixels wide and 20 pixels tall, translated by my current view.

Here is what I have currently, but it just fills my entire screen with green. If someone could show me the correct way to manipulate the scene so that it fills the camera, or the camera only shows like 20 triangles wide by 10 triangle high that would make my week.

When the surface changes:

GLES20.glViewport(0, 0, ScreenX, ScreenY);
float ratio = ScreenX / ScreenY;
Matrix.orthoM(_ProjMatrix, 0, 
    -ratio, 
    ratio, 
    -1, 1, 
    3, 7);
Matrix.setLookAtM(_VMatrix, 0, 
    60, 60, 7, 
    60, 60, 0, 
    0, 1, 0);

Beginning drawing:

GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
Matrix.multiplyMM(_MVPMatrix, 0, _ProjMatrix, 0, _VMatrix, 0);
if (_activeMap != null)
  _activeMap.draw(0, 0, (int)ScreenX, (int)ScreenY, _MVPMatrix);

The draw function:

public void draw(int x, int y, int width, int height, float[] MVPMatrix)
{
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glUseProgram(_pHandle);
    GLES20.glUniformMatrix4fv(_uMVPMatrixHandle, 1, false, MVPMatrix, 0);

    int minRow, minCol, maxRow, maxCol;
    minRow = (int) (y / Engine.TileSize);
    minCol = (int) (x / Engine.TileSize);
    maxRow = (int) (minRow + (height / Engine.TileSize));
    maxCol = (int) (minCol + (width / Engine.TileSize));
    minRow = (minRow < 0) ? 0 : minRow;
    minCol = (minCol < 0) ? 0 : minCol;
    maxRow = (maxRow > _rows) ? (int)_rows : maxRow;
    maxCol = (maxCol > _cols) ? (int)_cols : maxCol;

    for (int r = minRow; r < maxRow - 1; r++)
      for (int d = 0; d < _vBuffers.length; d++)
      {
        _vBuffers[d].position(0);
        GLES20.glVertexAttribPointer(_vAttHandle, 3, GLES20.GL_FLOAT, 
            false, 
            0, _vBuffers[d]);               
        GLES20.glEnableVertexAttribArray(_vAttHandle);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 
            (int) (r * 6 * _cols), 
            (maxCol - minCol) * 6);
      }
}

Shader script:

private static final String _VERT_SHADER =
  "uniform mat4 uMVPMatrix;                  \n"
+ "attribute vec4 vPosition;                 \n"
+ "void main()                               \n"
+ "{                                         \n"
+ "   gl_Position = uMVPMatrix * vPosition;  \n"
+ "}                                         \n";

private static final String _FRAG_SHADER = 
  "precision mediump float;                            \n"
+ "void main()                                         \n"
+ "{                                                   \n"
+ "    gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n"
+ "}                                                   \n";
2

2 Answers

1
votes

For a tile based game a simple translate would be far more appropriate, setLookAt is just overkill.

0
votes

I hope it may help you. Check this link for OpenGL programming.

OpenGL Programming Guide

Go to Chapter 3.Viewing here you can find information about projection.