Skip to content

Commit

Permalink
encapsulate Texture::image
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed May 7, 2024
1 parent 263d070 commit d8a50c8
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 38 deletions.
7 changes: 3 additions & 4 deletions include/threepp/textures/DataTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ namespace threepp {
public:
void setData(const ImageData& data) {

image.front().setData(data);
image().setData(data);
}

static std::shared_ptr<DataTexture> create(
const ImageData& data,
unsigned int width = 1, unsigned int height = 1) {

return std::shared_ptr<DataTexture>(new DataTexture(data, width, height));
}

private:
explicit DataTexture(const ImageData& data, unsigned int width, unsigned int height)
: Texture({}) {

this->image.emplace_back(data, width, height);
: Texture({Image(data, width, height)}) {

this->magFilter = Filter::Nearest;
this->minFilter = Filter::Nearest;
Expand Down
24 changes: 17 additions & 7 deletions include/threepp/textures/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "threepp/textures/Image.hpp"

#include <functional>
#include <memory>
#include <optional>

namespace threepp {
Expand All @@ -27,9 +26,6 @@ namespace threepp {

std::string name;

std::vector<Image> image;
std::vector<Image> mipmaps;

Mapping mapping = DEFAULT_MAPPING;

TextureWrapping wrapS{TextureWrapping::ClampToEdge};
Expand Down Expand Up @@ -71,6 +67,18 @@ namespace threepp {

[[nodiscard]] const std::string& uuid() const;

Image& image();

[[nodiscard]] const Image& image() const;

[[nodiscard]] std::vector<Image>& images();

[[nodiscard]] const std::vector<Image>& images() const;

[[nodiscard]] std::vector<Image>& mipmaps();

[[nodiscard]] const std::vector<Image>& mipmaps() const;

void updateMatrix();

void dispose();
Expand Down Expand Up @@ -98,11 +106,13 @@ namespace threepp {

private:
std::string uuid_;
std::vector<Image> images_;
std::vector<Image> mipmaps_;

bool disposed_ = false;
unsigned int version_ = 0;
bool disposed_{false};
unsigned int version_{0};

inline static unsigned int textureId = 0;
inline static unsigned int textureId{0};
};

}// namespace threepp
Expand Down
6 changes: 3 additions & 3 deletions src/threepp/renderers/GLRenderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ void GLRenderTarget::setSize(unsigned int width, unsigned int height, unsigned i
this->height = height;
this->depth = depth;

this->texture->image.front().width = width;
this->texture->image.front().height = height;
this->texture->image.front().depth = depth;
this->texture->image().width = width;
this->texture->image().height = height;
this->texture->image().depth = depth;

this->dispose();
}
Expand Down
6 changes: 3 additions & 3 deletions src/threepp/renderers/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,8 @@ struct GLRenderer::Impl {
void copyFramebufferToTexture(const Vector2& position, Texture& texture, int level) {

const auto levelScale = std::pow(2, -level);
const auto width = static_cast<int>(texture.image.front().width * levelScale);
const auto height = static_cast<int>(texture.image.front().height * levelScale);
const auto width = static_cast<int>(texture.image().width * levelScale);
const auto height = static_cast<int>(texture.image().height * levelScale);

textures.setTexture2D(texture, 0);

Expand All @@ -1132,7 +1132,7 @@ struct GLRenderer::Impl {

textures.setTexture2D(texture, 0);

auto& image = texture.image.front();
auto& image = texture.image();
auto& data = image.data();
auto newSize = image.width * image.height * (texture.format == Format::RGB ? 3 : 4);
data.resize(newSize);
Expand Down
2 changes: 1 addition & 1 deletion src/threepp/renderers/gl/GLCubeMaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void GLCubeMaps::get(Texture* texture) {

} else {

const auto& image = texture->image.front();
const auto& image = texture->image();

if (image.height > 0) {

Expand Down
26 changes: 13 additions & 13 deletions src/threepp/renderers/gl/GLTextures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void gl::GLTextures::setTextureParameters(GLuint textureType, Texture& texture)

void gl::GLTextures::uploadTexture(TextureProperties* textureProperties, Texture& texture, GLuint slot) {

if (texture.image.empty()) return;
if (texture.images().empty()) return;

GLint textureType = GL_TEXTURE_2D;

Expand All @@ -137,7 +137,7 @@ void gl::GLTextures::uploadTexture(TextureProperties* textureProperties, Texture

glPixelStorei(GL_UNPACK_ALIGNMENT, texture.unpackAlignment);

auto& image = texture.image.front();
auto& image = texture.image();

GLuint glFormat = toGLFormat(texture.format);

Expand All @@ -146,7 +146,7 @@ void gl::GLTextures::uploadTexture(TextureProperties* textureProperties, Texture

setTextureParameters(textureType, texture);

auto& mipmaps = texture.mipmaps;
auto& mipmaps = texture.mipmaps();

if (dataTexture3D) {

Expand Down Expand Up @@ -183,11 +183,11 @@ void gl::GLTextures::uploadTexture(TextureProperties* textureProperties, Texture
if (glType == GL_UNSIGNED_BYTE) {
state->texImage2D(GL_TEXTURE_2D, 0, glInternalFormat,
static_cast<int>(image.width), static_cast<int>(image.height),
glFormat, glType, texture.image.front().data().data());
glFormat, glType, texture.image().data().data());
} else if (glType == GL_FLOAT) {
state->texImage2D(GL_TEXTURE_2D, 0, glInternalFormat,
static_cast<int>(image.width), static_cast<int>(image.height),
glFormat, glType, texture.image.front().data<float>().data());
glFormat, glType, texture.image().data<float>().data());
} else {

std::cerr << "Unnsupported gltype=" << glType << std::endl;
Expand Down Expand Up @@ -284,11 +284,11 @@ int gl::GLTextures::allocateTextureUnit() {

void gl::GLTextures::setTexture2D(Texture& texture, GLuint slot) {

auto textureProperties = properties->textureProperties.get(&texture);
const auto textureProperties = properties->textureProperties.get(&texture);

if (texture.version() > 0 && textureProperties->version != texture.version()) {

const auto& image = texture.image;
const auto& image = texture.images();

if (image.empty()) {

Expand Down Expand Up @@ -361,8 +361,8 @@ void gl::GLTextures::uploadCubeTexture(TextureProperties* textureProperties, Tex
auto glInternalFormat = getInternalFormat(glFormat, glType);
setTextureParameters(GL_TEXTURE_CUBE_MAP, texture);

auto& images = texture.image;
auto& mipmaps = texture.mipmaps;
auto& images = texture.images();
auto& mipmaps = texture.mipmaps();
for (int i = 0; i < 6; i++) {
auto& image = images[i];
state->texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, image.width, image.height, glFormat, glType, image.data().data());
Expand Down Expand Up @@ -454,11 +454,11 @@ void gl::GLTextures::setupDepthTexture(unsigned int framebuffer, GLRenderTarget*

// upload an empty depth texture with framebuffer size
if (!properties->textureProperties.get(renderTarget->depthTexture.get())->glTexture ||
renderTarget->depthTexture->image.front().width != renderTarget->width ||
renderTarget->depthTexture->image.front().height != renderTarget->height) {
renderTarget->depthTexture->image().width != renderTarget->width ||
renderTarget->depthTexture->image().height != renderTarget->height) {

renderTarget->depthTexture->image.front().width = renderTarget->width;
renderTarget->depthTexture->image.front().height = renderTarget->height;
renderTarget->depthTexture->image().width = renderTarget->width;
renderTarget->depthTexture->image().height = renderTarget->height;
renderTarget->depthTexture->needsUpdate();
}

Expand Down
4 changes: 1 addition & 3 deletions src/threepp/textures/DataTexture3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace threepp;

DataTexture3D::DataTexture3D(const std::vector<unsigned char>& data,
unsigned int width, unsigned int height, unsigned int depth)
: Texture({}) {
: Texture({Image(data, width, height, depth)}) {

// We're going to add .setXXX() methods for setting properties later.
// Users can still set in DataTexture3D directly.
Expand All @@ -16,8 +16,6 @@ DataTexture3D::DataTexture3D(const std::vector<unsigned char>& data,
//
// See #14839

this->image.emplace_back(data, width, height, depth);

this->magFilter = Filter::Nearest;
this->minFilter = Filter::Nearest;

Expand Down
44 changes: 40 additions & 4 deletions src/threepp/textures/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#include "threepp/math/MathUtils.hpp"

#include <cmath>
#include <iostream>

using namespace threepp;


Texture::Texture(std::vector<Image> image)
: uuid_(math::generateUUID()),
image(std::move(image)) {}
images_(std::move(image)) {}

std::shared_ptr<Texture> Texture::create() {
return std::shared_ptr<Texture>(new Texture({}));
Expand All @@ -31,6 +32,41 @@ const std::string& Texture::uuid() const {
return uuid_;
}

Image& Texture::image() {

if (images_.empty()) {

throw std::runtime_error("Error, no Image set for texture");
}

return images_.front();
}

const Image& Texture::image() const {

return images_.front();
}

std::vector<Image>& Texture::images() {

return images_;
}

const std::vector<Image>& Texture::images() const {

return images_;
}

std::vector<Image>& Texture::mipmaps() {

return mipmaps_;
}

const std::vector<Image>& Texture::mipmaps() const {

return mipmaps_;
}

void Texture::updateMatrix() {

this->matrix.setUvTransform(this->offset.x, this->offset.y, this->repeat.x, this->repeat.y, this->rotation, this->center.x, this->center.y);
Expand Down Expand Up @@ -108,7 +144,7 @@ void Texture::transformUv(Vector2& uv) const {
}
}

if (!this->image.empty() && this->image.front().flipped()) {
if (!this->images_.empty() && this->image().flipped()) {

uv.y = 1 - uv.y;
}
Expand All @@ -126,8 +162,8 @@ unsigned int Texture::version() const {

Texture& Texture::copy(const Texture& source) {

this->image = source.image;
this->mipmaps = source.mipmaps;
this->images_ = source.images_;
this->mipmaps_ = source.mipmaps_;

this->mapping = source.mapping;

Expand Down

0 comments on commit d8a50c8

Please sign in to comment.