Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Optimize drawing 10.000 2D-lines with VBO?

  1. #1

    Optimize drawing 10.000 2D-lines with VBO?

    System: Windows Vista, AMD 64 X2 Dual Core 2.7 Ghz, ATI Radeon 2600 PRO HD
    Compiler/IDE: Delphi 2009 for Win32
    API: OpenGL

    Hello,

    I am trying to optimize my code since it seems to be rather slow. I am drawing 10.000 lines and I heard that the usage of a VBO would be drastically faster. But I have some problems with it. So maybe someone could help me since I can't find good examples/tutorials. Maybe someone can help me with my little example. I try to draw 2 lines:
    (0,0) - (100,100) and (0,100) - (100,0).

    This is what I have so far:

    Code:
    var:
      arrLines: array[4] of array [2] of GLFloat;
      FVBO: GLUInt; 
    begin
      glGenBuffers(1, @FVBO);
      glBindBufferARB(GL_ARRAY_BUFFER, FVBO);
      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(2, GL_FLOAT, 0, ); // What should be here?
    
      //Initialize array:
        arrlines[0][0] := 0;
        arrlines[0][1] := 0;
        arrlines[1][0] := 100;
        arrlines[1][1] := 100;
        arrlines[2][0] := 0;
        arrlines[2][1] := 100;
        arrlines[3][0] := 100;
        arrlines[3][1] := 0;
      //Draw the lines
    
        glDrawElements(GL_LINES, 2*4 , GL_FLOAT, arrLines); //Is this correct?
        glDisableClientState(GL_VERTEX_ARRAY);

    Please help and thanks in advance
    Last edited by WhatJac3; 23-09-2010 at 04:14 PM.

  2. #2
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Quote Originally Posted by WhatJac3 View Post
    glVertexPointer(2, GL_FLOAT, 0, ); // What should be here?
    To answer that, and I admit I am no good at OpenGl, I would say 0. See [URL="http://www.opengl.org/sdk/docs/man/xhtml/glVertexPointer.xml"]

    from their description I would assume where the first variable in the array is? Please correct me, I think I am probably wrong here, but you never know. It never hurts to try/help? As the expression kind of goes... I'm tired. Maybe I should just go to sleep already.

    edit:
    one other thing, what do you specifically mean by VBO?
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  3. #3
    OpenGL rendering with VBO's is quite confusing. Seems like you're mixing things up.
    You're missing a glBufferDataARB(). That function is used to fill the buffer with data.
    In your code, you send the vertex data to the draw call, which is wrong. The fourth parameter of glDrawElements is a pointer to the array of indices.

    This tutorial shows how to use VBO's to render indexed geometry. For lines, you don't really want indexed geometry so you have to dig a little further yourself. Check out this tutorial:


    http://www.songho.ca/opengl/gl_vbo.html


    Also have a look at their vertex array's tutorial. That may give you some ideas about the differences between VBO's and vertex arrays, because people confuse those two often.

    Good luck.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  4. #4
    I expected to do something wrong. So VBO is the thing I am looking for for speed improvement?
    Thanks for the link but it is in C++ code again and that is confusing for me (pointer things and such). Isn't there a good pascal example? (I am looking for that for over a week already, so therefore I posted it here, since all those C++ example aint doing it for me).

    And while we are at it? What would be better? VBO or PBO?
    I want to do a lot with transparency.
    Last edited by WhatJac3; 23-09-2010 at 12:08 AM.

  5. #5
    So it should be more like this? But I still have problems on 3 lines (4 parameters in total)

    Code:
    glGenBuffersARB(1, @FVBO);
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, FVBO);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, , ,GL_DYNAMIC_DRAW_ARB); //2nd and 3th parameter
      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(2, GL_FLOAT, 0, ); // 4th parameter?
    
      arrlines[0][0] := 0;
      arrlines[0][1] := 0;
      arrlines[1][0] := 100;
      arrlines[1][1] := 100;
      arrlines[2][0] := 0;
      arrlines[2][1] := 100;
      arrlines[3][0] := 100;
      arrlines[3][1] := 0;
    
      glDrawElements(GL_LINES, , GL_FLOAT, @arrLines[0][0]); //Second parameter?
      glDisableClientState(GL_VERTEX_ARRAY);
    
      // bind with 0, so, switch back to normal pointer operation
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
      glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
      glDeleteBuffersARB(1, &vboId);

  6. #6
    Thanks for the link but it is in C++ code again and that is confusing for me (pointer things and such). Isn't there a good pascal example? (I am looking for that for over a week already, so therefore I posted it here, since all those C++ example aint doing it for me).
    Just get used to it. You won't get far if you only want to read pascal-tutorials because there aren't many. Moreover, there is almost no difference between pascal and C++ in that example, because it's just a few function calls. Just look at the global flow of the program and look up the documentation for each function and read it carefully. Also, browse these forums because I think there are more people who had problems with VBO's.

    PBO's are totally different and I have not used them much. My guess would be that it's not the fastest way to draw lines.

    The last parameter of glVertexPointer() is the offset into the given vertex array. If you want to use your entire vertex array, just set this to zero. That was all covered in the link I gave you. The only way to know how to use functions is read the documentation. I'm not going to do that for you.

    Good luck!
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    Hi WhatJac3, I just modified your posts slightly to put your code into a code block. You should really try to use them as they help when you are trying to show your programming code nicely on the forums.

    Also, you have not followed the Help Me! forum rules for posting in this special forum. Please read item 3 from the FAQ for a clear definition of what I mean. However, since this forum here may be removed and each individual thread will be moved to a more appropriate on-topic forum instead, I've chosen to forgo the traditional warning. You may read about the decisions behind this at the following thread.

    Thanks for your attention and I hope that you get the help you require for your project.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8
    Hello Will,

    Thanks for your reply. Well I didn't really see why posting my system specs would result in a better understanding of the problem. The option to format it is code I was looking for in the editor but couldn't find it (not in the quick, neither in the advanced mode). So therefore I could but the correct tags around it since it might differ per forum. So maybe it would be nice to make an Icon in the editor to add the code tags.

    Thanks for the help so far, but it still gives me problems
    @chronozphere, I didn't asked to read the documentation for me. But I am trying for over a week already and cant seem to get it working. I have seen all kind of examples but almost all are C++. I have copied an example from this OpenGL tutorial which compiles but just doesn't display anything and I have no clue why not. So I was just looking for someone who could point me in the right direction.

    Code:
      VertexBuffer: array [0..5] of TVertex2f = (
        (X : 200; Y : 200;),
        (X : 0; Y : 200;),
        (X : 0; Y : 0;),
        (X : 200; Y : 200;),
        (X : 0; Y : 0;),
        (X : 200; Y : 0;)
      );
      ColorBuffer: array [0..5] of TColor3f = (
        (R : 1; G : 0; B : 1),
        (R : 0; G : 0; B : 1),
        (R : 0; G : 1; B : 0),
        (R : 1; G : 0; B : 1),
        (R : 0; G : 1; B : 0),
        (R : 1; G : 1; B : 0)
      );
    
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);
      glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
      glColorPointer(3, GL_FLOAT, 0, @ColorBuffer[0]);
    
      glDrawArrays(GL_TRIANGLES, 0, Length(VertexBuffer));
    
      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_COLOR_ARRAY);

  9. #9
    Ok, I'll give you some tips then.

    First of all, can you show me your complete sourcecode? The last post only contains the render code, not the creation of the buffer etc..

    The most confusing part of VBO's is that it uses the functions that were meant for vertex array's. I see that you are passing a pointer to the array to glVertexPointer() and glColorPointer(). This is correct for vertex array's but NOT for VBO's. The functions are used differently. You must pass the data in a glBufferDataARB() call and use 0 as the last parameter for both glVertexPointer and glColorPointer().

    Just google some more and ask more specific questions. You could also check the following tutorial:

    http://playcontrol.net/ewing/jibberj...er_object.html

    I know that VBO's are confusing. You just HAVE to go thourgh one of the tutorials, write down the functions that are used and figure out what happens exactly.

    I may write a VBO tutorial one day, for pascal. Right now, I dont have time for that.

    Good luck!
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  10. #10
    Thanks for giving me tips. I have been playing with it all day again and this is what I have at the moment (but still nothing is drawn)
    (is from: http://www.ozone3d.net/tutorials/opengl_vbo_p2.php, my translation from C++ to Delphi). I checked it with your tutorial and did not see any strange things. My viewwindow is set up from (0,0) - (1024,76

    Code:
    var
      vc: Integer;
      ColorSize, PositionSize : Integer;
    const
      BufferSize = 2;
      POSITION_OBJECT = 0;
      COLOR_OBJECT = 1;
    var
      BufferName: array [0..BufferSize-1] of GLuint;
    begin
    
      glGenBuffersARB := wglGetProcAddress('glGenBuffersARB');
      glBindBufferARB := wglGetProcAddress('glBindBufferARB');
      glBufferDataARB := wglGetProcAddress('glBufferDataARB');
      glDeleteBuffersARB := wglGetProcAddress('glDeleteBuffersARB');
    
      PositionSize := length(VertexBuffer) * SizeOf(TVertex2f);
      ColorSize := length(ColorBuffer) * SizeOf(TColor3f);
    
      vc := Length(VertexBuffer);
    
      // allocate a new buffer
      glGenBuffersARB(1, @BufferName[COLOR_OBJECT]);
      // bind the buffer object to use
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, BufferName[COLOR_OBJECT]);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, ColorSize, @ColorBuffer[0], GL_STREAM_DRAW);
      glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);
    
      glGenBuffersARB(1, @BufferName[POSITION_OBJECT]);
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, BufferName[POSITION_OBJECT]);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, PositionSize, @VertexBuffer[0], GL_STREAM_DRAW);
      glVertexPointer(2, GL_FLOAT, 0, 0);
    
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);
    
      glDrawArrays(GL_TRIANGLES, 0, vc);
    
      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_COLOR_ARRAY);
    with data:
    Code:
    type
      TVertex2f = record
        X, Y: TPositionType;
      end;
    
      TColor3f = record
       R, G, B: GLubyte;
      end;
    
    const
      VertexBuffer: array [0..5] of TVertex2f = (
        (X : 0; Y : 0;),
        (X : 200; Y : 0;),
        (X : 200; Y : 200;),
        (X : 200; Y : 200;),
        (X : 0;  Y : 200;),
        (X : 0; Y : 0;)
      );
      ColorBuffer: array [0..5] of TColor3f = (
        (R : 255; G : 0; B : 0),
        (R : 255; G : 255; B : 0),
        (R : 0; G : 255; B : 0),
        (R : 0; G : 255; B : 0),
        (R : 0; G : 0; B : 255),
        (R : 255; G : 0; B : 0)
      );
    Last edited by WhatJac3; 23-09-2010 at 09:47 PM. Reason: additional information

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •