protected void compile() { vao = glGenVertexArrays(); glBindVertexArray(vao); { vbo = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vbo); { glBufferData(GL_ARRAY_BUFFER, Buffer.createFloatBuffer(vertices), GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); } glBindBuffer(GL_ARRAY_BUFFER, 0); vio = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vio); { glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(indices), GL_STATIC_DRAW); } glBindBuffer(GL_ARRAY_BUFFER, 0); vto = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vto); { glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(texCoords), GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, false, 0, 1); } glBindBuffer(GL_ARRAY_BUFFER, 0); } glBindVertexArray(0); glActiveTexture(GL_TEXTURE1); shader.bind(); int uniform = glGetUniformLocation(shader.getID(), "texture"); glUniform1i(uniform, 1); shader.release(); }
public void RebuildMesh(float startX, float startY, float startZ) { VBOColorHandle = GL15.glGenBuffers(); VBOVertexHandle = GL15.glGenBuffers(); // Using 6 * 4 * 3 because 6 faces * 4 pts per face * 3 coords per point FloatBuffer VertexPositionData = BufferUtils.createFloatBuffer((CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) * 6 * 4 * 3); FloatBuffer VertexColorData = BufferUtils.createFloatBuffer((CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) * 6 * 4 * 3); for (float x = 0; x < CHUNK_SIZE; x += 1) { for (float y = 0; y < CHUNK_SIZE; y += 1) { for (float z = 0; z < CHUNK_SIZE; z += 1) { VertexPositionData.put( CreateCube( (float) startX + x * CUBE_LENGTH, (float) startY + y * CUBE_LENGTH, (float) startZ + z * CUBE_LENGTH)); VertexColorData.put(CreateCubeVertexCol(GetCubeColor(Blocks[(int) x][(int) y][(int) z]))); } } } VertexColorData.flip(); VertexPositionData.flip(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexPositionData, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, VertexColorData, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
public VertexArray(float[] vertices, byte[] indices, float[] textureCoordinates) { count = indices.length; vao = glGenVertexArrays(); glBindVertexArray(vao); vbo = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW); glVertexAttribPointer(Shader.VERTEX_ATTRIB, 3, GL_FLOAT, false, 0, 0); glEnableVertexAttribArray(Shader.VERTEX_ATTRIB); tbo = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, tbo); glBufferData( GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(textureCoordinates), GL_STATIC_DRAW); glVertexAttribPointer(Shader.TCOORD_ATTRIB, 2, GL_FLOAT, false, 0, 0); glEnableVertexAttribArray(Shader.TCOORD_ATTRIB); ibo = glGenBuffers(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); }
public Mesh(float[] vertices, int[] indices) { this.vertices = vertices; this.indices = indices; vbo = glGenBuffers(); ibo = glGenBuffers(); FloatBuffer fb = BufferUtils.createFloatBuffer(vertices.length); fb.put(vertices); fb.flip(); IntBuffer ib = BufferUtils.createIntBuffer(indices.length); ib.put(indices); ib.flip(); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, ib, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
protected void compile() { vao = glGenVertexArrays(); glBindVertexArray(vao); { vbo = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vbo); { glBufferData(GL_ARRAY_BUFFER, Buffer.createFloatBuffer(vertices), GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); } glBindBuffer(GL_ARRAY_BUFFER, 0); vio = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vio); { glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(indices), GL_STATIC_DRAW); } glBindBuffer(GL_ARRAY_BUFFER, 0); vto = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vto); { glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(texCoords), GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, false, 0, 1); } glBindBuffer(GL_ARRAY_BUFFER, 0); } glBindVertexArray(0); }
public void setupQuad() { // We'll define our quad using 4 vertices of the custom 'Vertex' class Vertex v0 = new Vertex(); v0.setXYZ(-0.5f, 0.5f, 0f); v0.setRGB(1, 0, 0); Vertex v1 = new Vertex(); v1.setXYZ(-0.5f, -0.5f, 0f); v1.setRGB(0, 1, 0); Vertex v2 = new Vertex(); v2.setXYZ(0.5f, -0.5f, 0f); v2.setRGB(0, 0, 1); Vertex v3 = new Vertex(); v3.setXYZ(0.5f, 0.5f, 0f); v3.setRGB(1, 1, 1); Vertex[] vertices = new Vertex[] {v0, v1, v2, v3}; // Put each 'Vertex' in one FloatBuffer FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length * Vertex.elementCount); for (int i = 0; i < vertices.length; i++) { verticesBuffer.put(vertices[i].getXYZW()); verticesBuffer.put(vertices[i].getRGBA()); } verticesBuffer.flip(); // OpenGL expects to draw vertices in counter clockwise order by default byte[] indices = { 0, 1, 2, 2, 3, 0 }; indicesCount = indices.length; ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount); indicesBuffer.put(indices); indicesBuffer.flip(); // Create a new Vertex Array Object in memory and select it (bind) vaoId = GL30.glGenVertexArrays(); GL30.glBindVertexArray(vaoId); // Create a new Vertex Buffer Object in memory and select it (bind) vboId = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW); // Put the positions in attribute list 0 GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes, 0); // Put the colors in attribute list 1 GL20.glVertexAttribPointer( 1, 4, GL11.GL_FLOAT, false, Vertex.sizeInBytes, Vertex.elementBytes * 4); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); // Deselect (bind to 0) the VAO GL30.glBindVertexArray(0); // Create a new VBO for the indices and select it (bind) - INDICES vboiId = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); }
public void glBufferData(int target, int size, Buffer data, int usage) { if (data == null) throw new GdxRuntimeException("Using null for the data not possible, blame LWJGL"); else if (data instanceof ByteBuffer) GL15.glBufferData(target, (ByteBuffer) data, usage); else if (data instanceof IntBuffer) GL15.glBufferData(target, (IntBuffer) data, usage); else if (data instanceof FloatBuffer) GL15.glBufferData(target, (FloatBuffer) data, usage); else if (data instanceof DoubleBuffer) GL15.glBufferData(target, (DoubleBuffer) data, usage); else if (data instanceof ShortBuffer) // GL15.glBufferData(target, (ShortBuffer) data, usage); }
@Override public void create() { if (isCreated()) { throw new IllegalStateException("Vertex array has already been created"); } if (vertexData == null) { throw new IllegalStateException("Vertex data has not been set"); } // Generate and bind the vao id = GL30.glGenVertexArrays(); GL30.glBindVertexArray(id); // Generate, bind and fill the indices vbo then unbind indicesBufferID = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferID); GL15.glBufferData( GL15.GL_ELEMENT_ARRAY_BUFFER, vertexData.getIndicesBuffer(), GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); // Save the count of indices to draw indicesCountCache = vertexData.getIndicesCount(); resetIndicesCountAndOffset(); // Create the map for attribute index to buffer ID attributeBufferIDs = new int[vertexData.getAttributeCount()]; // For each attribute, generate, bind and fill the vbo, then setup the attribute in the vao and // save the buffer ID for the index for (int i = 0; i < vertexData.getAttributeCount(); i++) { final VertexAttribute attribute = vertexData.getAttribute(i); final int bufferID = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, bufferID); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, attribute.getData(), GL15.GL_STATIC_DRAW); attributeBufferIDs[i] = bufferID; // Three ways to interpret integer data if (attribute.getType().isInteger() && attribute.getUploadMode() == UploadMode.KEEP_INT) { // Directly as an int GL30.glVertexAttribIPointer( i, attribute.getSize(), attribute.getType().getGLConstant(), 0, 0); } else { // Or as a float, normalized or not GL20.glVertexAttribPointer( i, attribute.getSize(), attribute.getType().getGLConstant(), attribute.getUploadMode().normalize(), 0, 0); } } // Unbind the vbo and vao GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL30.glBindVertexArray(0); // Update state super.create(); // Check for errors LWJGLUtil.checkForGLError(); }
private void setupQuad() { IndexedMemoryFast<Pos4> mem = Pos4.reserveFast(10, false); mem.add(new Pos4().xyzw(-0.5f, 0.5f, 0f, 1f)); mem.add(new Pos4().xyzw(-0.5f, -0.5f, 0f, 1f)); mem.add(new Pos4().xyzw(0.5f, -0.5f, 0f, 1f)); mem.add(new Pos4().xyzw(0.5f, 0.5f, 0f, 1f)); // RawMemory colors = new RawMemory(30); IndexedMemoryFast<Color4> colors = Color4.reserveFast(10, false); colors.add(new Color4().rgba(1f, 0f, 0f, 1f)); colors.add(new Color4().rgba(0f, 1f, 0f, 1f)); colors.add(new Color4().rgba(0f, 0f, 1f, 1f)); colors.add(new Color4().rgba(1f, 1f, 1f, 1f)); // OpenGL expects to draw vertices in counter clockwise order by default byte[] indices = {0, 1, 2, 2, 3, 0}; indicesCount = indices.length; ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount); indicesBuffer.put(indices); indicesBuffer.flip(); // Create a new Array Array Object in memory and select it (bind) vaoId = glCreateVertexArrays(); glBindVertexArray(vaoId); // Create a new Array Buffer Object in memory and select it (bind) - VERTICES vboId = glCreateBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, mem.burn().getArrayBuffer(), GL_STATIC_DRAW); glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // Create a new VBO for the indices and select it (bind) - COLORS vbocId = glCreateBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vbocId); glBufferData(GL_ARRAY_BUFFER, colors.burn().getArrayBuffer(), GL_STATIC_DRAW); glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // Deselect (bind to 0) the VAO glBindVertexArray(0); // Create a new VBO for the indices and select it (bind) - INDICES vboiId = glCreateBuffers(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboiId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
private void bindIndicesBuffer(int[] indices) { int vboID = GL15.glGenBuffers(); vbos.add(vboID); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID); IntBuffer buffer = storeDataInIntBuffer(indices); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); }
private int createTubeVbo(int tube, Color color) { float[] c = new float[] {color.getBlue() / 255f, color.getGreen() / 255f, color.getRed() / 255f}; totalNumVerts[tube] = tubes[tube].indeces.length; IntBuffer buf = BufferUtils.createIntBuffer(1); GL15.glGenBuffers(buf); int vbo = buf.get(); FloatBuffer data = BufferUtils.createFloatBuffer(tubes[tube].indeces.length * 9); for (int i = 0; i < tubes[tube].indeces.length; i++) { data.put(c); Vector3D vertex = tubes[tube].vertices[tubes[tube].indeces[i]]; Vector3D normal = tubes[tube].normals[tubes[tube].indeces[i]]; float[] vertexf = new float[] {vertex.x, vertex.y, vertex.z}; float[] normalf = new float[] {normal.x, normal.y, normal.z}; data.put(vertexf); data.put(normalf); } data.rewind(); int bytesPerFloat = Float.SIZE / Byte.SIZE; int numBytes = data.capacity() * bytesPerFloat; GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, data, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); return vbo; }
@Override protected void init() { initializeProgram(); try { cylinderMesh = new Mesh("UnitCylinder.xml"); planeMesh = new Mesh("LargePlane.xml"); } catch (Exception exception) { exception.printStackTrace(); System.exit(-1); } glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CW); glEnable(GL_DEPTH_TEST); glDepthMask(true); glDepthFunc(GL_LEQUAL); glDepthRange(0.0f, 1.0f); glEnable(GL_DEPTH_CLAMP); projectionUniformBuffer = glGenBuffers(); glBindBuffer(GL_UNIFORM_BUFFER, projectionUniformBuffer); glBufferData(GL_UNIFORM_BUFFER, ProjectionBlock.SIZE, GL_DYNAMIC_DRAW); // Bind the static buffers. glBindBufferRange( GL_UNIFORM_BUFFER, projectionBlockIndex, projectionUniformBuffer, 0, ProjectionBlock.SIZE); glBindBuffer(GL_UNIFORM_BUFFER, 0); }
@Override public void flush() { if (vertexOffset == 0) { return; } ctx.checkGLError("Shader.flush"); vertexData.position(0); glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STREAM_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementData, GL_STREAM_DRAW); ctx.checkGLError("Shader.flush BufferData"); glDrawElements(GL_TRIANGLES, elementOffset, GL_UNSIGNED_SHORT, 0); vertexOffset = elementOffset = 0; ctx.checkGLError("Shader.flush DrawElements"); }
private void storeDataInAttributeList(int attributeNumber, int attributeSize, float[] data) { int vboID = GL15.glGenBuffers(); vbos.add(vboID); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID); FloatBuffer buffer = storeDataInFloatBuffer(data); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(attributeNumber, attributeSize, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
public void setupVIBO() { ByteBuffer indexBuffer = BufferUtils.createByteBuffer(indexElementCount * getNumObjects()); for (int i = 0; i < getNumObjects(); ++i) { indexBuffer.put(Rectangle.getOrder(4 * i)); } indexBuffer.flip(); setVIBO(GL15.glGenBuffers()); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, getVIBO()); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); }
/** * @param id * @param data @Info Stocke le FloatBuffer dans le GPU */ public void bufferData() { buffer = BufferUtils.createFloatBuffer(floatlist.size()); for (Float f : floatlist) { buffer.put(f); } buffer.flip(); glBindBuffer(GL_ARRAY_BUFFER, vboID); glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); // glBufferSubData(vboID, 0, buffer); glBindBuffer(GL_ARRAY_BUFFER, 0); bufferSize = buffer.limit() / 7; // 7 = 3 vertex(x,y,z) + 4 color (r,g,b,a) }
private int initializeVertexBuffer() { int positionBufferObject = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject); FloatBuffer vertexData = BufferUtils.createFloatBuffer(vertexDataAry.length).put(vertexDataAry); vertexData.flip(); glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); return positionBufferObject; }
private static void setUpVBOs() { int[] vbos; try { model = OBJLoader.loadModel(new File(MODEL_LOCATION)); int vboVertexHandle = glGenBuffers(); int vboNormalHandle = glGenBuffers(); FloatBuffer vertices = BufferTools.reserveData(model.faces.size() * 9); FloatBuffer normals = BufferTools.reserveData(model.faces.size() * 9); for (Face face : model.faces) { vertices.put(BufferTools.asFloats(model.vertices.get((int) face.vertex.x - 1))); vertices.put(BufferTools.asFloats(model.vertices.get((int) face.vertex.y - 1))); vertices.put(BufferTools.asFloats(model.vertices.get((int) face.vertex.z - 1))); normals.put(BufferTools.asFloats(model.normals.get((int) face.normal.x - 1))); normals.put(BufferTools.asFloats(model.normals.get((int) face.normal.y - 1))); normals.put(BufferTools.asFloats(model.normals.get((int) face.normal.z - 1))); } vertices.flip(); normals.flip(); glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(attributeVertex); glVertexAttribPointer(attributeVertex, 3, false, 0, vertices); glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle); glEnableVertexAttribArray(attributeNormal); glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW); glNormalPointer(GL_FLOAT, 0, 0L); // TODO: This really isn't finished yet. :-( } catch (FileNotFoundException e) { e.printStackTrace(); cleanUp(); System.exit(1); } catch (IOException e) { e.printStackTrace(); cleanUp(); System.exit(1); } }
public static void main(String[] args) { Window window = new Window(1024, 768, false); window.setTitle("Hello OpenGL!"); window.setVisible(true); int vertexCount = 3; int floatSize = 4; FloatBuffer buffer = createFloatBuffer(vertexCount * 3 + vertexCount * 4); buffer.put(0).put(0.5f).put(0); buffer.put(1).put(0).put(0).put(1); buffer.put(-0.5f).put(-0.5f).put(0); buffer.put(0).put(1).put(0).put(1); buffer.put(0.5f).put(-0.5f).put(0); buffer.put(0).put(0).put(1).put(1); buffer.flip(); /*IntBuffer indices = ByteBuffer.allocateDirect(3 * 4).order(ByteOrder.nativeOrder()).asIntBuffer(); indices.put(0).put(1).put(2); indices.flip();*/ int vbo = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); while (!window.isCloseRequested()) { window.pollEvents(); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, vbo); glVertexPointer(3, GL_FLOAT, 7 * floatSize, 0); glColorPointer(4, GL_FLOAT, 7 * floatSize, 3 * floatSize); glDrawArrays(GL_TRIANGLES, 0, 3); window.swap(); window.sleep(16); } window.destroy(); }
@Override public ByteBuffer map(int off, int len) { if (off < 0 || len <= 0) { throw new IllegalArgumentException(); } final int end = off + len; if (end > bufferSizes[currentBufferIndex]) { bufferSizes[currentBufferIndex] = end; glBufferData(glTarget, bufferSizes[currentBufferIndex], glUsage); } ByteBuffer mapped; if (GLContext.getCapabilities().OpenGL30) { int flags = GL30.GL_MAP_WRITE_BIT | GL30.GL_MAP_UNSYNCHRONIZED_BIT; // | GL30.GL_MAP_INVALIDATE_RANGE_BIT; mapped = GL30.glMapBufferRange(glTarget, off, len, flags, null); } else if (GLContext.getCapabilities().GL_ARB_map_buffer_range) { int flags = ARBMapBufferRange.GL_MAP_WRITE_BIT | ARBMapBufferRange .GL_MAP_UNSYNCHRONIZED_BIT; // | ARBMapBufferRange.GL_MAP_INVALIDATE_RANGE_BIT; mapped = ARBMapBufferRange.glMapBufferRange(glTarget, off, len, flags, null); } else { mapped = GL15.glMapBuffer(glTarget, GL15.GL_WRITE_ONLY, null); } if (mapped == null) { throw new IllegalStateException( "mapped buffer is null: length=" + len + ", requestedSize=" + requestedSize + ", allocatedSize=" + allocatedSize); } return mapped; }
public void addVertices(Vertex[] vertices) { size = vertices.length; glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, Util.createFlippedBuffer(vertices), GL_STATIC_DRAW); }
@Override public void prepare(Program program) { // Create VAO vao = glGenVertexArrays(); glBindVertexArray(vao); // Create VBO FloatBuffer vertices = (FloatBuffer) BufferUtils.createFloatBuffer(6 * 6 * Program.VERTEX_SIZE) .put( new float[] { // X Y Z U V // bottom -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, // top -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // front -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // back -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, // left -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 0.0f, // right 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f }) .flip(); /* Generate Vertex Buffer Object */ vbo = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW); program.pointer("position", 3, 0); program.pointer("texcoord", 2, 3); }
public Mandelbrot( long platform, CLCapabilities platformCaps, GLFWWindow window, int deviceType, boolean debugGL, int maxIterations) { this.platform = platform; this.window = window; this.maxIterations = maxIterations; IntBuffer size = BufferUtils.createIntBuffer(2); nglfwGetWindowSize(window.handle, memAddress(size), memAddress(size) + 4); ww = size.get(0); wh = size.get(1); nglfwGetFramebufferSize(window.handle, memAddress(size), memAddress(size) + 4); fbw = size.get(0); fbh = size.get(1); glfwMakeContextCurrent(window.handle); GLCapabilities glCaps = GL.createCapabilities(); if (!glCaps.OpenGL30) throw new RuntimeException("OpenGL 3.0 is required to run this demo."); debugProc = debugGL ? GLUtil.setupDebugMessageCallback() : null; glfwSwapInterval(0); errcode_ret = BufferUtils.createIntBuffer(1); try { // Find devices with GL sharing support { long device = getDevice(platform, platformCaps, deviceType); if (device == NULL) device = getDevice(platform, platformCaps, CL_DEVICE_TYPE_CPU); if (device == NULL) throw new RuntimeException("No OpenCL devices found with OpenGL sharing support."); this.device = device; this.deviceCaps = CL.createDeviceCapabilities(device, platformCaps); } // Create the context PointerBuffer ctxProps = BufferUtils.createPointerBuffer(7); switch (Platform.get()) { case WINDOWS: ctxProps .put(CL_GL_CONTEXT_KHR) .put(glfwGetWGLContext(window.handle)) .put(CL_WGL_HDC_KHR) .put(wglGetCurrentDC()); break; case LINUX: ctxProps .put(CL_GL_CONTEXT_KHR) .put(glfwGetGLXContext(window.handle)) .put(CL_GLX_DISPLAY_KHR) .put(glfwGetX11Display()); break; case MACOSX: ctxProps .put(APPLEGLSharing.CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE) .put(CGLGetShareGroup(CGLGetCurrentContext())); } ctxProps.put(CL_CONTEXT_PLATFORM).put(platform).put(NULL).flip(); clContext = clCreateContext( ctxProps, device, clContextCB = new CLContextCallback() { @Override public void invoke(long errinfo, long private_info, long cb, long user_data) { log(String.format("cl_context_callback\n\tInfo: %s", memUTF8(errinfo))); } }, NULL, errcode_ret); checkCLError(errcode_ret); // create command queues for every GPU, setup colormap and init kernels IntBuffer colorMapBuffer = BufferUtils.createIntBuffer(32 * 2); initColorMap(colorMapBuffer, 32, Color.BLUE, Color.GREEN, Color.RED); clColorMap = clCreateBuffer( clContext, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, colorMapBuffer, errcode_ret); checkCLError(errcode_ret); // create command queue and upload color map buffer clQueue = clCreateCommandQueue(clContext, device, NULL, errcode_ret); checkCLError(errcode_ret); // load program(s) if (deviceType == CL_DEVICE_TYPE_GPU) log("OpenCL Device Type: GPU (Use -forceCPU to use CPU)"); else log("OpenCL Device Type: CPU"); log("Max Iterations: " + maxIterations + " (Use -iterations <count> to change)"); log("Display resolution: " + ww + "x" + wh + " (Use -res <width> <height> to change)"); log("OpenGL glCaps.GL_ARB_sync = " + glCaps.GL_ARB_sync); log("OpenGL glCaps.GL_ARB_cl_event = " + glCaps.GL_ARB_cl_event); buildProgram(); // Detect GLtoCL synchronization method syncGLtoCL = !glCaps.GL_ARB_cl_event; // GL3.2 or ARB_sync implied log(syncGLtoCL ? "GL to CL sync: Using clFinish" : "GL to CL sync: Using OpenCL events"); // Detect CLtoGL synchronization method syncCLtoGL = !deviceCaps.cl_khr_gl_event; log( syncCLtoGL ? "CL to GL sync: Using glFinish" : "CL to GL sync: Using implicit sync (cl_khr_gl_event)"); vao = glGenVertexArrays(); glBindVertexArray(vao); vbo = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData( GL_ARRAY_BUFFER, stackPush() .floats( 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f), GL_STATIC_DRAW); stackPop(); vsh = glCreateShader(GL_VERTEX_SHADER); glShaderSource( vsh, "#version 150\n" + "\n" + "uniform mat4 projection;\n" + "\n" + "uniform vec2 size;\n" + "\n" + "in vec2 posIN;\n" + "in vec2 texIN;\n" + "\n" + "out vec2 texCoord;\n" + "\n" + "void main(void) {\n" + "\tgl_Position = projection * vec4(posIN * size, 0.0, 1.0);\n" + "\ttexCoord = texIN;\n" + "}"); glCompileShader(vsh); String log = glGetShaderInfoLog(vsh, glGetShaderi(vsh, GL_INFO_LOG_LENGTH)); if (!log.isEmpty()) log(String.format("VERTEX SHADER LOG: %s", log)); fsh = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource( fsh, "#version 150\n" + "\n" + "uniform isampler2D mandelbrot;\n" + "\n" + "in vec2 texCoord;\n" + "\n" + "out vec4 fragColor;\n" + "\n" + "void main(void) {\n" + "\tfragColor = texture(mandelbrot, texCoord) / 255.0;\n" + "}"); glCompileShader(fsh); log = glGetShaderInfoLog(fsh, glGetShaderi(fsh, GL_INFO_LOG_LENGTH)); if (!log.isEmpty()) log(String.format("FRAGMENT SHADER LOG: %s", log)); glProgram = glCreateProgram(); glAttachShader(glProgram, vsh); glAttachShader(glProgram, fsh); glLinkProgram(glProgram); log = glGetProgramInfoLog(glProgram, glGetProgrami(glProgram, GL_INFO_LOG_LENGTH)); if (!log.isEmpty()) log(String.format("PROGRAM LOG: %s", log)); int posIN = glGetAttribLocation(glProgram, "posIN"); int texIN = glGetAttribLocation(glProgram, "texIN"); glVertexAttribPointer(posIN, 2, GL_FLOAT, false, 4 * 4, 0); glVertexAttribPointer(texIN, 2, GL_FLOAT, false, 4 * 4, 2 * 4); glEnableVertexAttribArray(posIN); glEnableVertexAttribArray(texIN); projectionUniform = glGetUniformLocation(glProgram, "projection"); sizeUniform = glGetUniformLocation(glProgram, "size"); glUseProgram(glProgram); glUniform1i(glGetUniformLocation(glProgram, "mandelbrot"), 0); } catch (Exception e) { // TODO: cleanup throw new RuntimeException(e); } glDisable(GL_DEPTH_TEST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); initGLObjects(); glFinish(); setKernelConstants(); glfwSetWindowSizeCallback( window.handle, window.windowsizefun = new GLFWWindowSizeCallback() { @Override public void invoke(long window, final int width, final int height) { if (width == 0 || height == 0) return; events.add( new Runnable() { @Override public void run() { Mandelbrot.this.ww = width; Mandelbrot.this.wh = height; shouldInitBuffers = true; } }); } }); glfwSetFramebufferSizeCallback( window.handle, window.framebuffersizefun = new GLFWFramebufferSizeCallback() { @Override public void invoke(long window, final int width, final int height) { if (width == 0 || height == 0) return; events.add( new Runnable() { @Override public void run() { Mandelbrot.this.fbw = width; Mandelbrot.this.fbh = height; shouldInitBuffers = true; } }); } }); glfwSetKeyCallback( window.handle, window.keyfun = new GLFWKeyCallback() { @Override public void invoke(long window, int key, int scancode, int action, int mods) { switch (key) { case GLFW_KEY_LEFT_CONTROL: case GLFW_KEY_RIGHT_CONTROL: ctrlDown = action == GLFW_PRESS; return; } if (action != GLFW_PRESS) return; switch (key) { case GLFW_KEY_ESCAPE: glfwSetWindowShouldClose(window, GLFW_TRUE); break; case GLFW_KEY_D: events.offer( new Runnable() { @Override public void run() { doublePrecision = !doublePrecision; log("DOUBLE PRECISION IS NOW: " + (doublePrecision ? "ON" : "OFF")); rebuild = true; } }); break; case GLFW_KEY_HOME: events.offer( new Runnable() { @Override public void run() { offsetX = -0.5; offsetY = 0.0; zoom = 1.0; } }); break; } } }); glfwSetMouseButtonCallback( window.handle, window.mousebuttonfun = new GLFWMouseButtonCallback() { @Override public void invoke(long window, int button, int action, int mods) { if (button != GLFW_MOUSE_BUTTON_LEFT) return; dragging = action == GLFW_PRESS; if (dragging) { dragging = true; dragX = mouseX; dragY = mouseY; dragOffsetX = offsetX; dragOffsetY = offsetY; } } }); glfwSetCursorPosCallback( window.handle, window.cursorposfun = new GLFWCursorPosCallback() { @Override public void invoke(long window, double xpos, double ypos) { mouseX = xpos; mouseY = wh - ypos; if (dragging) { offsetX = dragOffsetX + transformX(dragX - mouseX); offsetY = dragOffsetY + transformY(dragY - mouseY); } } }); glfwSetScrollCallback( window.handle, window.scrollfun = new GLFWScrollCallback() { @Override public void invoke(long window, double xoffset, double yoffset) { if (yoffset == 0) return; double scrollX = mouseX - ww * 0.5; double scrollY = mouseY - wh * 0.5; double zoomX = transformX(scrollX); double zoomY = transformY(scrollY); zoom *= (1.0 - yoffset * (ctrlDown ? 0.25 : 0.05)); offsetX += zoomX - transformX(scrollX); offsetY += zoomY - transformY(scrollY); } }); }