0
\$\begingroup\$

I am trying to familiarize myself with IBO's by drawing triangles etc but I'm finding no way to get the color to apply properly. Can I not use an IBO to index the color values as well? I am having another problem with the shapes themselves as I am trying to render two triangles to create a square but the triangles are not aligning properly.

Code:

//colors red, blue and green
float[] colors = {    1.0f, 0.0f, 0.0f, 1.0f, //Top color (red)
      0.0f, 1.0f, 0.0f, 1.0f, //Bottom Left color (black)
      0.0f, 0.0f, 1.0f, 0.9f
};

//the vertices I need to render a cube.
float[] dataBuffer = {-1,1,-1, //1
      -1,-1,-1,//2
      1,-1,-1,//3
      1, 1,-1,//4
      -1, 1,1,//5
      -1,-1, 1,//6
      1,-1,1,//7
      1,1,1//8
};

//int[] idxBuffer = {1,2,3,3,4,1,5,6,7,5,8,7,5,1,8,4,1,8,6,2,7,3,2,7,5,6,2,1,5,2,8,7,3,4,8,3};

//indexes for two triangles to create a square (result looks like roof of a house...)
int[] idxBuffer = {3,1,2,3,1,4};
//idx's for colors that I want to apply to each vertex in idxBuffer.
int[] idxColorBuffer = {1,2,3,1,2,3};


//*****************THE INITIALIZE ROUTINE ****************
 vboHandles = new int[4];
  gl.glGenBuffers(4, vboHandles, 0);

  //populate the position buffer
  FloatBuffer fbData = Buffers.newDirectFloatBuffer(dataBuffer);
  gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboHandles[VERTICES_IDX]); //the vertex data
  gl.glBufferData(GL4.GL_ARRAY_BUFFER, fbData.capacity() * 4, 
          fbData, GL4.GL_STATIC_DRAW);

   //populate the color buffer
  FloatBuffer fbColors = Buffers.newDirectFloatBuffer(colors);
  gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboHandles[COLOR_IDX]);
  gl.glBufferData(GL4.GL_ARRAY_BUFFER, fbColors.capacity() * 4, 
          fbColors, GL4.GL_STATIC_DRAW);   

  //index buffer IBO Vertex
  IntBuffer dtaIndc = Buffers.newDirectIntBuffer(idxBuffer);
  gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, vboHandles[2]); //bind the indices
  gl.glBufferData(GL4.GL_ELEMENT_ARRAY_BUFFER, dtaIndc.capacity() * (Integer.SIZE / Byte.SIZE), dtaIndc, GL4.GL_STATIC_DRAW);

  //index buffer IBO Colours (not sure if this is how it's done??)
  IntBuffer colorIndc = Buffers.newDirectIntBuffer(idxColorBuffer);
  gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, vboHandles[3]); 
  gl.glBufferData(GL4.GL_ELEMENT_ARRAY_BUFFER, colorIndc.capacity() * (Integer.SIZE / Byte.SIZE), colorIndc, GL4.GL_STATIC_DRAW);


  //set vertex array index
  IntBuffer intBuffer = BufferUtil.newIntBuffer(1);
  gl.glGenVertexArrays(1, intBuffer);
  iVao = intBuffer.get(0);
  gl.glBindVertexArray(iVao);

  gl.glEnableVertexAttribArray(0);
  gl.glEnableVertexAttribArray(1);

  // Select the VBO, GPU memory data, to use for colors
  gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, vboHandles[COLOR_IDX]);
  gl.glVertexAttribPointer(COLOR_IDX, 3, GL4.GL_FLOAT, false, 0, 0);

  gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboHandles[VERTICES_IDX]); //the vertex data
  // Associate Vertex attribute 1 with the last bound VBO
  gl.glVertexAttribPointer(VERTICES_IDX,4, GL2ES2.GL_FLOAT, false, 0,0);


  //use VERTICES_IDX to bind the element array to the vertex buffer.
  //this seems to sort of work...
  gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, vboHandles[2]); 
  gl.glVertexAttribPointer(VERTICES_IDX, 4, GL4.GL_INT, false, 0, 0); 

  //this I was hoping would bind the indexed colors but I get nothing...
/*    gl.glBindBuffer(GL4.GL_ELEMENT_ARRAY_BUFFER, vboHandles[3]); 
  //use COLOR_IDX to bind the element array to the vertex buffer.
  gl.glVertexAttribPointer(COLOR_IDX, 1, GL4.GL_INT, false, 0, 0);*/

//*********************THE DISPLAY ROUTINE*********************
//transform matrices to shaders etc...
//then...
//draw 2 triangles (6 vertices).
gl.glBindVertexArray(iVao);
//gl.glDrawArrays(GL4.GL_TRIANGLES, 0, dataBuffer.length / 3);

//draws two triangles that look like the roof of a house and colors
//them both red...should draw two triangles that look like a square
//and colors them varying shades of red, green, blue.
gl.glDrawElements(
                 GL4.GL_TRIANGLES,      // mode
                 idxBuffer.length,    // count
                 GL4.GL_UNSIGNED_INT,   // type
                 0           // element array buffer offset
);

Also, there is one parameter that confuses me in particular. In glVertexAttribPointer, the second param 'size' in the spec it states "Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, or 4. The initial value is 4." Is this not a setting for the number of components for a vertex which would be 3 (in my case anyway - I know there is a w)? Why is it in the following line that it must be 4?

gl.glVertexAttribPointer(VERTICES_IDX,4, GL2ES2.GL_FLOAT, false, 0,0);
\$\endgroup\$
1
\$\begingroup\$

The positions and color (and all other vertex attributes) all use the same index buffer, there is no way to change that. So if you have an index X then that vertex will have the position at index X and the color at index X.

The size parameter indicates how many values to take (1 to 4). Sometimes positions are stored in the homogenic format (essentially add another dimension which is always 1 for positions and 0 for directions). This helps with matrix transforms.

For your colors attribute that should be 4 because you also pass in the alpha channel. But for your vertex that should indeed be 3.

\$\endgroup\$
  • \$\begingroup\$ Ok, that makes sense then. It was likely the incorrect values being passed to glVertexAttribPointer that were causing my color to be red and the vertices to be off. Thanks. \$\endgroup\$ – eric_the_animal Oct 15 '14 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.