Re: MultiDrawElements
Posted by James on Jan 14, 2016; 1:29pm
URL: https://forum.jogamp.org/MultiDrawElements-tp4036014p4036022.html
Thanks I've been looking at the example and made a few changes it now draws first red square using MultidrawElements but still not fully working here's my updated code. Yes I would definitely be interested in GL4 example
package demo;
import com.jogamp.common.nio.PointerBuffer;
import com.jogamp.opengl.GL4;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.util.GLBuffers;
import demo.common.core.BasicDemo;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import static com.jogamp.opengl.GL.*;
/**
* Created by James on 20/12/2015.
*
* Draws 3 squares using method drawElements
* 3 primitive each made up of 2 triangles using 4 vertices (6 elements)
* 1 red, 1 green, and 1 blue
*/
public class MultiDrawElements2 extends BasicDemo {
private static final boolean fps = false;
public MultiDrawElements2(final boolean fps) {
super(fps);
}
public static void main(final String... args) {
new MultiDrawElements2(fps);
}
// 3 0
// 2 1
private static final float[] VERTEX_DATA = {
250.0f, 250.0f, 0.0f, 1.0f, // 0 right top
250.0f, 350.0f, 0.0f, 1.0f, // 1 right bottom - reused
150.0f, 350.0f, 0.0f, 1.0f, // 2 left bottom
150.0f, 250.0f, 0.0f, 1.0f, // 3 left top - reused
450.0f, 250.0f, 0.0f, 1.0f,
450.0f, 350.0f, 0.0f, 1.0f,
350.0f, 350.0f, 0.0f, 1.0f,
350.0f, 250.0f, 0.0f, 1.0f,
650.0f, 250.0f, 0.0f, 1.0f,
650.0f, 350.0f, 0.0f, 1.0f,
550.0f, 350.0f, 0.0f, 1.0f,
550.0f, 250.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
private static final int[] ELEMENT_DATA = { // Note that we start from 0!
0, 1, 3,
1, 2, 3
};
private static final int PRIM_COUNT = 3;
private static final int VERTICES_COUNT = 4;
private static final int VERTICES_SIZE = 4;
private static final int COLOUR_SIZE = 4;
private static final int ELEMENT_COUNT = 6;
private static final int COLOUR_START = PRIM_COUNT * VERTICES_COUNT * VERTICES_SIZE * GLBuffers.SIZEOF_FLOAT;
private static final int[] COUNT = { ELEMENT_COUNT, ELEMENT_COUNT, ELEMENT_COUNT };
private static final IntBuffer ELEMENT_COUNT_INT_BUFFER = GLBuffers.newDirectIntBuffer(COUNT);
private static final int[] VAO = new int[1];
private static final int[] VBO = new int[1];
private static final int[] EBO = new int[1];
@Override
public void init(GLAutoDrawable glAutoDrawable) {
super.init(glAutoDrawable);
final GL4 gl = glAutoDrawable.getGL().getGL4();
gl.glGenVertexArrays(VAO.length, IntBuffer.wrap(VAO));
gl.glGenBuffers(VBO.length, IntBuffer.wrap(VBO));
gl.glGenBuffers(EBO.length, IntBuffer.wrap(EBO));
gl.glBindVertexArray(VAO[0]);
gl.glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
{
final FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(VERTEX_DATA);
gl.glBufferData(GL_ARRAY_BUFFER, VERTEX_DATA.length * GLBuffers.SIZEOF_FLOAT, buffer, GL_STATIC_DRAW);
}
gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[0]);
{
final IntBuffer buffer = GLBuffers.newDirectIntBuffer(ELEMENT_DATA);
gl.glBufferData(GL_ELEMENT_ARRAY_BUFFER, ELEMENT_DATA.length * GLBuffers.SIZEOF_INT, buffer, GL_STATIC_DRAW);
}
gl.glVertexAttribPointer(0, VERTICES_SIZE, GL_FLOAT, false, 0, 0);
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(1, COLOUR_SIZE, GL_FLOAT, false, 0, COLOUR_START);
gl.glEnableVertexAttribArray(1);
}
@Override
public void display(GLAutoDrawable glAutoDrawable) {
super.display(glAutoDrawable);
final GL4 gl = glAutoDrawable.getGL().getGL4();
gl.glUseProgram(PROGRAM[0]);
{
gl.glBindVertexArray(VAO[0]);
final PointerBuffer pointerBuffer = PointerBuffer.allocateDirect(3);
pointerBuffer.put(0);
pointerBuffer.put(0);
pointerBuffer.put(0);
pointerBuffer.rewind();
gl.glUniformMatrix4fv(LOCATIONS[Location.PROJECTION_MATRIX.getLocation()], 1, false, PROJECTION_MATRIX, 0);
gl.glMultiDrawElements(GL_TRIANGLES, ELEMENT_COUNT_INT_BUFFER, GL_UNSIGNED_INT, pointerBuffer, PRIM_COUNT);
}
gl.glUseProgram(0);
}
}