To render a simple shape with a 2D texture, use the following steps:
- 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;}
- In the initialization ...