How to paint to a texture

Started by
4 comments, last by Dwiff 16 years, 1 month ago
I have a basic 3D room I can walk around in and I want the user to be able to click and draw on the wall, kind of like tagging. Right now I have fairly large walls that are texture mapped. I was thinking about painting the texture in very small quads and determining which quad the user had the mouse over and making that part of the texture a specific color. Unfortunately I don't know how to change the color of the original texture. Any suggestions?
Advertisement
There's two main ways you can do that. You can either

A) Lock the texture so you can directly access the pixel data, then have your app fill in the colors manually. This can be slow, as locking the texture can stall the GPU and will cause data to be sent across the bus. Reading back data is generally even worse performance, which you need for alpha blending.

B) Make the textures render targets (or copy the texture to a render target texture) and then draw on that like you would the backbuffer. This is fast, and allows you to use the same techniques you use elsewhere (drawing primitives, pixel shaders, etc.).

EDIT: A third option would be not to modify the textures at all, but to instead use decals. A decal being a flat quad with a texture that you render directly "on top of" the surface you want modified.
Here are a couple of easier (and faster) ways.

One possibility (your own "decal"):

Construct a separate small mesh of the size, shape and color you want for the mouseover. Draw it at the mouseover position. If necessary to avoid z-fighting, raise the mouseover mesh by a fraction of a unit, or disable depth testing for just this draw. (like a decal)

It's a bit more complicated but another option:

Construct the full texture you want but set 1 pixel (say at 0,0) to the color you want.

Construct the walls as small quads.

Detect the mouseover and set the texture coords to that quad to 0,0 to "texture map" the quad to the single pixel. You'll have to "remember" the original quad and tex coords to restore them when the mouse moves.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Thanks for the ideas. The decal thing won't work for my project because the general idea is I have these rooms that the user can walk around in and pick up various spray paint cans. So basically we want to be able to allow the user to paint the walls with various colors however they feel like it, and we don't want them to be able to undo anything.

I know this probably isn't the most efficient matter but right now we have particle systems for the various nozzles of the spray cans that allow to user to specify "paint brushes" if you will. Some spread out more than others and what not. Right now we just made it so if the particle collides with the wall it "sticks" and gives us an OK effect but obviously if you spray for a long time we are talking about a ton of objects and that just doesn't work.



If you can support multiple texture stages, how about maintaining a second alpha texture for each wall?

Initialize the second texture with alpha=0.

Draw to the second texture with a high-alpha paint :-) and alphablend it on top of the existing texture.

When a spray can is lit off, set the 2nd texture as a render target and draw particle sprites or colored lines at the appropriate locations.

If you can't support multiple texture stages, create a second wall vertex buffer, textured with the second texture and blend it over the first wall.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I'll try to decipher what you wrote Buckeye and try it out this weekend. Thanks.

This topic is closed to new replies.

Advertisement