[SOLVED] VBO and Shaders

Hey everybody, new here and I’m in a bit of a pickle. I have been researching VBOs for a couple weeks now and have gotten stuck. I cannot get the VBO to render when I use glVertexAttribPointer() and enable shaders. I don’t think I have to call glGenArrays(), but I’m not sure. Anyway I’ve attached the relevant code below.

Edit: I’m using JOGL

Init VBO code:


//=====================================
//VERTEX ARRAY===========================		
//=====================================
FloatBuffer vertex_data = Buffers.newDirectFloatBuffer(m_vertices.length);
vertex_data.put(m_vertices);
vertex_data.flip();

gl.glGenBuffers(1, m_vbo_handle, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_vbo_handle[0]);
gl.glBufferData(GL.GL_ARRAY_BUFFER, m_vertices.length * Buffers.SIZEOF_FLOAT, vertex_data, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

		
//=====================================
//COLOR ARRAY===========================	
//=====================================
FloatBuffer color_data = Buffers.newDirectFloatBuffer(m_vertices.length);
color_data.put(color_array);
color_data.flip();

gl.glGenBuffers(1, m_rgb_handle, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_rgb_handle[0]);
gl.glBufferData(GL.GL_ARRAY_BUFFER, m_vertices.length * Buffers.SIZEOF_FLOAT, color_data, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

//=====================================
//TEXCOORD ARRAY========================	
//=====================================
FloatBuffer tex_coord_data = Buffers.newDirectFloatBuffer(m_uvs.length);
tex_coord_data.put(m_uvs);
tex_coord_data.flip();

gl.glGenBuffers(1, m_tcd_handle, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_tcd_handle[0]);
gl.glBufferData(GL.GL_ARRAY_BUFFER, m_uvs.length * Buffers.SIZEOF_FLOAT, tex_coord_data, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
		
//=====================================
//INDEX ARRAY============================		
//=====================================
IntBuffer index_data = Buffers.newDirectIntBuffer(index_array.length);
index_data.put(index_array);
index_data.flip();

gl.glGenBuffers(1, m_ind_handle, 0);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, m_ind_handle[0]);
gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, index_array.length * Buffers.SIZEOF_INT, index_data, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
		
//Don't forget to save the number of indices so we can draw!
m_num_indices = index_array.length;

Draw code:


int vert_pos = gl.glGetAttribLocation(engine.m_simple_shader.program_index, "vert_pos");
int color = gl.glGetAttribLocation(engine.m_simple_shader.program_index, "color");
			
engine.m_simple_shader.useShader(gl);//This uses my own shader class, it works, I promise
			
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_vbo_handle[0]);
gl.glVertexAttribPointer(vert_pos, 3, GL.GL_FLOAT, false, 0, 0L);
gl.glEnableVertexAttribArray(vert_pos);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_rgb_handle[0]);
gl.glEnableVertexAttribArray(color);
gl.glVertexAttribPointer(color, 3, GL.GL_FLOAT, false, 0, 0L);
			
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, m_ind_handle[0]);
gl.glDrawElements(GL.GL_TRIANGLES, m_num_indices, GL.GL_UNSIGNED_SHORT, 0);
			
gl.glDisableVertexAttribArray(vert_pos);
gl.glDisableVertexAttribArray(color);

Vertex Shader:


in vec3 vert_pos;
in vec3 color;
out vec3 pass_color;

void main(void)
{
   gl_Position = gl_ModelViewMatrix * vec4(vert_pos.x, vert_pos.y, vert_pos.z, 1.0);
   pass_color = color;
}

Frag Shader:


in vec3 pass_color;
out vec4 out_color;

void main(void)
{
   out_color = vec4(pass_color.x, pass_color.y, pass_color.z, 1.0);
   gl_FragColor = out_color;
}

I can get this to render using the deprecated glEnableClientState() way, so my indices/vertices/color/texcoord generation is/should be fine. I am also sure that my Shader class code works as I’ve used it another project. So linking and using the shader shouldn’t be a problem. (I’ve also bound the vert_pos and color attribs to 4 and 5, and I get those as the returned value).

Any help would be appreciated, thanks!

Try simplifying it down to just vertices, get it working, and then build back up, and then it will be easier to find where you are going wrong.

I have, it still doesn’t render. Shouldn’t it at least render a black box if it doesn’t have color? (I don’t know if I mentioned it but this VBO only contains a cube)


int vert_pos = gl.glGetAttribLocation(engine.m_simple_shader.program_index, "vert_pos");
			
