How do you load an image and convert it to a texture?

Does anyone know how to load an image and convert it to a texture that is usable with IRenderer::DrawImage()?

Hi @alatnet,

You can use the function IRenderer::EF_LoadTexture( TextureName, LoadFlags ), which will return an ITexture pointer. You can then use the member function ITexture::GetTextureID() and use that as the ‘texture_id’ function parameter for DrawImage.

I hope that helps!

um… looking at that function, does it accept a file name or is it for just a texture name?

cus looking at the EF_LoadImage function, that one says that it loads from a file name.

EDIT: Looking at the flags for EF_LoadTexture, there’s a flag called “FT_FROMIMAGE”, does that allow loading from a filename?

EDIT2: Trying to figure out how to use it, looks like it wont let me use the functions for either classes because the inteligent thing that comes with vs2013 is saying that i cant use a pointer to an incomplete class. along with that, i cant even include the CImage.h or CTexture.h headers so that I could possibly fix that.

EDIT3: Well… looking at the LyShineDebug.cpp file, in hopes to figure out how to deal with lyshine in c++, once i get past the whole pointer is incomplete thing, i could possibly load the image via EF_LoadImage then use the DownLoadToVideoMemory function to create a texture.

Hello @alatnet!

You might be trying to construct the ITexture on the stack, which won’t work (might be the source of your incomplete class error. Hard to say without seeing the code). Here is some example code to load an image:

ITexture* myTexture = gEnv->pRenderer->EF_LoadTexture(“MyTextureFileName”, FT_DONT_STREAM);

The don’t stream flag tells the engine not to stream the texture in but to load it all at once (including mips). You don’t have to put an extension on the file name as the resource system will look for .dds files automatically for textures. Then as Jordan pointed out you can use GetTextureID() on myTexture to get the ID and pass that into the various rendering calls that require a texture ID.

You will need to include <CryCommon/ITexture.h> to get the definition for ITexture, <CryCommon/ISystem.h> to get the reference to gEnv pointer and <CryCommon/IRenderer.h> to get the IRenderer definition.

I hope this helps!

ah, i havent really done much coding. basically researching before i get deep into coding to figure out stuff.

but this is when you create a new game project. the includes are already included so it’s no problem with creating an ITexture variable but apparently trying to use the functions of the ITexture variable will cause the pointer to incomplete class thing.

I think it might be because ITexture is an interface (struct) rather than a class or something. Shouldnt really be doing this kind of thing anyway… really weird.

Anyway, as for loading a texture, can i specfiy a path or does the texture need to be in a specific spot?

EDIT: Ack. it was IImage that was giving me the pointer to incomplete class. my bad.

EDIT2: Ok, so I can specify a path and it does load an image (after i used the resource compiler to convert it to dds, dont know if it will load a png) but apparently it’s not drawing anything to the screen when i use:

this->iRenderer->Draw2dImage(40, 40, 50, 50, this->testTex->GetTextureID());

So… any clue here?

Here’s my entire update function:

    int TitleGame::Update(bool hasFocus, unsigned int updateFlags)
{
const float frameTime = gEnv->pTimer->GetFrameTime();
const bool continueRunning = m_gameFramework->PreUpdate(true, updateFlags);
//draw commands here
this->iRenderer->Draw2dLabel(10, 10, 2, this->textColor, false, "TEST");
this->iRenderer->Draw2dLabelWithFlags(30, 30, 2, this->textColor, eDrawText_2D | eDrawText_FixedSize, "TEST2");
this->iRenderer->Draw2dImage(40, 40, 50, 50, this->testTex->GetTextureID());
m_gameFramework->PostUpdate(true, updateFlags);
//gEnv->pSystem->RenderBegin();
//gEnv->pSystem->RenderEnd();
return static_cast<int>(continueRunning);
}

eh… in terms of the include files, not really.
StdAfx.h already includes ISystem.h, and I3DEngine.h (Which also includes IRenderer.h and IMaterial.h (Which I also assume that includes ITexture.h)).

Taking a look at the various include files and I don’t see where ITexture.h would be included (it is not in the stdafx.h in the Cry3DEngine directory) and is not included in the other I*.h files (Render,Material,System). So there is a chance that you may have to explicitly include it. :slight_smile:

If you would post the code you are using to load a texture that would help Jordan and I help figure out what is going on. Also include the directory path of the file you are adding/modify as well.

this->testTex = this->iRenderer->EF_LoadTexture("Images\\Button\\circle.dds", FT_DONT_STREAM);

That’s pretty much it for how im loading the picture. I did convert it to a dds file using the resource processor (apparently right clicking on an image and you get menu options for it, who knew?).

Im going to see if it’s like how the text is. I had to scale the text down from 12 to 2 cus it was freaking huge at 12, dont know why though…

EDIT: Nope, scalling didnt work.

The engine only loads images with a .dds extension (though it may not be a dds file) and the AssetProccessor is needed to build your raw image into the correct texture format for the platform you are building for. As long as you save your images in the data folder for the project the AssetProcessor will compile it and put it in the right place for the engine to load it. So you don’t need to specify paths for the file name. :slight_smile:

As for your current issue in that the texture is not rendering - I’m not sure yet. I’ll dig around the code to see if something jumps out to me for the cause of your problem. If you could also include the code that you use to load the texture that would be helpful.

Sorry for the long wait in the reply.

I have reproduced the issue you are seeing locally and will be debugging in as I try to finish up my other work. You could try using the IDraw2d class in the LyShine module to render the texture. An example of how it is used can be seen in the RenderSingleQuad in the UiImageComponent.cpp file in the LyShine module. It does things a little differently that may work.

well i did grab an IDraw2d from DrawHelper::GetDraw2d().

then tried

this->draw2d->DrawImage(this->testTex->GetTextureID(), { 50, 50 }, { 50, 50 });

but that didnt draw anything to the screen either… so… yea…
trying to figure out how to get an image to draw is… challenging… to say the least…

Awesome that you got it to work!

God damnit…

Got the image to draw.

		this->draw2d->BeginDraw2d(true);
this->draw2d->DrawImage(this->testTex->GetTextureID(), { 50, 50 }, { 50, 50 });
this->draw2d->DrawText("test3 \u0444", { 40, 40 }, 2);
this->draw2d->EndDraw2d();<br>

Image drawing works but text… not so much. Still working on that in another thread for unicode text.

But hey, got the damn thing to draw!

EDIT:

so… apparently that will also draw text, unicode doesnt work correctly yet (want to use msgothic font for that). Had to up the text size to get something to show so… yea…