Search code examples
androidopengl-esgpu

openGl es on Android displays a Square at the top right corner


I wrote a code which should draw a square with a color gradient on it to the screen, but for some reason it also draws a blue square to the top right of the screen. The blue square changes it's color sometimes, when I change stuff in the code, which has absolutely nothing to do with it, for example a line of code in my Renderer, where I read the screen size in pixels. Sometimes it also disappears when I take a screenshot. Here is the code:

import android.content.Context;
import android.opengl.GLES30;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;


public class Square {
int ShaderProgramID;
private FloatBuffer vertexBuffer;
private int vertexBufferID;
private int vertexCount;
private int vertexStride;
static final int COORDS_PER_VERTEX = 3;
static final int COLORS_PER_VERTEX = 4;
static final int SIZE_OF_FLOAT = 4;
static final float coords[] = {
        //x:    y:     z:            r:    g:    b:    a:
        -0.5f, 0.5f, 0.0f,         1.0f, 0.0f, 0.0f, 1.0f,
        -0.5f,-0.5f, 0.0f,         0.0f, 1.0f, 0.0f, 1.0f,
         0.5f,-0.5f, 0.0f,         0.0f, 0.0f, 1.0f, 1.0f,
        -0.5f, 0.5f, 0.0f,         1.0f, 0.0f, 0.0f, 1.0f,
         0.5f,-0.5f, 0.0f,         0.0f, 1.0f, 0.0f, 1.0f,
         0.5f, 0.5f, 0.0f,         0.0f, 0.0f, 1.0f, 1.0f

};
public Square(Context context)  {
    String vertexShaderSrc = ReadFromfile("defaultVertexShader.glsl", context);
    String fragmentShaderSrc = ReadFromfile("defaultFragmentShader.glsl", context);

    int vertexID = GLES30.glCreateShader(GLES30.GL_VERTEX_SHADER);
    GLES30.glShaderSource(vertexID, vertexShaderSrc);
    GLES30.glCompileShader(vertexID);
    Log.d("Golden", GLES30.glGetShaderInfoLog(vertexID));

    int fragmetID = GLES30.glCreateShader(GLES30.GL_FRAGMENT_SHADER);
    GLES30.glShaderSource(fragmetID, fragmentShaderSrc);
    GLES30.glCompileShader(fragmetID);
    Log.d("Golden", GLES30.glGetShaderInfoLog(fragmetID) );

    ShaderProgramID = GLES30.glCreateProgram();
    GLES30.glAttachShader(ShaderProgramID, vertexID);
    GLES30.glAttachShader(ShaderProgramID, fragmetID);
    GLES30.glBindAttribLocation(ShaderProgramID, 0, "aPos");
    GLES30.glBindAttribLocation(ShaderProgramID, 1, "aColor");
    GLES30.glLinkProgram(ShaderProgramID);

    positionHandle = GLES30.glGetAttribLocation(ShaderProgramID, "aPos");
    colorHandle = GLES30.glGetAttribLocation(ShaderProgramID, "aColor");

    //vertexBuffer = FloatBuffer.allocate(coords.length);
    //vertexBuffer = ByteBuffer.allocate(coords.length*4).asFloatBuffer();
    //vertexBuffer.put(coords);
    //vertexBuffer.position(0);
    ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(coords);
    vertexBuffer.position(0);

    IntBuffer buffer = IntBuffer.allocate(1);
    GLES30.glGenBuffers(1, buffer);
    vertexBufferID = buffer.get(0);
    GLES30.glBindBuffer(GLES30.GL_ELEMENT_ARRAY_BUFFER, vertexBufferID);
    GLES30.glBufferData(GLES30.GL_ELEMENT_ARRAY_BUFFER, coords.length * 4, vertexBuffer, GLES30.GL_STATIC_DRAW);

    vertexCount = coords.length / (COORDS_PER_VERTEX + COLORS_PER_VERTEX);
    vertexStride = (COORDS_PER_VERTEX + COLORS_PER_VERTEX) * 4;
}
private int positionHandle;
private int colorHandle;
public void draw() {
    GLES30.glUseProgram(ShaderProgramID);

    vertexBuffer.position(0);
    GLES30.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX, GLES30.GL_FLOAT, false, vertexStride, vertexBuffer);
    GLES30.glEnableVertexAttribArray(positionHandle);


    vertexBuffer.position(3);
    GLES30.glVertexAttribPointer(colorHandle, COLORS_PER_VERTEX, GLES30.GL_FLOAT, false, vertexStride, vertexBuffer);
    GLES30.glEnableVertexAttribArray(colorHandle);


    GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, coords.length);
    GLES30.glDisableVertexAttribArray(positionHandle);
    GLES30.glDisableVertexAttribArray(colorHandle);

}

public String ReadFromfile(String fileName, Context context) {
    StringBuilder ReturnString = new StringBuilder();
    InputStream fIn = null;
    InputStreamReader isr = null;
    BufferedReader input = null;
    try {
        fIn = context.getResources().getAssets()
                .open(fileName);
        isr = new InputStreamReader(fIn);
        input = new BufferedReader(isr);
        String line = "";
        while ((line = input.readLine()) != null) {
            ReturnString.append(line + "\n");
        }
    } catch (Exception e) {
        e.getMessage();
    } finally {
        try {
            if (isr != null)
                isr.close();
            if (fIn != null)
                fIn.close();
            if (input != null)
                input.close();
        } catch (Exception e2) {
            e2.getMessage();
        }
    }
    return ReturnString.toString();
}
}

Fragment Shader:

#version 300 es
precision mediump float;
in vec4 vColor;
out vec4 oColor;
void main()
{
oColor = vColor;
}

Vertex Shader:

#version 300 es
in vec3 aPos;
in vec4 aColor;
out vec4 vColor;

void main()
{
vColor = aColor;
gl_Position = vec4(aPos,1.0);
}

Solution

  • You must bind the vertices to the GL_ARRAY_BUFFER traget rather then to the GL_ELEMENT_ARRAY_BUFFER target:

    IntBuffer buffer = IntBuffer.allocate(1);
    GLES30.glGenBuffers(1, buffer);
    vertexBufferID = buffer.get(0);
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, vertexBufferID);
    GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, coords.length * 4, vertexBuffer, GLES30.GL_STATIC_DRAW);
    

    See glVertexAttribPointer:

    If a non-zero named buffer object is bound to the GL_ARRAY_BUFFER target (see glBindBuffer), pointer is treated as a byte offset into the buffer object's data store.

    In your case stride is 28 bytes ((3+4) * 4). The offset of the vertices is 0 bytes and the offset of the color attribute is 12 bytes (3 * 4):

    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, vertexBufferID);
    
    GLES30.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX, 
        GLES30.GL_FLOAT, false, 7*4, 0);
    GLES30.glVertexAttribPointer(colorHandle, COLORS_PER_VERTEX, 
        GLES30.GL_FLOAT, false, 7*4, 3*4);
    
    GLES30.glEnableVertexAttribArray(positionHandle);
    GLES30.glEnableVertexAttribArray(colorHandle);
    

    The last argument of glDrawArrays is the number of vertices, not the number of floats in the array:

    GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, coords.length / 7);

    GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, coords.length / 7);