Code example for FloatBuffer

Methods: glVertexAttribPointer, position, put

0
                                -1.0f, -1.0f,0.0f 
                              }; 
        ByteBuffer bb = ByteBuffer.allocateDirect(rect_coords.length * 4);
        bb.order(ByteOrder.nativeOrder());
        _rect_vb = bb.asFloatBuffer();
        _rect_vb.put(rect_coords);
        _rect_vb.position(0);
    } 
 
    private void initParams() 
    { 
        Random rand = new Random();
        _u_r_freq = (float)(2*Math.PI/256.0*(rand.nextInt(5) + 1));
        _u_g_freq = (float)(2*Math.PI/256.0*(rand.nextInt(5) + 1));
        _u_b_freq = (float)(2*Math.PI/256.0*(rand.nextInt(5) + 1));
        _u_r_phase = (float)(Math.PI*(rand.nextInt(9) + 1));
        _u_g_phase = (float)(Math.PI*(rand.nextInt(9) + 1));
        _u_b_phase = (float)(Math.PI*(rand.nextInt(9) + 1));
        _u_touch_x = _width/2;
        _u_touch_y = _height/2;
        _u_shift = 0.0f;
    } 
 
    private int loadShader(int type, String shader_code)
    { 
        int shader = GLES20.glCreateShader(type);
    	GLES20.glShaderSource(shader, shader_code);
    	GLES20.glCompileShader(shader);
		final int[] compileStatus = new int[1];
		GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
        //Check the compile status of your shaders 
        //if you don't see anything on the screen 
		//Log.d("compile status", "" + compileStatus[0]); 
    	return shader;
    } 
 
    public void setTouchCoords(float x, float y)
    { 
        _u_touch_x = x;
        //Touch coords are in a coord. system with y origin at top-left 
        //but, by default, the fragment shader uses a coord. system 
        //with the y-origin at bottom left. 
        _u_touch_y = _height - y;
        _touch_update = true;
    } 
 
    public void onSurfaceCreated(GL10 unused, EGLConfig config)
    { 
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        StringBuilder sb = new StringBuilder(600);
        for (String str: _vs_code)
        { 
            sb.append(str + "\n");
        } 
        int vertex_shader = loadShader(GLES20.GL_VERTEX_SHADER, sb.toString());
        sb = new StringBuilder(600);
        for (String str: _fs_code)
        { 
            sb.append(str + "\n");
        } 
        int fragment_shader = loadShader(GLES20.GL_FRAGMENT_SHADER, sb.toString());
        _program = GLES20.glCreateProgram();
		GLES20.glAttachShader(_program, vertex_shader);
		GLES20.glAttachShader(_program, fragment_shader);
		GLES20.glLinkProgram(_program);
		GLES20.glUseProgram(_program);
        _a_position_handle = GLES20.glGetAttribLocation(_program, "a_position");
        _u_r_freq_handle = GLES20.glGetUniformLocation(_program, "u_r_freq");
        _u_g_freq_handle = GLES20.glGetUniformLocation(_program, "u_g_freq");
        _u_b_freq_handle = GLES20.glGetUniformLocation(_program, "u_b_freq");
        _u_r_phase_handle = GLES20.glGetUniformLocation(_program, "u_r_phase");
        _u_g_phase_handle = GLES20.glGetUniformLocation(_program, "u_g_phase");
        _u_b_phase_handle = GLES20.glGetUniformLocation(_program, "u_b_phase");
        _u_shift_handle = GLES20.glGetUniformLocation(_program, "u_shift");
        _u_touch_x_handle = GLES20.glGetUniformLocation(_program, "u_touch_x");
        _u_touch_y_handle = GLES20.glGetUniformLocation(_program, "u_touch_y");
		GLES20.glEnableVertexAttribArray(_a_position_handle);
    } 
 
    public void onSurfaceChanged(GL10 unused, int width, int height)
    { 
        _width = width;
        _height = height;
        initShapes(); 
        initParams(); 
        GLES20.glVertexAttribPointer(_a_position_handle, 3, GLES20.GL_FLOAT, false, 0, _rect_vb);
        GLES20.glUniform1f(_u_r_freq_handle, _u_r_freq);
        GLES20.glUniform1f(_u_g_freq_handle, _u_g_freq);
        GLES20.glUniform1f(_u_b_freq_handle, _u_b_freq);
        GLES20.glUniform1f(_u_r_phase_handle, _u_r_phase);
        GLES20.glUniform1f(_u_g_phase_handle, _u_g_phase);