Horde3D http://www.horde3d.org/forums/ |
|
Texture swap and reload http://www.horde3d.org/forums/viewtopic.php?f=1&t=910 |
Page 1 of 1 |
Author: | zoombapup [ 20.08.2009, 15:57 ] |
Post subject: | Texture swap and reload |
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. |
Author: | marciano [ 20.08.2009, 20:20 ] |
Post subject: | Re: Texture swap and reload |
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. |
Author: | zoombapup [ 21.08.2009, 16:53 ] |
Post subject: | Re: Texture swap and reload |
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). |
Author: | craigomatic [ 07.02.2010, 10:36 ] | ||
Post subject: | Re: Texture swap and reload | ||
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 [ 112.15 KiB | Viewed 23390 times ]
|
Author: | zoombapup [ 07.02.2010, 11:01 ] |
Post subject: | Re: Texture swap and reload |
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. |
Author: | craigomatic [ 09.02.2010, 06:42 ] |
Post subject: | Re: Texture swap and reload |
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 |
Author: | marciano [ 10.02.2010, 00:53 ] |
Post subject: | Re: Texture swap and reload |
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 ![]() |
Author: | craigomatic [ 04.03.2010, 01:45 ] |
Post subject: | Re: Texture swap and reload |
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? |
Author: | Volker [ 04.03.2010, 10:32 ] |
Post subject: | Re: Texture swap and reload |
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. |
Author: | jimbo [ 16.03.2010, 09:08 ] |
Post subject: | Re: Texture swap and reload |
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 |
Page 1 of 1 | All times are UTC + 1 hour |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |