PDA

View Full Version : How to print out the data in VBO?



SagoO
01-13-2011, 09:44 PM
I have created an empty window. So, I already have the openGL context. The problem is I duno how to print the value of the VBO on the terminal but not the openGL window.



glGenBuffersARB(1, &pattern_buffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, pattern_buffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(pattern), pattern, GL_READ_WRITE_ARB);

int *ptr;
ptr = glMapBufferRange(pattern_buffer, pattern[0], 1, GL_MAP_READ_BIT);
printf("Ptr: \n",*ptr);

glutMainLoop();


The parameter for the glMapBufferRange correct? Or pointer problem?I think most probably is the pointer problem as segmentation fault happens again. glMapBufferRange return address of the buffer right?So, I print using *ptr...Is it wrong?I am totally confused already.. :confused:

Alfonse Reinheart
01-13-2011, 09:59 PM
You really don't seem to understand the whole buffer object thing.

Buffer objects are memory. They are memory that OpenGL owns, but you tell OpenGL when to allocate it and what it contains.

glBufferData (stop using the ARB extension. Buffer objects have been core OpenGL for over half a decade) allocates the memory and stores data into it. It's a combination of "malloc" and "memcpy".

So first, there's no need to map the buffer since you just copied data into it. You know what's there. There is no need to confirm it.

Second, "pattern" is the location in CPU memory that you told glBufferData to upload data from. Once the data has been uploaded, the contents of "pattern" are irrelevant.

Let's say you have this:



int pattern[5] = {1, 2, 3, 4, 5};
int bufferObjectData[5];

memcpy(bufferObjectData, pattern, sizeof(pattern));
pattern[2] = 10;


What is the value of "bufferObjectData[2]"? It's not 10.

After you have uploaded the data, if you are using the original pointer for anything other than size information, you're doing it wrong. So I have no idea what you were trying to do with that glMapBufferRange call, but "pattern[0]" is likely not doing anything useful.

Third, why are you mapping a pointer just to get one byte? That's what the third argument of glMapBufferRange is: the number of bytes to map. If you just want a byte, call glGetBufferSubData.

Fourth, even if you want to map a single byte (and you shouldn't), you can't pretend that the pointer you get back is a pointer to an int. Because you only mapped a byte, it is only legal to read a byte from the pointer you get back. The C/C++ type "int", for any hardware you would actually run this code on, is larger than a byte. If you wanted to read just an integer's worth of data (and again, use glGetBufferSubData), you need to get at least "sizeof(int)" bytes.

If you're going to read from a buffer object, you should in general be serious about it. Buffer objects live in OpenGL's memory, and they should be read only when you really need to.

Fifth:


glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(pattern), pattern, GL_READ_WRITE_ARB);

GL_READ_WRITE_ARB is not a legitimate buffer object usage hint. The wiki article (http://www.opengl.org/wiki/Buffer_Object#Buffer_Object_Usage) on the subject could tell you that.

SagoO
01-14-2011, 06:22 AM
glBufferData (stop using the ARB extension. Buffer objects have been core OpenGL for over half a decade) allocates the memory and stores data into it. It's a combination of "malloc" and "memcpy".

If I take off ARB extension, segmentation fault occurs. My graphic card is very old. ARB extension is used for old hardware right?I thought is the ARB extension cause the segmentation fault error.



So first, there's no need to map the buffer since you just copied data into it. You know what's there. There is no need to confirm it.

Actually what is the meaning of mapping?I just want to read the value inside the buffer. Any function that can read the value and return it to that particular function?



Third, why are you mapping a pointer just to get one byte? That's what the third argument of glMapBufferRange is: the number of bytes to map. If you just want a byte, call glGetBufferSubData.

This is the variable i declare and pass into the buffer.
GLubyte pattern[6] = {111, 114, 111, 106, 97, 110};

I just want to perform a test. What I want to do is to read each byte inside the buffer and compare which byte is matched. Eg: pattern[0] = pattern[2] in here...

Can show me some code how to use glGetBufferSubData? I not really understand the parameters I should put...

carsten neumann
01-14-2011, 08:19 AM
printf("Ptr: \n",*ptr);


Your format string (first arg to printf) does not contain any placeholder ('%d') for the value of *ptr. Also this will only show one value, you need to loop over all values in the VBO and print them.

SagoO
01-14-2011, 09:21 AM
I can read the value from the VBO using glMapBufferRange after many tries.



ptr = (GLubyte*) glMapBufferRange(GL_ARRAY_BUFFER_ARB,0,1,GL_MAP_RE AD_BIT);
printf("Ptr: %d\n",*ptr);


Is it possible to read value continuously? I found that glMapBufferRange cannot be used continuously.

I want to do something like this.

if(..glMapBufferRange(..)==..glMapBufferRange(..)) {
//do sth
}

Alfonse Reinheart
01-14-2011, 10:28 AM
If I take off ARB extension, segmentation fault occurs. My graphic card is very old.

If your graphics card is new enough that it has glMapBufferRange, then it's at least OpenGL 2.1 capable. And OpenGL 2.1 core has buffer objects.

You're getting a segmentation fault because you probably didn't get the function pointers correctly or something.


Actually what is the meaning of mapping?

The purpose of mapping the buffer is to access the data that is stored in it. You can issue OpenGL commands that will modify the data stored in a buffer object. And when this happens, you may need to read that data back on the CPU. This is when you might need to map the buffer for reading.

However, since you personally just loaded data into it, there is no need to read it. You know what is stored there. You could just read the "pattern" array, since that's exactly what is stored in the buffer object.


Can show me some code how to use glGetBufferSubData?

It's not a particularly complicated function (http://www.opengl.org/sdk/docs/man3/xhtml/glGetBufferSubData.xml). It's just doing a memcpy from a specific range of the buffer object into a pointer you provide.


Is it possible to read value continuously?

... what does that mean? How do you read any value continuously?

If you want to read from more than one byte of the buffer, then maybe you should map more than one byte of space. If you want a pointer to the whole buffer, then map the whole buffer instead of a small piece of it.

Your questions seem to indicate a lack of understanding of how low-level code works. Arrays, pointers, the fact that arrays and pointers are indistinguishable, memory, etc. You should have a firm understanding of how something like, "int anArray[20]" is laid out in memory as a sequence of bytes, as well as how functions like "memcpy", "malloc", and the like work. You may need to do some Google legwork.

ugluk
01-14-2011, 02:26 PM
SagO never mind the more experienced GL users, just head over to NeHe, do some VBO tuts. You can also search for VBO tuts elsewhere. Then once you see some examples, try to do your own thing, whatever it may be.

Alfonse Reinheart
01-14-2011, 03:34 PM
Then once you see some examples, try to do your own thing, whatever it may be.

And then he'll be right back here, asking about why his copy-and-pasted code doesn't work.

It's far better to understand why something does or doesn't work than to just copy someone else's code.

ugluk
01-14-2011, 03:52 PM
I'm an optimist, SagO, don't copy-paste.