engine.m_simple_shader.useShader(gl);
			
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_vbo_handle[0]);
gl.glVertexAttribPointer(vert_pos, 3, GL.GL_FLOAT, false, 0, 0L);
gl.glEnableVertexAttribArray(vert_pos);
			
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, m_ind_handle[0]);
gl.glDrawElements(GL.GL_TRIANGLES, m_num_indices, GL.GL_UNSIGNED_SHORT, 0);
			
gl.glDisableVertexAttribArray(vert_pos);

i think it’s the unsigned_short that makes your gpu explode.
really it does … with the opposite actually, drawing an integer elements … while it is a short buffer.

the element array looks like it’s filled with integers. change glDrawElements to integer type …

or make index_array a short[] array, index_data a ShortBuffer and allocate * Buffers.SIZEOF_SHORT (or simply 2). meshes smaller then 32767 vertices draw a bit faster then.

Oops, guess I auto-piloted that part. Alas it still doesn’t fix anything…

I feel like I say this a lot… but [icode]GL11.glGetError();[/icode] is invaluable when trying to debug OpenGL.
When you figure out which function returns an error, use the OpenGL API documentation to figure out why said error is being returned. In fact, even just reading up on the GL functions you’re using will help you in your understanding and ability to debug immensely.

Depends on what colour you’re passing in with your shader.

I’ve done this. There are no errors. It returns 0. I have a pretty good idea of what these functions do, but I’m missing something.

Well [icode]glVertexAttribPointer[/icode] should have it’s stride argument as the number of elements * Buffers.SIZEOF_FLOAT.
Therefore, you should have a line like [icode]glVertexAttribPointer(vert_pos, 3, GL.GL_FLOAT, false, 16, 0L);[/icode].

Also at nowhere in your presented code do you call [icode]glBindAttribLocation[/icode] before calling [icode]glGetAttribLocation[/icode], meaning the get function will have nothing to get.

If you print out the value that is returned by [icode]glGetAttribLocation[/icode], the docs say it should be returning -1.

EDIT: The Lion King pointed out to me that you’re creating your W value inside your shader, so it should be [icode]glVertexAttribPointer(vert_pos, 3, GL.GL_FLOAT, false, 12, 0L);[/icode] instead.
.

From my first post:

I've also bound the vert_pos and color attribs to 4 and 5, and I get those as the returned value

Also, I can draw the same cube using the following and it works just fine.


gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_vbo_handle[0]);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0L);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_rgb_handle[0]);
gl.glColorPointer(3, GL.GL_FLOAT, 0, 0L);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, m_ind_handle[0]);
gl.glDrawElements(GL.GL_TRIANGLES, m_num_indices, GL.GL_UNSIGNED_INT, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);

but this isn’t using the shader and doesn’t the 3 in

glVertexAttribPointer(vert_pos, 3, GL.GL_FLOAT, false, 0, 0L);

tell openGL the size so I shouldn’t need a stride listed since I’m not interleaving?


in vec3 vert_pos;
in vec3 color;
out vec3 pass_color;

void main(void)
{
   gl_Position = gl_ModelViewMatrix * vec4(vert_pos.x, vert_pos.y, vert_pos.z, 1.0);
   pass_color = color;
}

You are not specifying the ModelViewMatrix try removing that part from the code. Could you also tell us whats happening (what you are seeing) and throw in a couple glGetErrors. you are correct about not having the specify stride in that case though it is a good habit

OK, I’ll throw in the stride then, do I need to change the size to 4? Or do I not need to worry about that because I’m adding the W in the shader?

Removing the modelview_matrix doesn’t affect anything (that I can see). Let me see if I can get an image on here for you guys, but I can tell you if I take out the extra draw code I see nothing, just the clear color.

So here’s the draw code for the mesh, which is currently a cube…well three cubes, but one won’t draw…the other two are in the proper positions.


public boolean drawMesh(GL2 gl, GFX_Engine engine, int tga_handle)
	{
		boolean drawn = false;

		gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glPushMatrix();
		
		Vector3 translate = m_transform.getTranslate();
		Vector3 rotation = m_transform.getRotation();
		Vector3 scale = m_transform.getScale();

		gl.glTranslatef(translate.m_x, translate.m_y, translate.m_z);
		gl.glScalef(scale.m_x, scale.m_y, scale.m_z);
		gl.glRotatef(rotation.m_x, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(rotation.m_y, 0.0f, 1.0f, 0.0f);
		gl.glRotatef(rotation.m_z, 0.0f, 0.0f, 1.0f);

		gl.glEnable(GL.GL_TEXTURE_2D);
		gl.glActiveTexture(GL.GL_TEXTURE0);//This is always the text texture
		gl.glBindTexture(GL.GL_TEXTURE_2D, tga_handle);

		gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
//			gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
		gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_vbo_handle[0]);
		gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0L);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_rgb_handle[0]);
		gl.glColorPointer(3, GL.GL_FLOAT, 0, 0L);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_tcd_handle[0]);
		gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0L);
		gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, m_ind_handle[0]);
