-
Notifications
You must be signed in to change notification settings - Fork 0
Texturing the Rectangle
OpenGL stores asset images in memory as collections of color data called textures. Each texture is created, allocated the space it needs, assigned the color data of the asset image, and then bound for rendering. Once rendering is reached, the texture can be "sampled," where portions of the texture are applied to geometry on screen. Luckily, this engine handles most of this process for you.
Textures can be loaded from file or classpath in a single line. This performs the texture creation and allocation for you automatically. Textures can be created like so:
// Load a texture from a file
Texture fromFile = Texture.load(Resource.fromFile("assets/textures/weapon.png");
// Load a texture from the classpath
Texture fromClasspath = Texture.load(Resource.fromFile("textures/weapon.png");
Assuming there is a texture in the working directory at assets/textures/weapon.png
or in the classpath at textures/weapon.png
, this texture is now ready for rendering! Simply bind it before rendering your rectangle.
// . . .
// Bind the texture for rendering
texture.bind();
// Render the textured quad
renderer.drawRectangle(-0.5f, -0.5f, 1.0f, 1.0f);
// . . .
The centered rectangle should now appear as the specified weapon texture on screen.
The texture loader can also handle square animated GIFs. Address the GIF just like a PNG, and no additional work needs to be done. The rectangle will now be animated with the GIF.
// In the initialization:
animated = Texture.load(Resource.fromFile("textures/weapon.gif");
// In the updating:
animated.bind();
renderer.drawRectangle(-0.5f, -0.5f, 1.0f, 1.0f);
Enjoy the pretty moving colors.