PDA

View Full Version : drawing on textured object - then read texture



henniman
04-29-2011, 07:01 AM
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

_arts_
04-30-2011, 02:00 AM
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...

Ilian Dinev
04-30-2011, 05:15 AM
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.

Ilian Dinev
04-30-2011, 05:18 AM
Or is
there any magically simple way to get the texture
coordinate from a cursor position ( screen position )
i havent found so far?

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 ] .

henniman
05-02-2011, 07:59 AM
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

Ilian Dinev
05-02-2011, 12:08 PM
Yup, exactly. Though not a 1x1 viewport, just add scissors (otherwise you gotta toy with the projection-matrix a bit).

henniman
05-07-2011, 11:33 AM
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