Skip to content

Commit

Permalink
Add functionality for reading texture data back into memory (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren authored May 7, 2024
1 parent d00d061 commit d76b33a
Show file tree
Hide file tree
Showing 3 changed files with 23 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
19 changes: 19 additions & 0 deletions src/threepp/renderers/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,20 @@ 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();
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 +1362,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 d76b33a

Please sign in to comment.