Skip to content

Commit

Permalink
add functionality for reading texture data back into memory
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed May 6, 2024
1 parent d00d061 commit f91fdcb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/threepp/renderers/GLRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ namespace threepp {

void readPixels(const Vector2& position, const WindowSize& size, Format format, unsigned char* data);

// Experimental threepp function
void copyTextureToImage(Texture& texture);

void resetState();

void invokeLater(const std::function<void()>& task, float delay = 0);
Expand Down
2 changes: 1 addition & 1 deletion src/threepp/renderers/GLRenderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GLRenderTarget::GLRenderTarget(unsigned int width, unsigned int height, const GL
scissor(0.f, 0.f, static_cast<float>(width), static_cast<float>(height)),
viewport(0.f, 0.f, static_cast<float>(width), static_cast<float>(height)),
depthBuffer(options.depthBuffer), stencilBuffer(options.stencilBuffer), depthTexture(options.depthTexture),
texture(Texture::create({})) {
texture(Texture::create({Image({}, width, height)})) {

if (options.mapping) texture->mapping = *options.mapping;
if (options.wrapS) texture->wrapS = *options.wrapS;
Expand Down
21 changes: 21 additions & 0 deletions src/threepp/renderers/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,22 @@ struct GLRenderer::Impl {
glReadPixels(static_cast<int>(position.x), static_cast<int>(position.y), size.width, size.width, glFormat, GL_UNSIGNED_BYTE, data);
}

void copyTextureToImage(Texture& texture) {

textures.setTexture2D(texture, 0);

auto& image = texture.image.front();
auto& data = image.data();
if (data.empty()) {
auto newSize = image.width * image.height * (texture.format == Format::RGB ? 3 : 4);
data.resize(newSize);
}

glGetTexImage(GL_TEXTURE_2D, 0, gl::toGLFormat(texture.format), gl::toGLType(texture.type), data.data());

state.unbindTexture();
}

void setViewport(int x, int y, int width, int height) {

_viewport.set(static_cast<float>(x), static_cast<float>(y), static_cast<float>(width), static_cast<float>(height));
Expand Down Expand Up @@ -1348,6 +1364,11 @@ void GLRenderer::readPixels(const Vector2& position, const WindowSize& size, For
pimpl_->readPixels(position, size, format, data);
}

void GLRenderer::copyTextureToImage(Texture& texture) {

pimpl_->copyTextureToImage(texture);
}

void GLRenderer::resetState() {

pimpl_->reset();
Expand Down

0 comments on commit f91fdcb

Please sign in to comment.