简体   繁体   中英

store matrix from gl_triangles

I am just getting into game programming and have adapted a marching cubes example to fit my needs. I am rendering Goursat's Surface using marching cubes. Currently I am using a slightly adapted version of marching cubes . The code that I am looking at calls:

gl_pushmatrix();
glBegin(GL_TRIANGLES);
    vMarchingCubes(); // generate the mesh
glEnd();
glPopMatrix();

Every time that it renders! This gives me a framerate of ~20 fps. Since I am only using marching cubes to construct the mesh, I don't want to reconstruct the mesh every time that I render. I want to save the matrix (which has an unknown size though I could compute the size if necessary).

I have found a way to store and load a matrix on another stackoverflow question but it mentions that it is not the preferred way of doing this in openGL 4.0. I am wondering what the modern way of doing this is. If it is relevant, I would prefer that it is available in openGL ES as well (if possible).

I want to save the matrix (which has an unknown size though I could compute the size if necessary).

You don't want to store the "matrix". You want to store the mesh. Also what's causing the poor performance is the use of immediate mode (glBegin, glVertex, glEnd).

What you need is a so called Vertex Buffer Object holding all the triangle data. Isometric surfaces call for vertex sharing so you need to preprocess the data a little bit. First you put all your vertices into a key→value structure (C++ std::map for example) with the vertices being the key, or into a set ( boost::set ), which is effectively the same as a map but with a better memory structure for tasks like this. Everytime you encounter a new unique vertex you assign it an index and append the vertex into a vertex array. Also for every vertex you append to a faces array the index it was assigned (maybe already earlier):

Along the lines of this (Pseudocode):

vertex_index = 0
vertex_array = new array<vertex>
vertex_set = new set<vertex, unsigned int>
faces_array = new array<unsigned int>

foreach t in triangles:
    foreach v in t.vertices:
        if not vertex_set.has_key(vertex):
           vertex_set.add( vertex, vertex_index )
           vertex_array.append( vertex )
           vertex_index += 1
        faces_array.append( vertex_set(v) )

You can now upload the vertex_array into a GL_ARRAY_BUFFER buffer object and faces_array into a GL_ELEMENT_ARRAY_BUFFER buffer object. With that in place you can then do the usual glVertex…PointerglDrawElements stanza to draw the whole thing. See http://www.opengl.org/wiki/VBO_-_just_examples and other tutorials on VBOs for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM