Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Renderfield3D implements IDrawable{
- private int _verticesID = 0;
- private int _indicesID = 0;
- private int _coloursID = 0;
- @Override
- public void load() {
- float[] vertices = new float[]
- { // X Y Z
- 0.0f, 0.0f, 0.0f, // 0 LBF
- 1.0f, 0.0f, 0.0f, // 1 RBF
- 0.0f, 0.0f, 1.0f, // 2 LBB
- 1.0f, 0.0f, 1.0f, // 3 RBB
- 0.0f, 1.0f, 0.0f, // 4 LTF
- 1.0f, 1.0f, 0.0f, // 5 RTF
- 0.0f, 1.0f, 1.0f, // 6 LTB
- 1.0f, 1.0f, 1.0f // 7 RTB
- };
- int[] indices = new int[]
- {
- 4, 5, 1, 0, // FRONT
- 5, 7, 3, 1, // RIGHT
- 7, 6, 2, 3, // BACK
- 6, 4, 0, 2, // LEFT
- 0, 1, 3, 2, // BOTTOM
- 6, 7, 5, 4 // TOP
- };
- float[] colours = new float[]
- {
- 1.0f, 1.0f, 1.0f, 1.0f
- };
- _verticesID = VBOHandler.createVBO();
- _indicesID = VBOHandler.createVBO();
- _coloursID = VBOHandler.createVBO();
- FloatBuffer bufferedColours = BufferUtils.createFloatBuffer(colours.length);
- bufferedColours.put(colours);
- FloatBuffer bufferedvertices = BufferUtils.createFloatBuffer(vertices.length);
- bufferedvertices.put(vertices);
- IntBuffer bufferedindices = BufferUtils.createIntBuffer(indices.length);
- bufferedindices.put(indices);
- VBOHandler.bufferData(_verticesID, bufferedvertices);
- VBOHandler.bufferElementData(_indicesID, bufferedindices);
- }
- @Override
- public void draw() {
- GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
- ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, _verticesID);
- GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
- GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
- ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, _coloursID);
- GL11.glColorPointer(4, GL11.GL_FLOAT, 0, 0);
- ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, _indicesID);
- GL12.glDrawRangeElements(GL11.GL_QUADS, 0, 24, 24,
- GL11.GL_UNSIGNED_INT, 0);
- }
- }
- public class VBOHandler {
- public static IntBuffer createVBOs(int amount)
- {
- if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
- {
- IntBuffer buffer = BufferUtils.createIntBuffer(amount);
- ARBVertexBufferObject.glGenBuffersARB(buffer);
- return buffer;
- }
- return null;
- }
- public static int createVBO()
- {
- return createVBOs(1).get(0);
- }
- public static void bufferData(int id, FloatBuffer buffer)
- {
- if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
- {
- ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
- ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer,
- ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
- }
- }
- public static void bufferElementData(int id, IntBuffer buffer)
- {
- if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
- {
- ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, id);
- ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, buffer,
- ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement