Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Alpha blending a triangle with a textured triangle in openGL

This thread is locked; no one can reply to it. rss feed Print
Alpha blending a triangle with a textured triangle in openGL
altalena
Member #13,639
October 2011
avatar

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.

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Trent Gamblin
Member #261
April 2000
avatar

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
Member #13,639
October 2011
avatar

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();

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Trent Gamblin
Member #261
April 2000
avatar

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

altalena
Member #13,639
October 2011
avatar

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?

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Go to: