3

I'm doing a simple image processing app using OpenGL and C++. However, there is one particular thing that I don't know how to do, which is:

I need to let my user to draw a Histogram Graph. The way I thought to do this is by creating a grid of quads one quad for each pixel intesity of my image. Example: if the image is 8 bits, I would need 256x256 quads in my grid. After drawing the grid I want my to user manually paint the quads in a quantized way (each quad) in the way that he could "draw" the histogram. The problem is that I dont know how to do any of these things... Would anyone give me direction on how to draw the grid, and how to make the paiting thing.

Iif you're confused about "drawing histogram" just considerit as a regular graph.

1 Answer 1

5

You don't have to draw a grid of quads. Just one quad is enough, and then use a shader to sample from the histogram stored in a 1d-texture. Here is what I get:

enter image description here

Vertex shader:

#version 450 core
layout(std140, binding = 0) uniform view_block {
    vec2 scale, offset;
} VIEW;

layout(std140, binding = 1) uniform draw_block {
    vec4 position;
    float max_value;
} DRAW;

out gl_PerVertex {
    vec4 gl_Position;
};

void main()
{
    ivec2 id = ivec2(gl_VertexID&1, gl_VertexID>>1);
    vec2 position = vec2(DRAW.position[id.x<<1], DRAW.position[(id.y<<1) + 1]);
    gl_Position = vec4(fma(position, VIEW.scale, VIEW.offset), 0, 1);
}

Fragment shader:

#version 450 core
layout(std140, binding = 1) uniform draw_block {
    vec4 position;
    float max_value;
} DRAW;
layout(binding = 0) uniform sampler1D hist;
layout(location = 0) out vec4 OUT;

void main()
{
    const vec2 extent = DRAW.position.zw - DRAW.position.xy;
    vec2 texcoord = (gl_FragCoord.xy - DRAW.position.xy)/(DRAW.position.zw - DRAW.position.xy);
    OUT.rgb = vec3(lessThan(texcoord.yyy*DRAW.max_value, texture(hist, texcoord.x).rgb));
    OUT.a = 1;
}

Histogram texture creation:

image hist(256, 1, 3, type_float);
// ... calculate the histogram ...

tex.reset(glCreateTextureSN(GL_TEXTURE_1D));
glTextureStorage1D(tex.get(), 1, GL_RGB32F, hist.w);
glTextureSubImage1D(tex.get(), 0, 0, hist.w, GL_RGB, GL_FLOAT, hist.c[0]);
glTextureParameteri(tex.get(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);

Rendering routine:

const vec2i vs = { glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) };
glViewport(0, 0, vs[0], vs[1]);
glClear(GL_COLOR_BUFFER_BIT);

struct view_block {
    vec2f scale, offset;
} VIEW = {
    vec2f(2)/vec2f(vs), -vec2f(1)
};
GLbuffer view_buf(glCreateBufferStorageSN(sizeof(VIEW), &VIEW, 0));
glBindBufferBase(GL_UNIFORM_BUFFER, 0, view_buf.get());

struct draw_block {
    box2f position;
    float max_value;
} DRAW = {
    box2f(0, 0, vs[0], vs[1]),
    max_value
};
GLbuffer draw_buf(glCreateBufferStorageSN(sizeof(DRAW), &DRAW, 0));
glBindBufferBase(GL_UNIFORM_BUFFER, 1, draw_buf.get());

bind_textures(tex.get());
glBindProgramPipeline(pp.get());
glBindVertexArray(0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glutSwapBuffers();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.