PDA

View Full Version : Point VBO



WickedP
04-28-2015, 07:57 PM
Hi folks,

just looking for a bit of education on vertex buffers. I'm trying to draw a bunch of points via a vbo - firstly, is this fine? If yes..

I'm setting up my vbo in a custom function. The setup looks a little like this:



void My_Window::SetupBufferObject(int Size)
{
GLuint buffer = NULL;

// POSITION
glGenBuffers(1,&buffer);
PointVBO[Size][0] = buffer;
glBindBuffer(GL_ARRAY_BUFFER,buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(pointPos),poin tPos,GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,NULL,sizeof(pointP os),pointPos);

// COLOUR
glGenBuffers(1,&buffer);
PointVBO[Size][1] = buffer;
glBindBuffer(GL_ARRAY_BUFFER,buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(pointCol),poin tCol,GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,NULL,sizeof(pointC ol),pointCol);
}


For the purpose of the above, PointVBO is just a vector array of int's used for holding ids of buffers, and Size is set to 1. I'm confident the data is also good - I can draw the points manually each frame via a for loop, using pointPos and pointCol, and everything appears to be in order as far as that's concerned. At this stage, does that look alright?

The draw calls themselves is where I don't think I'm understanding things well. I've tried quite a few variations of the following, but none are getting me anywhere. I'll post where it's at now so you can at least see something I've tried with:



void My_Window::DrawGL(void)
{
... // setup/begin draw

glBindBuffer(GL_ARRAY_BUFFER,PointVBO[1][0]);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(200*100*3,GL_BYTE,3*sizeof(char),&pointPos[0]);

glBindBuffer(GL_ARRAY_BUFFER,PointVBO[1][1]);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(200*100*3,GL_BYTE,3*sizeof(char),0) ;

glDrawArrays(GL_POINTS,0,200*100);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

... // draw other/end draw
}


I'm trying to use separate buffers for the positioning and colours so that I can dig into those at some stage in the future and work on them individually. Is this where a VAO might come in handy?

What and where am I missing things here? Cheers,

WP.

Alfonse Reinheart
04-28-2015, 08:33 PM
glBufferData(GL_ARRAY_BUFFER,sizeof(pointPos),poin tPos,GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,NULL,sizeof(pointP os),pointPos);

This is redundant. glBufferData already took a pointer to the data you wanted to upload. You don't need to call glBufferSubData to upload data you already uploaded.

Also, NULL is not the same thing as 0.

But these aren't your problems.


glVertexPointer(200*100*3,GL_BYTE,3*sizeof(char),&pointPos[0]);

There are many things wrong with this. (https://www.opengl.org/sdk/docs/man2/xhtml/glVertexPointer.xml)

The first parameter is not the size of the array. It's the number of components in one vertex. If you're transmitting two-dimensional data, then you would put 2 there. If your points are 3-dimensional (X, Y and Z), you put 3 there. Since you set your stride to be 3 * sizeof(char) (which should be 3 * sizeof(GLbyte)), I'm guessing every 3 bytes represents the position of a single point. So you would set this to 3.

Equally importantly, when a buffer object is bound to GL_ARRAY_BUFFER, the meaning of the last parameter changes. It is no longer a pointer value. It is instead a byte offset from the beginning of the buffer bound to GL_ARRAY_BUFFER at the time this function was called. Since your vertex data starts at the beginning of that buffer, you should set it to zero.

Or, since the parameter's type is still a pointer, even though OpenGL will treat it as an integer byte offset, it should more accurately be reinterpret_cast<void*>(0);

I see you did your glColorPointer call correctly with regard to the zero (though without the reinterpret_cast), so this is probably just a copy-and-paste bug. But the size is also still wrong.

Also, since your size was not 2, 3, or 4, you should have gotten an OpenGL error (https://www.opengl.org/wiki/OpenGL_Error). You should always check your errors when things start going wrong.

WickedP
04-29-2015, 05:47 AM
My apologies if this appears twice, I seem to be having an few issues with this website and IE.

Thanks Alfonse, that's cleared a few things up. I've made a few small changes, mainly with the data type, and the point positions seem fine. But I'm still having an issue with the colour. Apart from one pixel, they are all black. These are the draw lines associated with it:



// in draw function
glBindBuffer(GL_ARRAY_BUFFER,PointVBO[1][1]);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3,GL_UNSIGNED_BYTE,0,reinterpret_ca st<void*>(0));


I've placed error checking at the end of each line, but no errors are reported (removed in above to save some reading). The colour data is coming from an unsigned *char container. Is that the cause of the black wash out?

WP.

WickedP
04-29-2015, 07:54 PM
Alrighty, I've managed to fix the colour issue. It had something to do with the colour pixel values themselves. I converted them to floating point, and all appears to be working well.

Just while I'm here, something is wonky with using the reply link and IE on this site. It isn't working for me every first time I use it. I get the reply edit text area, but hitting the 'Post Quick Reply' button never submits the reply on the first go. Should I report this somewhere?

WP.