Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » too slow.

This thread is locked; no one can reply to it. rss feed Print
too slow.
killocan
Member #1,556
October 2001
avatar

Hi there! I'm trying to implement bloom effect w/o shaders and proceed to use allegro+ogl to archive this. As i read in someplaces i need to render my scene to a texture and apply some blur and stuff. The problem is that when i copy the backbuffer to another BITMAP it's taking a LOT of time. Is anything very wrong in my code or my computer is very bad on this?

#SelectExpand
1#include <stdio.h> 2 3#include "allegro5/allegro.h" 4#include "allegro5/allegro_image.h" 5#include "allegro5/allegro_opengl.h" 6#include "allegro5/allegro_font.h" 7#include "allegro5/allegro_primitives.h" 8 9#include <GL/glu.h> 10 11#include "common.c" 12 13struct camera { 14 double xangle, yangle, zangle; 15 double dist; 16}; 17 18struct camera camera = { 19 0.0, 0.0, 0.0, 20 20.0 21}; 22 23double angle_speed = 5.0; 24double dist_speed = 1.0; 25 26GLuint tex; 27ALLEGRO_BITMAP *bmp,*buffer; 28bool key[ALLEGRO_KEY_MAX]; 29 30GLUquadricObj * quadratic; 31 32static void set_camera_position(void) 33{ 34 glMatrixMode(GL_PROJECTION); 35 glLoadIdentity(); 36 //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 90.0); 37 gluPerspective(45.0f,(GLfloat)1024.0/(GLfloat)768.0,0.1f,100.0f); 38 glTranslatef(0, 0, -camera.dist); 39 glRotatef(camera.xangle, 1, 0, 0); 40 glRotatef(camera.yangle, 0, 1, 0); 41 glRotatef(camera.zangle, 0, 0, 1); 42 glMatrixMode(GL_MODELVIEW); 43} 44 45 46 47static void keyboard(void) 48{ 49 if(key[ALLEGRO_KEY_LEFT]) camera.yangle += angle_speed; 50 if(key[ALLEGRO_KEY_RIGHT]) camera.yangle -= angle_speed; 51 52 if(key[ALLEGRO_KEY_UP]) camera.xangle += angle_speed; 53 if(key[ALLEGRO_KEY_DOWN]) camera.xangle -= angle_speed; 54 55 if(key[ALLEGRO_KEY_PGUP]) camera.dist -= dist_speed; 56 if(key[ALLEGRO_KEY_PGDN]) camera.dist += dist_speed; 57} 58 59 60static void draw(void) 61{ 62 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 63 64 glLoadIdentity(); 65 66 glBindTexture(GL_TEXTURE_2D, tex); 67 68 glRotatef(45.0f,0.0f,1.0f,0.0f); 69 glTranslatef(14.0f,8.0f,0.0f); 70 71 gluSphere(quadratic,3.f,64,64); 72} 73 74static void map(void) 75{ 76 glMatrixMode(GL_PROJECTION); 77 glLoadIdentity(); 78 glOrtho(0.0f, 1024 - 1.0, 768 - 1.0, 0.0, -1.0, 1.0); 79 glMatrixMode(GL_MODELVIEW); 80 glLoadIdentity(); 81 82 al_draw_filled_rectangle(0, 640, 1024, 768, al_map_rgba_f(1,0,0,1)); 83} 84 85static void setup_textures(ALLEGRO_DISPLAY *display) 86{ 87 ALLEGRO_BITMAP *tmp_bmp; 88// ALLEGRO_FONT *font; 89 int w, h;//, depth; 90 91// font = al_load_font("data/fixed_font.tga", 0, 0); 92// if(!font) { 93// abort_example("Error loading `data/fixed_font.tga'\n"); 94// exit(1); 95// } 96 97 glViewport(0,0,1024,768); 98 gluPerspective(45.0f,(GLfloat)1024.0/(GLfloat)768.0,0.1f,100.0f); 99 100 tmp_bmp = al_load_bitmap("data/moon.bmp"); 101 if(!tmp_bmp) { 102 abort_example("Error loading `data/moon.bmp'\n"); 103 exit(1); 104 } 105 106 w = 512; 107 h = 512; 108 bmp = al_create_bitmap(w, h); 109 110 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); 111 buffer = al_create_bitmap(1024, 1024); 112 113 al_set_target_bitmap(bmp); 114 al_draw_bitmap(tmp_bmp,0,0,0); 115 116// depth = al_get_display_option(display, ALLEGRO_DEPTH_SIZE); 117// if (!depth) 118// al_draw_textf(font, al_map_rgb(255, 0, 0), 0, 5, 0, "No Z-buffer!"); 119// else 120// al_draw_textf(font, al_map_rgb(255, 0, 0), 0, 5, 0, "Z-buffer: %i bits", depth); 121 122 al_set_target_backbuffer(display); 123 al_destroy_bitmap(tmp_bmp); 124 125// al_destroy_font(font); 126 127 glShadeModel(GL_SMOOTH); 128 129 glEnable(GL_TEXTURE_2D); 130 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 131 132 tex = al_get_opengl_texture(bmp); 133 134 quadratic=gluNewQuadric(); 135 gluQuadricNormals(quadratic, GLU_SMOOTH); 136 gluQuadricTexture(quadratic, GL_TRUE); 137} 138 139 140 141int main(void) 142{ 143 ALLEGRO_DISPLAY *display; 144 ALLEGRO_EVENT_QUEUE *queue; 145 ALLEGRO_TIMER *timer; 146 ALLEGRO_EVENT event; 147 148 if(!al_init()) { 149 abort_example("Could not init Allegro.\n"); 150 return 1; 151 } 152 153 al_init_image_addon(); 154 al_init_font_addon(); 155 al_init_primitives_addon(); 156 157 al_install_keyboard(); 158 159 al_set_new_display_flags(ALLEGRO_OPENGL); 160 al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 16, ALLEGRO_SUGGEST); 161 display = al_create_display(1024, 768); 162 if(!display) { 163 abort_example("Could not create display.\n"); 164 } 165 166 timer = al_create_timer(1. / 60.); 167 168 queue = al_create_event_queue(); 169 al_register_event_source(queue,al_get_keyboard_event_source()); 170 al_register_event_source(queue,al_get_display_event_source(display)); 171 al_register_event_source(queue,al_get_timer_event_source(timer)); 172 173 glEnable(GL_DEPTH_TEST); 174 ///glDisable(GL_CULL_FACE); 175 176// printf("%s\n",glGetString(GL_EXTENSIONS)); 177 178 setup_textures(display); 179 al_start_timer(timer); 180int test = 1; 181 while(true) { 182 al_wait_for_event(queue, &event); 183 switch(event.type) { 184 case ALLEGRO_EVENT_DISPLAY_CLOSE: 185 goto done; 186 187 case ALLEGRO_EVENT_KEY_DOWN: 188 if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 189 goto done; 190 key[event.keyboard.keycode] = true; 191 break; 192 193 case ALLEGRO_EVENT_KEY_UP: 194 key[event.keyboard.keycode] = false; 195 break; 196 197 case ALLEGRO_EVENT_TIMER: 198 keyboard(); 199 if(al_is_event_queue_empty(queue)) { 200 set_camera_position(); 201 draw(); 202 glDisable(GL_DEPTH_TEST); 203 map(); 204 glEnable(GL_DEPTH_TEST); 205 206 al_set_target_bitmap(buffer); 207 al_draw_bitmap(al_get_backbuffer(display), 0, 0, NULL); 208 al_set_target_backbuffer(display); 209 210 //printf("teste\n"); 211 //if (test){al_save_bitmap("./saveme.png", buffer);test=0;} 212 213 al_flip_display(); 214 } 215 break; 216 } 217 } 218 219done: 220 al_destroy_bitmap(bmp); 221 222 return 0; 223}

Yes, i use the ex_gldepth as my base code ;)

Thanks all!

William Labbett
Member #4,486
March 2004
avatar

Did you know if you call

al_set_target_bitmap() before ogl calls the opengl commands will get drawn on the target bitmap... ?

ie - you don't have to render to the backbuffer.

gnolam
Member #2,030
March 2002
avatar

killocan said:

Hi there! I'm trying to implement bloom effect w/o shaders

Then it's going to be slow, no matter what you do. :P
Why are you even attempting it? This is a problem that GPUs are built to solve.

Quote:

The problem is that when i copy the backbuffer to another BITMAP it's taking a LOT of time.

And reading back data from video memory is also slow.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Mark Oates
Member #1,146
March 2001
avatar

Ugh. I hate bloom. People use it like McDonald's uses salt. >:(

It's right up there next to the programmer's idea of a color palette.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Go to: