TextureLoader.java - GravityGames/scorpio3d GitHub Wiki

TextureLoader.java

TextureLoader.java is Scorpio Engine's main texture loading class. It is responsible for converting .png files, as well as Scorpio Engine's custom file formats, .obg and .tbg, into a format readable by OpenGL 3.0. Adding formats to be implemented officially into Scorpio Engine should be pushed as additions to this file. Adding formats for private use should NOT use this file, to keep added code safe for updates.

Loading .png files

Loading .png files requires two parameters: the name of the .png file to be loaded (don't forget to include the path to the file as well), and which OpenGL Texture Unit the texture will use. As an example, if you want to include a image file in the assets subfoldel called "Image", and bind it to texture unit 0, you would use this line of code to bind it to an int value.

int texture = TextureLoader.loadPNG("assets/image.png", GL13.GL_TEXTURE0);

Then using the texture is a simple as adding a MeshComponent with the desired mesh object and texture to the game object you wish to use:

addComponent(new MeshComponent(texturedMesh, texture));

This should be more than enough for pixel art styled games, but if you want your game to use higher resolution graphics, you may want to use OpenGL's filtering modes. To use them in Scorpio, you'll need to add two more parameters to your method call, like so:

int texture = TextureLoader.loadPNG("assets/image.png", GL13.GL_TEXTURE0, "GL_LINEAR", "GL_LINEAR_MIPMAP_LINEAR");

The first string is the near filter, and the second string is the far filter. All of the OpenGL filter modes are valid, but for reference will be noted here:

GL_NEAREST
GL_LINEAR
GL_NEAREST_MIPMAP_NEAREST
GL_NEAREST_MIPMAP_LINEAR
GL_LINEAR_MIPMAP_NEAREST
GL_LINEAR_MIPMAP_LINEAR

(Please note: Only the first two will work as the near filter.) When an invalid string is given, Scorpio will use GL_NEAREST as the default filter mode. When any of the last four filter modes are used for the far filter, mipmaps will automatically be generated.

Loading .obg files

Loading .obg files is very similar to loading .png files. OBG files can be loaded with either two or three parameters: the name and path of the .png file to be loaded (required), which OpenGL Texture Unit the texture will use (required) and which ScorpioPalette to use (optional). OBG files require a palette to be generated, as unlike .PNG files, which hold all of the data required to create a pixel in four bytes, .OBG files use just one bit to hold pixel information. This allows for massive amounts of space to be saved for images that require only two colors (or one + transparency, as in the case of most games will be more common), but requires the color data for each pixel to be stored separately. However, this palette is not always required, .OBG files can include an internal palette for use (though none of these .OBG files have yet been used in games by Gravity Games Interactive). To override this, use a custom ScorpioPalette. If no ScorpioPalette is used, and the .OBG contains no internal palette, the image will still be loaded with some default colors, pure white for bits of value 0, and pure black for bits of value 1.

Here is an example of usage:

int texture = TextureLoader.loadOBG("assets/image.obg", GL13.GL_TEXTURE0, palette);

The near filter and far filter parameters are also available for .obg files, but you need to put them before the palette parameter.

int texture = TextureLoader.loadOBG("assets/image.obg", GL13.GL_TEXTURE0, "GL_LINEAR", "GL_LINEAR_MIPMAP_LINEAR", palette);

Loading .tbg files

Loading .tbg files is nearly identical to loading .obg files, with the only difference being the default palette used and how much data is used for each pixel. From a coding standpoint, there will be virtually no difference, outside of calling "loadTBG" rather than "loadOBG". Keep in mind that that despite the similarities, you can not load .tbg files properly with "loadOBG" and vice versa.

Here is an example of usage:

int texture = TextureLoader.loadTBG("assets/image.tbg", GL13.GL_TEXTURE0, palette);

The near filter and far filter parameters are also available for .tbg files, but you need to put them before the palette parameter, just like with .obg files.

int texture = TextureLoader.loadTBG("assets/image.tbg", GL13.GL_TEXTURE0, "GL_LINEAR", "GL_LINEAR_MIPMAP_LINEAR", palette);