Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro and OpenGL Drawing Problem

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Allegro and OpenGL Drawing Problem
jBacher18
Member #16,743
September 2017

I'm working on a large project that was initially developed on an OSX machine but is now being ported to windows. I expected some degree of difficulty in porting the code, but I solved all issues pertaining to problematic includes and have already made sure to include the proper OpenGL libraries.

My current issue is that, for example, with the following code segment which should display a Quad that takes up most of the screen displays nothing, or, rather, it displays the clearing color (blue or green) but never the quad. Peculiarly, this same code works on my OSX machine.

#SelectExpand
1void init(){ 2 glMatrixMode(GL_PROJECTION); 3 glLoadIdentity(); 4 glOrtho(0, viewWidth, 0, viewHeight, -1, 1); 5 glMatrixMode(GL_MODELVIEW); 6 glClearColor (0.0, 0.0, 0.0, 1.0); 7 glColor3f(1.0,0.0,0.0); 8 glDisable(GL_DEPTH_TEST); 9} 10 11int main(int argc, char **argv) { 12 al_init(); 13 al_install_keyboard(); 14 al_init_primitives_addon(); 15 16 std::cout << winWidth << "\t" << winHeight << std::endl; 17 ALLEGRO_DISPLAY* display = al_create_display(winWidth, winHeight); 18 19 init(); // Init OpenGL vars 20 21 int j = 0, target = 0; 22 23 while (true) { 24 if (j > 750) { 25 glClear(GL_COLOR_BUFFER_BIT); 26 al_clear_to_color(al_map_rgb(0, 255, 0)); 27 al_flip_display(); 28 if (j < 1500) { 29 j++; 30 } 31 else { 32 j = 0; 33 } 34 } 35 else{ 36 glClear(GL_COLOR_BUFFER_BIT); 37 al_clear_to_color(al_map_rgb(0, 0, 255)); 38 glColor3ub(125, 125, 0); 39 glBegin(GL_QUADS); 40 glVertex2i(10, 10); 41 glVertex2i(viewWidth - 10, 10); 42 glVertex2i(viewWidth - 10, viewHeight - 10); 43 glVertex2i(10, viewHeight - 10); 44 glEnd(); 45 al_flip_display(); 46 j++; 47 } 48 49 50 } 51}

As it stands, my best guess is that I've made some mistake with my includes or I missed something important to setting up OpenGL in Allegro that I didn't have to do on my OSX machine.

Also important: I know now that Allegro includes primitives that I should've been using from the get-go, but I wanted to use OpenGL for a variety of reasons and so, at the moment, switching to Allegro primitives is not a preferred option.

Any ideas as to what is going wrong would be appreciated.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Allegro and OpenGL each have their own transformations. Allegro uses OpenGL's under the hood, but doesn't respect OpenGL transformations you set yourself.

In your 'init' function you're not setting an identity matrix for the MODELVIEW matrix. Also, just try to draw one thing once and see if it works or not.

Mixing Allegro and OpenGL does work, but has some problems when you change global GL state that one or the other was expecting.

This simple example should show you how to draw your Quad with OpenGL :

#SelectExpand
1 2 3 4#include "allegro5/allegro.h" 5#include "allegro5/allegro_primitives.h" 6#include "allegro5/allegro_opengl.h" 7 8int ww = 800; 9int wh = 600; 10 11 12void resetOpenGLmatrices(); 13 14int main(int argc , char** argv) { 15 16 if (!al_init()) {return 1;} 17 if (!al_init_primitives_addon()) {return 2;} 18 19 if (!al_install_keyboard()) {return 3;} 20 21 22 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED); 23 24 ALLEGRO_DISPLAY* d = al_create_display(ww,wh); 25 26 if (!d) {return -1;} 27 28 ALLEGRO_TIMER* t = al_create_timer(1.0/60.0); 29 30 if (!t) {return -2;} 31 32 ALLEGRO_EVENT_QUEUE* q = al_create_event_queue(); 33 34 if (!q) {return -3;} 35 36 al_register_event_source(q , al_get_display_event_source(d)); 37 al_register_event_source(q , al_get_timer_event_source(t)); 38 al_register_event_source(q , al_get_keyboard_event_source()); 39 40 bool redraw = true; 41 bool quit = false; 42 43 int shrink = 0; 44 int shrink_dir = 1; 45 46 al_start_timer(t); 47 48 al_clear_to_color(al_map_rgb(0,0,0)); 49 al_flip_display(); 50 51 while (!quit) { 52 if (redraw) { 53 al_set_target_backbuffer(d); 54 al_clear_to_color(al_map_rgb(255,255,255)); 55 56 resetOpenGLmatrices(); 57 58 glColor3ub(0,0,0); 59 60 glBegin(GL_QUADS); 61 62 glVertex2i(shrink,shrink); 63 glVertex2i(shrink,wh-shrink); 64 glVertex2i(ww-shrink,wh-shrink); 65 glVertex2i(ww-shrink,shrink); 66 67 glEnd(); 68 69 al_flip_display(); 70 71 redraw = false; 72 } 73 do { 74 ALLEGRO_EVENT ev; 75 al_wait_for_event(q , &ev); 76 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {quit = true;} 77 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {quit = true;} 78 if (ev.type == ALLEGRO_EVENT_TIMER) { 79 redraw = true; 80 shrink += shrink_dir; 81 if (shrink > ww/2) { 82 shrink = ww/2; 83 shrink_dir = -1; 84 } 85 if (shrink < 0) { 86 shrink = 0; 87 shrink_dir = 1; 88 } 89 } 90 } while (!al_is_event_queue_empty(q)); 91 92 } 93 94 95 return 0; 96} 97 98 99void resetOpenGLmatrices() { 100 glMatrixMode(GL_PROJECTION); 101 glLoadIdentity(); 102 glOrtho(0 , ww , 0 , wh , -1 , 1); 103 glMatrixMode(GL_MODELVIEW); 104 glLoadIdentity(); 105}

Basically, any time you draw with allegro, you reset the target bitmap, which resets opengl state. Any time you want to draw with opengl, you need to reset the state yourself.

jBacher18
Member #16,743
September 2017

Thank you so much, Wonderful explanation. That fixes the problem and provides me a nice example to look back on. Thanks Again!

kenmasters1976
Member #8,794
July 2007

I had a similar problem just recently with mixing Allegro and OpenGL. Take a look at this thread for the details.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I managed to get a colored textured spinning cube drawn. Here's how I did it :

Download src + win32 binary here

{"name":"611190","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/a\/6aab5988fe9697b0f23d12cce0e2da92.png","w":802,"h":641,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/a\/6aab5988fe9697b0f23d12cce0e2da92"}611190

#SelectExpand
1 2#include "allegro5/allegro.h" 3#include "allegro5/allegro_memfile.h" 4#include "allegro5/allegro_primitives.h" 5#include "allegro5/allegro_font.h" 6#include "allegro5/allegro_ttf.h" 7#include "allegro5/allegro_color.h" 8#include "allegro5/allegro_opengl.h" 9 10#include "GL/glu.h" 11#include <cmath> 12#include <cstdio> 13 14#include "Consola.h" 15 16 17int ww = 800; 18int wh = 600; 19 20 21 22void resetOpenGLmatrices(); 23 24struct XYZ { 25 float x,y,z; 26}; 27 28XYZ uc[8] = { 29 {-0.5f,-0.5f,-0.5f}, 30 {-0.5f, 0.5f,-0.5f}, 31 { 0.5f, 0.5f,-0.5f}, 32 { 0.5f,-0.5f,-0.5f}, 33 { 0.5f,-0.5f, 0.5f}, 34 { 0.5f, 0.5f, 0.5f}, 35 {-0.5f, 0.5f, 0.5f}, 36 {-0.5f,-0.5f, 0.5f} 37}; 38 39struct RGB { 40 float r,g,b,a; 41}; 42 43RGB cc1[8] = { 44 {1.0f,1.0f,1.0f,1.0f}, 45 {1.0f,0.0f,0.0f,1.0f}, 46 {0.0f,1.0f,0.0f,1.0f}, 47 {0.0f,0.0f,1.0f,1.0f}, 48 {1.0f,1.0f,0.0f,1.0f}, 49 {0.0f,0.0f,0.0f,1.0f}, 50 {0.0f,1.0f,1.0f,1.0f}, 51 {1.0f,0.0f,1.0f,1.0f} 52}; 53 54RGB cc[8] = { 55 {1.0f,1.0f,1.0f,1.0f}, 56 {1.0f,0.0f,0.0f,1.0f}, 57 {0.0f,0.0f,1.0f,1.0f}, 58 {0.0f,1.0f,1.0f,1.0f}, 59 {1.0f,1.0f,0.0f,1.0f}, 60 {0.0f,0.0f,0.0f,1.0f}, 61 {0.0f,1.0f,1.0f,1.0f}, 62 {1.0f,0.0f,1.0f,1.0f} 63}; 64 65struct TEX2D { 66 float u,v; 67}; 68 69TEX2D tex2d[4] = { 70 {0.0,1.0}, 71 {0.0,0.0}, 72 {1.0,0.0}, 73 {1.0,1.0} 74}; 75 76struct TRI { 77 XYZ* p1; 78 XYZ* p2; 79 XYZ* p3; 80 RGB* c1; 81 RGB* c2; 82 RGB* c3; 83 TEX2D* tuv1; 84 TEX2D* tuv2; 85 TEX2D* tuv3; 86 87}; 88 89TRI cube_faces[12] = { 90 {&uc[0] , &uc[1] , &uc[2], &cc[0] , &cc[1] , &cc[2] , &tex2d[0] , &tex2d[1] , &tex2d[2]}, 91 {&uc[2] , &uc[3] , &uc[0], &cc[2] , &cc[3] , &cc[0] , &tex2d[2] , &tex2d[3] , &tex2d[0]}, 92 {&uc[3] , &uc[2] , &uc[5], &cc[3] , &cc[2] , &cc[5] , &tex2d[0] , &tex2d[1] , &tex2d[2]}, 93 {&uc[5] , &uc[4] , &uc[3], &cc[5] , &cc[4] , &cc[3] , &tex2d[2] , &tex2d[3] , &tex2d[0]}, 94 {&uc[4] , &uc[5] , &uc[6], &cc[4] , &cc[5] , &cc[6] , &tex2d[0] , &tex2d[1] , &tex2d[2]}, 95 {&uc[6] , &uc[7] , &uc[4], &cc[6] , &cc[7] , &cc[4] , &tex2d[2] , &tex2d[3] , &tex2d[0]}, 96 {&uc[7] , &uc[6] , &uc[1], &cc[7] , &cc[6] , &cc[1] , &tex2d[0] , &tex2d[1] , &tex2d[2]}, 97 {&uc[1] , &uc[0] , &uc[7], &cc[1] , &cc[0] , &cc[7] , &tex2d[2] , &tex2d[3] , &tex2d[0]}, 98 {&uc[0] , &uc[3] , &uc[4], &cc[0] , &cc[3] , &cc[4] , &tex2d[0] , &tex2d[1] , &tex2d[2]}, 99 {&uc[4] , &uc[7] , &uc[0], &cc[4] , &cc[7] , &cc[0] , &tex2d[2] , &tex2d[3] , &tex2d[0]}, 100 {&uc[1] , &uc[6] , &uc[5], &cc[1] , &cc[6] , &cc[5] , &tex2d[0] , &tex2d[1] , &tex2d[2]}, 101 {&uc[5] , &uc[2] , &uc[1], &cc[5] , &cc[2] , &cc[1] , &tex2d[2] , &tex2d[3] , &tex2d[0]} 102}; 103 104 105int main(int argc , char** argv) { 106 107 if (!al_init()) {return 1;} 108 if (!al_init_primitives_addon()) {return 2;} 109 if (!al_init_font_addon()) {return 3;} 110 if (!al_init_ttf_addon()) {return 4;} 111 112 113 if (!al_install_keyboard()) {return 5;} 114 if (!al_install_mouse()) {return 6;} 115 116/// al_set_new_display_option(ALLEGRO_FLOAT_DEPTH , ALLEGRO_REQUIRE , 1); 117 al_set_new_display_option(ALLEGRO_DEPTH_SIZE , ALLEGRO_SUGGEST , 32); 118 119 al_set_new_display_flags(ALLEGRO_RESIZABLE | ALLEGRO_OPENGL | ALLEGRO_WINDOWED); 120 121 ALLEGRO_DISPLAY* d = al_create_display(ww,wh); 122 123 if (!d) {return -1;} 124 125 ALLEGRO_TIMER* t = al_create_timer(1.0/60.0); 126 127 if (!t) {return -2;} 128 129 ALLEGRO_EVENT_QUEUE* q = al_create_event_queue(); 130 131 if (!q) {return -3;} 132 133 al_register_event_source(q , al_get_display_event_source(d)); 134 al_register_event_source(q , al_get_timer_event_source(t)); 135 al_register_event_source(q , al_get_keyboard_event_source()); 136 al_register_event_source(q , al_get_mouse_event_source()); 137 138 int tw = 256; 139 140 /// Consola_ttf comes from our Consola.h header, it is the data for the Consola.ttf file 141 142 ALLEGRO_FILE* file = al_open_memfile(Consola_ttf , sizeof(Consola_ttf)/sizeof(unsigned char) , "r"); 143 144 if (!file) { 145 return -5; 146 } 147 ALLEGRO_FONT* f = al_load_ttf_font_f(file , "Consola.ttf" , 3*tw/8 , 0); 148 149 if (!f) { 150 printf("Failed to load ttf font from memfile.\n"); 151 return -6; 152 } 153 154 155 /// Create a 2x2 black and white checker texture with text on it 156 ALLEGRO_BITMAP* atex = al_create_bitmap(tw,tw); 157 158 al_set_target_bitmap(atex); 159 al_clear_to_color(al_map_rgba(255,255,255,255)); 160 161 al_draw_filled_rectangle(0,0,tw/2,tw/2 , al_map_rgba(0,0,0,255)); 162 al_draw_filled_rectangle(tw/2,tw/2,tw,tw,al_map_rgba(0,0,0,255)); 163 164 al_draw_text(f , al_map_rgb(255,255,255) , tw/4 , tw/4 - al_get_font_line_height(f)/2 , ALLEGRO_ALIGN_CENTER , "A"); 165 al_draw_text(f , al_map_rgb(0,0,0) , 3*tw/4 , tw/4 - al_get_font_line_height(f)/2 , ALLEGRO_ALIGN_CENTER , "B"); 166 al_draw_text(f , al_map_rgb(0,0,0) , tw/4 , 3*tw/4 - al_get_font_line_height(f)/2 , ALLEGRO_ALIGN_CENTER , "C"); 167 al_draw_text(f , al_map_rgb(255,255,255) , 3*tw/4 , 3*tw/4 - al_get_font_line_height(f)/2 , ALLEGRO_ALIGN_CENTER , "D"); 168 169 float rotx = 0.0; 170 float roty = 0.0; 171 float rotz = 180.0; 172 173 float rotv = 0.5; 174 float rotxv = 4*rotv; 175 float rotyv = 2*rotv; 176 float rotzv = rotv; 177 float rotlimit = 360; 178 179 bool redraw = true; 180 bool quit = false; 181 182 int shrink = 0; 183 int shrink_dir = 1; 184 185 al_start_timer(t); 186 187 glEnable(GL_TEXTURE_2D); 188 glEnable(GL_DEPTH_TEST); 189 glEnable(GL_CULL_FACE); 190 glDisable(GL_LIGHTING); 191 glFrontFace(GL_CCW); 192 glCullFace(GL_NONE); 193 194 while (!quit) { 195 if (redraw) { 196 al_set_target_backbuffer(d); 197 al_clear_to_color(al_map_rgb(255,255,255)); 198 199 glClear(GL_DEPTH_BUFFER_BIT); 200 201 resetOpenGLmatrices(); 202 203 float scale = 1.0 - (shrink/(ww/2.0f)); 204/// glScalef(2.0*scale , 2*scale , 2*scale); 205 scale = 1.5; 206 glScalef(scale , scale , scale); 207 glRotatef(rotz , 0 , 0 , 1); 208 glRotatef(rotx , 1 , 0 , 0); 209 glRotatef(roty , 0 , 1 , 0); 210 211 GLuint texid = al_get_opengl_texture(atex); 212 glBindTexture(GL_TEXTURE_2D , texid); 213 214//* 215 /// Draw outside of cube 216 glFrontFace(GL_CCW); 217 glCullFace(GL_BACK); 218 glBegin(GL_TRIANGLES); 219 int i = 0; 220 while (i < 12) { 221 float r,g,b; 222 al_color_hsl_to_rgb((i/2)*60.0f , 1.0 , 0.5 , &r , &g , &b); 223 224 RGB hue = {r,g,b}; 225 226 TRI* face = &cube_faces[i]; 227 XYZ* f[3] = {face->p1 , face->p2 , face->p3}; 228/// XYZ* f2[3] = {face->p3 , face->p2 , face->p1}; 229/// RGB* col[3] = {face->c1 , face->c2 , face->c3}; 230 TEX2D* txy[3] = {face->tuv1 , face->tuv2 , face->tuv3}; 231 232 int j = 0; 233 while (j < 3) { 234 XYZ* p = f[j]; 235/// RGB* c = col[j]; 236 RGB* c = &hue; 237 TEX2D* t = txy[j]; 238 glColor4f(c->r,c->g,c->b,1.0f); 239 glTexCoord2f(t->u , t->v); 240 glVertex3f(p->x , p->y , p->z); 241 ++j; 242 } 243 ++i; 244 } 245 glEnd(); 246 247 al_flip_display(); 248 249 redraw = false; 250 } 251 do { 252 ALLEGRO_EVENT ev; 253 al_wait_for_event(q , &ev); 254 if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { 255 al_acknowledge_resize(d); 256 ww = al_get_display_width(d); 257 wh = al_get_display_height(d); 258 } 259 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {quit = true;} 260 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {quit = true;} 261 if (ev.type == ALLEGRO_EVENT_TIMER) { 262 redraw = true; 263 264 shrink += shrink_dir; 265 if (shrink > ww/2) { 266 shrink = ww/2; 267 shrink_dir = -1; 268 } 269 if (shrink < 0) { 270 shrink = 0; 271 shrink_dir = 1; 272 } 273 /* 274 rotx += rotxv; 275 rotx = fmod(rotx , rotlimit); 276 roty += rotyv; 277 roty = fmod(roty , rotlimit); 278 rotz += rotzv; 279 rotz = fmod(rotz , rotlimit); 280 */ 281 } 282//* 283 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) { 284 roty = 360.0f*((ww/2) - ev.mouse.x)/(ww/2); 285 rotx = 360.0f*((wh/2) - ev.mouse.y)/(wh/2); 286 rotz += 30.0f*ev.mouse.dz; 287 rotz = fmod(rotz , rotlimit); 288 redraw = true; 289 } 290//*/ 291 } while (!al_is_event_queue_empty(q)); 292 293 } 294 295 296 return 0; 297} 298 299 300void resetOpenGLmatrices() { 301 /// Set the projection matrix 302 glMatrixMode(GL_PROJECTION); 303 glLoadIdentity();/// Reset 304 305/// glOrtho(0 , ww , 0 , wh , -1 , 1);/// Use this for 2D projections the size of the screen (ww,wh) 306 307 gluPerspective(90 , ww/(float)wh , 0.5 , 5);/// gluPerspective values for Z should ALWAYS be postive and non-zero 308 309 310 /// Set the model view matrix 311 glMatrixMode(GL_MODELVIEW); 312 glLoadIdentity();/// Reset 313 gluLookAt(0 , 0 , -2 , 0 , 0 , 0 , 0 , 1 , 0);/// setup our camera matrix at z = -2 looking at the origin, with a regular up vector 314}

Go to: