How to calculate the X/Y Coords of the Pixel under the cursor?


i build a small 3D modelling tool which build models in Minecraft style. Each part of the model consists out of 6 primitives (Quads), each part is textured from a texture atlas. Currently i open a 2. window to edit the texture bitmap.


The idea now is a simple one:
I want to repaint the texture directly on the primitive, basically pick a block and use a paint like brush to alter the pixels under the cursor, similar to the texture painter tool from blender.

The question i have is this:

Is there a way to calculate the X/Y Coords of the Pixel under the cursor when i click on a primitive?

X/Y coords? Surely you already have those from the mouse event?

Or do you mean the texture coordinates?

For the latter, I’d be inclined to render the scene with a 1x1 pixel viewport using a fragment shader which stores the texture ID and texture coordinates in the framebuffer.

Otherwise, you’re looking at doing a fair amount of work in software. You can use transform-feedback mode to capture the transformed vertices; you could use a geometry shader to discard primitives which are back-facing, lie entirely outside of the clip region or which don’t overlap the pointer position. But you’re still likely to be left with multiple clip-space primitives, for which you’ll need to determine the front-most one, then calculate the texture coordinates at the pointer position. Also, if you want to be able to pick through primitives whose texture has an alpha channel, you’ll need to handle that yourself.