I decided to translate all NeHe's tutorials to AllegroGL to learn more about OpenGL and 3D programming, but I have a problem with the first one. It compiles without errors but it shows a black window and doesn't check the keyboard. I can't find what's wrong with it.
The source:
| 1 | /* |
| 2 | * This code is a translation of the NeHe OpenGL Tutorial. |
| 3 | * The original one was writen by Jeff Molofee 2000. |
| 4 | * It was translated by Ñuño Martínez 2006. |
| 5 | * |
| 6 | * Visit NeHe's site at nehe.gamedev.net |
| 7 | */ |
| 8 | |
| 9 | #include <alleggl.h> /* Header file for Allegro and AllegroGL. */ |
| 10 | #include <GL/gl.h> /* Header file for the OpenGL32 library. */ |
| 11 | #include <GL/glu.h> /* Header file for the GLu32 library. */ |
| 12 | |
| 13 | |
| 14 | |
| 15 | /* Variables. */ |
| 16 | int FullScreen = FALSE; /* Fullscreen flag set to windowed mode by default. */ |
| 17 | |
| 18 | |
| 19 | |
| 20 | /* Function prototypes. */ |
| 21 | void ResizeGLScene (GLsizei Width, GLsizei Height); /* Resize and initialize the OpenGL window. */ |
| 22 | int InitGL (void); /* All setup for OpenGL goes here. */ |
| 23 | int DrawGLScene (void); /* Here's where we do all the drawing. */ |
| 24 | int CreateGLWindow (char* Title, int Width, int Height, int bpp, |
| 25 | int FullScreenFlag); /* Create the OpenGL window. */ |
| 26 | |
| 27 | |
| 28 | |
| 29 | /* main: |
| 30 | * Entry point of our application. */ |
| 31 | int main (void) |
| 32 | { |
| 33 | int Done = FALSE; /* Bool variable to exit loop. */ |
| 34 | |
| 35 | /* Initialise Allegro and AllegroGL. */ |
| 36 | set_uformat (U_ASCII); |
| 37 | if (allegro_init () != 0) { |
| 38 | allegro_message ("Error initialising Allegro.\n"); |
| 39 | return EXIT_FAILURE; |
| 40 | } |
| 41 | install_timer (); |
| 42 | install_keyboard (); |
| 43 | if (install_allegro_gl () < 0) { |
| 44 | allegro_message ("Error initialising AllegroGL.\n"); |
| 45 | return EXIT_FAILURE; |
| 46 | } |
| 47 | /* Create our OpenGL window. */ |
| 48 | if (!CreateGLWindow ("NeHe's First Polygon Tutorial", 640, 480, 16, FullScreen)) |
| 49 | return EXIT_FAILURE; /* Quit if window was not created. */ |
| 50 | /* Loop that runs until Done = TRUE. */ |
| 51 | while (!Done) { |
| 52 | if (keyboard_needs_poll ()) poll_keyboard (); |
| 53 | /* Was ESC Pressed? */ |
| 54 | if (key[KEY_ESC]) |
| 55 | Done = TRUE; /* ESC signalled a quit. */ |
| 56 | else { |
| 57 | DrawGLScene(); /* Draw the scene. */ |
| 58 | allegro_gl_flip (); /* Swap buffers (double buffering). */ |
| 59 | } |
| 60 | /* Is F1 being pressed? */ |
| 61 | if (key[KEY_F1]) { |
| 62 | key[KEY_F1] = 0; /* If so make key FALSE. */ |
| 63 | /* Kill our current window. */ |
| 64 | set_gfx_mode (GFX_TEXT, 0, 0, 0, 0); |
| 65 | /* Toggle fullscreen / windowed mode. */ |
| 66 | FullScreen = !FullScreen; |
| 67 | /* Recreate Our OpenGL window. */ |
| 68 | if (!CreateGLWindow ("NeHe's First Polygon Tutorial", 640, 480, 16, |
| 69 | FullScreen)) |
| 70 | return EXIT_FAILURE; /* Quit if window was not created. */ |
| 71 | } |
| 72 | } |
| 73 | return EXIT_SUCCESS; |
| 74 | } |
| 75 | END_OF_MAIN () /* Needed by Allegro. */ |
| 76 | |
| 77 | |
| 78 | |
| 79 | /* ResizeGLScene: |
| 80 | * Resize and initialize the OpenGL window. */ |
| 81 | void ResizeGLScene (GLsizei Width, GLsizei Height) |
| 82 | { |
| 83 | /* Prevent a "divide by zero" error. */ |
| 84 | if (Height == 0) Height = 1; |
| 85 | /* Reset the current viewport. */ |
| 86 | glViewport (0, 0, Width, Height); |
| 87 | /* Select the projection matrix. */ |
| 88 | glMatrixMode (GL_PROJECTION); |
| 89 | /* Calculate the aspect ratio of the window.*/ |
| 90 | gluPerspective (45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f); |
| 91 | /* Select the modelview matrix. */ |
| 92 | glMatrixMode (GL_MODELVIEW); |
| 93 | /* Reset the modelview matrix.*/ |
| 94 | glLoadIdentity (); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | /* InitGL: |
| 100 | * All setup for OpenGL goes here. */ |
| 101 | int InitGL (void) |
| 102 | { |
| 103 | /* Enables smooth shading. */ |
| 104 | glShadeModel (GL_SMOOTH); |
| 105 | /* Black background. */ |
| 106 | glClearColor (0.0f, 0.0f, 0.0f, 0.0f); |
| 107 | /* Depth buffer setup. */ |
| 108 | glClearDepth (1.0f); |
| 109 | glEnable (GL_DEPTH_TEST); |
| 110 | glDepthFunc (GL_LEQUAL); |
| 111 | /* Really nice perspective calculations. */ |
| 112 | glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
| 113 | /* Initialization went 0k. */ |
| 114 | return TRUE; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | /* DrawGLScene: |
| 120 | * Here's where we do all the drawing. */ |
| 121 | int DrawGLScene (void) |
| 122 | { |
| 123 | /* Clear the screen and the depth buffer. */ |
| 124 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 125 | /* Reset the current modelview matrix. */ |
| 126 | glLoadIdentity (); |
| 127 | /* Move left 1.5 units and into the screen 6.0. */ |
| 128 | glTranslatef (-1.5f, 0.0f, -6.0f); |
| 129 | glBegin (GL_TRIANGLES); /* Drawing using triangles. */ |
| 130 | glVertex3f ( 0.0f, 1.0f, 0.0f); /* Top. */ |
| 131 | glVertex3f (-1.0f, -1.0f, 0.0f); /* Bottom Left. */ |
| 132 | glVertex3f ( 1.0f, -1.0f, 0.0f); /* Bottom Right. */ |
| 133 | glEnd (); /* Finished drawing the triangle. */ |
| 134 | /* Move right 3 units. */ |
| 135 | glTranslatef (3.0f, 0.0f, 0.0f); |
| 136 | glBegin (GL_QUADS); /* Draw a quad. */ |
| 137 | glVertex3f (-1.0f, 1.0f, 0.0f); /* Top Left. */ |
| 138 | glVertex3f ( 1.0f, 1.0f, 0.0f); /* Top Right. */ |
| 139 | glVertex3f ( 1.0f, -1.0f, 0.0f); /* Bottom Right. */ |
| 140 | glVertex3f (-1.0f, -1.0f, 0.0f); /* Bottom Left. */ |
| 141 | glEnd (); /* Done drawing the quad. */ |
| 142 | glFlush (); |
| 143 | /* Everything went 0k. */ |
| 144 | return TRUE; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | /* CreateGLWindow; |
| 150 | * Create the OpenGL window. */ |
| 151 | int CreateGLWindow (char* Title, int Width, int Height, int bpp, |
| 152 | int FullScreenFlag) |
| 153 | { |
| 154 | /* Set the global fullscreen flag. */ |
| 155 | FullScreen = FullScreenFlag; |
| 156 | /* Set window configuration. */ |
| 157 | allegro_gl_set (AGL_COLOR_DEPTH, bpp); |
| 158 | allegro_gl_set (AGL_WINDOWED, !FullScreen); |
| 159 | allegro_gl_set (AGL_RENDERMETHOD, TRUE); |
| 160 | allegro_gl_set (AGL_DOUBLEBUFFER, TRUE); |
| 161 | allegro_gl_set (AGL_Z_DEPTH, 16); |
| 162 | allegro_gl_set (AGL_SUGGEST, |
| 163 | AGL_COLOR_DEPTH | AGL_WINDOWED | AGL_RENDERMETHOD | |
| 164 | AGL_DOUBLEBUFFER | AGL_Z_DEPTH); |
| 165 | /* Open window. */ |
| 166 | if (set_gfx_mode (GFX_OPENGL, Width, Height, 0, 0) != 0) { |
| 167 | allegro_message ("Window creation error."); |
| 168 | return FALSE; |
| 169 | } |
| 170 | set_window_title (Title); |
| 171 | /* Initialize our newly created OpenGL window. */ |
| 172 | if (!InitGL ()) { |
| 173 | set_gfx_mode (GFX_TEXT, 0, 0, 0, 0); /* Reset the display. */ |
| 174 | allegro_message ("Initialization Failed."); |
| 175 | return FALSE; |
| 176 | } |
| 177 | /* Everything went 0k. */ |
| 178 | return TRUE; |
| 179 | } |
The command used to compile:
gcc -s -Wall -DDEBUGMODE -Isrc -o lesson02.bin lesson02.c -lagl `allegro-config --libs` -lGL -lGLU
I'm using Allegro 4.2 and one of the AllegroGL VSN versions that Miran posts here.
EDIT: Oops didn't see the flip command.
What OS?
I'm using the latest stable and updated Debian Sarge Linux.
Now, I've re-compiled an AllegroGL project I did before to update AllegroGL and it renders the objects but doesn't check keyboard nor mouse. Maybe it's an AllegroGL's bug?
You never setup the projection matrix.
EDIT: Here's a quick fix
| 1 | int InitGL (void) |
| 2 | { |
| 3 | /* Enables smooth shading. */ |
| 4 | ResizeGLScene(SCREEN_W, SCREEN_H); //added |
| 5 | glShadeModel (GL_SMOOTH); |
| 6 | /* Black background. */ |
| 7 | glClearColor (0.0f, 0.0f, 0.0f, 0.0f); |
| 8 | /* Depth buffer setup. */ |
| 9 | glClearDepth (1.0f); |
| 10 | glEnable (GL_DEPTH_TEST); |
| 11 | glDepthFunc (GL_LEQUAL); |
| 12 | /* Really nice perspective calculations. */ |
| 13 | glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
| 14 | /* Initialization went 0k. */ |
| 15 | return TRUE; |
| 16 | } |
Good, now it renders, but keyboard doesn't work!
Thats strange... the keyboard works when I compile that exact code, but I'm running WinXP Sp2 W/ MingW. Sounds like something funky. Does input work in other allegro programs?
Yes, it does. Also in OpenGL programs. Only AllegroGL has problems, but if I use the latest stable version seems to work right...
Hmm, this might be a question for the AllegroGL Developers then... cause maybe something is up with the CVS version and your hardware or something.
edit: Nevermind, you have a poll_keyboard()...
It works here, on linux too. Can you check install_keyboard() return value? Which version of X11 do you have?
Actually, you should never need to call poll_keyboard...
Can you check install_keyboard() return value?
It returns 0.
Which version of X11 do you have?
I am not sure. Synaptic says that package "xfree86-common" is version "4.3.0.dfsg.1-14sarge1" and same for packages "xbase-clients", "x-dev" and "xlibs-dev".
Actually, you should never need to call poll_keyboard...
I know. I put it trying to fix the problem.
Seems the version is 4.3.0 - a bit old but it definitely should work.
You should build a debug version of AGL and check the allegro.log that your program will produce.
Oh, and try to switch in and out od the program (atl+tab). It usually helps 
And which version of AGL exactly are you using? Where did you get it?
edit: and do AGL examples work?
I can't rebuild the library... Can't find the makefile! And I don't remember how I did it! I think I'll try to get the latest SVN and try to rebuild it.
[edit]Running "autoconf":
configure.in:140: error: possibly undefined macro: AM_PATH_ALLEGRO
run
aclocal -I usr/local/share/aclocal
if your allegro.m4 is in usr/local/share/aclocal
[edit]Forget it, I've find it in "automake" package.
AAAAAAAAAAARG...!
configure: creating ./config.status
config.status: creating makefile
config.status: creating include/alleggl_config.h
config.status: error: cannot find input file: include/alleggl_config.hin
Somebody hates me... sure!>:(
autoheader
edit: yeah, that should be fixed in SVN
I've downloaded the AllegroGL 0.4.0 RC4 and I've find that the examples runs except 3 of them: exmipmaps, exgui and excamera. They show the first frame(s) and fails whith next message:
xlib: unexpected async reply (sequence 0x11c)!
Then I must kill the process.
I've attached the log files for each example.
[edit]I confused myself...
sorry

you read mine
Ok. AllegroGL 0.4.0 RC4 seems to work. Next time I'll check if a newer version is avaiable before to ask.