Alpha blending a triangle with a textured triangle in openGL
altalena

in this program,
https://www.allegro.cc/forums/thread/612527/981976#target
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

glBegin(GL_TRIANGLE_STRIP);
glColor3d(0.75,0.25,0.25);
glVertex3d( 0,1,0 );
glColor3d(0.25,0.75,0.25);
glVertex3d( 1,0,0 );
glColor3d(0.25,0.25,0.75);
glVertex3d( 1,1,0 );
glEnd();

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);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);

glBegin(GL_TRIANGLES);
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).

altalena

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?

edit:

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

#SelectExpand
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.

altalena

Thanks. I'm gonna try http://wiki.allegro.cc/index.php?title=2D_using_OpenGL, and I'll edit if I get it to work.

edit:

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 Allegro.cc