Question

currently I have a simple shader that renders a static mesh into the scene. I would like to know if it is possible to have a second output in my vertex shader that gives me the 2D bounding box of my object after the projection has been applied. Something like this:

#version 330 core

in vec4 Vertex_ms;
in vec4 Normal_ms;

out vec4 Normal_ws;

uniform mat4 Proj;
uniform mat4 View;
uniform mat4 Model;

uniform out vec2 topRight;
uniform out vec2 bottomLeft;

void main() {
    gl_Position = Proj * View * Model * Vertex_ms;
    topRight = max(gl_Position.xy, topRight);
    bottomLeft = min(gl_Position.xy, bottomLeft);

    Normal_ws = Model * Normal_ms;
}

this doesn't work, because I can't write to uniforms, but the intenion is, that I can make a reduce over the projectet vertices to get a bounding box.

Était-ce utile?

La solution

OpenGL-4.3 added a few functions to GLSL that allow to implement this, namely all the functions starting with atomic…. Of course those require an OpenGL-4.3 capable implementation, i.e. the latest generation of GPUs.

Most usefull for you are atomicMax and atomicMin.

However be advised that using those comes with a severe performance penality. A far better solution would be to calculate the bounding volume in advance an then just transforming that. If you're thinking about deformable meshes, keep in mind that those will stay withing certain bounds for any given deformation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top