Dealing with large amount of data

Started by
11 comments, last by 3DViewer 15 years, 11 months ago
Hi! I must start saying that I am new to OpenGL. I have inherited an old piece of code that draws surfaces made of triangles. Its basic method is the following: glBegin(GL_TRIANGLES); for (i = 0; i < *n; i++) { glVertex2fv(vert[endpts[nx]-1]); nx++; glVertex2fv(vert[endpts[nx]-1]); nx++; glVertex2fv(vert[endpts[nx]-1]); } glEnd(); On top of all, this code is not using specific matrix calls such as glRotate, glTranslate and glScale to handle the object movement. It holds onto the complete view matrix and does it's own adjustments based on the inputs and then just sets the whole matrix before rendering. It is fine with small models, but when I start playing with millions of triangles, it takes about 5 seconds to render the result of a rotation/translation. I am planning to re-code the stuff, grouping the triangles to render them in 1 call, using index buffering to avoid duplicated vertices, but at the moment, I am having a problem with the following basic problem: Assuming I have already drawn the model using the above piece of code and want now to translate it using the mouse (the mouse event has returned dx and dy), How do I make the graphic card understand to use what it has already in memory and apply on it the glTranslatef(dx, dy, 0) without having to re-do the above piece of code, ie the glBegin ... glVertex2fv ... glEnd stuff? I thought of using glMultMatrixf(), so I did the following: GLfloat new_matrix[] = {1,0,0,0.5,0,1,0,0.5,0,0,1,0,0,0,0,1}; float mat[16]; int r,c; glGetFloatv(GL_MODELVIEW_MATRIX, mat); qDebug("Before glMultMatrixf"); for (c=0 ; c<16 ; c++) std::cout<<mat[c]<<" " ;std::cout<<std::endl; glMultMatrixf(new_matrix); qDebug("After glMultMatrixf"); glGetFloatv(GL_MODELVIEW_MATRIX, mat); for (c=0 ; c<16 ; c++) std::cout<<mat[c]<<" " ;std::cout<<std::endl; So I just want the model to be translate by a factor 0.5 in x and y. The outputs of my debu info are: Before glMultMatrixf -0.53704 -3.71254 -15.0809 5 -0.83904 -3.41054 -15.0809 5 0 0 0.431428 0 -0.167808 -0.742508 -3.01619 1 After glMultMatrixf -0.620944 -4.08379 -16.589 5.5 -0.922944 -3.78179 -16.589 5.5 0 0 0.431428 0 -0.167808 -0.742508 -3.01619 1 So the viewing matrix gets updated, so why not the model? Thanks for your help! -Pierre.
Advertisement
to speed up the code you can try one of the following methods :
1. Display lists
2. vertex arays .
3. Vertex buffer objects.

as far as the matrix is concerned, where are u placing the glMultMatrix functions ?
Also, since your translation is 0.5 in each direction , probably thats why the change is not apparent ? Try drawing an axis at the origin and then see if the model is translated.

cheers
Quote:It is fine with small models, but when I start playing with millions
of triangles, it takes about 5 seconds to render the result of a
rotation/translation.


Move up to VBO. It requires GL 1.5
Avoid glGet calls.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by _gl_coder_one_
1. Display lists
Display lists are handy, but they wont do much for you if you are feeding a ton of data.
Quote:2. vertex arays.
3. Vertex buffer objects.
Vertex arrays should help a lot, and VBOs will help even more - what is best is that the second is only a small step from the first. For best results, interleave the vertices, and possibly even rearrange them for vertex cache coherence.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Quote:Original post by _gl_coder_one_
1. Display lists
Display lists are handy, but they wont do much for you if you are feeding a ton of data.
Quote:2. vertex arays.
3. Vertex buffer objects.
Vertex arrays should help a lot, and VBOs will help even more - what is best is that the second is only a small step from the first. For best results, interleave the vertices, and possibly even rearrange them for vertex cache coherence.


Display list is usually as good as static VBO (at least on a NVidia display card).
Quote:
Assuming I have already drawn the model using the above piece of code
and want now to translate it using the mouse (the mouse event has
returned dx and dy),

How do I make the graphic card understand to use what it has already
in memory and apply on it the glTranslatef(dx, dy, 0) without having
to re-do the above piece of code, ie the glBegin ... glVertex2fv ...
glEnd stuff?

There is absolutely nothing you can do. That is why the original code is considered bad.

Hi!

Thank you to all of you!

I think that I started with the wrong assumption. I thought that when a scene has been rendered, if this one has not changed in term of new object being rendered/deleted, a simple rotation could be done without having to redraw everything.

In fact as soon I change a bit in my scene I have to render everything
all again, ie going through the original bit of code:

glBegin(GL_TRIANGLES);
for (i = 0; i < *n; i ) {
glVertex2fv(vert[endpts[nx]-1]);
nx ;
glVertex2fv(vert[endpts[nx]-1]);
nx ;
glVertex2fv(vert[endpts[nx]-1]);
}
glEnd();

With millions of triangles, I will have too many function calls.


So, my only solution is in fact to implement some VBO approach...

Could anyone redirect me to some place where VBO is explained with some code sample?

Cheers..
Use display list instead, let the driver optimize it for you.
VBO
http://www.opengl.org/wiki/index.php/GL_ARB_vertex_buffer_object
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
use vbo's.

This topic is closed to new replies.

Advertisement