We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Color Array in VBO (Read 2553 times)
Color Array in VBO
Feb 19th, 2010, 1:15pm
 
Wondering if someone could point out my error here.  I'm testing out some VBO stuff and I can't quite get the color buffer matched up.  The vertex and element array seem to be working fine.  As a small example, I created three lines, which should (from left to right) be red, green, blue.  However, I get an mix of colors.  I'm also getting an error 1280 at top endDraw(): invalid enumerant if anyone knows what that is about.

Help get my color array working - Thanks!

Code:
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 - 3 lines, 2 vertexs
 IntBuffer el  = BufferUtil.newIntBuffer( 3 * 2 );
 el.put( 0 );
 el.put( 1 );
 el.put( 2 );
 el.put( 3 );
 el.put( 4 );
 el.put( 5 );
 el.rewind();

 //Colors - 3 lines, 2 colors (one for each vertex - correct?), 4 floats rgba
 FloatBuffer col  = BufferUtil.newFloatBuffer( 3 * 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.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, 3 * 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, 3 * 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, positions_vbo[0] );
 gl.glVertexPointer(3,GL.GL_FLOAT,0,0);
 gl.glBindBuffer( GL.GL_COLOR_ARRAY, colors_vbo[0] );
 gl.glColorPointer(4,GL.GL_FLOAT,0,0);
 gl.glBindBuffer( GL.GL_ELEMENT_ARRAY_BUFFER, elements_vbo[0] );
 gl.glDrawElements( GL.GL_LINES, 3 * 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 );
}
Re: Color Array in VBO
Reply #1 - Feb 19th, 2010, 2:13pm
 
Figured it out... I had it out of order and I was calling a COLOR_ARRAY instead of an ARRAY_BUFFER.  I'm still not sure what the 1280 error was about but "hint(DISABLE_OPENGL_ERROR_REPORT);" removes it, though this may just be cutting the dashboard lights.

Code:
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, 3 * 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();
}
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 );
}
Re: Color Array in VBO
Reply #3 - Mar 16th, 2010, 3:34pm
 
your element index list will index into the vertex and color array
for example the first line picks vertex 0 and vertex 1 from the vertexlist.
it will also pick the color0 and color 1 from the color list..

what you need is duplicate vertex position and different colors.

hope that helps,

have fun.
Re: Color Array in VBO
Reply #4 - Mar 16th, 2010, 3:51pm
 
This does clarify things... and gives me some other ways I might try to implement it.  It looks like, at least for the lines, this would be better implemented as a glDrawArray.  I can then create a index on the unique vertexes.
Re: Color Array in VBO
Reply #5 - Mar 16th, 2010, 4:14pm
 
just think of it this way:
if your have a vertex that for some reason has different component values, color, texcoord, tangent, whatever.. in practice it is a different vertex.
Page Index Toggle Pages: 1