//			gl.glDrawArrays(GL2.GL_TRIANGLE_STRIP, 0, m_vertices.length/3);//draw type, position, amount of vertices
		gl.glDrawElements(GL.GL_TRIANGLES, m_num_indices, GL.GL_UNSIGNED_INT, 0);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
		gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
		gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
//			gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
		gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
			

		gl.glTranslatef(translate.m_x, translate.m_y + 5.0f, translate.m_z);
			
			
//			gl.glDisable(GL.GL_TEXTURE_2D);


//================vTHIS IS THE BROKEN PARTv=====================================
//================vTHIS IS THE BROKEN PARTv=====================================
		engine.m_simple_shader.useShader(gl);

		int vert_pos = gl.glGetAttribLocation(engine.m_simple_shader.program_index, "vert_pos");
		int color = gl.glGetAttribLocation(engine.m_simple_shader.program_index, "color");
			
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_vbo_handle[0]);
		gl.glVertexAttribPointer(vert_pos, 3, GL.GL_FLOAT, false, 0, 0L);
		gl.glEnableVertexAttribArray(vert_pos);

		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_rgb_handle[0]);
		gl.glEnableVertexAttribArray(color);
		gl.glVertexAttribPointer(color, 3, GL.GL_FLOAT, false, 0, 0L);
			
		gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, m_ind_handle[0]);
		gl.glDrawElements(GL.GL_TRIANGLES, m_num_indices, GL.GL_UNSIGNED_INT, 0);
			
		gl.glDisableVertexAttribArray(vert_pos);
		gl.glDisableVertexAttribArray(color);

	        gl.glUseProgram(0);
//================^THIS IS THE BROKEN PART^===================================
//================^THIS IS THE BROKEN PART^===================================
			
		gl.glTranslatef(translate.m_x + 5.0f, translate.m_y, translate.m_z);
			
		gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_vbo_handle[0]);
		gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0L);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_rgb_handle[0]);
		gl.glColorPointer(3, GL.GL_FLOAT, 0, 0L);
		gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, m_ind_handle[0]);
		gl.glDrawElements(GL.GL_TRIANGLES, m_num_indices, GL.GL_UNSIGNED_INT, 0);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
		gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
		gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
		gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);

		drawn = true;
		
                gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glPopMatrix();

		return drawn;
	}




Could it be a case that you’ve transformed the camera the wrong way and you’re just not looking at what’s being drawn?

Nope =(, I can draw to the exact spot and see it if I use any other draw method…

I think it’s been said before, but the stride should not be zero.

This shouldn’t fix it, but [icode]glVertexAttribPointer[/icode] doesn’t change every frame: you can call it at the beginning and leave it, you don’t have to call it each frame.


gl.glVertexAttribPointer(vert_pos, 3, GL.GL_FLOAT, false, 0, 0L);
      gl.glEnableVertexAttribArray(vert_pos);

try flipping these two lines

here’s what I see…


http://i.imgur.com/uN4Lt7K.png

Imgur

@NegativeZero, oh really? Thanks

@The Lion King, I have and just did again, no dice.

@HeroesGraveDev, turns out I wasn’t looking at the geometry…Apparently I need to send a new matrix over to my shader.

OK so here goes, this was a combination of two things, gl_FragColor = out_color; <–That broke my shader and it started returning -1 for the attrib locations, after removing that line again it fixed the Shader. However, it still wouldn’t render in view, because for whatever reason gl_ModelViewMatrix doesn’t work in this app (or maybe just with VBOs). I’m really confused as to why, but here are some images to help explain the situation. I think I can figure it out from here, but if anyone has comments to nudge me in the right direction I won’t be opposed.

So, if I DO NOT call glEnable(GL_CULL_FACE); I get this:

http://i.imgur.com/Ee6uodb.png

If I DO call glEnable(GL_CULL_FACE); I get this:

http://i.imgur.com/I6RIrEi.png

Now I’m including this next image to show that the cube is, in fact, inverted:

If you look at the corners of the background, infinitely large cube, you can see that the colors are flipped from what they should be. If I try to translate the infinite cube it disappears, if I move the camera around the corners/color stay where they are.


http://i.imgur.com/Wzyrjxd.png

Anyway I appreciate all your help!