|
Need help initializing OpenGL 3.0+ in Allegro |
chex_5
Member #14,081
February 2012
|
Hello, I have been trying to search around the internet for info or tutorials on OpenGL 3.0, but every tutorial I've found requires extra libraries. I figured that with Allegro 5, I can completely skip GLUT altogether. Unfortunately, I haven't been able to get even a simple blue window to pop up. 1#include <cstdio>
2#include <allegro5/allegro.h>
3#include <allegro5/allegro_opengl.h>
4#include <GL/gl3.h>
5using namespace std;
6
7#ifndef FPS
8#define FPS 60.0
9#endif
10
11bool initAL();
12void initGL();
13
14int main() {
15 if (initAL()) {
16 initGL();
17 }
18 else {
19 return -1;
20 }
21 if (!al_install_keyboard()) {
22 printf("ERROR: could not install keyboard driver.\n");
23 return -1;
24 }
25
26 bool done = false;
27 bool redraw = false;
28 int scrW = 640;
29 int scrH = 480;
30 ALLEGRO_DISPLAY* display = NULL;
31 ALLEGRO_TIMER* timer = NULL;
32 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
33 ALLEGRO_EVENT event;
34
35 al_set_new_display_flags( ALLEGRO_WINDOWED |
36 ALLEGRO_RESIZABLE |
37 ALLEGRO_OPENGL_3_0 |
38 ALLEGRO_OPENGL_FORWARD_COMPATIBLE);
39 al_set_new_display_option( ALLEGRO_COLOR_SIZE, 32, ALLEGRO_REQUIRE);
40 al_set_new_display_option( ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE);
41 al_set_new_display_option( ALLEGRO_STENCIL_SIZE, 8, ALLEGRO_REQUIRE);
42 al_set_new_display_option( ALLEGRO_AUX_BUFFERS, 0, ALLEGRO_REQUIRE);
43 al_set_new_display_option( ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
44 al_set_new_display_option( ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST);
45 display = al_create_display(scrW, scrH);
46 if (display == NULL) {
47 printf("ERROR: Failed to create Display.\n");
48 return -1;
49 }
50 if ((al_get_new_display_flags() ^ (ALLEGRO_OPENGL_FORWARD_COMPATIBLE | ALLEGRO_OPENGL_3_0)) == 0) {
51 printf("ERROR: Could not get an OpenGL 3.0 display.\n");
52 return -1;
53 }
54 else {
55 printf("OpenGL 3.0 capable display detected.\n");
56 al_set_current_opengl_context(display);
57 }
58
59 timer = al_create_timer(1.0f / FPS);
60 event_queue = al_create_event_queue();
61 if (event_queue == NULL) {
62 printf("ERROR: Failed to create Event Queue.\n");
63 return -1;
64 }
65 al_register_event_source(event_queue, al_get_timer_event_source(timer));
66 al_register_event_source(event_queue, al_get_display_event_source(display));
67 al_register_event_source(event_queue, al_get_keyboard_event_source());
68 al_start_timer(timer);
69
70 while (!done) {
71 al_wait_for_event(event_queue, &event);
72
73 if (event.type == ALLEGRO_EVENT_TIMER) {
74 redraw = true;
75 }
76 else if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
77 if (event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) {
78
79 }
80 else {
81 done = true;
82 }
83 }
84 else if (event.type == ALLEGRO_EVENT_KEY_UP) {
85
86 }
87 else if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
88 if (al_acknowledge_resize(event.display.source)) {
89 scrW = al_get_display_width(display);
90 scrH = al_get_display_height(display);
91 }
92 else {
93 printf("WARNING: Failed to resize display.\n");
94 }
95 }
96 else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
97 done = true;
98 }
99 if (redraw == true && al_is_event_queue_empty(event_queue)) {
100 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
101 glViewport(0,0,scrW,scrH);
102 al_flip_display();
103 redraw = false;
104 }
105 }
106
107 al_destroy_timer(timer);
108 al_destroy_event_queue(event_queue);
109 al_destroy_display(display);
110 return 0;
111}
112
113bool initAL() {
114 if (!al_init()) {
115 printf("ERROR: Allegro failed to initialize.\n");
116 return false;
117 }
118 if (!al_install_keyboard()) {
119 printf("ERROR: Failed to install Allegro Keyboard.\n");
120 return false;
121 }
122 return true;
123}
124
125void initGL() {
126 glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
127}
|
Erin Maus
Member #7,537
July 2006
|
initGL() needs to be called after the display is successfully created. --- |
Arthur Kalliokoski
Second in Command
February 2005
|
I tried it and had to comment out the "#include <GL/gl3.h>" line to get it to compile, and got this error: /home/prog/allegro-5.0.5/src/opengl/extensions.c:190: print_extensions: Assertion `extension' failed. Aborted glxinfo said this: GLX version: 1.4 OpenGL version string: 3.3.0 NVIDIA 270.41.19 All the relevant examples supplied with the library work properly. They all watch too much MSNBC... they get ideas. |
chex_5
Member #14,081
February 2012
|
I tried putting initGL() after the display initialization, but it still made no difference. The screen is still black
|
Erin Maus
Member #7,537
July 2006
|
Oh, looks like you forgot to use ALLEGRO_OPENGL when creating the display. To use a forward compatible OpenGL 3 display, you have to pass, e.g.: al_set_new_display_flags(ALLEGRO_OPENGL_3_0 | ALLEGRO_OPENGL_FORWARD_COMPATIBLE | ALLEGRO_OPENGL); If you're on Windows, it will still use the DirectX backend if you don't request to use an OpenGL one. Without ALLEGRO_OPENGL, the other two options are ignored. --- |
chex_5
Member #14,081
February 2012
|
Thanks for the feedback so far. Arthur, I used the gl3.h header from the OpenGL Registry (versus the regular gl.h), although I don't know if there are any extra steps I needed to take after including it. Arron, after I included the ALLEGRO_OPENGL flag, the program now crashes when creating a window. I have an Nvidia gtx260, so I know it's OpenGL 3.x compatible, but I have no idea why it crashes now. EDIT: Anyway here's the final initialization code (just in case anyone else runs into these problems): 3#include <cstdio>
4#include <allegro5/allegro.h>
5#include <allegro5/allegro_opengl.h>
6#include <GL/gl.h>
7using namespace std;
8
9#ifndef FPS
10#define FPS 60.0
11#endif
12
13bool initAL();
14void initGL();
15
16int main() {
17 if (!initAL()) return -1;
18 if (!al_install_keyboard()) {
19 printf("ERROR: could not install keyboard driver.\n");
20 return -1;
21 }
22
23 bool done = false;
24 bool redraw = false;
25 int scrW = 640;
26 int scrH = 480;
27 ALLEGRO_DISPLAY* display = NULL;
28 ALLEGRO_TIMER* timer = NULL;
29 ALLEGRO_EVENT_QUEUE* event_queue = NULL;
30 ALLEGRO_EVENT event;
31
32 al_set_new_display_flags( ALLEGRO_WINDOWED |
33 ALLEGRO_RESIZABLE |
34 ALLEGRO_OPENGL_3_0 |
35 ALLEGRO_OPENGL );
36 al_set_new_display_option( ALLEGRO_COLOR_SIZE, 32, ALLEGRO_REQUIRE);
37 al_set_new_display_option( ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE);
38 al_set_new_display_option( ALLEGRO_STENCIL_SIZE, 8, ALLEGRO_REQUIRE);
39 al_set_new_display_option( ALLEGRO_AUX_BUFFERS, 0, ALLEGRO_REQUIRE);
40 al_set_new_display_option( ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
41 al_set_new_display_option( ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST);
42 display = al_create_display(scrW, scrH);
43 if (display == NULL) {
44 printf("ERROR: Failed to create Display.\n");
45 return -1;
46 }
47 printf("Successfully created the display.\n");
48 if ((al_get_display_flags(display) ^ ALLEGRO_OPENGL_3_0) == 0) {
49 printf("ERROR: Could not get an OpenGL 3.x display.\n");
50 return -1;
51 }
52 printf("OpenGL 3.x capable display detected.\n");
53 al_set_current_opengl_context(display);
54 initGL();
55
56 timer = al_create_timer(1.0 / FPS);
57 event_queue = al_create_event_queue();
58 if (event_queue == NULL) {
59 printf("ERROR: Failed to create Event Queue.\n");
60 return -1;
61 }
62 al_register_event_source(event_queue, al_get_timer_event_source(timer));
63 al_register_event_source(event_queue, al_get_display_event_source(display));
64 al_register_event_source(event_queue, al_get_keyboard_event_source());
65 al_start_timer(timer);
66
67 while (!done) {
68 al_wait_for_event(event_queue, &event);
69
70 if (event.type == ALLEGRO_EVENT_TIMER) {
71 redraw = true;
72 }
73 else if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
74 if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true;
75 }
76 else if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
77 if (al_acknowledge_resize(event.display.source)) {
78 scrW = al_get_display_width(display);
79 scrH = al_get_display_height(display);
80 }
81 else {
82 printf("WARNING: Failed to resize display.\n");
83 }
84 }
85 else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
86 done = true;
87 }
88 if (redraw == true && al_is_event_queue_empty(event_queue)) {
89 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
90 glViewport(0,0,scrW,scrH);
91 al_flip_display();
92 redraw = false;
93 }
94 }
95
96 al_destroy_timer(timer);
97 al_destroy_event_queue(event_queue);
98 al_destroy_display(display);
99 return 0;
100}
101
102bool initAL() {
103 if (!al_init()) {
104 printf("ERROR: Allegro failed to initialize.\n");
105 return false;
106 }
107 if (!al_install_keyboard()) {
108 printf("ERROR: Failed to install Allegro Keyboard.\n");
109 return false;
110 }
111 return true;
112}
113
114void initGL() {
115 glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
116}
|
|