jeffg
Re: Color Array in VBO
Reply #2 - Mar 16th , 2010, 7:41am
Well, I thought I had figured it out.. but not quite. The color array is somehow tied to the positions vertex array, and not the element array. So, here is a new example, the bar that crosses should be orange, but instead it ties the color point to 0 and the color point to 5 making a gradient line. Could someone straiten me out and get this forth line orange. import processing.opengl.*; import javax.media.opengl.*; import com.sun.opengl.util.*; import java.nio.*; PGraphicsOpenGL pgl; GL gl; int[] elements_vbo = new int[1]; int[] positions_vbo = new int[1]; int[] colors_vbo = new int[1]; void setup() { size( 400, 400, OPENGL ); pgl = (PGraphicsOpenGL) g; gl = pgl.gl; //Vertexs - 6 vertexes, 3 floats xyz FloatBuffer pos = BufferUtil.newFloatBuffer( 6 * 3 ); pos.put( 100 ); pos.put( 50 ); pos.put( 0 ); pos.put( 100 ); pos.put( 300 ); pos.put( 0 ); pos.put( 200 ); pos.put( 50 ); pos.put( 0 ); pos.put( 200 ); pos.put( 300 ); pos.put( 0 ); pos.put( 300 ); pos.put( 50 ); pos.put( 0 ); pos.put( 300 ); pos.put( 300 ); pos.put( 0 ); pos.rewind(); //Elements - 4 lines, 2 vertexs IntBuffer el = BufferUtil.newIntBuffer( 4 * 2 ); el.put( 0 ); el.put( 1 ); el.put( 2 ); el.put( 3 ); el.put( 4 ); el.put( 5 ); el.put( 0 ); el.put( 5 ); el.rewind(); //Colors - 4 lines, 2 colors (one for each vertex - correct?), 4 floats rgba FloatBuffer col = BufferUtil.newFloatBuffer( 4 * 2 * 4 ); col.put( 1.0 ); //first line should be RED (1,0,0,1) * 2 col.put( 0.0 ); col.put( 0.0 ); col.put( 1.0 ); col.put( 1.0 ); col.put( 0.0 ); col.put( 0.0 ); col.put( 1.0 ); col.put( 0.0 ); //second line should be GREEN (0,1,0,1) * 2 col.put( 1.0 ); col.put( 0.0 ); col.put( 1.0 ); col.put( 0.0 ); col.put( 1.0 ); col.put( 0.0 ); col.put( 1.0 ); col.put( 0.0 ); //third line should be BLUE (0,0,1,1) * 2 col.put( 0.0 ); col.put( 1.0 ); col.put( 1.0 ); col.put( 0.0 ); col.put( 0.0 ); col.put( 1.0 ); col.put( 1.0 ); col.put( 1.0 ); //forth line should be ORANGE (1,.5,0,1) * 2 col.put( 0.5 ); col.put( 0.0 ); col.put( 1.0 ); col.put( 1.0 ); col.put( 0.5 ); col.put( 0.0 ); col.put( 1.0 ); col.rewind(); pgl.beginGL(); gl.glGenBuffers( 1, positions_vbo, 0 ); gl.glBindBuffer( GL.GL_ARRAY_BUFFER, positions_vbo[0] ); gl.glBufferData( GL.GL_ARRAY_BUFFER, 6 * 3 * BufferUtil.SIZEOF_FLOAT, pos, GL.GL_STATIC_DRAW ); pos = null; gl.glGenBuffers( 1, elements_vbo, 0 ); gl.glBindBuffer( GL.GL_ELEMENT_ARRAY_BUFFER, elements_vbo[0] ); gl.glBufferData( GL.GL_ELEMENT_ARRAY_BUFFER, 4 * 2 * BufferUtil.SIZEOF_INT, el, GL.GL_STATIC_DRAW ); el = null; gl.glGenBuffers( 1, colors_vbo, 0 ); gl.glBindBuffer( GL.GL_ARRAY_BUFFER, colors_vbo[0] ); gl.glBufferData( GL.GL_ARRAY_BUFFER, 4 * 2 * 4 * BufferUtil.SIZEOF_FLOAT, col, GL.GL_STATIC_DRAW ); col = null; pgl.endGL(); } void draw() { pgl.beginGL(); gl.glLineWidth(10.0); gl.glEnable (gl.GL_LINE_SMOOTH); gl.glEnableClientState(GL.GL_COLOR_ARRAY); gl.glEnableClientState(GL.GL_VERTEX_ARRAY); gl.glBindBuffer( GL.GL_ARRAY_BUFFER, colors_vbo[0] ); gl.glColorPointer(4,GL.GL_FLOAT,0,0); gl.glBindBuffer( GL.GL_ARRAY_BUFFER, positions_vbo[0] ); gl.glVertexPointer(3,GL.GL_FLOAT,0,0); gl.glBindBuffer( GL.GL_ELEMENT_ARRAY_BUFFER, elements_vbo[0] ); gl.glDrawElements( GL.GL_LINES, 4 * 2, GL.GL_UNSIGNED_INT, 0 ); gl.glBindBuffer( gl.GL_ARRAY_BUFFER, 0); gl.glBindBuffer( gl.GL_ELEMENT_ARRAY_BUFFER, 0); gl.glDisableClientState(GL.GL_VERTEX_ARRAY); gl.glDisableClientState(GL.GL_COLOR_ARRAY); gl.glDisable (gl.GL_LINE_SMOOTH); pgl.endGL(); } void stop() { gl.glDeleteBuffers( 1, positions_vbo, 0 ); gl.glDeleteBuffers( 1, colors_vbo, 0 ); gl.glDeleteBuffers( 1, elements_vbo, 0 ); }