Skip to main content
GameDev.net gamedev.net
🔒 Locked

Copying textures

Started by BloodLust666 Oct 20, 2005 at 12:47 PM 10 replies 850+ views
Original Post
BloodLust666
BloodLust666
I have one texture that (with code) i extract different tiles out of and draw them in a certain order to make a map. Is there anyway i could load the texture, store the tile numbers as data, then draw onto a NEW texture and just draw that one texture? instead of iterating through the tiles and drawing them each separately. edit: C++ by the way
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
matthughson
matthughson
EDIT: I just reread your post and I'm not sure that this is what you were asking.

If you are drawing the mesh as a triangle list, you can just store the UVs for each vert, to properly represent the part of the texture you want to draw.

For example say you have a base texture with 2 tile textures in it (beside each other horizontally). You could do some like this:
// Top leftverts[index].u = 0.0f;verts[index].v = 0.0f;// Top Rightverts[index].u = 0.5f;verts[index].v = 0.0f;// Bottom leftverts[index].u = 0.0f;verts[index].v = 1.0f;// Bottom Rightverts[index].u = 0.5f;verts[index].v = 1.0f;

This would use the tile texture on the left. If you want to draw the texture on the right you would change all the 0.0f to 0.5f and all the current 0.5f to 1.0f.

Hope that makes sense. If you need me to explain a little more, just let me know.

Matt
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
BloodLust666
BloodLust666
no, that's not what i mean :-P...

ok, it's a 2D game. i have a texture of say about 16 tiles (4 by 4). i have a 2D array short for keeping the data, the actual map is about 100x100 tiles. my array keeps numbers for every tile that is to be extracted from the texture. everytime i render the map, it goes through every value in the array and draws that tile in the correct spot.

i was wondering if there's a way to iterate through the array ONCE and draw the big map to ANOTHER texture (or surface or something) so when i draw the map, i only have to draw that one texture (or wherever else i saved the tiles to)

hope this makes more sense
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
BloodLust666
BloodLust666
ok basically all i need to do is take a texture from an IDirect3DTexture9 and draw it onto another IDirect3DTexture9 OR any other interface so i can use that to draw to the screen instead of the original texture.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
sirob
sirob
You basicly want to use a texture to store stuff you already rendered, so you don't have to redo a costly proccess every time you want to draw?

If so, look into Render Targets. You can make a texture a render target, thus allowing you to render directly to it. Then, you can render using the texture that was the render target, onto the back buffer. You don't have to perform the first render-to-texture if nothing has changed.

Look up Render Targets in the SDK for more info about it. It's certainly possible.
Sirob Yes.» - status: Work-O-Rama.
BloodLust666
BloodLust666
exactly. ok, so i have the IDirect3DTexture9, that renders to an IDirect3DSurface9, would i draw the surface onto the screen at a certain coordinate?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
sirob
sirob
You don't need to use surfaces directly. You should render a texture to a texture. Textures contain one (or more, as with MIP mapping) surfaces. You need to create a texture with one surface, render to the surface, and then use that texture, to render to the backbuffer.

I understand you're doing 2D, so if you wanted to render the whole screen like this, you'd create a texture big enough to fit your whole screen, and render to it. This is done one, or whenever something needs to be changed.

Then, every time you want to render, you draw the texture you rendered to across the whole screen. This would be much faster if you don't change too much too often.

Hope this helps.
Sirob Yes.» - status: Work-O-Rama.
BloodLust666
BloodLust666
ok, i get it. how would i render to a texture surface? can someone show me some code?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
BloodLust666
BloodLust666
is this correct?

pTex // is my texture with the tiles
pTexMap // is the big map where i want to render the tiles to

pTex->GetSurfaceLevel(0, surface)

then do...

Device->UpdateSurface(pTexMap, SomeRect, pTex, SomeOtherRect);

... to copy the tiles to the big map?

(i would try it, but i'm not at home...)

edit: b4 i UpdateSurface, i would have to do:
device->CreateTexture(blah...) and that would create a blank texture right?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
BloodLust666
BloodLust666
can someone verify?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
neneboricua19
neneboricua19
Using UpdateSurface will be slow because your source surface needs to be in system memory while your destination surface needs to be in video memory.

What sirob was saying was to render your primitives to a texture. So first, you need to create a texture that can be used as a render target...
LPDIRECT3DTEXTURE9 pTargetTexture = NULL;LPDIRECT3DSURFACE9 pTargetSurface = NULL;D3DXCreateTexture( pDevice, width, height, 0, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pTargetTexture );pTargetTexture->GetSurfaceLevel(0, &pTargetSurface );


Then, in your render loop, you need to set this texture as the render target...
pDevice->BeginScene();LPDIRECT3DSURFACE9 pBackBuffer = NULL;pDevice->GetRenderTarget(0, &pBackBuffer );pDevice->SetRenderTarget(0, pTargetSurface);// call Draw*Primitive here.  Everything will be rendered to the texture.// Set the the render target to the back buffer and the source texture to the// surface we just rendered to.pDevice->SetRenderTarget(0, pBackBuffer);pDevice->SetTexture(0, pTargetTexture);// Call Draw*Primitive here.  All primitives drawn can now use the texture you// previously rendered into.  It is set as the texture in sampler 0.// Make sure for next frame, that pTargetTexture isn't set as the active// texture because a texture cannot be set as both the source and the// destination at the same time.pDevice->SetTexture(0, NULL);SAFE_RELEASE(pTargetSurface);pDevice->EndScene()

Hope this helps,
neneboricua
BloodLust666
BloodLust666
ok thanks, i'll try that :) if i have problems i'll post again
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.