0

I am trying to create a gl::Texture object using image data as a BYTE * with the parameters below.

FreeGLUT - I use this to create a 2d texture and bind it to a quad.:

 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_BGRA_EXT,   GL_UNSIGNED_BYTE, data);
 glBindTexture(GL_TEXTURE_2D, 0); etc etc

This works fine, however I cannot find a way to create a gl::Texture object in Cinder.

 text = gl::Texture(loadImage(loadAsset("text.jpg"))); // works with images files
 text = gl::Texture(data, GL_RGBA8, 640, 480); // BTYE * data This gives me a grey screen -

This seemed the most likely, however I have no idea what to do with Format format = format();

Texture (const unsigned char *data, int dataFormat, int aWidth, int aHeight, Format format=Format()) 

I really have no understanding of how this works and cant find any better tutorials online. Thanks.

1 Answers1

1

apparently dataFormat is the format parameter to glTexImage2D and should be something like GL_RGBA or GL_RGB (or GL_BGRA_EXT as in the example you provided)

What you want is the "internal format" which is set via the Format struct. So you set it like:

gl::Texture::Format fmt;
fmt.setInternalFormat(GL_RGBA8);
text = gl::Texture(data, GL_RGBA, 640, 480, fmt);

The GL_UNSIGNED_BYTE format is pre-set in that constructor of the cinder wrapper

Sometimes it can be easier to just look at the code instead of trying to find the 100% applicable documentation

PeterT
  • 7,981
  • 1
  • 26
  • 34
  • Thanks a lot. Your answer makes sense, but when I call gl::draw(text, getWindowBounds()); I still get a grey background. It works when using gl::Texture(loadImage from a file. Is there anything else I need to do differently? In freeGLUT I was mapping the texture onto a quad but my understanding is the texture should work fine with gl::draw (as my texture loaded from a file displays correctly). – user1546650 Oct 05 '14 at 22:33
  • @user1546650 I guess making sure that `data` actually points to the image data is rather important, use your debugger and see if the values there make sense. – PeterT Oct 05 '14 at 22:39
  • Thanks, it does look like the remaining problem is a bug in my main code (I did attempt to check the debugger but not all that sure what I was looking for :P). – user1546650 Oct 05 '14 at 23:04
  • @user1546650 if you don't know how to inspect the pointed-to data in your debugger then just do it in your code for debugging purposes (i.e. printf debugging). Like doing `std::cout << (int)data[100] << (int)data[101] << (int)data[102]; ` for a few values that make sense. And of course checking `data` itself (does it point to `0` or some other garbage location) – PeterT Oct 05 '14 at 23:20