Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro + OpenGL

This thread is locked; no one can reply to it. rss feed Print
Allegro + OpenGL
Luiji99
Member #12,254
September 2010

Hey guys,

I'm working on a 3-D OpenGL game and am using Allegro. However, I have no idea how to do this (with Allegro, that is, I understand OpenGL).

Here's some code:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_opengl.h> 3 4int 5main (void) 6{ 7 al_init (); 8 9 al_set_new_display_flags (ALLEGRO_OPENGL); 10 11 ALLEGRO_DISPLAY *display = al_create_display (800, 600); 12 al_set_current_opengl_context (display); 13 14 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue (); 15 al_register_event_source (event_queue, al_get_display_event_source (display)); 16 17 glEnable (GL_DEPTH_TEST); 18 glViewport (0, 0, 800, 600); 19 20 for (;;) 21 { 22 ALLEGRO_EVENT event; 23 while (al_get_next_event (event_queue, &event)) 24 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 25 return EXIT_SUCCESS; 26 27 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 28 29 glBegin (GL_POLYGON); 30 glColor3f (1.0, 1.0, 1.0); 31 glVertex3f (0.5, -0.5, 0.5); 32 glVertex3f (0.5, 0.5, 0.5); 33 glVertex3f (-0.5, 0.5, 0.5); 34 glVertex3f (-0.5, -0.5, 0.5); 35 glEnd (); 36 37 glBegin (GL_POLYGON); 38 glColor3f (1.0, 0.0, 1.0); 39 glVertex3f (0.5, -0.5, -0.5); 40 glVertex3f (0.5, 0.5, -0.5); 41 glVertex3f (0.5, 0.5, 0.5); 42 glVertex3f (0.5, -0.5, 0.5); 43 glEnd (); 44 45 glBegin (GL_POLYGON); 46 glColor3f (0.0, 1.0, 0.0); 47 glVertex3f (-0.5, -0.5, 0.5); 48 glVertex3f (-0.5, 0.5, 0.5); 49 glVertex3f (-0.5, 0.5, -0.5); 50 glVertex3f (-0.5, -0.5, -0.5); 51 glEnd (); 52 53 glBegin (GL_POLYGON); 54 glColor3f (0.0, 0.0, 1.0); 55 glVertex3f (0.5, 0.5, 0.5); 56 glVertex3f (0.5, 0.5, -0.5); 57 glVertex3f (-0.5, 0.5, -0.5); 58 glVertex3f (-0.5, 0.5, 0.5); 59 glEnd (); 60 61 glBegin (GL_POLYGON); 62 glColor3f (1.0, 0.0, 0.0); 63 glVertex3f (0.5, -0.5, -0.5); 64 glVertex3f (0.5, -0.5, 0.5); 65 glVertex3f (-0.5, -0.5, 0.5); 66 glVertex3f (-0.5, -0.5, -0.5); 67 glEnd (); 68 69 glFlush (); 70 al_flip_display (); 71 } 72} 73 74/* vim:set ts=8 sts=2 sw=2 noet: */

Pardon how hideous it is, lacking any form of error checking and all. I'm just trying to figure out how Allegro and OpenGL work together. I'm sure a few of those calls aren't even necessary, but I've been trying everything to get something other than a blank, black screen.

Any help would be greatly, GREATLY appreciated.
- Luiji

Programming should be fun. That's why I hate Java.
http://www.entertainingsoftware.com/

jmasterx
Member #11,410
October 2009

One thing that comes to mind, I think Allegro does not set a depth buffer, check this out to set one:
http://www.liballeg.org/a5docs/refman/display.html#al_set_new_display_option

ALLEGRO_DEPTH_SIZE

How many depth buffer (z-buffer) bits to use.

You might also want to check out:
ex_opengl.c

Luiji99
Member #12,254
September 2010

Hm, I've modified the code to set the ALLEGRO_DEPTH_SIZE to 30 (sort of a random number, I figured it'd be big enough to play with later):

#SelectExpand
1#include <err.h> 2 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_opengl.h> 5 6int 7main (void) 8{ 9 if (!al_init ()) 10 errx (1, "failed to initialize allegro"); 11 12 al_set_new_display_flags (ALLEGRO_OPENGL); 13 al_set_new_display_option (ALLEGRO_DEPTH_SIZE, 30, ALLEGRO_REQUIRE); 14 15 ALLEGRO_DISPLAY *display = al_create_display (800, 600); 16 if (!display) 17 errx (1, "failed to create display"); 18 19 al_set_current_opengl_context (display); 20 21 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue (); 22 if (!event_queue) 23 errx (1, "failed to create event queue"); 24 25 al_register_event_source (event_queue, al_get_display_event_source (display)); 26 27 glEnable (GL_DEPTH_TEST); 28 glViewport (0, 0, 800, 600); 29 30 for (;;) 31 { 32 ALLEGRO_EVENT event; 33 while (al_get_next_event (event_queue, &event)) 34 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 35 { 36 al_destroy_event_queue (event_queue); 37 return EXIT_SUCCESS; 38 } 39 40 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 41 42 glBegin (GL_POLYGON); 43 glColor3f (1.0, 1.0, 1.0); 44 glVertex3f (0.5, -0.5, 0.5); 45 glVertex3f (0.5, 0.5, 0.5); 46 glVertex3f (-0.5, 0.5, 0.5); 47 glVertex3f (-0.5, -0.5, 0.5); 48 glEnd (); 49 50 glBegin (GL_POLYGON); 51 glColor3f (1.0, 0.0, 1.0); 52 glVertex3f (0.5, -0.5, -0.5); 53 glVertex3f (0.5, 0.5, -0.5); 54 glVertex3f (0.5, 0.5, 0.5); 55 glVertex3f (0.5, -0.5, 0.5); 56 glEnd (); 57 58 glBegin (GL_POLYGON); 59 glColor3f (0.0, 1.0, 0.0); 60 glVertex3f (-0.5, -0.5, 0.5); 61 glVertex3f (-0.5, 0.5, 0.5); 62 glVertex3f (-0.5, 0.5, -0.5); 63 glVertex3f (-0.5, -0.5, -0.5); 64 glEnd (); 65 66 glBegin (GL_POLYGON); 67 glColor3f (0.0, 0.0, 1.0); 68 glVertex3f (0.5, 0.5, 0.5); 69 glVertex3f (0.5, 0.5, -0.5); 70 glVertex3f (-0.5, 0.5, -0.5); 71 glVertex3f (-0.5, 0.5, 0.5); 72 glEnd (); 73 74 glBegin (GL_POLYGON); 75 glColor3f (1.0, 0.0, 0.0); 76 glVertex3f (0.5, -0.5, -0.5); 77 glVertex3f (0.5, -0.5, 0.5); 78 glVertex3f (-0.5, -0.5, 0.5); 79 glVertex3f (-0.5, -0.5, -0.5); 80 glEnd (); 81 82 glFlush (); 83 al_flip_display (); 84 } 85} 86 87/* vim:set ts=8 sts=2 sw=2 noet: */

However, now my program aborts with "cube: failed to create display". It seems that Allegro can't set the z-depth. I don't think it's anything wrong with my hardware/drivers, though, since GlxGears runs perfectly and I'm pretty sure that it uses a z-buffer, too.

Thanks,
- L

Programming should be fun. That's why I hate Java.
http://www.entertainingsoftware.com/

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'm pretty sure z buffer sizes are limited to 16, 24 or 32 bits, since it's bits per element, it might have been better if they'd specified how many bytes per element, not bits.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Luiji99
Member #12,254
September 2010

Ah, I totally misunderstood what that field did. Anyway...

