🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How do I edit a texture that is already on a model in OpenGL?

Started by
21 comments, last by hplus0603 1 year, 10 months ago

I have a texture that is on a terrain. I used texture coordinates to make the grass appear as it would on a normal landscape. However, I am not aware of how I could just throw a texture over the grassy one. How could I edit the texture as it would be done in a terrain editor? Would I need to simply create the textures and then set the individual vertices to carry that texture? But if I did that, would it not just texture one small piece of the terrain? How could this be done?

If I need to elaborate more, just let me know what I need to clarify!

Thank you!

-rydog

Advertisement

You mean something like this?

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

@fleabay After seeing this, it provides sort of what I am looking for, though I do not want to have to rely on a colormap. I want to be able to edit the image in “real time” (the user can drag the mouse across a texture and a dirt texture will appear).

Something like this (specifically near the end of the video):

yaboiryan said:
I do not want to have to rely on a colormap. I want to be able to edit the image in “real time”

You're looking at it wrong. The colormap is just data that can be visualized as color. Really just a set of alpha maps in the different color channels of one texture.

You could create an interactive terrain painting tool and paint using that ‘color’ but never need to know that the color existed (as a user). You would chose the terrain type from a list and by painting on the map, the data/texture would be modified to show whatever terrain type was selected.

I think I might try to create a system for my game engine but use a stacked set of textures, each with it's own alpha map. Mostly same idea but maybe more versatile.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

I agree with what fleabay said above. But I'll put things a bit differently. . . You can edit regular textures and/or colormaps real time if you want to. They are just pixel data. Which is just data.

So, to get to the basic question of this thread : “How do I edit a texture that is already on a model in OpenGL

To start off with, you should know that textures, and other types of images, in OpenGL often get altered in real time. For example, shadow maps, and reflection maps may need to be updated every frame. You can decide to simply draw into a background image, instead outputting directly to the screen. So you can draw various things into a texture, and apply that resulting texture to your objects as the game continues to run. That is one way to edit a texture in real time.

If you want to edit based on a mouse drag, that is possible too. . .

You need to get the image data for your texture, or colormap. Find the location of the pixel, or pixels, you want to change in that data. Change the data for that pixel, or pixels. And then let OpenGL know that the image data has been changed.

Then you should be good to go, and OpenGL will draw using the modified image.

@Warp9 What you and @fleabay are suggesting makes sense, though I am still a bit confused regarding execution. I watched through the tutorial that fleabay sent me and the terrain now works with multitexturing… but it only works with a colormap. I cannot visualize how I would change which texture is active at each point… would I use a state machine or something to enable certain textures at certain points during the rendering?

It seems like the only way to do it would be to edit the color map in real time, but I still have issues because I cannot edit a texture while the program is running. It requires a restart of the game, when, in reality, I will need these textures to be changed while the program is running.

@yaboiryan This thread from stack overflow shows 4 ways to update a texture. You may need to create a working copy of your texture to paint on and continuous overwrite the original. Or this may be completely unnecessary, not sure.

https://stackoverflow.com/questions/9863969/updating-a-texture-in-opengl-with-glteximage2d

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

@fleabay Ok so I have figured it out to the point where I can get the terrain texture to change. However, I have it set to where everything behind the coordinates of the mouse pointer will become a dirt texture… I just want it to paint certain areas… Would I have to make a sort of vector in the GLSL in order to make a note of all the coordinates that have changed? But how would I do that? I am not even sure if GLSL has such a feature… though I could in theory do it in the normal code and maintain such coordinates in a vector… but how would I save that?

To make this clearer, in the coordinates that are less than my mouse coordinates, it looks literally like there is just a square chunk of the terrain that is using texture different than the grass background texture.

So I just used the blendmap and textures from the tutorial that @fleabay recommended:

@yaboiryan

I just want it to paint certain areas… Would I have to make a sort of vector in the GLSL in order to make a note of all the coordinates that have changed? But how would I do that?

You need to figure out which pixels in your texture to affect. And then change that data in your texture.

Perhaps others here will have different suggestions, but there are 2 ways which occur to me that you could approach the problem. . .

The first solution is the one I've used when I've approached this problem in the past. This solution involves going from screen-space, which is the x,y pixel coordinates you'll get from your mouse click, to the 3D space your landscape exists in. You can read about this process at this link here. If you apply this process correctly, you should get a picking ray (which is as if you shot a bullet out of your screen, into your 3D world at the same location as your mouse click). The next step is to figure out how your picking ray relates to the uv coordinates in your landscape mesh. Which in turn will tell you which pixels you want to affect. This can be easier, or harder, depending on how you've set up your landscape and the uv coordinates.

Perhaps a somewhat more simple solution is to draw a second version of your landscape to a back buffer. This back buffer will be exactly the same size as your on screen image. But, rather than rendering out the normal image data, you'll render out the uv data from your mesh instead. You could then sample this back buffer (based on the mouse pixel coordinates) to find what your mouse is over in terms of uv space. Which in turn will tell you which pixels you want to affect. (I've never used this method myself, but it seems like it should work)

@Warp9 @fleabay after playing with it for a few hours, I cannot figure out how to get past the issue still. It works with the mouse, but I am stumped at this piece of code:

	rTextureColor = texture(rTexture, tiledCoords) * blendMapColor.r;
	gTextureColor = texture(gTexture, tiledCoords) * blendMapColor.g;
	bTextureColor = texture(bTexture, tiledCoords) * blendMapColor.b;
	
	if((tiledCoords.x - mouseTexCoords.x) < 1)
	{
	if((tiledCoords.y - mouseTexCoords.y) < 1)
	{
	rTextureColor = texture(rTexture, tiledCoords) * 0;
	gTextureColor = texture(gTexture, tiledCoords) * 1;
	bTextureColor = texture(bTexture, tiledCoords) * 0;
	}
	}

I have the blend map set up and it reads the different values. What I am trying to do here is make it so that it edits those values wherever the mouse is. I subtracted the values here so that it will create sort of an area where the pixels are changed… but instead of that, I get exactly what is in the photo I posted in my last reply.

This topic is closed to new replies.

Advertisement