About this Blog

This is my first blog. Ever.

It is simply going to be about my hobby; playing with computer programming. I do not know much about blogging, but I will use this one to learn a bit more about it.

Programming has always been a bit of a passion for me, as from those early days when I first tapped in a sample BASIC program on my old Sinclair Spectrum back in 1986. I have been through many platforms, languages and OS's since, but always carried the hobby with me. I am not particularly good at it; perfection requires a large time investment and continuous practice. I do not have the luxury of the amount of time required to keep the fire burning constantly, so the hobby has inevitably gone through periods of extreme withering. I have, however, finally settled for C++, as the title of this blog implies, and play around with it for some entertainment when ever I can.

This here will serve me as a written record of what I am up to, and hopefully be a reinforcement to my memory every now and then. That is all there is to it.

So, if you read this blog, please don't expect anything snazzy, but be you welcome just the same!

Monday 19 December 2011

OpenGL / Allegro 5.1 gluSphere()

Another snippet to document for posterity. I have been very busy working the last few days, but try to keep my hand in whenever I can. Here's what I tried.

#include "allegro5/allegro.h" 
#include "allegro5/allegro_opengl.h"

#include "allegro5/allegro_image.h"

#include "GL/glu.h"


const int SCREEN_X = 800;
const int SCREEN_Y = 600;

int main(int argc, char *argv[])
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_BITMAP *bmp_earth = NULL;
GLuint tex_bmp = 0;

al_init();
al_init_image_addon();

al_set_new_display_flags(ALLEGRO_OPENGL);
display = al_create_display(SCREEN_X, SCREEN_Y);
bmp_earth = al_load_bitmap("dry_world.jpg");
tex_bmp = al_get_opengl_texture(bmp_earth);

GLUquadricObj *gl_quad_obj;
gl_quad_obj=gluNewQuadric();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(35.0f, (GLdouble)SCREEN_X / (GLdouble)SCREEN_Y, 1.0f, 200.0f);
glRotatef(23.0f, 0.0f,0.0f, 1.0f); // Trick photography; rotate the camera...

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0f,0.0f,-20.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);

//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex_bmp);

//gluQuadricNormals(gl_quad_obj, GLU_SMOOTH
);
gluQuadricTexture(gl_quad_obj, GL_TRUE);
gluSphere(gl_quad_obj,5.0f,32,32);

glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);

al_flip_display();
al_rest(5.0);

glDeleteTextures(1, &tex_bmp);
gluDeleteQuadric(gl_quad_obj);
al_destroy_display(display);
al_destroy_bitmap(bmp_earth);
return 0;
}

Short and sweet. I had some curiosity about using the glu extension for something other than gluPerspective(), and as I had a hankering to make a planet, I experimented with gluSphere(). Turns out it works okay with Allegro 5.1. The above is pretty minimalistic, just enough to get results.

Here's the map I used, from Celestia...
In this one, it became pretty important to enable the GL_DEPTH_TEST. Otherwise, you WILL get some funny effects. Note also, in the above code, the demo of how to rotate the camera!

Of course, gluSphere() this is not the only gluQuadricObject that OpenGL can do. Again, the main reference OpenGL Manual. Scroll the index on the left to glu, and there you have it; lots of cool stuff.

1 comment: