How to do it...

To render a simple shape with a 2D texture, use the following steps: 

  1. We'll define a simple (static) function for loading and initializing textures: 
GLuint Texture::loadTexture( const std::string & fName ) {  int width, height;  unsigned char * data = Texture::loadPixels(fName, width, height);  GLuint tex = 0;  if( data != nullptr ) {    glGenTextures(1, &tex);    glBindTexture(GL_TEXTURE_2D, tex);    glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, width, height);    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,               width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,     GL_LINEAR);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,     GL_NEAREST);    Texture::deletePixels(data);  }  return tex;}
  1. In the initialization ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.