Horde3D

Next-Generation Graphics Engine
It is currently 29.03.2024, 06:29

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Texture swap and reload
PostPosted: 20.08.2009, 15:57 
Offline

Joined: 16.05.2009, 12:43
Posts: 207
Hey all.

There's a couple of features I'd like to implement for my artists. The first is the ability to swap textures for a given submesh of a model on the fly. The second is to actually refresh the loaded texture from the file.

Is there a preferred method for switching textures and/or updating them from the original file? I seem to recall reading on the forums some plan to have hot-reloading of assets, or am I wrong?

Any pointers greatly received, otherwise I'll just have a hack at it.


Top
 Profile  
Reply with quote  
PostPosted: 20.08.2009, 20:20 
Offline
Engine Developer

Joined: 10.09.2006, 15:52
Posts: 1217
You can switch the texture by accessing the mesh material with the resource API.

Hot reloading of resources is possible and in fact done by the Horde Editor. You just need to unload a resource with h3dUnloadResource and after that you can load it again.

Resource unloading does not work well for all resource types. Models for example are directly merged into the scene graph, so there will not be an effect for the current scene if you reload the scene graph file. It can also be problematic if you reload a geometry resource that has a different structure than before (e.g. less vertices) because then some vertex indices could become invalid. However, reloading textures and shaders as well as some other resource types should not be a problem.


Top
 Profile  
Reply with quote  
PostPosted: 21.08.2009, 16:53 
Offline

Joined: 16.05.2009, 12:43
Posts: 207
Its mainly textures that I'm thinking about. Sounds like I need to really figure out how to use the Resource API then. I'm not having much luck with it right now (keeps returning nothing for my scene resource).


Top
 Profile  
Reply with quote  
PostPosted: 07.02.2010, 10:36 
Offline

Joined: 18.09.2009, 07:44
Posts: 17
Did you get anywhere with this zoombapup?

I'm a little stuck on a similar task, I want to change a model's current texture or add a texture where there was not one previously.

This is the code I'm using (C# bindings):

Code:
var resId = h3d.createTexture(name, width, height, (int)h3d.H3DFormats.TEX_BGRA8, 0);
var ptr = h3d.mapResStream(resId, (int)h3d.H3DTexRes.ImageElem, 0, (int)h3d.H3DTexRes.ImgPixelStream, false, true);
Marshal.Copy(bytes, 0, ptr, bytes.Length);
h3d.unmapResStream(resId);
h3d.setResParamI(materialID, (int)h3d.H3DMatRes.SamplerElem, 0, (int)h3d.H3DMatRes.SampTexResI, resId);

My byte array in this case is a JPG image.

Some questions:
1. For the case where the material doesn't already reference a texture, it looks like I need to create a sampler or create an entire new material?
It hits this condition in egMaterial.cpp
Code:
if( (unsigned)elemIdx < _samplers.size() )

2. Updating an existing texture results in corruption, I suppose there is something wrong with my code?
Attachment:
overwrite-existing-texture.PNG
overwrite-existing-texture.PNG [ 112.15 KiB | Viewed 13381 times ]


Attachments:
box.zip [733 Bytes]
Downloaded 686 times
Top
 Profile  
Reply with quote  
PostPosted: 07.02.2010, 11:01 
Offline

Joined: 16.05.2009, 12:43
Posts: 207
I ended up just swapping materials with h3dSetNodeParamI(iNode,H3DMesh::MatResI,Material);

Remember you will need to recurse the model (different parts may have different materials).

So I have a "normal" material for each object, which I grab at initialisation time. Then I swap that in and out with a highlight material.

So I never really figured out the texture swapping.


Top
 Profile  
Reply with quote  
PostPosted: 09.02.2010, 06:42 
Offline

Joined: 18.09.2009, 07:44
Posts: 17
Thanks for the reply, it looks like I might need to do similar.

I did find this entry in the wiki, but I still end up with the texture corruption: http://horde3d.org/wiki/index.php5?titl ... _of_a_mesh


Top
 Profile  
Reply with quote  
PostPosted: 10.02.2010, 00:53 
Offline
Engine Developer

Joined: 10.09.2006, 15:52
Posts: 1217
craigomatic wrote:
I did find this entry in the wiki, but I still end up with the texture corruption

Did not check that but it is well possible that the C# bindings have an issue regarding the returned pointer. I guess this part of the bindings was never tested. We really need some more regression tests for these API functions which are used less frequently ;)


Top
 Profile  
Reply with quote  
PostPosted: 04.03.2010, 01:45 
Offline

Joined: 18.09.2009, 07:44
Posts: 17
marciano wrote:
craigomatic wrote:
I did find this entry in the wiki, but I still end up with the texture corruption

Did not check that but it is well possible that the C# bindings have an issue regarding the returned pointer. I guess this part of the bindings was never tested.

Perhaps I was being naive, there is a good chance the texture I was trying to swap in is of a different size to the original texture. Is this scenario supported?

Quote:
We really need some more regression tests for these API functions which are used less frequently ;)

Are there any unit tests for horde available in the repository somewhere?


Top
 Profile  
Reply with quote  
PostPosted: 04.03.2010, 10:32 
Offline
Tool Developer

Joined: 13.11.2007, 11:07
Posts: 1150
Location: Germany
craigomatic wrote:
Perhaps I was being naive, there is a good chance the texture I was trying to swap in is of a different size to the original texture. Is this scenario supported?

If you want to reload a texture that does not match the properties of the previous one, you have to unload the texture resource and reload it again with the different texture, as it is done by the editor.
Code:
   
// Find resource handle
H3DRes resource = h3dFindResource(H3DResTypes::Texture, textureFileName );
// Mark it invalid
h3dUnloadResource(resource);
// Reload it again (adjust your loading path or use h3dLoadResource instead of using the utility wrapper)
h3dutLoadResourcesFromDisk(".");

If you want to use h3dMapResStream the resource data's properties have to match with the previous ones.
craigomatic wrote:
Are there any unit tests for horde available in the repository somewhere?

Unfortunately not yet.


Top
 Profile  
Reply with quote  
PostPosted: 16.03.2010, 09:08 
Offline

Joined: 10.04.2008, 09:13
Posts: 86
Replacing (part of) a texture is done in this code for Flash rendering, it goes something like this (for TEX_BGRA8 type):
Code:
void render() {
      if(this->dirty) {
         UINT bytes = flashRect.right * flashRect.bottom * sizeof(UINT);
         RECTL rectl;
         rectl.top = 0;
         rectl.left = 0;
         rectl.right = flashRect.right;
         rectl.bottom = flashRect.bottom;
         memset(frame_buffer,0,bytes);
         CHECKHR(flashViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL, NULL, this->hdcCompatible, &rectl, NULL, NULL, NULL));         
         // for partial replacement read flag is needed.
         UINT* data = (UINT*)h3dMapResStream(this->textureRes, H3DTexRes::ImageElem, 0, H3DTexRes::ImgPixelStream, useRead, true );
         // copy buffer with flipped y coordinates
          UINT i = 0;
         for(LONG y=targetRect.bottom-1;y>=targetRect.top;y--) {
            UINT offsetY = y*this->texWidth;
            for(LONG x=targetRect.left;x<targetRect.right;x++) {
               data[x+offsetY] = frame_buffer[i];
               i++;
            }
         }
      //   printf("copied: %d pixels total: %d\n",i,flashRect.right*flashRect.bottom);
         h3dUnmapResStream(this->textureRes);
         this->dirty = false;
      }
   }

Please look here for more information


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group