Skip to content

Instantly share code, notes, and snippets.

@dennda
Created October 5, 2011 20:37
Show Gist options
  • Save dennda/1265632 to your computer and use it in GitHub Desktop.
Save dennda/1265632 to your computer and use it in GitHub Desktop.
// Storing RGBA binary buffer on disk:
unsigned char *dest = (unsigned char*) malloc(width * height * 4 * sizeof(unsigned char));
short *subscript = (short*) imgData;
for (size_t i = 0; i < width * height; ++i) {
unsigned short x = subscript[i] + 1000;
dest[i*4] = x >> 8;
dest[i*4+1] = x;
dest[i*4+2] = 255;
dest[i*4+3] = 255;
}
FILE *fp = fopen("/Users/dennda/Desktop/binary.buf", "wb");
size_t count;
if(fp == NULL) {
printf("failed to open sample.txt\n");
assert(0);
}
count = fwrite((const unsigned char*) dest, sizeof(unsigned char), width*height*4, fp);
printf("Wrote %zu char. fclose(fp) %s.\n", width*height*4, fclose(fp) == 0 ? "succeeded" : "failed");
// Reading RGBA binary buffer & creating OGL texture:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
FILE *fp = fopen([[[NSBundle mainBundle] pathForResource:@"binary"
ofType:@"buf"]
cStringUsingEncoding:NSUTF8StringEncoding],
"rb");
size_t count = 512*512*4;
unsigned char *buffer = malloc(count * sizeof(unsigned char));
bzero(buffer, count);
if (fp == NULL) {
perror("Failed to open file \"myfile\"");
}
fread(buffer, sizeof(unsigned char), 512*512*4, fp);
fclose(fp);
glGenTextures(1, &_name);
GetError();
glBindTexture(GL_TEXTURE_2D, _tf_texname);
GetError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GetError();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
GetError();
// Fragment shader:
gl_FragColor = (sample.r * 256. + sample.g) / 3000.;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment