Results 1 to 9 of 9

Thread: How to print out the data in VBO?

  1. #1
    Senior Member Regular Contributor
    Join Date
    Aug 2010
    Posts
    114

    How to print out the data in VBO?

    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.

    Code :
    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..

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,673

    Re: How to print out the data in VBO?

    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:

    Code :
    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:

    Code :
    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 on the subject could tell you that.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Aug 2010
    Posts
    114

    Re: How to print out the data in VBO?

    Quote Originally Posted by Alfonse Reinheart
    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.

    Quote Originally Posted by Alfonse Reinheart
    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?

    Quote Originally Posted by Alfonse Reinheart
    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...

  4. #4
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896

    Re: How to print out the data in VBO?

    Code :
    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.

  5. #5
    Senior Member Regular Contributor
    Join Date
    Aug 2010
    Posts
    114

    Re: How to print out the data in VBO?

    I can read the value from the VBO using glMapBufferRange after many tries.

    Code :
    ptr = (GLubyte*) glMapBufferRange(GL_ARRAY_BUFFER_ARB,0,1,GL_MAP_READ_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
    }

  6. #6
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,673

    Re: How to print out the data in VBO?

    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. 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.

  7. #7
    Senior Member Frequent Contributor
    Join Date
    Oct 2009
    Posts
    592

    Re: How to print out the data in VBO?

    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.

  8. #8
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,673

    Re: How to print out the data in VBO?

    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.

  9. #9
    Senior Member Frequent Contributor
    Join Date
    Oct 2009
    Posts
    592

    Re: How to print out the data in VBO?

    I'm an optimist, SagO, don't copy-paste.

Similar Threads

  1. Why canīt I print a GL_POINTS?
    By marce in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-10-2009, 07:00 AM
  2. about print help
    By hollandwolf in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-28-2004, 01:59 AM
  3. pls help to print
    By arc in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 03-24-2004, 11:02 PM
  4. How to print
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-29-2003, 01:59 PM
  5. GL Print
    By Wei in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 11-13-2001, 01:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean