Last active
August 29, 2015 14:07
-
-
Save acidleaf/411b2d057c468fcc71d4 to your computer and use it in GitHub Desktop.
OpenGL 3.3 PBO Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void init() { | |
// Generate PBO | |
glGenBuffers(1, &_pbo); | |
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, _pbo); | |
glBufferData(GL_PIXEL_UNPACK_BUFFER, TEX_SIZE * TEX_SIZE * 3, nullptr, GL_STREAM_DRAW); | |
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); | |
// Generate a texture | |
glGenTextures(1, &_texID); | |
glActiveTexture(GL_TEXTURE0); | |
glBindTexture(GL_TEXTURE_2D, _texID); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEX_SIZE, TEX_SIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); | |
} | |
void update() { | |
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, _pbo); | |
uint8_t* ptr = (uint8_t*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY); | |
// Do stuff with ptr here | |
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); | |
glBindTexture(GL_TEXTURE_2D, _texID); | |
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEX_SIZE, TEX_SIZE, GL_RGB, GL_UNSIGNED_BYTE, nullptr); | |
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); | |
} | |
void capture() { | |
uint8_t* ptr = new uint8_t[w * h * 3]; | |
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, ptr); | |
stbi_write_png("capture.png", w, h, 1, ptr, w * 3); | |
delete[] ptr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment