-
Notifications
You must be signed in to change notification settings - Fork 0
Preparing the Pipeline
The graphical engine of this project uses modern versions of OpenGL. On the PC platform, OpenGL 4.3 Core is used. On embedded systems, OpenGL ES 3.0 is used. Both of these versions are at feature parody, and both require the user to define their own shading pipeline. A basic default shader is provided by the engine. For reference, refer to the default vertex and fragment shaders. These simple GLSL sources will provide everything required to draw basic geometry with texturing and colors.
A GLSL shader program can be loaded and compiled by providing two Resource instances; a vertex shader and a fragment shader.
// Load the default shaders provided with the engine
Shader defaultShader = Shader.create(
Resource.fromClasspath("shaders/textured.vert"),
Resource.fromClasspath("shaders/textured.frag")
);
// Bind this program for use
defaultShader.use();While the default shader is in use, the Renderer engine object will be able to draw simple geometry.
The default pipeline also provides a set of matrices used to manipulate what you see on screen. Two matrices are managed, called the projection matrix and the model matrix. The projection matrix allows for perspective and orthographic projection. Use this matrix to change the bounds of the screen, such as setting the coordinates on screen to range from negative one to positive one.
public class GameMainClass implements CheckGame
{
@EngineObject
private Matrices matrices;
@Override
public void update()
{
// Set the projection matrix to orthographic; the coordinates
// will be -1 on the left and 1 on the right, and -1 on the bottom
// and 1 on the top
matrices.getProjection()
// Make sure the projection matrix is reset
.identity()
// Apply the actual orthographic projection
.ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
}
// . . .
}The other matrix, the model matrix, can be used to scale, rotate, and translate geometry on screen. The following example will translate everything on the screen to the right.
matrices.getModel()
// Make sure the model matrix is reset
.identity()
// Translate everything to the right by 0.5 units
.translate(0.5f, 0.0f, 0.0f);Test the pipeline by attempting to draw a quad covering the middle of the screen. The screen's coordinates span from a -1 to a 1. In order to draw a rectangle in the center of the screen, use the Renderer engine object to draw a rectangle at (-0.5, -0.5) with a width and height of one.
In order to use the renderer, add an @EngineObject annotation to the main class for the Renderer instance.
public class GameMainClass implements CheckGame
{
@EngineObject
private Matrices matrices;
@EngineObject
private Renderer renderer;
private Shader defaultShader;
@Override
public void initialize()
{
defaultShader = Shader.create(
Resource.fromClasspath("shaders/textured.vert"),
Resource.fromClasspath("shaders/textured.frag")
);
}
@Override
public void update()
{
// Use the default shader created above
defaultShader.use();
// Set the orthographic projection
matrices.getProjection()
.identity()
.ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
// Make sure there are no transformations
matrices.getModel()
.identity();
// Draw the rectangle every frame
renderer.drawRectangle(-0.5f, -0.5f, 1.0f, 1.0f);
}
// . . .
}When running this example, you may not see anything. This is because no texture is bound. Don't worry, there is square on screen! It's just black on black.