Alpha blending a triangle with a textured triangle in openGL

in this program,
I've got pairs of 3d triangles making up a terrain. I also subdivide them into powers of 2, depending on how close the camera is to the middle of the 2 triangles' shared edge.

I want to draw the triangles with the 3 corner colours interpolated smoothly blended with a repeating procedurally generated detail texture.

I'm wondering if there's a way to draw a normal triangle, like this

glVertex3d( 0,1,0 );
glVertex3d( 1,0,0 );
glVertex3d( 1,1,0 );

with a repeating texture modifying the colour of each pixel in the triangle. The detail textures in Deus Ex are my inspiration for wanting to do this.

Sorry if there's an obvious answer I should have found.

Trent Gamblin

Assuming you're using Allegro and you have loaded the texture into a bitmap, then

GLint tex = al_get_opengl_texture(allegro_bitmap);
glBindTexture(GL_TEXTURE_2D, tex);

glTexCoord3d(tu, tv)
/* Define the rest of this vertex */
... more vertices ...

tu and tv are texture coordinates. In OpenGL by default those are from 0 (left and bottom) to 1 (right and top).


I tried what you suggested, but the program crashes now. Is there a short example program that just loads a .png and draws a textured triangle?


I attached the program I'm working on
here's some of the code I'm using(example):

1#include <GL/glu.h> 2ALLEGRO_BITMAP *image = NULL; 3image = al_load_bitmap("noise.png"); 4GLuint texture = al_get_opengl_texture(image); 5if (texture != 0) 6 glBindTexture(GL_TEXTURE_2D, texture); 7glEnable(GL_DEPTH_TEST); 8glDepthMask(GL_TRUE); 9glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 10glMatrixMode(GL_PROJECTION); 11glLoadIdentity(); 12gluPerspective(85,aspect_ratio,0.001,1000000); 13gluLookAt(eye[0],eye[1],eye[2], // camera is here 14 looking_at[0],looking_at[1],looking_at[2], // camera is looking towards this point 15 0.0f,0.0f,1.0f); // up vector 16 17glMatrixMode(GL_MODELVIEW); 18glLoadIdentity(); 19glClear(GL_DEPTH_BUFFER_BIT); 20glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 21glClear(GL_DEPTH_BUFFER_BIT); 22 23glBegin(GL_TRIANGLES); 24glColor3d(0.5,0.5,0.5); 25glTexCoord2d(1, 1); 26glVertex3d( 1,1,0 ); 27glTexCoord2d(1, 0); 28glVertex3d( 1,0,0 ); 29glTexCoord2d(0, 1); 30glVertex3d( 0,1,0 ); 31glEnd();

Trent Gamblin

The code I posted will not crash as long as allegro_bitmap is valid.


Thanks. I'm gonna try, and I'll edit if I get it to work.


I'm embarrassed to say I couldn't get it to work. Is there an example program anywhere that draws textured triangles?

Thread #612551. Printed from