Skip to content

Instantly share code, notes, and snippets.

@onevcat
Created December 9, 2015 01:31
Show Gist options
  • Save onevcat/9d1f6c7549741696b9d8 to your computer and use it in GitHub Desktop.
Save onevcat/9d1f6c7549741696b9d8 to your computer and use it in GitHub Desktop.
// The plain image data is stored in `bitmap`
NSInteger samplesPerPixel = [bitmap samplesPerPixel];
int rowLength = 0;
int unpackAlign = 0;
// Save original value temporary
glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowLength);
glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpackAlign);
// Calculate and store necessary values for texture
glPixelStorei(GL_UNPACK_ROW_LENGTH, (int)[bitmap bytesPerRow] / samplesPerPixel);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// `textureId` is passed from Unity
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if (![bitmap isPlanar] &&
(samplesPerPixel == 3 || samplesPerPixel == 4)) {
glTexImage2D(GL_TEXTURE_2D,
0,
samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8,
(int)[bitmap pixelsWide],
(int)[bitmap pixelsHigh],
0,
samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
GL_UNSIGNED_BYTE,
[bitmap bitmapData]);
}
// Restore values
glPixelStorei(GL_UNPACK_ROW_LENGTH, rowLength);
glPixelStorei(GL_UNPACK_ALIGNMENT, unpackAlign);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment