Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

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

Thread: Trying to render a heightmap as a VBO, no error just not showing up.

  1. #11
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by Exempt View Post
    If I wanted to create a real index like 0, 1, 2, 3, 2, 1 to reuse vertice correctly I'd also have to remove the duplicated vertex from my data right?
    There would be no point in duplicating the vertices, but if they were present you could construct an index array which either ignored them or used them. The example code I posted assumes that the vertices are "packed", i.e. the vertex comprising elements 3*n,3*n+1,3*n+2 is for row (n/cols) and column (n%cols).

    Quote Originally Posted by Exempt View Post
    I think I may go ahead and bump up to opengl 3.3 soon so I can use the primitive restart method but that's for triangle strips and I'm not sure why I need a index for triangle strips since really I just need the first 2 point and then just one point to continue the strip after that...
    If you try to render a grid using strips, you'll find that the end of each row doesn't work out; one of the last two vertices will be on the "far" side of the strip. Triangle strips/fans used to have a performance benefit on older hardware, but nowadays there isn't really any advantage beyond reducing the size of the index arrays.

    The main problem with using glDrawArrays() for typical meshes is that you have to duplicate most of the interior vertices, which wastes memory and complicates matters if you need to modify vertices dynamically. It's more useful for points and line strips/loops.

    Note that, for strips, you can get by without primitive restart by just repeating the first vertex of a new section, effectively linking the sections with degenerate triangles (their area will be zero, so nothing will be drawn). That won't work with fans, though (or with glPolygonMode(GL_LINE) if the sections are disjoint).

  2. #12
    Intern Newbie
    Join Date
    Aug 2013
    Posts
    32
    I see, so I just fill my data with complete triangles then i can just pass 0,1,2,3,4... As my index and it will work with glDrawElements.

    Just to make sure I'm right, the index is just a single point right?

    Edit: to optimize this more I could use indexing like i said before to reuse points for the triangles...

  3. #13
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by Exempt View Post
    I see, so I just fill my data with complete triangles then i can just pass 0,1,2,3,4... As my index and it will work with glDrawElements.

    Just to make sure I'm right, the index is just a single point right?
    The vertex data passed to glVertexPointer() is just the vertices, e.g. for a rectangular grid:

    GLfloat vertices[rows*cols][3];

    The ordering can be whatever is convenient, but it would make sense to use the same order in which you read the heights from the image file.

    The index array is one GLubyte/GLushort/GLuint (depending upon how many bits you need) per vertex. You need as many indices for glDrawElements() as you would need complete vertices for glDrawArrays(). Rather than using vertex[first+0], vertex[first+1], etc, it uses vertex[indices[0]], vertex[indices[1]], etc. So instead of than duplicating or re-ordering vertices, you just duplicate or re-order the indices.

    For a height map generated from an M*N image and rendered with GL_TRIANGLES, you'd have M*N vertices (M*N*3 floats) and 6*(M-1)*(N-1) indices. For GL_QUADS, the number of indices would be 4*(M-1)*(N-1). Using triangle strips could reduce the number of indices further, but there probably isn't a need unless M*N is huge (in which case, storing the heights as a texture and generating the vertex data in a vertex shader would provide much bigger savings).

  4. #14
    Intern Newbie
    Join Date
    Aug 2013
    Posts
    32
    What exactly do you mean by "packed" for data? Just removing all duplicate vertex?

    edit:I'm starting from the bottom again and I can make glDrawElements work perfectly fine using this index and data.. IS this what you mean by packing the data?
    Code :
    unsigned int vboModelIndArray[] = {
    	0, 1, 2,
    	2, 1, 3,
    	2, 3, 4,
    	4, 3, 5,
    	4, 5, 6,
    	6, 5, 7,
    	6, 7, 8,
    	8, 7, 9
    };
    float modelData[] = {
    	0.0, 0.0, 0.0, 
    	0.0, 0.0, 1.0,
    	1.0, 0.0, 0.0,
    	1.0, 0.0, 1.0,
    	2.0, 0.0, 0.0,
    	2.0, 0.0, 1.0,
    	3.0, 0.0, 0.0,
    	3.0, 0.0, 1.0,
    	4.0, 0.0, 0.0,
    	4.0, 0.0, 1.0
    };

    EDIT: After some thought I figured out how I could make the vertex listed like this but I'll have to create a function to return the index...
    Code :
    unsigned int vboModelIndArray[] = {
    	//first row
    	0, 5, 1,
    	1, 5, 6,
    	1, 6, 2,
    	2, 6, 7,
    	2, 7, 3,
    	3, 7, 8,
    	3, 8, 4,
    	4, 8, 9,
    	//second row
    	5, 10, 6,
    	6, 10, 11,
    	6, 11, 7,
    	7, 11, 12,
    	7, 12, 8,
    	8, 12, 13,
    	8, 13, 9,
    	9, 13, 14
    };
    float modelData[] = {
    	1.0, 0.0, 0.0,
    	2.0, 0.0, 0.0,
    	3.0, 0.0, 0.0,
    	4.0, 0.0, 0.0,
    	5.0, 0.0, 0.0,
     
    	1.0, 0.0, 1.0,
    	2.0, 0.0, 1.0,
    	3.0, 0.0, 1.0,
    	4.0, 0.0, 1.0,
    	5.0, 0.0, 1.0,
     
    	1.0, 0.0, 2.0,
    	2.0, 0.0, 2.0,
    	3.0, 0.0, 2.0,
    	4.0, 0.0, 2.0,
    	5.0, 0.0, 2.0,
    };

    Edit: I have a new question so I'll make a new thread on this since this is far off from the original topic.
    Last edited by Exempt; 08-16-2013 at 01:59 PM.

  5. #15
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by Exempt View Post
    What exactly do you mean by "packed" for data? Just removing all duplicate vertex?
    Yes. Just fill the vertex array with rows*cols vertices in "raster" order.

    Quote Originally Posted by Exempt View Post
    edit:I'm starting from the bottom again and I can make glDrawElements work perfectly fine using this index and data.. IS this what you mean by packing the data?
    That looks correct.

Posting Permissions

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