[osg-users] IVE writer repeatedly saves identical textures (osgconv)

37 views
Skip to first unread message

Andrew Lett

unread,
Feb 18, 2009, 1:30:09 PM2/18/09
to osg-...@lists.openscenegraph.org
Hello everyone!

I've been using 'osgconv' to make native ive binary versions of osg ascii
files. However, if a texture is used more than once, it is resaved each time it
is used. For your reference, I've created a sample file called 'billboards.zip'
which you can download from the following URL:
http://www.geocities.com/andrlet/index.html

So I traced through the code, and found that 'write' in
'osgPlugins/ive/Textures2D.cpp' doesn't check to see if the texture has already
been written out. So here I put in a check to make sure that the texture data is
written out only the first time (see code example below), and subsequently only
references to the texture filename are written out.

Unfortunately this didn't solve my problem, as the texture only appears once in
the first child Geode (where it is saved), and not in subsequent children.

Is there another solution which can prevent repeat saves of the same texture?
(I've thought about modifying the ive loader too but would like to avoid that).

All suggestions are very much welcome.

Best regards,
- Andrew


// modified part of Texture2D.cpp, from osgPlugins/ive (replace ::write)

std::set<std::string> savedFileNames;
void Texture2D::write(DataOutputStream* out){
// Write Texture2D's identification.
out->writeInt(IVETEXTURE2D);
// If the osg class is inherited by any other class we should also write
this to file.
osg::Texture* tex = dynamic_cast<osg::Texture*>(this);
if(tex){
((ive::Texture*)(tex))->write(out);
}
else
throw Exception("Texture2D::write(): Could not cast this osg::Texture2D
to an osg::Texture.");
// Write Texture2D's properties.
// Write image.

// Should we include images date in stream
IncludeImageMode includeImg = out->getIncludeImageMode();

// get filename for texture image, and write out data only once
osg::Image *myImage = getImage();
std::string imgName = myImage->getFileName();

if (savedFileNames.find(imgName) == savedFileNames.end()) {
savedFileNames.insert(imgName);
} else {
includeImg = IMAGE_REFERENCE_FILE;
}
out->writeChar(includeImg);
out->writeImage(includeImg,getImage());
}

_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Andrew Lett

unread,
Feb 18, 2009, 5:03:04 PM2/18/09
to osg-...@lists.openscenegraph.org

One follow-up on this topic - a workaround.

If I also modify the IVE loader, then I can load binary IVE files that only have
a single instance of each texture saved, as documented in my previous post, and
have the texture correctly appear in all subsequent children. I've added a
single line to the loader as documented at the end of this message. So this is a
'solution' to my immediate need.

However, a few questions for the experts: is this a good solution, or was the
IVE save and load meant to work differently? Also, would multiple saves of a
single texture in an IVE file be considered a bug or just an oversight? A full
solution might also require the modification of the all of the Texture*cpp files
in the ive plugin.

Kind regards,
- Andrew

// 'workaround'
// modify DataInputStream.cpp in osgPlugins/ive as follows
// add single line with comment NEW!
osg::Image* DataInputStream::readImage(IncludeImageMode mode)
{
switch(mode) {
case IMAGE_INCLUDE_DATA:
// Read image data from stream
if(readBool())
{
osg::Image* image = new osg::Image();
((ive::Image*)image)->read(this);
_imageMap[image->getFileName()] = image; // NEW! - also save filename
return image;
}
break;
...

Steven Saunderson

unread,
Feb 18, 2009, 11:58:31 PM2/18/09
to osg-...@lists.openscenegraph.org

Andrew Lett wrote:
> Is there another solution which can prevent repeat saves of the same texture?

Can you change your scene (or whatever generates the .osg files) so it uses the same texture multiple times instead of multiple textures that just happen to contain the same image file ?

I do this and the .osg output file is full of "UseTexture_??" and "UseArray_??" for the co-ords but the texture specs and image file name appear only once.

------------------------
-- Steven Saunderson

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=7004#7004

Andrew Lett

unread,
Feb 19, 2009, 9:56:04 AM2/19/09
to osg-...@lists.openscenegraph.org
For all interested parties, I have my solution posted on the Internet at the
following URL (it's for OSG2.6):

http://www.geocities.com/andrlet/ive_plugin_fix.zip

Basically all that needs to be modified are IVE loader/save files
DataInputStream.cpp and DataOutputStream.cpp

Only the first instance of a texture will be saved in the IVE writer with this
mod; currently the IVE writer appears to have a bug which saves the same texture
multiple times if this texture is in different children (Geode). The loader had
to also be modified (check if texture is already loaded).

Kind regards,
- Andrew

Tomlinson, Gordon

unread,
Feb 19, 2009, 9:59:23 AM2/19/09
to OpenSceneGraph Users
Hi Andrew

To get this fix as a candidate inclusion in OSG see
http://www.openscenegraph.org/projects/osg/wiki/MailingLists/Submissions
Protocol


Gordon

__________________________________________________________
Gordon Tomlinson

Product Manager 3D
Email : gtomlinson @ overwatch.textron.com
__________________________________________________________
(C): (+1) 571-265-2612
(W): (+1) 703-437-7651

"Self defence is not a function of learning tricks
but is a function of how quickly and intensely one
can arouse one's instinct for survival"
- Master Tambo Tetsura

Robert Osfield

unread,
Feb 19, 2009, 10:01:58 AM2/19/09
to OpenSceneGraph Users
Hi Andrew,

If you feel the changes are appropriate for merging with svn/trunk
could you post your changes to osg-submissions. I'll do a review from
there. osg-submissions exists as both an avenue for sending in
submissions in a list that is entirely focused on it so changes aren't
lost, and also a permanent record thanks to the archives.

Cheers,
Robert.

Andrew Lett

unread,
Feb 19, 2009, 10:05:38 AM2/19/09
to osg-...@lists.openscenegraph.org
Steven Saunderson <osgforum@...> writes:
>
> Can you change your scene (or whatever generates the .osg files) so it uses
the same texture multiple times
> instead of multiple textures that just happen to contain the same image file ?
>

Unfortunately, no, I cannot put all of the data into a single child (Geode). I
use metadata in the form of the name of the node for other purposes.
So I am using the same texture filename, in more than one Geode. The data
actually comes via the 3DS converter, organized in layers.

Interestingly enough, the ASCII OSG file loader which references a texture file
actually does check to see if the texture file is loaded before attempting to
load it. This isn't done with the IVE loader which blindly loads the same
texture data multiple times.

- Andrew

Reply all
Reply to author
Forward
0 new messages