All pixel-based images in Polycode are stored in Textures, which can be loaded from image files or created manually. They can be assigned as simple flat textures to SceneMesh instances or used in material shaders (see Materials).

Contents

Textures and images overview.

The Texture class is the main way that image textures are stored and manipulated in Polycode. There are a number of ways to create a texture, though all of them are done via the MaterialManager, which you can access via the CoreServices singleton . Textures can be either loaded from file or created manually via an Image class or directly from memory data in C++.

Loading textures from file.

The easiest way to create a texture from an image is to use the createTextureFromFile method and pass it the path to the image file you want to load. Polycode supports only the PNG file format.

If you try to load the same image twice using createTextureFromFile into the same resource pool, it will not load the file again, and instead return the already loaded textures from that resource pool.

C++
MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
Texture *texture = materialManager->createTextureFromFile("dummy.png", true, true);
Lua
texture = Services.MaterialManager:createTextureFromFile("dummy.png", true, true, Services.ResourceManager:getGlobalPool())
The first argument is the path to the image that we are loading. The second argument is a boolean that defines whether this texture will clamp or repeat and the third argument is a boolean that controls whether mipmaps are created for this texture. The last argument (which you can skip in C++, but is required in Lua), is the resource pool to load the texture into, which we set to the defaul global resource pool .

Creating via the Image class.

Polycode provides an Image class to create and manipulate image data in memory. It provides the ability to set individual pixels, copy image parts and otherwise manipulate image data.

Once you create an Image, you can use the createTextureFromImage method of the MaterialManager to create a texture from the image.

C++
Image *customImage = new Image(16, 16, Image::IMAGE_RGBA);
for(int x=0; x < 16; x++) {
	for(int y=0; y < 16; y++) {
		customImage->setPixel(x, y, ((Number)x)/16.0, ((Number)y)/16.0, 1.0, 1.0);
	}
}
MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
Texture *texture = materialManager->createTextureFromImage(customImage, true, true);
Lua
customImage = Image.BlankImage(16, 16, Image.IMAGE_RGBA)
for x=0,15 do
	for y=0,15 do
            customImage:setPixel(x, y, x/16.0, y/16.0, 1.0, 1.0)
	end
end
texture = Services.MaterialManager:createTextureFromImage(customImage, true, true)

Setting textures to SceneMesh.

You can set textures to SceneMeshes as simple color textures that bypass the material system. You can do this to display images to the screen or load textures on models when shading is not required.

C++
Scene *scene = new Scene(Scene::SCENE_2D);
ScenePrimitive *imageRect = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 0.5, 0.5);
scene->addChild(imageRect);
imageRect->setTexture(texture);
Lua
scene = Scene(Scene.SCENE_2D)
imageRect = ScenePrimitive(ScenePrimitive.TYPE_VPLANE, 0.5, 0.5)
scene:addChild(imageRect)
imageRect:setTexture(texture)