Skip to content

Instantly share code, notes, and snippets.

@paulhoux
Last active June 19, 2018 18:47
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 paulhoux/c619204fd24ba2598b25f6e7449b91f8 to your computer and use it in GitHub Desktop.
Save paulhoux/c619204fd24ba2598b25f6e7449b91f8 to your computer and use it in GitHub Desktop.
#include "cinder/Log.h"
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
using namespace ci;
using namespace ci::app;
using namespace std;
void APIENTRY logDebug( GLenum source​, GLenum type​, GLuint id​, GLenum severity​, GLsizei length​, const GLchar* message​, const void* userParam​ )
{
CI_LOG_E(std::string( message​ ));
}
class CompressedTextureProblemApp : public App {
public:
void setup() override;
void draw() override;
gl::Texture2dRef mTexture;
};
void CompressedTextureProblemApp::setup()
{
glEnable( GL_DEBUG_OUTPUT );
glDebugMessageCallback( logDebug, this );
// Create the data for a simple gradient.
const int width = 256;
const int height = 256;
const int bytesPerPixel = 3;
std::vector<uint8_t> pixels;
pixels.resize( width * height * bytesPerPixel );
for( int y = 0; y < height; ++y ) {
memset( pixels.data() + y * width * bytesPerPixel, uint8_t( y ), width * bytesPerPixel );
}
// Create texture. Use default format.
mTexture = gl::Texture2d::create( width, height );
// Bind texture.
gl::ScopedTextureBind stb( mTexture );
// Upload uncompressed data. OpenGL will compress the data for us.
glTexImage2D( mTexture->getTarget(), 0, GL_COMPRESSED_RGB_ARB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixels.data() );
// Find out if our texture was compressed.
GLint isCompressed = GL_FALSE;
glGetTexLevelParameteriv( mTexture->getTarget(), 0, GL_TEXTURE_COMPRESSED_ARB, &isCompressed );
if( isCompressed == GL_TRUE ) {
// Find out the compressed format used.
GLint internalFormat = 0;
glGetTexLevelParameteriv( mTexture->getTarget(), 0, GL_TEXTURE_INTERNAL_FORMAT, &internalFormat );
// Retrieve the compressed image size in bytes.
GLint imageSize = 0;
glGetTexLevelParameteriv( mTexture->getTarget(), 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &imageSize );
// Obtain the image data.
std::vector<uint8_t> image;
image.resize( imageSize, 0 );
glGetCompressedTexImage( mTexture->getTarget(), 0, image.data() );
// TODO: write file to disk.
}
}
void CompressedTextureProblemApp::draw()
{
gl::clear( Color( 0.4f, 0.6f, 0.8f ) );
if( mTexture ) {
gl::draw( mTexture );
}
}
CINDER_APP( CompressedTextureProblemApp, RendererGl )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment