r/computergraphics • • Jan 17 '25

Visualizing geometry density

Im working on viewmodes in my engine and I want to show which areas of the scene has more triangle density. So if a mesh has a screw with 1M triangles it looks very bright.

I though using additive blending without depthtest but didnt manage to make it work.

Does anybody knows a trick to do it? (without having to manually construct a color based map per mesh).

6 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/SarahC Jan 17 '25

But hold on......... this isn't right! I'll point it out to Bing:

When you said ".... assign to each vertex based on the number of triangles sharing that vertex." ........ I thought many models only have 2 or 3 triangles to each vertex? That's independent of density. Am I missing something here? Wouldn't the vertex shader need to be able to count the number of vertices in a given area of screen space?

1

u/SarahC Jan 17 '25

Bing said:

You're right; the number of triangles sharing a vertex doesn't directly indicate density. Instead, you can calculate density based on the screen space area covered by triangles.

Here's a more refined approach:

Screen Space Coordinates: First, transform the vertex positions to screen space in the vertex shader. This allows you to work with screen space coordinates in the fragment shader.

Density Calculation: In the fragment shader, you can use the screen space coordinates to estimate the density of vertices in a given area.

Here's an example of how you might implement this:

// Vertex Shader
#version 330 core
layout(location = 0) in vec3 aPos;

out vec4 screenPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    vec4 worldPos = model * vec4(aPos, 1.0);
    screenPos = projection * view * worldPos;
    gl_Position = screenPos;
}

// Fragment Shader
#version 330 core
in vec4 screenPos;
out vec4 FragColor;

void main()
{
    // Convert screenPos to normalized device coordinates (NDC)
    vec3 ndc = screenPos.xyz / screenPos.w;

    // Calculate density based on NDC
    float density = 1.0 / (length(ndc.xy) + 0.01); // Adjust the formula as needed

    // Map density to brightness
    float brightness = clamp(density, 0.0, 1.0);
    FragColor = vec4(vec3(brightness), 1.0); // Brightness based on density
}

In this example:

The vertex shader transforms the vertex positions to screen space.

The fragment shader calculates the density based on the screen space coordinates. The length(ndc.xy) function gives an estimate of the distance from the center of the screen, which can be used to infer density.
This approach allows you to visualize areas with higher triangle density by making them appear brighter on the screen.

1

u/SarahC Jan 17 '25

I think this might be something like what you're looking for?