Skip to content

Commit 62f8aae

Browse files
committed
Implement DepthTexture #310
1 parent 016a854 commit 62f8aae

File tree

4 files changed

+23
-51
lines changed

4 files changed

+23
-51
lines changed

examples/textures/depth_texture.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ int main() {
6060
options.stencilBuffer = false;
6161
options.depthBuffer = true;
6262

63-
options.depthTexture = DepthTexture::create(0,0);
63+
options.depthTexture = DepthTexture::create();
6464
options.depthTexture->format = Format::Depth;
65-
options.depthTexture->type = Type::UnsignedShort;
65+
options.depthTexture->type = Type::Float;
6666

6767

6868
GLRenderTarget target(canvas.size().width(), canvas.size().height(), options);
@@ -72,13 +72,13 @@ int main() {
7272
postMaterial->vertexShader = R"(
7373
varying vec2 vUv;
7474
75-
void main() {
76-
vUv = uv;
77-
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
78-
}
75+
void main() {
76+
vUv = uv;
77+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
78+
}
7979
)";
8080
postMaterial->fragmentShader = R"(
81-
#include <packing>
81+
#include <packing>
8282
8383
varying vec2 vUv;
8484
uniform sampler2D tDiffuse;
@@ -95,7 +95,7 @@ int main() {
9595
9696
void main() {
9797
//vec3 diffuse = texture2D( tDiffuse, vUv ).rgb;
98-
float depth = readDepth( tDepth, vUv )*100000;
98+
float depth = readDepth( tDepth, vUv );
9999
100100
gl_FragColor.rgb = 1 - vec3( depth );
101101
gl_FragColor.a = 1.0;

include/threepp/textures/DepthTexture.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ namespace threepp {
1010
class DepthTexture: public Texture {
1111

1212
public:
13-
static std::shared_ptr<DepthTexture> create(unsigned width, unsigned height, std::optional<Type> type = std::nullopt, Format format = Format::Depth);
13+
static std::shared_ptr<DepthTexture> create(std::optional<Type> type = std::nullopt, Format format = Format::Depth);
1414

1515
private:
16-
DepthTexture(unsigned width, unsigned height, std::optional<Type> type, Format format);
16+
DepthTexture(std::optional<Type> type, Format format);
1717
};
1818

1919
}// namespace threepp

src/threepp/renderers/gl/GLTextures.cpp

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ void gl::GLTextures::uploadTexture(TextureProperties* textureProperties, Texture
148148

149149
if (dynamic_cast<DepthTexture*>(&texture)) {
150150

151-
glInternalFormat = GL_DEPTH_COMPONENT;
152-
153151
if (texture.type == Type::Float) {
154152

155153
glInternalFormat = GL_DEPTH_COMPONENT32F;
@@ -164,39 +162,7 @@ void gl::GLTextures::uploadTexture(TextureProperties* textureProperties, Texture
164162

165163
} else {
166164

167-
glInternalFormat = GL_DEPTH_COMPONENT16;
168-
}
169-
170-
if (texture.format == Format::Depth && glInternalFormat == GL_DEPTH_COMPONENT) {
171-
172-
// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
173-
// DEPTH_COMPONENT and type is not UNSIGNED_SHORT or UNSIGNED_INT
174-
// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
175-
if (texture.type != Type::UnsignedShort && texture.type != Type::UnsignedInt) {
176-
177-
std::cerr << "THREE.GLRenderer: Use UnsignedShortType or UnsignedIntType for DepthFormat DepthTexture." << std::endl;
178-
179-
texture.type = Type::UnsignedShort;
180-
glType = toGLType(texture.type);
181-
}
182-
}
183-
184-
if (texture.format == Format::DepthStencil && glInternalFormat == GL_DEPTH_COMPONENT) {
185-
186-
// Depth stencil textures need the DEPTH_STENCIL internal format
187-
// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
188-
glInternalFormat = GL_DEPTH_STENCIL;
189-
190-
// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
191-
// DEPTH_STENCIL and type is not UNSIGNED_INT_24_8_WEBGL.
192-
// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
193-
if (texture.type != Type::UnsignedInt248) {
194-
195-
std::cerr << "THREE.GLRenderer: Use UnsignedInt248Type for DepthStencilFormat DepthTexture." << std::endl;
196-
197-
texture.type = Type::UnsignedInt248;
198-
glType = toGLType(texture.type);
199-
}
165+
glInternalFormat = GL_DEPTH_COMPONENT24;
200166
}
201167

202168
//
@@ -514,11 +480,17 @@ void gl::GLTextures::setupDepthTexture(unsigned int framebuffer, GLRenderTarget*
514480
if (renderTarget->depthTexture->format == Format::Depth) {
515481

516482
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, *glDepthTexture, 0);
517-
483+
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
484+
if (status != GL_FRAMEBUFFER_COMPLETE) {
485+
std::cerr << "GLTextures: Depth texture framebuffer (depth) incomplete: 0x" << std::hex << status << std::dec << std::endl;
486+
}
518487
} else if (renderTarget->depthTexture->format == Format::DepthStencil) {
519488

520489
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, *glDepthTexture, 0);
521-
490+
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
491+
if (status != GL_FRAMEBUFFER_COMPLETE) {
492+
std::cerr << "GLTextures: Depth texture framebuffer (stencil) incomplete: 0x" << std::hex << status << std::dec << std::endl;
493+
}
522494
} else {
523495

524496
throw std::runtime_error("Unknown depthTexture format");

src/threepp/textures/DepthTexture.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
using namespace threepp;
55

6-
DepthTexture::DepthTexture(unsigned width, unsigned height,std::optional<Type> type, Format format)
7-
: Texture({Image(std::vector<unsigned char>(width * height * 4, 255), width, height)}) {
6+
DepthTexture::DepthTexture(std::optional<Type> type, Format format)
7+
: Texture({Image({}, 0, 0)}) {
88

99
if ( !type && format == Format::Depth ) type = Type::UnsignedShort;
1010
if ( !type && format == Format::DepthStencil ) type = Type::UnsignedInt248;
@@ -15,6 +15,6 @@ DepthTexture::DepthTexture(unsigned width, unsigned height,std::optional<Type> t
1515
generateMipmaps = false;
1616
}
1717

18-
std::shared_ptr<DepthTexture> DepthTexture::create(unsigned width, unsigned height, std::optional<Type> type, Format format) {
19-
return std::shared_ptr<DepthTexture>(new DepthTexture(width, height, type, format));
18+
std::shared_ptr<DepthTexture> DepthTexture::create(std::optional<Type> type, Format format) {
19+
return std::shared_ptr<DepthTexture>(new DepthTexture(type, format));
2020
}

0 commit comments

Comments
 (0)