Jump to content
  • Advertisement
Sign in to follow this  
Kurdur

Create new Texture2D by rotating/flipping part of Texture2D

This topic is 2265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I have some 2D textures, each of which I would like to cut a part out of using a sourceRect. The program would then rotate that part or flip it horisontally.
The end result would be some new 2D textures that I can draw on the screen.

Can someone guide me in the right direction?

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I've never really written something like that before, so maybe if someone more experienced comes along they can verify?

Anyways, it would seem like you could just swap the rows and columns of the textures to rotate them by 90 degrees. So for an example 4x4 texture you would make an empty texture. And then loop through the data in the first one. And for each "row" (line of pixels on the X axis) in the first one you would copy that into a "column" in the new texture (a line of pixels on the Y axis). To flip them you should just be able to mirror one set of either rows or columns. So for example the top row would get moved to the bottom, the second row would go to second from the bottom, and so on.

Share this post


Link to post
Share on other sites
This might be easier to accomplish on the GPU - create a temporary render target, draw the old texture on it with the critical section cut out with scissor planes or similar, draw the critical section on top with the transformation applied in the shader, create a new texture out of the render target.

Share this post


Link to post
Share on other sites
Thanks for your ideas!

Both solutions seems quite simple. However, I haven't used neither render targets nor created new textures using information from each pixel before. Can any of you help me by writing a small bit of code portraying the idea? :)

Share this post


Link to post
Share on other sites
I don't have any actual code, but here's some psuedocode showing what I was thinking. I'm going to assume that you can't set pixel values based on X and Y coordinates; just like with plain DirectX.

Texture rotateTexture90Degrees(Texture inputTex)
{
Texture returnValue;

int x;
int y;
for ( x=0; x<inputTex.getWidth(); ++x)
{
for (y=0; y<inputTex.getHeight(); ++y)
{
returnValue.setPixel(x, y, inputTex.getPixel(y, x));
//Or
//returnValue.setPixel(y, x, inputTex.getPixel(x, y));
//I'm not sure whether this last one would work for flipping or not, but here's what I would think...
//returnValue.setPixel(x, y, inputTex.getPixel(x, inputTex.getHeight()-y));
}
}
}



And like hupsilardee said, you could do it on the GPU if you used render target textures. It would have the advantage of being faster, and you could stretch, and squash, and rotate by arbitrary angles.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!