Error on loading and using Ogre::TexturePtr

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
User avatar
robert_sasu
Google Summer of Code Student
Google Summer of Code Student
Posts: 237
Joined: Mon Apr 02, 2012 11:07 am
x 42

Error on loading and using Ogre::TexturePtr

Post by robert_sasu »

I am trying to load a texture from resources and I get an error of Object reference not set to an instance of an object on the line 12 (from the pasted code). The strange thing is that the loading part goes well, nothing in log (I am using ogreLogLevel=High) and on that line where I am writing if the resources exists or not I get TRUE for the 2 textures I am trying to open.

I might miss out something, but I do not have any idea what. I need a little help here :)

Code: Select all

	void DataManager::addTextures(const Ogre::String& destinationTextureName, const Ogre::String& sourceTextureName, const Ogre::String& outputFileName)
	{
		Ogre::ResourceGroupManager::getSingletonPtr()->loadResourceGroup(SKYX_RESOURCE_GROUP);
		TraceWriteLine("EXISTS {0} {1}",Ogre::ResourceGroupManager::getSingletonPtr()->resourceExists(SKYX_RESOURCE_GROUP,destinationTextureName),
			                            Ogre::ResourceGroupManager::getSingletonPtr()->resourceExists(SKYX_RESOURCE_GROUP,sourceTextureName));
		Ogre::TextureManager *textureManager = Ogre::TextureManager::getSingletonPtr();
		assert(textureManager != NULL);

		const Ogre::TexturePtr& destinationTexture = textureManager->getByName(destinationTextureName, SKYX_RESOURCE_GROUP);
		const Ogre::TexturePtr& sourceTexture = textureManager->getByName(sourceTextureName, SKYX_RESOURCE_GROUP);

		Ogre::HardwarePixelBufferSharedPtr destinationBuffer = destinationTexture->getBuffer(0,0);
		Ogre::HardwarePixelBufferSharedPtr sourceBuffer = sourceTexture->getBuffer(0,0);

Thanks,

Robert
Google Summer of Code 2013 Student
Topic: "DirectX 11 & Tessellation samples"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Assaf Raman
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Error on loading and using Ogre::TexturePtr

Post by bstone »

A sneaky bug in your code:

Code: Select all

      const Ogre::TexturePtr& destinationTexture = textureManager->getByName(destinationTextureName, SKYX_RESOURCE_GROUP);
      const Ogre::TexturePtr& sourceTexture = textureManager->getByName(sourceTextureName, SKYX_RESOURCE_GROUP);
and the fix is simple:

Code: Select all

      const Ogre::TexturePtr destinationTexture = textureManager->getByName(destinationTextureName, SKYX_RESOURCE_GROUP);
      const Ogre::TexturePtr sourceTexture = textureManager->getByName(sourceTextureName, SKYX_RESOURCE_GROUP);
User avatar
robert_sasu
Google Summer of Code Student
Google Summer of Code Student
Posts: 237
Joined: Mon Apr 02, 2012 11:07 am
x 42

Re: Error on loading and using Ogre::TexturePtr

Post by robert_sasu »

That change does not resolve my problem, I also tried to use just Ogre::TexturePtr, without const, but the same error happens every time.
Google Summer of Code 2013 Student
Topic: "DirectX 11 & Tessellation samples"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Assaf Raman
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Error on loading and using Ogre::TexturePtr

Post by bstone »

Looks like I was wrong. A const reference to a pure rvalue is okay, it just looks suspicious and I usually don't see code like that. So the problem is something else.
User avatar
robert_sasu
Google Summer of Code Student
Google Summer of Code Student
Posts: 237
Joined: Mon Apr 02, 2012 11:07 am
x 42

Re: Error on loading and using Ogre::TexturePtr

Post by robert_sasu »

It is just a few line code here, and I do not see where is the error, as the resource truly exists. And this would really help me to optimize a big part of the code, and I need to load those textures (there are just .dds files saved earlier, and needed to be loaded). I just generated them, and need some modifications, so normally this code will not rule, it is just for creating the textures.
Google Summer of Code 2013 Student
Topic: "DirectX 11 & Tessellation samples"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Assaf Raman
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Error on loading and using Ogre::TexturePtr

Post by bstone »

I guess you'll have to step through that and see where and why the reference loses its object instance.
User avatar
robert_sasu
Google Summer of Code Student
Google Summer of Code Student
Posts: 237
Joined: Mon Apr 02, 2012 11:07 am
x 42

Re: Error on loading and using Ogre::TexturePtr

Post by robert_sasu »

Is there any way to load a texture known by name and resource_group to a Ogre::TexturePtr ?
I do not see how could the object loses its intance between 2 line.

The problem is that the texture loaded by textureManager->getByName(destinationTextureName, SKYX_RESOURCE_GROUP) is NULL. But the resource exists, and on that line I get TRUE for existing resource. But I do not see why I can't load that texture. I am surely missing something, but I do not have any idea.
Google Summer of Code 2013 Student
Topic: "DirectX 11 & Tessellation samples"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Assaf Raman
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Error on loading and using Ogre::TexturePtr

Post by bstone »

There's a lot happening after the two lines, inside the getBuffer() call for e.g.
User avatar
robert_sasu
Google Summer of Code Student
Google Summer of Code Student
Posts: 237
Joined: Mon Apr 02, 2012 11:07 am
x 42

Re: Error on loading and using Ogre::TexturePtr

Post by robert_sasu »

When using the code like this:

Code: Select all

		Ogre::ResourceGroupManager::getSingletonPtr()->loadResourceGroup(SKYX_RESOURCE_GROUP);
		TraceWriteLine("EXISTS {0} {1}",Ogre::ResourceGroupManager::getSingletonPtr()->resourceExists(SKYX_RESOURCE_GROUP,destinationTextureName),
			                            Ogre::ResourceGroupManager::getSingletonPtr()->resourceExists(SKYX_RESOURCE_GROUP,sourceTextureName));

		Ogre::TextureManager *textureManager = Ogre::TextureManager::getSingletonPtr();
		assert(textureManager != NULL);

		const Ogre::TexturePtr sourceTexture = textureManager->getByName(sourceTextureName, SKYX_RESOURCE_GROUP);
		if (textureManager->getByName(sourceTextureName, SKYX_RESOURCE_GROUP).isNull()) TraceWriteLine("SOURCE IS NULL");
I get in the output log file that SOURCE IS NULL, even if that it exists as I get TRUE TRUE on the 2nd line. So it's normal that when calling any method from sourceTexture it will fail, because of not an instance of an Object.
So the problem is that I can't load the resource, while the resource exists. Is there any other method for this ? Or am I missing something ?
Google Summer of Code 2013 Student
Topic: "DirectX 11 & Tessellation samples"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Assaf Raman
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Error on loading and using Ogre::TexturePtr

Post by bstone »

Hmm.. what if the resource exists but Ogre thinks it's not a texture?
User avatar
robert_sasu
Google Summer of Code Student
Google Summer of Code Student
Posts: 237
Joined: Mon Apr 02, 2012 11:07 am
x 42

Re: Error on loading and using Ogre::TexturePtr

Post by robert_sasu »

The resource was saved using this code:

Code: Select all

		Ogre::Image image;
		mVolTextures[0]->convertToImage(image);
		image.save(filename);
Where mVolTextures is Ogre::TexturePtr mVolTextures[2];
Google Summer of Code 2013 Student
Topic: "DirectX 11 & Tessellation samples"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Assaf Raman
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Error on loading and using Ogre::TexturePtr

Post by bstone »

And what codec saves the .dds files? Ogre's DDS codec is only for loading them and doesn't support encoding. Can you open them with some other tools to see if they are correct?
User avatar
robert_sasu
Google Summer of Code Student
Google Summer of Code Student
Posts: 237
Joined: Mon Apr 02, 2012 11:07 am
x 42

Re: Error on loading and using Ogre::TexturePtr

Post by robert_sasu »

Yes, I can open them with any .dds viewer, and they are correct.
Google Summer of Code 2013 Student
Topic: "DirectX 11 & Tessellation samples"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Assaf Raman
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Error on loading and using Ogre::TexturePtr

Post by Kojack »

robert_sasu wrote:When using the code like this:

Code: Select all

		Ogre::ResourceGroupManager::getSingletonPtr()->loadResourceGroup(SKYX_RESOURCE_GROUP);
		TraceWriteLine("EXISTS {0} {1}",Ogre::ResourceGroupManager::getSingletonPtr()->resourceExists(SKYX_RESOURCE_GROUP,destinationTextureName),
			                            Ogre::ResourceGroupManager::getSingletonPtr()->resourceExists(SKYX_RESOURCE_GROUP,sourceTextureName));

		Ogre::TextureManager *textureManager = Ogre::TextureManager::getSingletonPtr();
		assert(textureManager != NULL);

		const Ogre::TexturePtr sourceTexture = textureManager->getByName(sourceTextureName, SKYX_RESOURCE_GROUP);
		if (textureManager->getByName(sourceTextureName, SKYX_RESOURCE_GROUP).isNull()) TraceWriteLine("SOURCE IS NULL");
I get in the output log file that SOURCE IS NULL, even if that it exists as I get TRUE TRUE on the 2nd line. So it's normal that when calling any method from sourceTexture it will fail, because of not an instance of an Object.
So the problem is that I can't load the resource, while the resource exists. Is there any other method for this ? Or am I missing something ?
loadResourceGroup will only load known resources. These are things added automatically by scripts, or manually declared resources. Meshes, skeletons and textures are not known (and will not be loaded by loadResourceGroup) unless declared with ResourceGroupManager::declareResource first (and then the group is initialised), or when you first try to use them (creating an entity with a mesh file works because ogre searches the resource paths for the file if it's not already in the mesh manager).
If you haven't declared those textures, they weren't loaded by the first line.

So why are they shown as true in the second line? Because resourceExists has three steps: 1 - do a case sensitive search of the resource manager. 2 - if that failed, do a case insensitive search. 3 - if both failed, search the hard drive in the resource group's registered paths.
Your files aren't declared or loaded, so they fail the first two checks, but the third succeeds because they do exist on disk.

TextureManager::getByName doesn't go that far, it purely looks at the list of known resources (could be loaded, unloaded, etc, as long as they were declared). So it can't see that the textures are on disk.

You could load them manually into Images (using Image::load) then create a texture from them with TextureManager::loadImage.
Or if they are known at the beginning you could declare them in the ResourceGroupManager so they will be known after initialiseResourceGroup is called (note: each resource group can only be declared once, there's a flag to block later calls. So you can't init the group, then later declare more stuff in it and init again.
scrawl
OGRE Expert User
OGRE Expert User
Posts: 1119
Joined: Sat Jan 01, 2011 7:57 pm
x 216

Re: Error on loading and using Ogre::TexturePtr

Post by scrawl »

Nice answer, Kojack.

I believe that calling TextureManager::load with the texture name should also fix the problem.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Error on loading and using Ogre::TexturePtr

Post by Kojack »

I was going to say that, but while checking the code I thought I saw that it wouldn't work. If a texture isn't known, load will call createOrRetrieve which will create a new empty texture resource of the name given. But what I didn't pay attention to is that the next step of the load method is to load the resource it just created (it was created as unloaded), so it then finds the one on disk with the same name and group and loads that.

So yep, scrawl's solution is better. :)
User avatar
robert_sasu
Google Summer of Code Student
Google Summer of Code Student
Posts: 237
Joined: Mon Apr 02, 2012 11:07 am
x 42

Re: Error on loading and using Ogre::TexturePtr

Post by robert_sasu »

Thank you for your help :) I am in holiday and I will try your methods after 7th of January :)
Google Summer of Code 2013 Student
Topic: "DirectX 11 & Tessellation samples"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Assaf Raman
formula123
Gnoblar
Posts: 7
Joined: Thu Aug 14, 2014 6:23 am

Re: Error on loading and using Ogre::TexturePtr

Post by formula123 »

Nice shot! Solve my problem. Thank you, Kojack.
Post Reply