#SelectExpand
1#include <err.h> 2 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_opengl.h> 5 6int 7main (void) 8{ 9 if (!al_init ()) 10 errx (1, "failed to initialize allegro"); 11 12 al_set_new_display_flags (ALLEGRO_OPENGL); 13 al_set_new_display_option (ALLEGRO_DEPTH_SIZE, 16, ALLEGRO_REQUIRE); 14 15 ALLEGRO_DISPLAY *display = al_create_display (300, 300); 16 if (!display) 17 errx (1, "failed to create display"); 18 19 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue (); 20 if (!event_queue) 21 errx (1, "failed to create event queue"); 22 23 al_register_event_source (event_queue, al_get_display_event_source (display)); 24 25 glEnable (GL_DEPTH_TEST); 26 glClearColor (0.5, 0.5, 0.5, 1.0); 27 28 for (;;) 29 { 30 ALLEGRO_EVENT event; 31 while (al_get_next_event (event_queue, &event)) 32 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 33 { 34 al_destroy_event_queue (event_queue); 35 return EXIT_SUCCESS; 36 } 37 38 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 39 glLoadIdentity (); 40 41 glRotatef (45, 1, 0, 0); 42 glRotatef (45, 0, 1, 0); 43 44 glBegin(GL_POLYGON); 45 glColor3f(1, 0, 0 ); glVertex3f( 0.5, -0.5, -0.5); 46 glColor3f(0, 1, 0 ); glVertex3f( 0.5, 0.5, -0.5); 47 glColor3f(0, 0, 1 ); glVertex3f(-0.5, 0.5, -0.5); 48 glColor3f(1, 0, 1 ); glVertex3f(-0.5, -0.5, -0.5); 49 glEnd(); 50 51 glBegin (GL_POLYGON); 52 glColor3f (1, 1, 1); 53 glVertex3f (0.5, -0.5, 0.5); 54 glVertex3f (0.5, 0.5, 0.5); 55 glVertex3f (-0.5, 0.5, 0.5); 56 glVertex3f (-0.5, -0.5, 0.5); 57 glEnd (); 58 59 glBegin (GL_POLYGON); 60 glColor3f (1, 0, 1); 61 glVertex3f (0.5, -0.5, -0.5); 62 glVertex3f (0.5, 0.5, -0.5); 63 glVertex3f (0.5, 0.5, 0.5); 64 glVertex3f (0.5, -0.5, 0.5); 65 glEnd (); 66 67 glBegin (GL_POLYGON); 68 glColor3f (0, 1, 0); 69 glVertex3f (-0.5, -0.5, 0.5); 70 glVertex3f (-0.5, 0.5, 0.5); 71 glVertex3f (-0.5, 0.5, -0.5); 72 glVertex3f (-0.5, -0.5, -0.5); 73 glEnd (); 74 75 glBegin (GL_POLYGON); 76 glColor3f (0, 0, 1); 77 glVertex3f (0.5, 0.5, 0.5); 78 glVertex3f (0.5, 0.5, -0.5); 79 glVertex3f (-0.5, 0.5, -0.5); 80 glVertex3f (-0.5, 0.5, 0.5); 81 glEnd (); 82 83 glBegin (GL_POLYGON); 84 glColor3f (1, 0, 0); 85 glVertex3f (0.5, -0.5, -0.5); 86 glVertex3f (0.5, -0.5, 0.5); 87 glVertex3f (-0.5, -0.5, 0.5); 88 glVertex3f (-0.5, -0.5, -0.5); 89 glEnd (); 90 91 glFlush (); 92 al_flip_display (); 93 } 94} 95 96/* vim:set ts=8 sts=2 sw=2 noet: */

Played around with it a bit more. The display creates now, but all I see is grey (I set the clear color just to make sure that the OpenGL calls were working).

This code is essentially a GLUT equivalent that I'm basing this off of, and it works perfectly. It seems to be a problem with Allegro, or at least a difference in what can be assumed from each library:

#SelectExpand
1#include <GL/glut.h> 2 3static void 4display () 5{ 6 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 7 8 glLoadIdentity (); 9 10 glRotatef (45, 1, 0, 0); 11 glRotatef (45, 0, 1, 0); 12 13 glBegin (GL_POLYGON); 14 glColor3f (1, 0, 0); 15 glVertex3f (0.5, -0.5, -0.5); 16 glColor3f (0, 1, 0); 17 glVertex3f (0.5, 0.5, -0.5); 18 glColor3f (0, 0, 1); 19 glVertex3f (-0.5, 0.5, -0.5); 20 glColor3f (1, 0, 1); 21 glVertex3f (-0.5, -0.5, -0.5); 22 glEnd (); 23 24 glBegin (GL_POLYGON); 25 glColor3f (1, 1, 1); 26 glVertex3f (0.5, -0.5, 0.5); 27 glVertex3f (0.5, 0.5, 0.5); 28 glVertex3f (-0.5, 0.5, 0.5); 29 glVertex3f (-0.5, -0.5, 0.5); 30 glEnd (); 31 32 glBegin (GL_POLYGON); 33 glColor3f (1, 0, 1); 34 glVertex3f (0.5, -0.5, -0.5); 35 glVertex3f (0.5, 0.5, -0.5); 36 glVertex3f (0.5, 0.5, 0.5); 37 glVertex3f (0.5, -0.5, 0.5); 38 glEnd (); 39 40 glBegin (GL_POLYGON); 41 glColor3f (0, 1, 0); 42 glVertex3f (-0.5, -0.5, 0.5); 43 glVertex3f (-0.5, 0.5, 0.5); 44 glVertex3f (-0.5, 0.5, -0.5); 45 glVertex3f (-0.5, -0.5, -0.5); 46 glEnd (); 47 48 glBegin (GL_POLYGON); 49 glColor3f (0, 0, 1); 50 glVertex3f (0.5, 0.5, 0.5); 51 glVertex3f (0.5, 0.5, -0.5); 52 glVertex3f (-0.5, 0.5, -0.5); 53 glVertex3f (-0.5, 0.5, 0.5); 54 glEnd (); 55 56 glBegin (GL_POLYGON); 57 glColor3f (1, 0, 0); 58 glVertex3f (0.5, -0.5, -0.5); 59 glVertex3f (0.5, -0.5, 0.5); 60 glVertex3f (-0.5, -0.5, 0.5); 61 glVertex3f (-0.5, -0.5, -0.5); 62 glEnd (); 63 64 glFlush (); 65 glutSwapBuffers (); 66} 67 68int 69main (int argc, char *argv[]) 70{ 71 glutInit (&argc, argv); 72 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 73 glutCreateWindow (argv[0]); 74 75 glEnable (GL_DEPTH_TEST); 76 glClearColor (0.5, 0.5, 0.5, 1.0); 77 78 glutDisplayFunc (display); 79 80 glutMainLoop (); 81 82 return 0; 83}

==================== EDIT ====================

I did it! All I had to do was add "glMatrixMode (GL_PROJECTION)", which apparently GLUT automatically does and Allegro does not. It's kind of an odd difference, but I have a cube and that's satisfying!!

Thanks for the help,
- L

Programming should be fun. That's why I hate Java.
http://www.entertainingsoftware.com/

Thomas Fjellstrom
Member #476
June 2000
avatar

Allegro by default sets up a 2d "Allegro" matrix, so when you draw with allegro functions, things appear to work with a plain old 2d coordinate space with the top left being 0,0.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Luiji99
Member #12,254
September 2010

Ah.

Just wondering, might it be useful to include something like this in Allegro? I can clean this example up a bit...the resulting cube is pretty cool. It also takes advantage of ALLEGRO_TIMER, which is nice.

Thanks,
- L

Programming should be fun. That's why I hate Java.
http://www.entertainingsoftware.com/

Thomas Fjellstrom
Member #476
June 2000
avatar

If its showing stuff that existing examples don't show, I don't see why not.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Luiji99
Member #12,254
September 2010

Unlike the current OpenGL example, this one actually renders 3-D. I don't know what the Direct3D example shows...

