Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
396 changes: 396 additions & 0 deletions data/textures/env/san_giuseppe_bridge/LICENSE.txt

Large diffs are not rendered by default.

Binary file not shown.
1 change: 1 addition & 0 deletions examples/textures/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

add_example(NAME "cubemap")
add_example(NAME "hdr_envmap")
add_example(NAME "texture3d" LINK_IMGUI)
add_example(NAME "texture2d" WEB WEB_EMBED
"${PROJECT_SOURCE_DIR}/data/textures/crate.gif@data/textures/crate.gif"
Expand Down
45 changes: 45 additions & 0 deletions examples/textures/hdr_envmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

#include "threepp/loaders/RGBELoader.hpp"
#include "threepp/threepp.hpp"

using namespace threepp;

int main() {

Canvas canvas("HDR Environment Map");
GLRenderer renderer(canvas.size());
renderer.toneMapping = ToneMapping::ACESFilmic;
renderer.toneMappingExposure = 1.0f;

auto scene = Scene::create();

PerspectiveCamera camera(45, canvas.aspect(), 0.1f, 1000);
camera.position.set(0, 0, 5);

RGBELoader hdrLoader;

if (auto hdrTexture = hdrLoader.load(std::string(DATA_FOLDER) + "/textures/env/san_giuseppe_bridge/san_giuseppe_bridge_4k.hdr")) {
scene->background = hdrTexture;
scene->environment = hdrTexture;
}

// Metallic sphere — IBL from the HDR environment
auto sphereGeo = SphereGeometry::create(1.0f, 64, 32);
auto sphereMat = MeshStandardMaterial::create();
sphereMat->metalness = 1.0f;
sphereMat->roughness = 0.0f;
auto sphere = Mesh::create(sphereGeo, sphereMat);
scene->add(sphere);

OrbitControls controls{camera, canvas};

canvas.onWindowResize([&](WindowSize size) {
camera.aspect = size.aspect();
camera.updateProjectionMatrix();
renderer.setSize(size);
});

canvas.animate([&] {
renderer.render(*scene, camera);
});
}
23 changes: 23 additions & 0 deletions include/threepp/loaders/RGBELoader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#ifndef THREEPP_RGBELOADER_HPP
#define THREEPP_RGBELOADER_HPP

#include <filesystem>
#include <memory>

namespace threepp {

class Texture;

class RGBELoader {

public:
// Load a Radiance HDR (.hdr) file.
// Returns a float-type RGB texture with EquirectangularReflection mapping,
// ready to assign to scene.background / scene.environment.
std::shared_ptr<Texture> load(const std::filesystem::path& path, bool flipY = true);
};

}// namespace threepp

#endif//THREEPP_RGBELOADER_HPP
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ set(publicHeaders
"threepp/loaders/GLTFLoader.hpp"
"threepp/loaders/MTLLoader.hpp"
"threepp/loaders/ImageLoader.hpp"
"threepp/loaders/RGBELoader.hpp"
"threepp/loaders/ModelLoader.hpp"
"threepp/loaders/OBJLoader.hpp"
"threepp/loaders/STLLoader.hpp"
Expand Down Expand Up @@ -355,6 +356,7 @@ set(sources
"threepp/loaders/FontLoader.cpp"
"threepp/loaders/GLTFLoader.cpp"
"threepp/loaders/ImageLoader.cpp"
"threepp/loaders/RGBELoader.cpp"
"threepp/loaders/MTLLoader.cpp"
"threepp/loaders/ModelLoader.cpp"
"threepp/loaders/OBJLoader.cpp"
Expand Down
45 changes: 45 additions & 0 deletions src/threepp/loaders/RGBELoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

#include "threepp/loaders/RGBELoader.hpp"

#include "threepp/textures/Texture.hpp"

// stb_image.h is already compiled via ImageLoader.cpp — just declare the API.
#include "stb_image.h"

#include <iostream>

using namespace threepp;

std::shared_ptr<Texture> RGBELoader::load(const std::filesystem::path& path, bool flipY) {

if (!std::filesystem::exists(path)) {
std::cerr << "[RGBELoader] No such file: '" << absolute(path).string() << "'!" << std::endl;
return nullptr;
}

stbi_set_flip_vertically_on_load(flipY);

int width{}, height{}, channels{};
// stbi_loadf decodes RGBE encoding internally and returns linear float RGB(A).
float* pixels = stbi_loadf(path.string().c_str(), &width, &height, &channels, 3);

if (!pixels) {
std::cerr << "[RGBELoader] Failed to load '" << path.string() << "': " << stbi_failure_reason() << std::endl;
return nullptr;
}

std::vector<float> data(pixels, pixels + (3 * width * height));
stbi_image_free(pixels);

Image image{std::move(data), static_cast<unsigned int>(width), static_cast<unsigned int>(height), 0};

auto texture = Texture::create(image);
texture->name = path.stem().string();
texture->format = Format::RGB;
texture->type = Type::Float;
texture->encoding = Encoding::Linear;// stbi_loadf already decoded RGBE → linear floats
texture->mapping = Mapping::EquirectangularReflection;
texture->needsUpdate();

return texture;
}
19 changes: 10 additions & 9 deletions src/threepp/renderers/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,9 @@ struct GLRenderer::Impl {
materialProperties->fog = scene->fog;
auto materialWithEnvMap = material->as<MaterialWithEnvMap>();
if (materialWithEnvMap && materialWithEnvMap->envMap) {
cubemaps.get(materialWithEnvMap->envMap.get());
materialProperties->envMap = materialWithEnvMap->envMap.get();
materialProperties->envMap = cubemaps.get(materialWithEnvMap->envMap.get());
} else {
cubemaps.get(materialProperties->environment);
materialProperties->envMap = materialProperties->environment;
materialProperties->envMap = cubemaps.get(materialProperties->environment);
}

if (programs.empty()) {
Expand Down Expand Up @@ -787,11 +785,9 @@ struct GLRenderer::Impl {
Texture* envMap;
auto materialWithEnvMap = material->as<MaterialWithEnvMap>();
if (materialWithEnvMap && materialWithEnvMap->envMap) {
cubemaps.get(materialWithEnvMap->envMap.get());
envMap = materialWithEnvMap->envMap.get();
envMap = cubemaps.get(materialWithEnvMap->envMap.get());
} else {
cubemaps.get(environment.get());
envMap = environment.get();
envMap = cubemaps.get(environment.get());
}
bool vertexAlphas = material->vertexColors &&
object->geometry() &&
Expand Down Expand Up @@ -1106,7 +1102,12 @@ struct GLRenderer::Impl {

if (renderTarget) {

framebuffer = *properties.renderTargetProperties.get(renderTarget)->glFramebuffer;
const auto* rtProps = properties.renderTargetProperties.get(renderTarget);
if (rtProps->glCubeFramebuffers) {
framebuffer = (*rtProps->glCubeFramebuffers)[activeCubeFace];
} else {
framebuffer = *rtProps->glFramebuffer;
}

_currentViewport.copy(renderTarget->viewport);
_currentScissor.copy(renderTarget->scissor);
Expand Down
72 changes: 37 additions & 35 deletions src/threepp/renderers/gl/GLBackground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ void GLBackground::render(GLRenderList& renderList, Object3D* scene) {
background = scene->as<Scene>()->background;
}

// Resolve equirectangular textures to cubemaps up front (mirrors three.js WebGLBackground).
Texture* resolvedBackground = nullptr;
if (background && background->isTexture()) {

cubemaps.get(background->texture().get());
resolvedBackground = cubemaps.get(background->texture().get());
}

if (!background || (background && background->empty())) {
Expand All @@ -43,51 +44,52 @@ void GLBackground::render(GLRenderList& renderList, Object3D* scene) {
renderer.clear(renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil);
}

if (background && background->isTexture()) {
// resolvedBackground is either the original texture or the converted CubeTexture.
// Only render the skybox if it resolves to a CubeTexture.
if (auto* cubeBackground = dynamic_cast<CubeTexture*>(resolvedBackground)) {

auto tex = background->texture();
if (auto cubeTexture = std::dynamic_pointer_cast<CubeTexture>(tex)) {

if (!boxMesh) {
auto shaderMaterial = ShaderMaterial::create();
shaderMaterial->name = "BackgroundCubeMaterial";
shaderMaterial->uniforms = shaders::ShaderLib::instance().cube.uniforms;
shaderMaterial->vertexShader = shaders::ShaderLib::instance().cube.vertexShader;
shaderMaterial->fragmentShader = shaders::ShaderLib::instance().cube.fragmentShader;
shaderMaterial->side = Side::Back;
shaderMaterial->depthTest = false;
shaderMaterial->depthWrite = false;
shaderMaterial->fog = false;

auto geometry = BoxGeometry::create(1, 1, 1);
geometry->deleteAttribute("normal");
geometry->deleteAttribute("uv");
if (!boxMesh) {
auto shaderMaterial = ShaderMaterial::create();
shaderMaterial->name = "BackgroundCubeMaterial";
shaderMaterial->uniforms = shaders::ShaderLib::instance().cube.uniforms;
shaderMaterial->vertexShader = shaders::ShaderLib::instance().cube.vertexShader;
shaderMaterial->fragmentShader = shaders::ShaderLib::instance().cube.fragmentShader;
shaderMaterial->side = Side::Back;
shaderMaterial->depthTest = false;
shaderMaterial->depthWrite = false;
shaderMaterial->fog = false;

boxMesh = std::make_unique<Mesh>(geometry, shaderMaterial);
auto geometry = BoxGeometry::create(1, 1, 1);
geometry->deleteAttribute("normal");
geometry->deleteAttribute("uv");

boxMesh->onBeforeRender = [&](void*, Object3D*, Camera* camera, BufferGeometry*, Material*, std::optional<GeometryGroup>) {
boxMesh->matrixWorld->copyPosition(*camera->matrixWorld);
};
boxMesh = std::make_unique<Mesh>(geometry, shaderMaterial);

objects.update(boxMesh.get());
}
boxMesh->onBeforeRender = [&](void*, Object3D*, Camera* camera, BufferGeometry*, Material*, std::optional<GeometryGroup>) {
boxMesh->matrixWorld->copyPosition(*camera->matrixWorld);
};

auto shaderMaterial = boxMesh->material()->as<ShaderMaterial>();
shaderMaterial->envMap = background->texture();
shaderMaterial->uniforms.at("envMap").setValue(background->texture().get());
shaderMaterial->uniforms.at("flipEnvMap").setValue(cubeTexture->_needsFlipEnvMap);
objects.update(boxMesh.get());
}

if (currentBackground != &background.value() || currentBackgroundVersion != tex->version() || currentTonemapping != renderer.toneMapping) {
auto shaderMaterial = boxMesh->material()->as<ShaderMaterial>();
// envMap must be non-null for USE_ENVMAP to be compiled in; the uniform carries the actual ptr.
shaderMaterial->envMap = tex;
shaderMaterial->uniforms.at("envMap").setValue(cubeBackground);
shaderMaterial->uniforms.at("flipEnvMap").setValue(cubeBackground->_needsFlipEnvMap);

shaderMaterial->needsUpdate();
if (currentBackground != &background.value() || currentBackgroundVersion != tex->version() || currentTonemapping != renderer.toneMapping) {

currentBackground = &background.value();
currentBackgroundVersion = tex->version();
currentTonemapping = renderer.toneMapping;
}
shaderMaterial->needsUpdate();

renderList.unshift(boxMesh.get(), boxMesh->geometry().get(), boxMesh->material().get(), 0, 0, std::nullopt);
currentBackground = &background.value();
currentBackgroundVersion = tex->version();
currentTonemapping = renderer.toneMapping;
}

renderList.unshift(boxMesh.get(), boxMesh->geometry().get(), boxMesh->material().get(), 0, 0, std::nullopt);
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/threepp/renderers/gl/GLCubeMaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ namespace {
GLCubeMaps::GLCubeMaps(GLRenderer& renderer)
: renderer(renderer) {}

void GLCubeMaps::get(Texture* texture) {
Texture* GLCubeMaps::get(Texture* texture) {

if (texture) {

const auto mapping = texture->mapping;

if (mapping == Mapping::EquirectangularReflection || mapping == Mapping::EquirectangularRefraction) {

if (cubemaps.count(texture)) {
if (cubemaps.contains(texture)) {

const auto cubemap = cubemaps.at(texture)->texture;
const auto cubemap = cubemaps.at(texture)->texture.get();
mapTextureMapping(*cubemap, texture->mapping);
return cubemap;

} else {

Expand All @@ -52,13 +53,19 @@ void GLCubeMaps::get(Texture* texture) {

renderer.setRenderTarget(currentRenderTarget);

//TODO
auto* cubemap = cubemaps[texture]->texture.get();
mapTextureMapping(*cubemap, texture->mapping);
return cubemap;

mapTextureMapping(*renderTarget->texture, texture->mapping);
} else {

return nullptr;
}
}
}
}

return texture;
}

void GLCubeMaps::dispose() {
Expand Down
5 changes: 4 additions & 1 deletion src/threepp/renderers/gl/GLCubeMaps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ namespace threepp {
public:
explicit GLCubeMaps(GLRenderer& renderer);

void get(Texture* texture);
// Returns the converted CubeTexture for equirectangular inputs,
// the original texture for anything else, or nullptr if not ready.
// Mirrors three.js WebGLCubeMaps.get().
Texture* get(Texture* texture);

void dispose();

Expand Down
2 changes: 2 additions & 0 deletions src/threepp/renderers/gl/GLProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "threepp/renderers/GLRenderTarget.hpp"
#include "threepp/textures/Texture.hpp"

#include <array>
#include <optional>
#include <unordered_map>

Expand All @@ -30,6 +31,7 @@ namespace threepp::gl {
struct RenderTargetProperties {

std::optional<unsigned int> glFramebuffer;
std::optional<std::array<unsigned int, 6>> glCubeFramebuffers;
std::optional<unsigned int> glDepthbuffer;
};

Expand Down
2 changes: 1 addition & 1 deletion src/threepp/renderers/gl/GLRenderStates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void GLRenderState::setupLightsView(Camera* camera) {

GLRenderState* GLRenderStates::get(Object3D* scene, size_t renderCallDepth) {

if (renderCallDepth >= renderStates_[scene->uuid].size()) {
while (renderCallDepth >= renderStates_[scene->uuid].size()) {

renderStates_[scene->uuid].emplace_back(std::make_unique<GLRenderState>());
}
Expand Down
Loading
Loading