Draw texture in AllegroGL
James Stanley

I have recently (just today...) decided that it is time for me to learn OpenGL/AllegroGL. I have managed to draw a square on the screen, but I am unable to texture that square. I have the following code:

1#include <allegro.h>
2#include "alleggl.h"
3 
4void opengl_init(void) {
5 //Set state to edit projection matrix
6 glMatrixMode(GL_PROJECTION);
7
8 //Clear projection matrix
9 glLoadIdentity();
10
11 //Create orthoscopic view, -1 to 1 in every direction
12 glOrtho(-1, 1, -1, 1, -1, 1);
13
14 //Set state to edit model-view matrix
15 glMatrixMode(GL_MODELVIEW);
16
17 //Clear model-view matrix
18 glLoadIdentity();
19
20 //Disable depth test - things get drawn in order I draw them
21 glDisable(GL_DEPTH_TEST);
22
23 //Enable texturing on 2d models
24 glEnable(GL_TEXTURE_2D);
25}
26 
27int main() {
28 GLuint gl_image;
29 BITMAP *bitmap;
30
31 allegro_init();
32 install_allegro_gl();
33
34 set_window_title("GLTest");
35
36 allegro_gl_set(AGL_COLOR_DEPTH, 32);
37 allegro_gl_set(AGL_SUGGEST, AGL_COLOR_DEPTH);
38 allegro_gl_set(AGL_REQUIRE, AGL_DOUBLEBUFFER);
39
40 if(set_gfx_mode(GFX_OPENGL_WINDOWED, 640, 480, 0, 0) != 0) {
41 if(set_gfx_mode(GFX_OPENGL_FULLSCREEN, 640, 480, 0, 0) != 0) {
42 allegro_message("Video error. Allegro said: %s\n", allegro_error);
43 return 1;
44 }
45 }
46
47 opengl_init();
48
49 bitmap = load_bitmap("images/gui/button.tga", NULL);
50 gl_image = allegro_gl_make_texture_ex(AGL_TEXTURE_MASKED, bitmap, GL_RGBA);
51
52 //Generate and bind a texture
53 glGenTextures(1, &gl_image);
54 glBindTexture(GL_TEXTURE_2D, gl_image);
55
56 //Begin drawing a quadrilateral
57 glBegin(GL_QUADS);
58 glColor4ub(255, 255, 255, 255);
59
60 glTexCoord2f(0, 0); glVertex3f(-0.5, 0.5, 0);
61 glTexCoord2f(1, 0); glVertex3f(0.5, 0.5, 0);
62 glTexCoord2f(1, 1); glVertex3f(0.5, -0.5, 0);
63 glTexCoord2f(0, 1); glVertex3f(-0.5, -0.5, 0);
64 glEnd();
65 //Finish drawing it
66
67 //Flip the boards in the back of the monitor so that I can see it
68 allegro_gl_flip();
69
70 while(1);//Pause so that I can see what happens
71}

Anybody know what's wrong with that?
Thanks.

EDIT:
Sorry for filling opengl_init() with comments and not putting any anywhere else.
It's like that because I was trying to use AllegroGL in my game, but it was too hard. I then ripped the init function out and stuck it in a very basic test program. I'm commenting the main() now.

Archon
  bitmap = load_bitmap("images/gui/button.tga", NULL);
  gl_image = allegro_gl_make_texture_ex(AGL_TEXTURE_MASKED, bitmap, GL_RGBA);

  //Don't need anymore
  destroy_bitmap(bitmap); //ADD

  //Generate and bind a texture
  //glGenTextures(1, &gl_image); DELETE
  glBindTexture(GL_TEXTURE_2D, gl_image);

James Stanley

Still just a white rectangle.

Archon

Can you check the return value of the load_bitmap call?

[edit]
Also, make sure that you keep the 'draw order' of your polygons consistent. The default is counter-clockwise.

    glTexCoord2f(0, 0); glVertex3f(-0.5, -0.5, 0);
    glTexCoord2f(1, 0); glVertex3f(0.5, -0.5, 0);
    glTexCoord2f(1, 1); glVertex3f(0.5, 0.5, 0);
    glTexCoord2f(0, 1); glVertex3f(-0.5, 0.5, 0);

James Stanley

load_bitmap() returns a bitmap, and the current order is the same as yours.

Archon

OK. What does allegro_gl_make_texture_ex return? It should be above 0.

gnolam
Quote:

glGenTextures(1, &gl_image);

Remove that.

James Stanley

Aha!
Archon: It was 0.
Gnolam: I already have.

What could be causing it to be 0, then? Bad options?

GullRaDriel

File not found, Not a bmp file, wrong filename.

shouldn't it be "my_dir\\myname.bmp"

James Stanley

Aha! The problem is that I had forgot to use a power-of-2 image!
It's all working now.

Quote:

shouldn't it be "my_dir\\myname.bmp"

No...

Archon
Quote:

Aha! The problem is that I had forgot to use a power-of-2 image!

I wasn't sure if AllegroGL did something about that or not but it would've been my next question.

Quote:

shouldn't it be "my_dir\\myname.bmp"

Using forward slashes are perfectly fine (and recommended) with Windows. I don't think that using blackslashes work in Unix though.

GullRaDriel

AllegroGL handles non power of two texture, even if the gfx card does not support it.

James Stanley

So why did it suddenly start working when I put it as a power of two?

Also, is there a way to get mouse co-ordinates in OpenGL? Or would I have to convert Allegro co-ordinates?

Jakub Wasilewski

AllegroGL only allows npo2 textures on unsupporting hardware if you pass AGL_TEXTURE_RESCALE. This causes AGL to rescale the texture itself to fit the nearest power-of-2 size.

Quote:

Also, is there a way to get mouse co-ordinates in OpenGL? Or would I have to convert Allegro co-ordinates?

The latter. One of the ways to do that is gluUnProject from GLUT.

Archon
Quote:

So why did it suddenly start working when I put it as a power of two?

Probably because the way textures work in 3D graphical interfaces since the type used are float. Floating point numbers, with their dimensions in the powers of 2, fit 'nicely' in binary.

Quote:

Also, is there a way to get mouse co-ordinates in OpenGL? Or would I have to convert Allegro co-ordinates?

Are you talking about the coordinates respective to the screen or coordinates to measure with what 3D objects you are "pointing at"?

X-G

Er, it's mostly because you can assume a whole bunch of things if you do, not really because of floats.

James Stanley

Jakub: I see. I'll use AGL_TEXTURE_RESCALE then. Thanks.

Archon: I'm not doing 3D just yet, it's actual co-ordinate on the screen that I'm after. I've done it by converting allegro's co-ordinates now.

EDIT:
I know I'm getting a bit off topic on my own thread, but how would I use the mask colour of a BITMAP to set the alpha channel of a texture?

EDIT2:
Never mind, found out how to use glBlendFunc properly.

Archon
Quote:

I know I'm getting a bit off topic on my own thread, but how would I use the mask colour of a BITMAP to set the alpha channel of a texture?

EDIT2:
Never mind, found out how to use glBlendFunc properly.

Also, look into:

glAlphaTest
glAlphaFunc

Thread #591663. Printed from Allegro.cc