onsdag 25 februari 2009

Going 3D with Allegro 5


I decided to play with 3D in Allegro 4.9.x today.
It was really simple to get started, compared to other ways of doing it.
Allegro takes care of all the setup and stuff that is messy when you're trying to learn opengl.

This example code shows how simple it is to get going.
It loads a font and a texture using allegro and draws a bit in 2D and a bit in 3D.

I've only built this on Linux, you'll have to translate the linking to your favourite compiler yourself.
g++ Allegro3D.cpp `allegro5-config --libs` -la5_font-4.9.8 -la5_ttf-4.9.8 -la5_iio-4.9.8 -o Allegro3D



#include <cmath>
#include <allegro5/allegro5.h>
#include <allegro5/a5_iio.h>
#include <allegro5/a5_font.h>
#include <allegro5/a5_ttf.h>
#include <allegro5/a5_opengl.h>

ALLEGRO_BITMAP* texture = NULL;
ALLEGRO_FONT* font = NULL;
int width = 640;
int height = 480;

/* gluPerspective is a function that exists in an addon to gl.
* I put my own version in here to avoid messing with more libraries than neccesary.
*/
void gluPerspective(float FOV, float ASPECT, float NEARPLANE, float FARPLANE)
{
double left, right;
double bottom, top;
top = tan (FOV*3.14159/360.0)*NEARPLANE;
bottom = -top;
left = ASPECT*bottom;
right = ASPECT*top;
glFrustum (left, right, bottom, top, NEARPLANE, FARPLANE);
}

void Render()
{
//A bit of 2D drawing before venturing into 3D
double ms = al_current_time();
al_font_textout(font, 0, 0, "Goodbye 2D world", -1);
al_draw_bitmap(texture, 0, 20, 0);

//Set up perspective projection
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

// Do your 3D stuff
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBindTexture(GL_TEXTURE_2D, al_get_opengl_texture(texture));
glEnable(GL_TEXTURE_2D);
glTranslatef(0, 0, -10);
glRotatef(ms*36, 1, 1, 1);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-1, -1, 0);
glTexCoord2f(1, 0); glVertex3f(+1, -1, 0);
glTexCoord2f(1, 1); glVertex3f(+1, +1, 0);
glTexCoord2f(0, 1); glVertex3f(-1, +1, 0);
glEnd();

//Return to Allegros 2D world
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//Do some 2D stuff
al_font_textout_right(font, width, height-20, "Welcome back 2D world", -1);
}

int main()
{
al_init();
al_install_keyboard();
al_iio_init();
al_font_init();

ALLEGRO_DISPLAY *display;
al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_OPENGL);
display = al_create_display(width, height);

ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
al_register_event_source(event_queue, (ALLEGRO_EVENT_SOURCE *)display);
al_register_event_source(event_queue, (ALLEGRO_EVENT_SOURCE *)al_get_keyboard());

font = al_ttf_load_font("font.ttf", 20, 0);
texture = al_iio_load("texture.jpg");

double last_time = al_current_time();

bool quit = false;
while(!quit)
{
ALLEGRO_EVENT event;
if (al_get_next_event(event_queue, &event))
{
if (ALLEGRO_EVENT_KEY_DOWN == event.type && ALLEGRO_KEY_ESCAPE == event.keyboard.keycode
|| ALLEGRO_EVENT_DISPLAY_CLOSE == event.type)
{
break;
}
}

al_clear(al_map_rgb(0, 0, 0));
Render();
al_flip_display();

al_rest(0.001);
}

al_destroy_event_queue(event_queue);
al_destroy_display(display);
}
END_OF_MAIN()

Inga kommentarer: