package destr.jogl.pointer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import javax.media.opengl.GL; public abstract class Pointer { private static byte[] makeMD5(ByteBuffer data) throws NoSuchAlgorithmException { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(data); return md5.digest(); } private static String Bytes2HexStr(byte[] data) { StringBuilder result = new StringBuilder(); for(int i = 0; i < data.length; ++i) result.append(Integer.toString((data[i] & 0xff) + 0x100, 16).substring(1)); return result.toString(); } private String md5Str; protected int id, bytes, glType; public Pointer(int id){ this.id = id; } public Pointer(GL gl){ int glID[] = new int[1]; gl.glGenBuffers(1, glID, 0); id = glID[0]; } public void load(GL gl, float[] data){ ByteBuffer buffer = ByteBuffer.allocateDirect(data.length * Float.SIZE); buffer.order(ByteOrder.nativeOrder()); buffer.asFloatBuffer().put(data).position(0); load(gl, buffer, Float.SIZE); } //TODO загрузка остальных типов public void load(GL gl, ByteBuffer buffer, int glType) { // Все загрузки ведут сюда this.glType = glType; bytes = buffer.capacity(); gl.glBindBuffer(GL.GL_ARRAY_BUFFER, id); gl.glBufferData(GL.GL_ARRAY_BUFFER, bytes, buffer, getUsage()); gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0); try { buffer.mark(); md5Str = Bytes2HexStr(makeMD5(buffer)); buffer.reset(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); md5Str = "[no MD5hash: "+this.hashCode()+"]"; } } public void free(GL gl){ gl.glDeleteBuffers(1, new int[]{id}, 0); } public String hashMD5(){ return md5Str; } public abstract void bind(GL gl); // Наследник устанавливает нужный тип буфера public abstract void unBind(GL gl); protected abstract int getUsage(); /* Specifies the expected usage pattern of the data store. The symbolic constant must be: * GL_STREAM_DRAW * GL_STREAM_READ * GL_STREAM_COPY * GL_STATIC_DRAW * GL_STATIC_READ * GL_STATIC_COPY * GL_DYNAMIC_DRAW * GL_DYNAMIC_READ * GL_DYNAMIC_COPY */ }