Created
August 18, 2016 17:48
-
-
Save rtv/f2b12c1439f7e7244bf0c0ce6de0a703 to your computer and use it in GitHub Desktop.
Old-school OpenGL to draw a grey-level image texture between four 3D points.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Draws a grey-level image between four 3D points. | |
@pixels points to an array of @cols*@rows grey values. | |
@tl[x,y,z] 3D coordinate for top-left image pixel | |
@tr[x,y,z] 3D coordinate top-right | |
@bl[x,y,z] 3D coordinate bottom-left | |
@tr[x,y,z] 3D coordinate bottom-right | |
You probably need to have these calls in your OpenGL initialization: | |
glEnable(GL_BLEND); | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
*/ | |
void DrawTexture( const uint8_t* pixels, | |
const unsigned int cols, | |
const unsigned int rows, | |
const float tl[3], | |
const float tr[3], | |
const float bl[3], | |
const float br[3] ) | |
{ | |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glEnable(GL_TEXTURE_2D); | |
glTexImage2D(GL_TEXTURE_2D, 0, 4, cols, rows, 0, | |
GL_BGR, GL_UNSIGNED_BYTE, pixels ); | |
glColor3f(1,1,1); // white | |
glBegin(GL_QUADS); | |
glTexCoord2f(0.0, 0.0); | |
glVertex3fv( tl ); | |
glTexCoord2f(0.0, 1.0); | |
glVertex3fv( bl ); | |
glTexCoord2f(1.0, 1.0); | |
glVertex3fv( br ); | |
glTexCoord2f(1.0, 0.0); | |
glVertex3fv( tr ); | |
glEnd(); | |
glDisable(GL_TEXTURE_2D); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment