Skip to content

Instantly share code, notes, and snippets.

@acidleaf
Last active August 29, 2015 14:07

Revisions

  1. acidleaf revised this gist May 14, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions pbo.cc
    Original file line number Diff line number Diff line change
    @@ -30,4 +30,11 @@ void update() {
    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;
    }
  2. acidleaf revised this gist Oct 21, 2014. 1 changed file with 1 addition and 8 deletions.
    9 changes: 1 addition & 8 deletions pbo.cc
    Original file line number Diff line number Diff line change
    @@ -22,14 +22,7 @@ void update() {
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, _pbo);
    uint8_t* ptr = (uint8_t*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);

    for (int i = 0; i < TEX_SIZE; ++i) {
    for (int j = 0; j < TEX_SIZE; ++j) {

    ptr[0 + (j + i * TEX_SIZE) * 3] = i;
    ptr[1 + (j + i * TEX_SIZE) * 3] = j;
    ptr[2 + (j + i * TEX_SIZE) * 3] = 255;
    }
    }
    // Do stuff with ptr here

    glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);

  3. acidleaf created this gist Oct 21, 2014.
    40 changes: 40 additions & 0 deletions pbo.cc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    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);

    for (int i = 0; i < TEX_SIZE; ++i) {
    for (int j = 0; j < TEX_SIZE; ++j) {

    ptr[0 + (j + i * TEX_SIZE) * 3] = i;
    ptr[1 + (j + i * TEX_SIZE) * 3] = j;
    ptr[2 + (j + i * TEX_SIZE) * 3] = 255;
    }
    }

    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);
    }