init OpenGL Textures - Android android.opengl

Android examples for android.opengl:OpenGL Texture

Description

init OpenGL Textures

Demo Code


//package com.java2s;

import java.nio.FloatBuffer;

import android.opengl.GLES20;

public class Main {

    private static final int FSIZE = Float.SIZE / Byte.SIZE;

    private static int program;

    private static int ma_texCoord;

    private static int u_Sampler;

    private static int u_alpha;

    private static FloatBuffer texBuffer;

    public static void initTextures() {
        // /*from w ww .  j  av a  2 s  .c  om*/
        int[] vertexTexCoord = new int[1];
        GLES20.glGenBuffers(1, vertexTexCoord, 0);

        // 
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexTexCoord[0]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER,
                FSIZE * texBuffer.limit(), texBuffer,
                GLES20.GL_DYNAMIC_DRAW);

        GLES20.glVertexAttribPointer(ma_texCoord, 2, GLES20.GL_FLOAT,
                false, 0, 0);
        GLES20.glEnableVertexAttribArray(ma_texCoord); // 

        // 
        int[] textures = new int[1];
        GLES20.glGenTextures(1, textures, 0);

        // 
        loadTexture(textures[0], "u_Sampler");
    }

    //
    private static void loadTexture(int texture, String sampler) {
        // u_Sampler
        u_Sampler = GLES20.glGetUniformLocation(program, sampler);
        if (u_Sampler == -1) {
            throw new RuntimeException("u_Sampler");
        }
        u_alpha = GLES20.glGetUniformLocation(program, "u_alpha");
        if (u_alpha == -1) {
            throw new RuntimeException("u_alpha");
        }

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0); // 0

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture); // 

        // 
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    }
}

Related Tutorials