[Java] [OpenGL ES3.2] BufferUtil VBO和VAO的使用

[Java] [OpenGL ES3.2] BufferUtil VBO和VAO的使用,第1张

[Java] [OpenGL ES3.2] BufferUtil VBO和VAO的使用 BufferUtil.java
package com.Diamond.gl04;

import android.opengl.GLES32;
import java.nio.Buffer;
import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

public class BufferUtil {
	public static ByteBuffer newNativeByteBuffer(int capacity) {
		ByteBuffer bb = ByteBuffer.allocateDirect(capacity);
		bb.order(ByteOrder.nativeOrder());
		return bb;
	}
	public static FloatBuffer toFloatBuffer(float[] array) {
		FloatBuffer fb = newNativeByteBuffer(array.length * 4).asFloatBuffer();
		fb.put(array).position(0);
		return fb;
	}
	public static IntBuffer toIntBuffer(int[] array) {
		IntBuffer ib = newNativeByteBuffer(array.length * 4).asIntBuffer();
		ib.put(array).position(0);
		return ib;
	}
	
	
	
    public static int genBuffer() {
		int[] buffer = new int[1];
		GLES32.glGenBuffers(1,buffer,0);
		return buffer[0];
	}
	public static int[] genBuffers(int bufferNumber) {
		bufferNumber = Math.max(bufferNumber,1);
		int[] buffers = new int[bufferNumber];
		GLES32.glGenBuffers(bufferNumber,buffers,0);
		return buffers;
	}
	
	public static void bindBuffer(int buffer,int type) {
		if(buffer != 0) {
			GLES32.glBindBuffer(type,buffer);
		}
	}
	public static void bindBuffers(int[] buffers,int type) {
		for(int i : buffers) {
			if(i != 0) {
			GLES32.glBindBuffer(type,i);
			}
		}
	}
	public static void bindBuffer(int type) {
		GLES32.glBindBuffer(type,0);
	}
	
	public static void bufferData(int type,int size,Buffer data,int usage) {
		GLES32.glBufferData(type,size,data,usage);
	}
	
	public static void deleteBuffer(int buffer) {
		GLES32.glDeleteBuffers(1,new int[]{ buffer },0);
	}
	public static void deleteBuffers(int[] buffers) {
		GLES32.glDeleteBuffers(buffers.length,buffers,0);
	}
	
	
	
	public static int genVertexArray() {
		int[] vao = new int[1];
		GLES32.glGenVertexArrays(1,vao,0);
		return vao[0];
	}
	public static int[] genVertexArrays(int VAONumber) {
		VAONumber = Math.max(VAONumber,1);
		int[] VAOs = new int[VAONumber];
		GLES32.glGenVertexArrays(VAONumber,VAOs,0);
		return VAOs;
	}
	
	public static void bindVertexArray(int vao) {
		GLES32.glBindVertexArray(vao);
	}
	public static void bindVertexArray() {
		GLES32.glBindVertexArray(0);
	}
	
	public static void deleteVertexArray(int vao) {
		if(vao != 0) {
			GLES32.glDeleteVertexArrays(1,new int[]{ vao },0);
		}
	}
	public static void deleteVertexArrays(int[] VAOs) {
		GLES32.glDeleteVertexArrays(VAOs.length,VAOs,0);
	}
}

使用例

改进后的Axis.java

package com.Diamond.gl04;

import android.opengl.GLES32;
import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import android.renderscript.Float4;
import android.opengl.Matrix;
import android.renderscript.Matrix4f;

public class Axis extends MyObject {
    public float[] vertices,colors;
	public GLES32 gl;
	public int VAO;
	
	public Axis() {
		vertices = new float[]{
			-100,0,0,
			100,0,0,
			0,-100,0,
			0,100,0,
			0,0,-100,
			0,0,100
		};
		
		colors = new float[]{
			1,0,0,
			1,0,0,
			0,1,0,
			0,1,0,
			0,0,1,
			0,0,1
		};
		
		BufferUtil bu = new BufferUtil();
		VAO = bu.genVertexArray();
		bu.bindVertexArray(VAO);
		int[] VBOs = bu.genBuffers(2);
		
		bu.bindBuffer(VBOs[0],gl.GL_ARRAY_BUFFER);
		bu.bufferData(gl.GL_ARRAY_BUFFER,vertices.length * 4,bu.toFloatBuffer(vertices),gl.GL_STATIC_DRAW);
		gl.glEnableVertexAttribArray(0);
		gl.glVertexAttribPointer(0,3,gl.GL_FLOAT,false,3 * 4,0);
		
		bu.bindBuffer(VBOs[1],gl.GL_ARRAY_BUFFER);
		bu.bufferData(gl.GL_ARRAY_BUFFER,colors.length * 4,bu.toFloatBuffer(colors),gl.GL_STATIC_DRAW);
		gl.glEnableVertexAttribArray(1);
		gl.glVertexAttribPointer(1,3,gl.GL_FLOAT,false,3 * 4,0);
		
		bu.bindVertexArray();
		bu.deleteBuffers(VBOs);
	}
	
	public void draw(Program program) {
		program.setUniform("u_model",getModelMatrixArray());
		BufferUtil.bindVertexArray(VAO);
		gl.glDrawArrays(gl.GL_LINES,0,6);
		BufferUtil.bindVertexArray();
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5707857.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存