OpenGL ES2 Android



  • That's the code that runs the texture on the screen, but it's replaced by a black rectangle.

    static ShaderProgram shaderProgram;
    private FloatBuffer vertexBuffer;
    private FloatBuffer textureBuffer;
    private int textures[] = new int[1];
    

    private float vertices[] = {
    0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 1.0f, 0.0f
    };

    private float texture[] = {
    0.0f, 0.0f,
    0.0f, 1.0f,
    1.0f, 0.0f,
    1.0f, 1.0f
    };

    public Sprite(String path) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuffer.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeStream(core.getAssets().open(path));
    } catch (IOException e) { }
    
    GLES20.glGenTextures(1, textures, 0);
    GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
    
    byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    textureBuffer = byteBuffer.asFloatBuffer();
    textureBuffer.put(texture);
    textureBuffer.position(0);
    

    }

    public void draw() {
    shaderProgram.use();
    GLES20.glEnable(GL10.GL_TEXTURE_2D);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
    
    int positionAttrib = shaderProgram.getAttribLocation("vertices");
    int textureUniform = shaderProgram.getUniformLocation("tex");
    int textureAttrib = shaderProgram.getAttribLocation("texCoord");
    
    GLES20.glEnableVertexAttribArray(positionAttrib);
    GLES20.glEnableVertexAttribArray(textureAttrib);
    
    GLES20.glUniform1i(textureUniform, 0);
    GLES20.glVertexAttribPointer(positionAttrib, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
    GLES20.glVertexAttribPointer(textureAttrib, 2, GLES20.GL_FLOAT, false, 0, textureBuffer);
    
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
    
    GLES20.glDisableVertexAttribArray(positionAttrib);
    GLES20.glDisableVertexAttribArray(textureAttrib);
    

    }

    Vertex shader

    #version 150
    in vec3 vertices;
    in vec2 texCoord;
    out vec2 fTexCoord;

    void main() {
    fTexCoord = texCoord;

    gl_Position = vec4(vertices, 1);
    

    }

    Fragment shader

    #version 150
    uniform sampler2D tex;
    in vec2 fTexCoord;
    out vec4 finalColor;

    void main() {
    finalColor = texture(tex, fTexCoord);
    }



  • https://ru.wikipedia.org/wiki/OpenGL_Shading_Language :

    The following function is used in GLSL 1.30 and Nova:

    glBindFragDataLocation(Programm, 0, "MyFragColor");
    

    where: Programm - programme indicator; 0 - colour buffer number if You don't use MRT(Multiple Render Targets), meaning must be " MyFragColor " is the name of the cervical output, recording in this buffer.

    #version 150
    void main(void)
    {
        MyFragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
    

    Or use old synthaxis and write it:

    Fragment shader

    #version 150
    uniform sampler2D tex;
    in vec2 fTexCoord;
    

    void main() {
    gl_FragColor= texture2D(tex, fTexCoord);
    }

    It's supposed to work.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2