Skip to content

Instantly share code, notes, and snippets.

@rtv
Created August 18, 2016 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtv/f2b12c1439f7e7244bf0c0ce6de0a703 to your computer and use it in GitHub Desktop.
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.
/*
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