Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [OpenGL / Allegro5]

Credits go to Arthur Kalliokoski for helping out!
This thread is locked; no one can reply to it. rss feed Print
[OpenGL / Allegro5]
Ariesnl
Member #2,902
November 2002
avatar

I'm trying to make some basic opengl tryout nwith allegro 5.
Seems like backface culling or Z sorting goes wrong

Anyone

#SelectExpand
1#include <stdio.h> 2#include <cmath> 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_image.h> 5#include <allegro5/allegro_primitives.h> 6#include <allegro5/allegro_font.h> 7#include <allegro5/allegro_ttf.h> 8#include <allegro5/allegro_opengl.h> 9 10#define SCREENWIDTH 800 11#define SCREENHEIGHT 600 12 13 14// The display for graphics... 15ALLEGRO_DISPLAY * g_pDisplay = NULL; 16 17// The event queue 18ALLEGRO_EVENT_QUEUE * g_pEventQueue = NULL; 19 20// Timer 21ALLEGRO_TIMER * g_pTimer = NULL; 22 23// Global Path 24ALLEGRO_PATH * g_pPath = NULL; 25 26// a font for text 27ALLEGRO_FONT * g_pFont; 28 29//Some colors 30ALLEGRO_COLOR clBLACK; 31ALLEGRO_COLOR clGREEN; 32ALLEGRO_COLOR clRED; 33ALLEGRO_COLOR clYELLOW; 34ALLEGRO_COLOR clMAGENTA; 35 36// Other Globals 37bool g_blQuit = false; 38bool g_blRedraw = false; 39 40int g_nCyclesPerSecond = 60; 41 42//Keys 43enum KEYS{ UP, DOWN, LEFT, RIGHT}; 44bool g_blKeys[4] = {false, false, false, false}; 45 46// testvars 47double tx,ty; 48 49 50// forward declarations 51void Perspective(float a_fFOV, float a_fAspect, float a_fNearPlane, float a_fFarPlane); 52 53 54// Setup Allegro 55bool Setup(int a_nWidth, int a_nHeight) 56{ 57 if(al_init()) 58 { 59 al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_OPENGL); 60 g_pDisplay = al_create_display(a_nWidth, a_nHeight); 61 if (g_pDisplay != NULL) 62 { 63 // Install allegro modules 64 al_init_font_addon(); 65 al_init_ttf_addon(); 66 67 al_init_primitives_addon(); 68 al_init_image_addon(); 69 al_install_keyboard(); 70 al_install_mouse(); 71 72 // Set the timer 73 g_pTimer = al_create_timer(1.000 /g_nCyclesPerSecond); 74 75 // install event queue 76 g_pEventQueue = al_create_event_queue(); 77 al_register_event_source(g_pEventQueue, al_get_keyboard_event_source()); 78 al_register_event_source(g_pEventQueue, al_get_mouse_event_source()); 79 al_register_event_source(g_pEventQueue, al_get_display_event_source(g_pDisplay)); 80 al_register_event_source(g_pEventQueue, al_get_timer_event_source(g_pTimer)); 81 82 83 // switch to correct path 84 ALLEGRO_PATH * g_pPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 85 //al_append_path_component(g_pPath, "resources"); 86 87 // Load a font 88 al_set_path_filename(g_pPath, "Times.ttf"); 89 g_pFont=al_load_ttf_font(al_path_cstr(g_pPath, '/'),100,0); 90 if (g_pFont == NULL) 91 { 92 printf("Error loading font"); 93 return false; 94 } 95 96 97 // set some colors 98 clBLACK = al_map_rgb(0,0,0); 99 clGREEN = al_map_rgb(0,255,0); 100 clRED = al_map_rgb(255,0,0); 101 clYELLOW = al_map_rgb(255,255,0); 102 clMAGENTA = al_map_rgb(255,0,255); 103 104 // set quit to false 105 g_blQuit = false; 106 107 tx=10; 108 ty=10; 109 110 glShadeModel(GL_SMOOTH); // Enable Smooth Shading 111 glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background 112 glClearDepth(1.0f); // Depth Buffer Setup 113 glEnable(GL_DEPTH_TEST); // Enables Depth Testing 114 glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do 115 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations 116 117 118 al_start_timer(g_pTimer); 119 } 120 else 121 { 122 return false; 123 } 124 } 125 else 126 { 127 return false; 128 } 129 130 return true; 131} 132 133 134 135// Shutdown Allegro 136void ShutDown() 137{ 138 al_destroy_event_queue(g_pEventQueue); 139 al_destroy_display(g_pDisplay); 140 al_destroy_font(g_pFont); 141 al_destroy_path(g_pPath); 142 al_uninstall_system(); 143} 144 145 146 147// Render the view 148void Render(int a_nWidth, int a_nHeight) 149{ 150 double ms = al_current_time(); 151 al_clear_to_color(clBLACK); 152 153 //Set up perspective projection 154 glMatrixMode(GL_PROJECTION); 155 glPushMatrix(); 156 glLoadIdentity(); 157 //glOrtho(-100, 100, -100, 100, -1, 1); 158 Perspective(45.0f,(GLfloat)a_nWidth/(GLfloat)a_nHeight,0.1f,100.0f); 159 160 // Do your 3D stuff 161 glMatrixMode(GL_MODELVIEW); 162 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 163 glLoadIdentity(); 164 165 166 glTranslatef(0, 0, -5); 167 glRotatef(ms*36, 0, 1, 0); 168 169 glBegin(GL_QUADS); // Draw A Quad 170 glColor3f(0.0f,1.0f,0.0f); // Set The Color To Blue 171 glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top) 172 glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top) 173 glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top) 174 glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top) 175 glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange 176 glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom) 177 glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom) 178 glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom) 179 glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom) 180 glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red 181 glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front) 182 glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front) 183 glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front) 184 glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front) 185 glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow 186 glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back) 187 glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back) 188 glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back) 189 glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back) 190 glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue 191 glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left) 192 glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left) 193 glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left) 194 glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left) 195 glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet 196 glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right) 197 glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right) 198 glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right) 199 glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right) 200 glEnd(); 201 202 203 204 //Return to Allegros 2D world 205 glMatrixMode(GL_PROJECTION); 206 glPopMatrix(); 207 glMatrixMode(GL_MODELVIEW); 208 glLoadIdentity(); 209 210 al_draw_filled_rectangle(tx,ty,tx+20,ty+20,clGREEN); 211 al_draw_text(g_pFont,clYELLOW,10,10,0,"Font test"); 212 al_flip_display(); 213} 214 215 216void GameCycle() 217{ 218 if (g_blKeys[UP]) 219 { 220 ty-=1; 221 } 222 if (g_blKeys[DOWN]) 223 { 224 ty+=1; 225 } 226 227 if (g_blKeys[LEFT]) 228 { 229 tx-=1; 230 } 231 232 if (g_blKeys[RIGHT]) 233 { 234 tx+=1; 235 } 236 237} 238 239 240// Keydown event 241void KeyDown(int a_nKey) 242{ 243 switch(a_nKey) 244 { 245 case ALLEGRO_KEY_UP: 246 g_blKeys[UP] = true; 247 break; 248 case ALLEGRO_KEY_DOWN: 249 g_blKeys[DOWN] = true; 250 break; 251 case ALLEGRO_KEY_RIGHT: 252 g_blKeys[RIGHT] = true; 253 break; 254 case ALLEGRO_KEY_LEFT: 255 g_blKeys[LEFT] = true; 256 break; 257 } 258 259} 260 261 262// Keyup event 263void KeyUp(int a_nKey) 264{ 265 switch(a_nKey) 266 { 267 case ALLEGRO_KEY_UP: 268 g_blKeys[UP] = false; 269 break; 270 case ALLEGRO_KEY_DOWN: 271 g_blKeys[DOWN] = false; 272 break; 273 case ALLEGRO_KEY_RIGHT: 274 g_blKeys[RIGHT] = false; 275 break; 276 case ALLEGRO_KEY_LEFT: 277 g_blKeys[LEFT] = false; 278 break; 279 280 case ALLEGRO_KEY_ESCAPE: 281 g_blQuit = true; 282 break; 283 } 284 285} 286 287void Mouse(ALLEGRO_MOUSE_EVENT * mouse_event) 288{ 289 290} 291 292void Timer(ALLEGRO_TIMER_EVENT * timer_event) 293{ 294 GameCycle(); 295 g_blRedraw = true; 296} 297 298 299void Perspective(float a_fFOV, float a_fAspect, float a_fNearPlane, float a_fFarPlane) 300{ 301 double left, right; 302 double bottom, top; 303 top = tan (a_fFOV*3.14159/360.0)*a_fNearPlane; 304 bottom = -top; 305 left = a_fAspect*bottom; 306 right = a_fAspect*top; 307 glFrustum (left, right, bottom, top, a_fNearPlane, a_fFarPlane); 308} 309 310 311int main(int argc, char **argv) 312{ 313 if (Setup(SCREENWIDTH,SCREENHEIGHT)) 314 { 315 while (!g_blQuit) 316 { 317 ALLEGRO_EVENT ev; 318 if (al_get_next_event(g_pEventQueue, &ev)) 319 { 320 321 switch (ev.type) 322 { 323 case ALLEGRO_EVENT_KEY_DOWN: 324 KeyDown(ev.keyboard.keycode); 325 break; 326 case ALLEGRO_EVENT_KEY_UP: 327 KeyUp(ev.keyboard.keycode); 328 break; 329 330 case ALLEGRO_EVENT_DISPLAY_CLOSE: 331 g_blQuit = true; 332 break; 333 334 case ALLEGRO_EVENT_MOUSE_AXES: 335 Mouse(&ev.mouse); 336 break; 337 338 case ALLEGRO_EVENT_TIMER: 339 Timer(&ev.timer); 340 break; 341 342 343 } 344 } 345 else 346 { 347 // No events to handle, do rendering here ! 348 if (g_blRedraw) 349 { 350 g_blRedraw = false; 351 Render(SCREENWIDTH,SCREENHEIGHT); 352 } 353 } 354 } 355 } 356 else 357 { 358 return -1; 359 } 360 ShutDown(); 361 return 0; 362}

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Arthur Kalliokoski
Second in Command
February 2005
avatar

You need a

  al_set_new_display_option(ALLEGRO_DEPTH_SIZE,24,ALLEGRO_REQUIRE);

before creating the display to use the depth buffer. There may be other things wrong, I didn't try it.

They all watch too much MSNBC... they get ideas.

Ariesnl
Member #2,902
November 2002
avatar

THANK YOU ! ;D

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Arthur Kalliokoski
Second in Command
February 2005
avatar

Ariesnl said:

backface culling

If all your objects are closed manifolds, and if your backface culling isn't working (at all) then it won't look any different, it'll just make about 50% more work for the graphics card. If it is working, the only way to mess up is if the winding is wrong for some polygons, which will show up as "holes" in the objects.

They all watch too much MSNBC... they get ideas.

Go to: