1. Are you ready for the Galaxy S20? Here is everything we know so far!

Apps OpenGL Projection and Camera not working

Discussion in 'Android Development' started by Gravity Games, Aug 1, 2013.

  1. Gravity Games

    Gravity Games Newbie
    Thread Starter

    I've been having trouble using cameras and projection in OpenGL. I've followed the tutorial on the android site EXACTLY and it still won't properly apply (the screen still stretches). I've searched Google for about an hour and wasn't able to find any info besides that it MIGHT be with Google's vertex shader code.

    My Square:
    Code (Text):
    1. package com.gravitygamesinteractive.shiftengine;
    2.  
    3. import java.nio.ByteBuffer;
    4. import java.nio.ByteOrder;
    5. import java.nio.FloatBuffer;
    6. import java.nio.ShortBuffer;
    7.  
    8. import android.opengl.GLES20;
    9.  
    10. public class Square {
    11.  
    12.     private FloatBuffer vertexBuffer;
    13.     private ShortBuffer drawListBuffer;
    14.  
    15.     // number of coordinates per vertex in this array
    16.     static final int COORDS_PER_VERTEX = 3;
    17.     static float squareCoords[] = { -0.5f,  0.5f, 0.0f,   // top left
    18.                                     -0.5f, -0.5f, 0.0f,   // bottom left
    19.                                      0.5f, -0.5f, 0.0f,   // bottom right
    20.                                      0.5f,  0.5f, 0.0f }; // top right
    21.  
    22.     private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices
    23.  
    24.     private final String vertexShaderCode =
    25.             "attribute vec4 vPosition;" +
    26.             "void main() {" +
    27.             "  gl_Position = vPosition;" +
    28.             "}";
    29.  
    30.         private final String fragmentShaderCode =
    31.             "precision mediump float;" +
    32.             "uniform vec4 vColor;" +
    33.             "void main() {" +
    34.             "  gl_FragColor = vColor;" +
    35.             "}";
    36.    
    37.         public int mProgram;
    38.        
    39.     public Square() {
    40.         // initialize vertex byte buffer for shape coordinates
    41.         ByteBuffer bb = ByteBuffer.allocateDirect(
    42.         // (# of coordinate values * 4 bytes per float)
    43.                 squareCoords.length * 4);
    44.         bb.order(ByteOrder.nativeOrder());
    45.         vertexBuffer = bb.asFloatBuffer();
    46.         vertexBuffer.put(squareCoords);
    47.         vertexBuffer.position(0);
    48.  
    49.         // initialize byte buffer for the draw list
    50.         ByteBuffer dlb = ByteBuffer.allocateDirect(
    51.         // (# of coordinate values * 2 bytes per short)
    52.                 drawOrder.length * 2);
    53.         dlb.order(ByteOrder.nativeOrder());
    54.         drawListBuffer = dlb.asShortBuffer();
    55.         drawListBuffer.put(drawOrder);
    56.         drawListBuffer.position(0);
    57.        
    58.         int vertexShader = GLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    59.         int fragmentShader = GLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
    60.        
    61.         mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
    62.         GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    63.         GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    64.         GLES20.glLinkProgram(mProgram);                  // creates OpenGL ES program executables
    65.     }
    66.    
    67.     public void draw(float[] mvpMatrix) {
    68.         // Add program to OpenGL ES environment
    69.         GLES20.glUseProgram(mProgram);
    70.  
    71.         // get handle to vertex shader's vPosition member
    72.         int mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    73.  
    74.         // Enable a handle to the triangle vertices
    75.         GLES20.glEnableVertexAttribArray(mPositionHandle);
    76.  
    77.         // Prepare the triangle coordinate data
    78.         GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
    79.                                      GLES20.GL_FLOAT, false,
    80.                                      0, vertexBuffer);
    81.  
    82.         // get handle to fragment shader's vColor member
    83.         int mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
    84.  
    85.         float[] color = {
    86.                 1.0f, 1.0f, 1.0f, 1.0f
    87.         };
    88.        
    89.         // Set color for drawing the triangle
    90.         GLES20.glUniform4fv(mColorHandle, 1, color, 0);
    91.        
    92.         int mMVPMatrixHandle;
    93.        
    94.      // get handle to shape's transformation matrix
    95.         mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    96.  
    97.         // Apply the projection and view transformation
    98.         GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    99.  
    100.         // Draw the triangle
    101.         GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
    102.  
    103.         // Disable vertex array
    104.         GLES20.glDisableVertexAttribArray(mPositionHandle);
    105.     }
    106.    
    107. }
    Please help! I pretty much have no other choice but to use OpenGL.(SurfaceView is too slow, I'm making a game that requires real time movement.)
     

    Advertisement

Loading...
Similar Threads - OpenGL Projection Camera
  1. Android News
    Replies:
    0
    Views:
    59
  2. Android News
    Replies:
    0
    Views:
    55
  3. Android News
    Replies:
    0
    Views:
    13
  4. Android News
    Replies:
    0
    Views:
    4
  5. Aseem Chishti
    Replies:
    0
    Views:
    160
  6. lupin92
    Replies:
    2
    Views:
    4,356
  7. Cyberdev
    Replies:
    2
    Views:
    620
  8. Cofyka
    Replies:
    0
    Views:
    301
  9. Cofyka3
    Replies:
    1
    Views:
    371

Share This Page

Loading...