I think it's also just pretty fun to run in the background to make you look cool in front of people looking over your shoulder. =)

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_opengl.h> 3 4#include "common.c" 5 6int main(void) 7{ 8 ALLEGRO_DISPLAY *display; 9 ALLEGRO_TIMER *timer; 10 ALLEGRO_EVENT_QUEUE *event_queue; 11 int rotation = 0; 12 13 if (!al_init()) 14 abort_example("failed to initialize allegro"); 15 16 al_set_new_display_flags(ALLEGRO_OPENGL); 17 al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 16, ALLEGRO_REQUIRE); 18 19 display = al_create_display(600, 600); 20 if (!display) 21 abort_example("failed to create display"); 22 23 timer = al_create_timer(ALLEGRO_BPS_TO_SECS(180)); 24 if (!timer) 25 abort_example("failed to create timer"); 26 27 event_queue = al_create_event_queue(); 28 if (!event_queue) 29 abort_example("failed to create event queue"); 30 31 al_register_event_source(event_queue, al_get_display_event_source(display)); 32 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 33 34 glEnable(GL_DEPTH_TEST); 35 glClearColor(0.5, 0.5, 0.5, 1.0); 36 glMatrixMode(GL_PROJECTION); 37 38 al_start_timer(timer); 39 40 for (;;) { 41 ALLEGRO_EVENT event; 42 while (al_get_next_event(event_queue, &event)) 43 if (event.type == ALLEGRO_EVENT_TIMER) { 44 rotation = rotation == 359 ? 0 : rotation + 1; 45 } 46 else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 47 al_destroy_event_queue(event_queue); 48 al_destroy_timer(timer); 49 al_destroy_display(display); 50 return EXIT_SUCCESS; 51 } 52 53 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 54 glLoadIdentity(); 55 56 glRotatef(rotation, 1, 0, 0); 57 glRotatef(rotation, 0, 1, 0); 58 59 glBegin(GL_POLYGON); 60 glColor3f(1, 0, 0); 61 glVertex3f(0.5, -0.5, -0.5); 62 glColor3f(0, 1, 0); 63 glVertex3f(0.5, 0.5, -0.5); 64 glColor3f(0, 0, 1); 65 glVertex3f(-0.5, 0.5, -0.5); 66 glColor3f(1, 0, 1); 67 glVertex3f(-0.5, -0.5, -0.5); 68 glEnd(); 69 70 glBegin(GL_POLYGON); 71 glColor3f(1, 1, 1); 72 glVertex3f(0.5, -0.5, 0.5); 73 glVertex3f(0.5, 0.5, 0.5); 74 glVertex3f(-0.5, 0.5, 0.5); 75 glVertex3f(-0.5, -0.5, 0.5); 76 glEnd(); 77 78 glBegin(GL_POLYGON); 79 glColor3f(1, 0, 1); 80 glVertex3f(0.5, -0.5, -0.5); 81 glVertex3f(0.5, 0.5, -0.5); 82 glVertex3f(0.5, 0.5, 0.5); 83 glVertex3f(0.5, -0.5, 0.5); 84 glEnd(); 85 86 glBegin(GL_POLYGON); 87 glColor3f(0, 1, 0); 88 glVertex3f(-0.5, -0.5, 0.5); 89 glVertex3f(-0.5, 0.5, 0.5); 90 glVertex3f(-0.5, 0.5, -0.5); 91 glVertex3f(-0.5, -0.5, -0.5); 92 glEnd(); 93 94 glBegin(GL_POLYGON); 95 glColor3f(0, 0, 1); 96 glVertex3f(0.5, 0.5, 0.5); 97 glVertex3f(0.5, 0.5, -0.5); 98 glVertex3f(-0.5, 0.5, -0.5); 99 glVertex3f(-0.5, 0.5, 0.5); 100 glEnd(); 101 102 glBegin(GL_POLYGON); 103 glColor3f(1, 0, 0); 104 glVertex3f(0.5, -0.5, -0.5); 105 glVertex3f(0.5, -0.5, 0.5); 106 glVertex3f(-0.5, -0.5, 0.5); 107 glVertex3f(-0.5, -0.5, -0.5); 108 glEnd(); 109 110 al_flip_display(); 111 } 112} 113 114/* vim: set sts=3 sw=3 et: */

I think that under the Allegro.cc terms you guys now own that code or something. I don't exactly know how that provision works, I still own the Copyright, don't I? Anyway, for the wonders of legal clarity, I hereby give the Allegro developers permission to redistribute that code under any license they want, with or without modification. I PROVIDE NOT WARRANTY AT ALL.

Programming should be fun. That's why I hate Java.
http://www.entertainingsoftware.com/

Arthur Kalliokoski
Second in Command
February 2005
avatar

Luiji99 said:

Unlike the current OpenGL example, this one actually renders 3-D.

The ex_gldepth example shows interlocking 3D pyramids with textures.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Luiji99
Member #12,254
September 2010

Ah, well then my example doesn't really add anything new. I like the way mine looks better, though. =P

The current example actually has more stuff, too, such as drawing graphical textures onto the polygons.

Thanks,
- L

Programming should be fun. That's why I hate Java.
http://www.entertainingsoftware.com/

Go to: