PDA

View Full Version : Need help for path simulation



mclz522
08-29-2009, 07:38 PM
Hi all!
I'm trying to make an application to do path simulation and show the moving point animation. Basically, I have a set of ordered points that are pre-calculated and I want to show this path to a user. Currently My code is like following

int total = points.size();
int current = 0;
for(int current = 0;current<total;current++)
{
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
for(int i = 0; i<current; i++)
{
glVertex3f(points[i].x,points[i].y,points[i].z);
}
glEnd();
glutPostRedisplay(); //repaint the display
glFlush();
glutSwapBuffers();
}

As you can see, it repaint for every new point and the user can see a moving point. However, it works fine for small number of points. If I have a large number of points (eg 10 million), it becomes very slow at the end.

If I dont repaint previous points, everything disappear. Is there anyway to persist what I have drawn on the screen?

Thank you

Alfonse Reinheart
08-29-2009, 07:59 PM
Is there anyway to persist what I have drawn on the screen?

If you really, really want to, you can render to an FBO, then copy (via blit) that to the main framebuffer. That way, you have the image you want preserved.

I'm not sure how that's going to help you if the user, say, rotates the camera. But it'll do what you're looking for.

However, bear in mind that a more reasonable solution is to store these points in a buffer object and render them through that.

mclz522
08-29-2009, 08:31 PM
Hi Alfonse!
Thanks for your reply! Is there any tutorial page that you can point me to for those techniques you mentioned?

Thank you

scratt
08-29-2009, 10:21 PM
Check this out on FBOs:

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

What you can do is draw each new point to an FBO with a texture, and then use the texture from that FBO and draw it as a full screen QUAD to the main frame-buffer. Each frame you simply draw the next dot (into the FBO texture), don't clear the FBO, and use it's texture to draw a full screen quad to the main framebuffer (which you do clear each frame).

You could get around the problem of rotations by using your FBO texture as an Impostor (but that really is a bit advanced).

The other method is to allocate an empty Vertex Buffer (either CPU side, or GPU side) with a size equivalent to the number of points you may want to draw. Each frame add a new point to the Vertex Buffer, and then clear your screen, and redraw the entire Vertex Buffer each frame using the current updated point count.

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

mclz522
08-29-2009, 11:24 PM
Thank you scratt!
Your suggestion is really useful to me.

Alfonse Reinheart
08-29-2009, 11:47 PM
The OpenGL Wiki is increasingly becoming a more legitimate source. For example, articles on:

* FBOs (http://www.opengl.org/wiki/Framebuffer_Objects)
* VAOs (http://www.opengl.org/wiki/Vertex_Array_Objects)
* Buffer Objects (http://www.opengl.org/wiki/Buffer_Objects)
* How to send vertex data to OpenGL (http://www.opengl.org/wiki/Vertex_Specification)

scratt
08-30-2009, 01:09 AM
That's good to know. I still have good memories of the links I recommended above as they were what I used to give myself a leg up aways back. I will check out the OpenGL Wiki though.

Also the code examples on the 'songho' site are very nice to base experiments on when you are learning, particularly the Texture Streaming ones.

mclz522
08-30-2009, 04:20 AM
Thank you very much for all your information. Learning 3D stuf is really fun.

mclz522
08-31-2009, 03:28 AM
Hi all!
I I have successfully used VBO to draw all points. The program runs pretty fast. However, it seems that this program consumes lots of CPU. Is there anyway to reduce the CPU usage?
My display function is called every 400ms. My display function likes the following:

glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(3, GL_FLOAT, 0, (void*)(datasize_));
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_LINES, 0, index_);
index_+=600;
glFlush();
glutSwapBuffers();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);


Thanks

Alfonse Reinheart
08-31-2009, 11:33 AM
It uses a lot of CPU because you're sitting inside of a glutSwapBuffers for most of the application. This function will stop all CPU processing until the GPU is entirely finished rendering with the data that it has been sent.

Instead of doing swap buffers right after rendering, do it right before rendering. That way, you will be doing other useful CPU work while the GPU is doing its thing.

mclz522
08-31-2009, 10:09 PM
Thank you Alfonse
I will give it a try :)

mclz522
09-02-2009, 03:11 AM
Hi all!
I just resolved the CPU problem. Find out frequency of updating and number of vertexes drawn per update effect CPU usage a lot.
However, I got a new problem :sorrow:. The program runs fine on machines with separated GPU unit. However, It crushes in the middle of drawing on machines with embedded GPU on motherboard. It always crushes at glDrawArrays and the error message is something saying: the program is trying to access memory not for read.In addition, the image also blinks and can not be shown properly.
Does anyone have any idea why this happens? Is VBO video card dependent?

My init and display function are shown below

void MFCopenGL::init()
{
glGenBuffersARB = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffersARB");
glBindBufferARB = (PFNGLBINDBUFFERARBPROC)wglGetProcAddress("glBindBufferARB");
glBufferDataARB = (PFNGLBUFFERDATAARBPROC)wglGetProcAddress("glBufferDataARB");
glBufferSubDataARB = (PFNGLBUFFERSUBDATAARBPROC)wglGetProcAddress("glBufferSubDataARB");
glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)wglGetProcAddress("glDeleteBuffersARB");
glGetBufferParameterivARB = (PFNGLGETBUFFERPARAMETERIVARBPROC)wglGetProcAddres s("glGetBufferParameterivARB");
glMapBufferARB = (PFNGLMAPBUFFERARBPROC)wglGetProcAddress("glMapBufferARB");
glUnmapBufferARB = (PFNGLUNMAPBUFFERARBPROC)wglGetProcAddress("glUnmapBufferARB");
int bufferSize = 0;

glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

vboId = 0;
glGenBuffersARB(1, &amp;vboId);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId);

glBufferDataARB(GL_ARRAY_BUFFER_ARB, datasize_, 0, GL_STATIC_DRAW_ARB);
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, datasize_, vertices_); // copy vertices starting from 0 offest

}

// The OpenGL display callback
void MFCopenGL::display(void)
{
if(index_ <= datasize_)
{

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_LINES, 0, index_);
glutSwapBuffers();
index_+=600;
glFlush();

glDisableClientState(GL_VERTEX_ARRAY);

}

Here are two graphic cards's settings:

OpenGL Driver Info
==================
Vendor: NVIDIA Corporation
Version: 2.1.2
Renderer: GeForce 7300 GT/PCI/SSE2

Color Bits(R,G,B,A): (8, 8, 8, 0)
Depth Bits: 24
Stencil Bits: 0

Max Texture Size: 4096x4096
Max Lights: 8
Max Clip Planes: 6
Max Modelview Matrix Stacks: 32
Max Projection Matrix Stacks: 4
Max Attribute Stacks: 16
Max Texture Stacks: 10

Total Number of Extensions: 124
==============================
GL_ARB_color_buffer_float
GL_ARB_depth_texture
GL_ARB_draw_buffers
GL_ARB_fragment_program
GL_ARB_fragment_program_shadow
GL_ARB_fragment_shader
GL_ARB_half_float_pixel
GL_ARB_imaging
GL_ARB_multisample
GL_ARB_multitexture
GL_ARB_occlusion_query
GL_ARB_pixel_buffer_object
GL_ARB_point_parameters
GL_ARB_point_sprite
GL_ARB_shader_objects
GL_ARB_shading_language_100
GL_ARB_shadow
GL_ARB_texture_border_clamp
GL_ARB_texture_compression
GL_ARB_texture_cube_map
GL_ARB_texture_env_add
GL_ARB_texture_env_combine
GL_ARB_texture_env_dot3
GL_ARB_texture_float
GL_ARB_texture_mirrored_repeat
GL_ARB_texture_non_power_of_two
GL_ARB_texture_rectangle
GL_ARB_transpose_matrix
GL_ARB_vertex_buffer_object
GL_ARB_vertex_program
GL_ARB_vertex_shader
GL_ARB_window_pos
GL_ATI_draw_buffers
GL_ATI_texture_float
GL_ATI_texture_mirror_once
GL_EXT_Cg_shader
GL_EXT_abgr
GL_EXT_bgra
GL_EXT_blend_color
GL_EXT_blend_equation_separate
GL_EXT_blend_func_separate
GL_EXT_blend_minmax
GL_EXT_blend_subtract
GL_EXT_compiled_vertex_array
GL_EXT_depth_bounds_test
GL_EXT_draw_range_elements
GL_EXT_fog_coord
GL_EXT_framebuffer_blit
GL_EXT_framebuffer_multisample
GL_EXT_framebuffer_object
GL_EXT_gpu_program_parameters
GL_EXT_multi_draw_arrays
GL_EXT_packed_depth_stencil
GL_EXT_packed_pixels
GL_EXT_pixel_buffer_object
GL_EXT_point_parameters
GL_EXT_rescale_normal
GL_EXT_secondary_color
GL_EXT_separate_specular_color
GL_EXT_shadow_funcs
GL_EXT_stencil_two_side
GL_EXT_stencil_wrap
GL_EXT_texture3D
GL_EXT_texture_compression_s3tc
GL_EXT_texture_cube_map
GL_EXT_texture_edge_clamp
GL_EXT_texture_env_add
GL_EXT_texture_env_combine
GL_EXT_texture_env_dot3
GL_EXT_texture_filter_anisotropic
GL_EXT_texture_lod
GL_EXT_texture_lod_bias
GL_EXT_texture_mirror_clamp
GL_EXT_texture_object
GL_EXT_texture_sRGB
GL_EXT_timer_query
GL_EXT_vertex_array
GL_IBM_rasterpos_clip
GL_IBM_texture_mirrored_repeat
GL_KTX_buffer_region
GL_NVX_conditional_render
GL_NV_blend_square
GL_NV_copy_depth_to_color
GL_NV_depth_clamp
GL_NV_fence
GL_NV_float_buffer
GL_NV_fog_distance
GL_NV_fragment_program
GL_NV_fragment_program2
GL_NV_fragment_program_option
GL_NV_framebuffer_multisample_coverage
GL_NV_half_float
GL_NV_light_max_exponent
GL_NV_multisample_filter_hint
GL_NV_occlusion_query
GL_NV_packed_depth_stencil
GL_NV_pixel_data_range
GL_NV_point_sprite
GL_NV_primitive_restart
GL_NV_register_combiners
GL_NV_register_combiners2
GL_NV_texgen_reflection
GL_NV_texture_compression_vtc
GL_NV_texture_env_combine4
GL_NV_texture_expand_normal
GL_NV_texture_rectangle
GL_NV_texture_shader
GL_NV_texture_shader2
GL_NV_texture_shader3
GL_NV_vertex_array_range
GL_NV_vertex_array_range2
GL_NV_vertex_program
GL_NV_vertex_program1_1
GL_NV_vertex_program2
GL_NV_vertex_program2_option
GL_NV_vertex_program3
GL_S3_s3tc
GL_SGIS_generate_mipmap
GL_SGIS_texture_lod
GL_SGIX_depth_texture
GL_SGIX_shadow
GL_SUN_slice_accum
GL_WIN_swap_hint
WGL_EXT_swap_control

OpenGL Driver Info
==================
Vendor: Intel
Version: 1.4.0 - Build 7.14.10.4926
Renderer: Intel 945G

Color Bits(R,G,B,A): (8, 8, 8, 8)
Depth Bits: 16
Stencil Bits: 0

Max Texture Size: 2048x2048
Max Lights: 16
Max Clip Planes: 6
Max Modelview Matrix Stacks: 32
Max Projection Matrix Stacks: 4
Max Attribute Stacks: 16
Max Texture Stacks: 10

Total Number of Extensions: 49
==============================
GL_3DFX_texture_compression_FXT1
GL_ARB_depth_texture
GL_ARB_fragment_program
GL_ARB_multitexture
GL_ARB_point_parameters
GL_ARB_shadow
GL_ARB_texture_border_clamp
GL_ARB_texture_compression
GL_ARB_texture_cube_map
GL_ARB_texture_env_add
GL_ARB_texture_env_combine
GL_ARB_texture_env_crossbar
GL_ARB_texture_env_dot3
GL_ARB_transpose_matrix
GL_ARB_vertex_buffer_object
GL_ARB_vertex_program
GL_ARB_window_pos
GL_EXT_abgr
GL_EXT_bgra
GL_EXT_blend_color
GL_EXT_blend_func_separate
GL_EXT_blend_minmax
GL_EXT_blend_subtract
GL_EXT_clip_volume_hint
GL_EXT_compiled_vertex_array
GL_EXT_cull_vertex
GL_EXT_draw_range_elements
GL_EXT_fog_coord
GL_EXT_multi_draw_arrays
GL_EXT_packed_pixels
GL_EXT_rescale_normal
GL_EXT_secondary_color
GL_EXT_separate_specular_color
GL_EXT_shadow_funcs
GL_EXT_stencil_two_side
GL_EXT_stencil_wrap
GL_EXT_texture3D
GL_EXT_texture_compression_s3tc
GL_EXT_texture_env_add
GL_EXT_texture_env_combine
GL_EXT_texture_filter_anisotropic
GL_EXT_texture_lod_bias
GL_IBM_texture_mirrored_repeat
GL_NV_blend_square
GL_NV_texgen_reflection
GL_SGIS_generate_mipmap
GL_SGIS_texture_edge_clamp
GL_SGIS_texture_lod
GL_WIN_swap_hint
================================================== ====================

mclz522
09-02-2009, 10:42 PM
Thanks for everyone.
It's all my mistake....
I did not really understand glBufferDataARB and I calculated the buffer size wrong

Rsantina
09-07-2009, 05:36 AM
you can check out vbo and vao which will decrease the cpu usage cuz data copy can be controled and anyhow the gl*ClientState is removed in OGL 3.x