123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #include <iostream>
- #include "LayeredFramebuffer.h"
- LayeredFramebuffer::LayeredFramebuffer(int width, int height, int depth)
- : texture(0), buffer(0) {
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_3D, texture);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
- glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, width, height, depth, 0, GL_RED,
- GL_FLOAT, nullptr);
- glGenFramebuffers(1, &buffer);
- glBindFramebuffer(GL_FRAMEBUFFER, buffer);
- GLenum c = GL_COLOR_ATTACHMENT0;
- glFramebufferTexture3D(GL_FRAMEBUFFER, c, GL_TEXTURE_3D, texture, 0, 0);
- glDrawBuffers(1, &c);
- }
- LayeredFramebuffer::~LayeredFramebuffer() {
- glDeleteFramebuffers(1, &buffer);
- glDeleteTextures(1, &texture);
- }
- void LayeredFramebuffer::bindAndClear() {
- glBindFramebuffer(GL_FRAMEBUFFER, buffer);
- glClear(GL_COLOR_BUFFER_BIT);
- }
- void LayeredFramebuffer::bindTextureTo(int unit) const {
- glActiveTexture(GL_TEXTURE0 + unit);
- glBindTexture(GL_TEXTURE_3D, texture);
- }
- void LayeredFramebuffer::bindLayer(int layer) {
- glBindFramebuffer(GL_FRAMEBUFFER, buffer);
- glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D,
- texture, 0, layer);
- }
- bool LayeredFramebuffer::hasError() const {
- GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
- if(error == GL_FRAMEBUFFER_COMPLETE) {
- return false;
- }
- switch(error) {
- case GL_FRAMEBUFFER_UNDEFINED:
- std::cout << "undefined framebuffer\n";
- return true;
- case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
- std::cout << "incomplete framebuffer attachment\n";
- return true;
- case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
- std::cout << "incomplete missing framebuffer attachment\n";
- return true;
- case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
- std::cout << "incomplete framebuffer draw buffer\n";
- return true;
- case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
- std::cout << "incomplete framebuffer read buffer\n";
- return true;
- case GL_FRAMEBUFFER_UNSUPPORTED:
- std::cout << "unsupported framebuffer\n";
- return true;
- case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
- std::cout << "incomplete framebuffer multisample\n";
- return true;
- case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
- std::cout << "incomplete framebuffer layer targets\n";
- return true;
- }
- return "unknown error";
- }
|