Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

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

  1. #11
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    Quote Originally Posted by WhatJac3 View Post
    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.
    Hey no problem, I'm here to help. Well the Help Me forum may be nuked soon anyhow. If that happens, don't worry, I'll move your thread over to the Programming -> Graphics forum so it is not lost. (Along with all the other threads too.) Next time you are using the advanced editor, look for an icon that looks like a number sign ala "#". That's the code BB tag button you're looking for. It's not in the quick reply editor unfortunately, but clicking on 'Go Advanced' will keep any quoted text you have and enable all the other nice WYSIWYG buttons.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #12
    What WYSIWUG buttons? Can you please show them in my screenshot? This is what I get when I use the advanced editor:

    ps: I am using FireFox, but my IE8 gives me exactly the same view.
    Attached Images Attached Images

  3. #13
    I noticed the following line:

    Code:
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, ColorSize, @ColorBuffer[0], GL_STREAM_DRAW);
    Have a look at the last GL_STREAM_DRAW parameter. I couldn't find official documentation of this function, so I just quote the tutorial:

    "stream" means the data will be changed every frame (specified once and used once).
    I suggest you change it to GL_STATIC_DRAW, because I think GL_STREAM_DRAW will throw away your data after it is rendered once.

    The rest of your code looks ok.

    Hope this helps.

    Edit: About the WYSIWYG problem, you have to enable those buttons in your profile. Forum -> Forum Actions -> General settings, and then scroll down until you see the "Message Editor Interface" optoin.
    Last edited by chronozphere; 24-09-2010 at 06:43 AM.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  4. #14
    Well I finally solved it. The GL_STREAM_DRAW was not the solution. My problem was that I was using multiple libraries. The default 'OpenGL' from Delphi combined with the dglOpenGL (and sometimes even GL library and copied GL functions from NeHe). The combination of this all made it impossible to use VBO's. So I removed all other code except for the dglOpenGL. Thanks for the help guys.

  5. #15
    Good to know you solved it.

    I'd suggest that you only use dglOpengl. It's the best set of headers out there IMHO.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  6. #16
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    Here's some VBO code from my P3D Model lib :

    Code:
    procedure TP3DModel.UploadVBOS;
    var
       VerticiesSize : uint32;
       NormalsSize : uint32;
       UVsSize : uint32;
       VertexBonesSize : uint32;
       VertexRelToJointSize : uint32;
       CurrentOffset : uint32;
       TotalSize : uint32;
    begin
         if FLoaded and not FVBOUploaded then
         begin
             //Generate VBOs
             glGenBuffers(1, @VBO);
    
             VerticiesSize := length(mesh.Vertex) * sizeof(tvertex);
             NormalsSize := length(mesh.Normal) * sizeof(tvertex);
             UVsSize := length(mesh.UV) * sizeof(TVector2f);
             VertexBonesSize := length(mesh.VertexBones) * sizeof(tvector);
             VertexRelToJointSize := length(mesh.VertexRelToJoint) * sizeof(tvector);
    
             VBOVerticesOffset := 0;
             VBONormalsOffset := VerticiesSize;
             VBOUVsOffset := VBONormalsOffset + NormalsSize;
             VBOVertexBonesOffset := VBOUVsOffset + UVsSize;
             VBOVertexRelToJointOffset := VBOVertexBonesOffset + VertexBonesSize;
    
             TotalSize := VerticiesSize + NormalsSize + UVsSize + VertexBonesSize + VertexRelToJointSize;
    
             //bind and upload data
             glBindBuffer(GL_ARRAY_BUFFER, VBO);
    
             glBufferData(GL_ARRAY_BUFFER, TotalSize  , nil , GL_STATIC_DRAW); //allocate memory for buffer
    
             //load in seperate buffers at correct offsets
             glBufferSubData(GL_ARRAY_BUFFER, VBOVerticesOffset, VerticiesSize  , @mesh.Vertex[0]);
             glBufferSubData(GL_ARRAY_BUFFER, VBONormalsOffset, NormalsSize , @mesh.normal[0]);
             glBufferSubData(GL_ARRAY_BUFFER, VBOUVsOffset, UVsSize , @mesh.UV[0]);
             glBufferSubData(GL_ARRAY_BUFFER, VBOVertexBonesOffset, VertexBonesSize , @mesh.VertexBones[0]);
             glBufferSubData(GL_ARRAY_BUFFER, VBOVertexRelToJointOffset, VertexRelToJointSize , @mesh.VertexRelToJoint[0]);
             glBindBuffer(GL_ARRAY_BUFFER, 0);
    
             //create index vbo
             glGenBuffers(1, @VBOIndicies);
             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBOIndicies);
             glBufferData(GL_ELEMENT_ARRAY_BUFFER, length(Mesh.Indicies)*sizeof(GLuint), @mesh.Indicies[0], GL_STATIC_DRAW);
    
             glbindbuffer(GL_ARRAY_BUFFER,0);
             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
         end;
    end;
    
    procedure TP3DModel.DeleteVBOS;
    begin
         if FVBOUploaded then
         begin
              glDeleteBuffers(1, @VBO);
              glDeleteBuffers(1, @VBOIndicies);
              FVBOUploaded := false;
              VBO := 0;
              VBOIndicies := 0;
         end;
    end;
    and here's how it renders :

    Code:
    procedure TP3DModel.RenderDynamic;
    var
       i1, i2, k, m:  integer;
       currentMaterial: integer;
       currentShaderId: integer;
       temptime: single;
    
    
       attribname : string;
       v : TVector;
    begin
         //enable shader
         FArmatureShader.Enable;
    
         if FDynamicRenderMode = DRMVBO then
         begin
             //Bind VBO Before setting pointers
             glBindBuffer(GL_ARRAY_BUFFER, VBO);
    
             //enable Attrib Arrays VBO
             glEnableVertexAttribArray(FModelManagerLink^.AttribNormals);
             glEnableVertexAttribArray(FModelManagerLink^.AttribVertices);
             glEnableVertexAttribArray(FModelManagerLink^.AttribVertexRelToJoint);
             glEnableVertexAttribArray(FModelManagerLink^.AttribUVs);
             glEnableVertexAttribArray(FModelManagerLink^.AttribVertexBones);
    
             //set attrib pointers VBO
             glVertexAttribPointer(FModelManagerLink^.AttribVertices,3,GL_FLOAT,false,0,nil);
             glVertexAttribPointer(FModelManagerLink^.AttribNormals,3,GL_FLOAT,false,0,pglvoid(VBONormalsOffset));
             glVertexAttribPointer(FModelManagerLink^.AttribUVs,2,GL_FLOAT,false,0,pglvoid(VBOUVsOffset));
             glVertexAttribPointer(FModelManagerLink^.AttribVertexBones,4,GL_FLOAT,false,0,pglvoid(VBOVertexBonesOffset));
             glVertexAttribPointer(FModelManagerLink^.AttribVertexRelToJoint,4,GL_FLOAT,false,0,pglvoid(VBOVertexRelToJointOffset));
    
             //bind index buffer for rendering
             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBOIndicies);
        end;
    
        if FDynamicRenderMode = DRMAttribArray then
        begin
         //enable the attrib arrays used in shader
               glEnableVertexAttribArray(FModelManagerLink^.AttribNormals);
               glEnableVertexAttribArray(FModelManagerLink^.AttribVertices);
               glEnableVertexAttribArray(FModelManagerLink^.AttribVertexRelToJoint);
               glEnableVertexAttribArray(FModelManagerLink^.AttribUVs);
               glEnableVertexAttribArray(FModelManagerLink^.AttribVertexBones);
    
               //point the arrays at the data in system memory
               glVertexAttribPointer(FModelManagerLink^.AttribVertices,3,GL_FLOAT,false,0,@Mesh.Vertex[0][0]);
               glVertexAttribPointer(FModelManagerLink^.AttribNormals,3,GL_FLOAT,false,0,@Mesh.Normal[0][0]);
               glVertexAttribPointer(FModelManagerLink^.AttribUVs,2,GL_FLOAT,false,0,@Mesh.UV[0][0]);
               glVertexAttribPointer(FModelManagerLink^.AttribVertexBones,4,GL_FLOAT,false,0,@Mesh.VertexBones[0][0]);
               glVertexAttribPointer(FModelManagerLink^.AttribVertexRelToJoint,4,GL_FLOAT,false,0,@Mesh.VertexRelToJoint[0][0]);
    
        end;
    
        //i3 := round((SDL_GetTicks/10)) mod 100;
    
    
        inc(i7);
        if i7 >= length(Self.Armature.Actions[0].AnimFrames) then i7 := 0;
    
        //bind uniforms for mesh (can only bind after shader is bound)
    
        if (FModelManagerLink^.SkinningMode = SMQuat) or (FModelManagerLink^.SkinningMode = SMDualQuat) then
        begin
             glUniform4fv(FModelManagerLink^.UPos,31,@armature.Actions[0].AnimFrames[i7].BonePos[0]);
             glUniform4fv(FModelManagerLink^.URot,31,@armature.Actions[0].AnimFrames[i7].BoneRot[0]);
        end else
        if (FModelManagerLink^.SkinningMode = SMMatrix) then
        begin
             gluniformmatrix4fv(FModelManagerLink^.UMat,31,false,@armature.Actions[0].AnimFrames[i7].BoneMatrix[0][0][0]);
        end;
    
        glActiveTexture( GL_TEXTURE0 );
        glBindTexture( GL_TEXTURE_2D, TextureID );
    
    
        if DynamicRenderMode = DRMVBO then
        glDrawElements( GL_TRIANGLES,Mesh.IndexCount, GL_UNSIGNED_INT, nil) else
        if DynamicRenderMode = DRMAttribArray then
        glDrawElements( GL_TRIANGLES,Mesh.IndexCount, GL_UNSIGNED_INT, @Mesh.Indicies[0]);
    
        FArmatureShader.disable;
    
        if FDynamicRenderMode = DRMVBO then
        begin
             gldisableVertexAttribArray(FModelManagerLink^.AttribNormals);
             gldisableVertexAttribArray(FModelManagerLink^.AttribVertices);
             gldisableVertexAttribArray(FModelManagerLink^.AttribVertexRelToJoint);
             gldisableVertexAttribArray(FModelManagerLink^.AttribUVs);
             gldisableVertexAttribArray(FModelManagerLink^.AttribVertexBones);
    
             glbindbuffer(GL_ARRAY_BUFFER,0);
             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        end;
    
    
        if FDynamicRenderMode = DRMAttribArray then
        begin
             gldisableVertexAttribArray(FModelManagerLink^.AttribNormals);
             gldisableVertexAttribArray(FModelManagerLink^.AttribVertices);
             gldisableVertexAttribArray(FModelManagerLink^.AttribVertexRelToJoint);
             gldisableVertexAttribArray(FModelManagerLink^.AttribUVs);
             gldisableVertexAttribArray(FModelManagerLink^.AttribVertexBones);
        end;
    
    end;

    sorry if it's a bit confusing, it's designed for bone-animation on the GPU (called hardware skinning) but it does show how to populate a VBO, how to render from it and a reasonably good way of falling back on cards without VBO.

    I'll try and help best I can if you get stuck, let me know.
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

Page 2 of 2 FirstFirst 12

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
  •