drawing on textured object - then read texture

Hi all,

i want to draw on a textured object. The object should
be a textured polygon, and i would like to alter
( and then read back ) the applied texture. Like eg. drawing
a face on a sphere and then retrieving the texture.

An implementation could be

  • get cursor/object intersection position
  • get texture coordinate at that position
  • alter texture at these pixels

Is there any alternative implementation possible
without the cursor/object intersection code? Or is
there any magically simple way to get the texture
coordinate from a cursor position ( screen position )
i havent found so far?

Best & thanks,
Henniman

This looks OK.

You can check if the opposite way is feasible: move your cursor on the image, and draw the cursor position on a secondary image, then when applied on the object, you’ll see where your cursor is on the object. And for drawing, simply modify the first image.
With glTexSubImage it could work. However for complicated objects, this might not be easy to draw…

Try projective texturing, similarly to shadowmaps.
Clear a RGBA texture to vec4(0,0,0,0), draw the blotches of paint on it (do update the Alpha channel) in screen-space.
Then, start drawing the object’s triangles on the object’s texture, in 2D. (use the ST coords as gl_Position). On each vertex or fragment, project that RGBA texture and sample from it. (need alphablending enabled).
The next step would be to do custom face-culling and depthtest in the shader.

render the scene to multiple render targets, with a specific shader, that outputs texcoords, positions (in whichever space), [ and optionally “uniform int objectIdx;” and gl_PrimitiveID ] .

you mean

  • apply shader that writes tex coord
  • render into 1x1 pixel viewport at cursor location
  • read pixel
  • restore texture coordinate
  • alter texture at position

OMG - thats cool!

Best & Merci
Henniman

Yup, exactly. Though not a 1x1 viewport, just add scissors (otherwise you gotta toy with the projection-matrix a bit).

hi, it works like a charm!

this is the shader i load before i draw the object:


if(!glIsProgram(bitmask_shader))
{
 const GLchar* source =
 "\
 void main(void)\
 {\
 vec4 c = vec4(0.0,0.0,0.0,0.0);\
 vec2 f = vec2( 1.0, 256.0 );\
 c.rg = fract(f*gl_TexCoord[0].x);\
 c.ba = fract(f*gl_TexCoord[0].y);\
 gl_FragColor = c;\
 }\
 ";
 bitmask_shader = make_gl_program_with_fragment_shader(source);
}

and this is how i rertrieve the tex coordinate
in my app:


 glUseProgram(bitmask_shader);
 element->render();
 glUseProgram(0);	

 GLubyte the_color[4];
 Point mouse_pos = getMousePositionInGLPixels();
 glReadPixels( mouse_pos.x, mouse_pos.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, the_color );
 tex_coord_x = the_color[0] / (256.0) + the_color[1] / (256.0*256.0);
 tex_coord_y = the_color[2] / (256.0) + the_color[3] / (256.0*256.0);

fantastic!

Best
H