Skip to content

Advanced Tutorial

Josh edited this page Apr 14, 2024 · 23 revisions

Onyx - Advanced Tutorial

Welcome to the advanced tutorial! It is recommended to do the Basic Tutorial before coming here, but if you already have, great!

Let's get started with some boring boilerplate code, we can just use all the code we left off with for the basic tutorial.

#include <string>

#include <Onyx/Core.h>
#include <Onyx/Math.h>
#include <Onyx/Window.h>
#include <Onyx/InputHandler.h>
#include <Onyx/RenderablePresets.h>
#include <Onyx/Renderer.h>
#include <Onyx/Camera.h>
#include <Onyx/TextRenderer.h>

using Onyx::Math::Vec2, Onyx::Math::Vec3;

int main()
{
    Onyx::ErrorHandler errorHandler(true, true, false);
    Onyx::Init(errorHandler);

    Onyx::Window window("Onyx Tutorial", 1280, 720);
    window.init();
    window.setBackgroundColor(Vec3(0.0f, 0.2f, 0.4f));

    Onyx::InputHandler input(window);

    Onyx::Texture container = Onyx::Texture::Load(Onyx::Resources("textures/container.jpg"));
    Onyx::Renderable cube = Onyx::RenderablePresets::TexturedCube(1.0f, container);

    Onyx::Camera cam(window, Onyx::Projection::Perspective(60.0f, 1280, 720));
    cam.translateFB(-2.0f);

    Onyx::Renderer renderer(window, cam);
    renderer.add(cube);

    Onyx::TextRenderer textRenderer(window);
    Onyx::Font roboto = Onyx::Font::Load(Onyx::Resources("fonts/Roboto/Roboto-Bold.ttf"), 48);
    textRenderer.setFont(roboto);

    input.setCursorLock(true);

    const float CAM_SPEED = 4.0f;
    const float CAM_SENS = 50.0f;

    while (window.isOpen())
    {
        input.update();

        if (input.isKeyDown(Onyx::Key::Escape)) window.close();

        if (input.isKeyDown(Onyx::Key::W))      cam.translateFB( CAM_SPEED * window.getDeltaTime());
        if (input.isKeyDown(Onyx::Key::A))      cam.translateLR(-CAM_SPEED * window.getDeltaTime());
        if (input.isKeyDown(Onyx::Key::S))      cam.translateFB(-CAM_SPEED * window.getDeltaTime());
        if (input.isKeyDown(Onyx::Key::D))      cam.translateLR( CAM_SPEED * window.getDeltaTime());
        if (input.isKeyDown(Onyx::Key::Space))  cam.translateUD( CAM_SPEED * window.getDeltaTime());
        if (input.isKeyDown(Onyx::Key::C))      cam.translateUD(-CAM_SPEED * window.getDeltaTime());

        cam.rotate(input.getMouseDeltas().getX() * 0.005f * CAM_SENS, input.getMouseDeltas().getY() * 0.005f * CAM_SENS);

        cam.update();

        cube.rotate(20.0f * window.getDeltaTime(), Vec3(1, 1, 1));

        window.startRender();
        renderer.render();

        Onyx::TextRenderer::StartRender();
        textRenderer.render("FPS: " + std::to_string(window.getFPS()), Vec2(20.0f, window.getBufferHeight() - roboto.getSize() - 7.0f), 0.8f, Vec3(0.0f, 1.0f, 1.0f));
        textRenderer.render(
            "XYZ: (" + std::to_string((int)cam.getPosition().getX()) + ", " +
            std::to_string((int)cam.getPosition().getY()) + ", " +
            std::to_string((int)cam.getPosition().getZ()) + ")",
            Vec2(20.0f, window.getBufferHeight() - roboto.getSize() * 1.6f - 7.0f), 0.5f,
            Vec3(1.0f, 1.0f, 1.0f)
        );
        Onyx::TextRenderer::EndRender();
        window.endRender();
    }

    window.dispose();
    renderer.dispose();
    Onyx::Terminate();

    return 0;
}

Running this should get the same old textured spinning cube with an FPS counter and position on the top left. Let's get into the big topics of the advanced tutorial!

Lighting

Before I say anything - change the textured cube to a colored one, it will make the lighting pop out more.

// replace cube creation
Onyx::Renderable cube = Onyx::RenderablePresets::ColoredCube(1.0f, Vec3(1.0f, 0.0f, 0.0f));

Ok, now we can get started. See how its all just one blob, and if it wasn't rotating it would just look like a weird 2D shape? That's because there's no lighting; no shading. We need to add some lighting to the scene, consisting of a color, ambient strength, and direction; the brightness of a face of the cube will be based off of how directly the light is hitting it. If you want crazy fancy lighting with shininess and reflections and whatnot, head over to Unreal Engine - we don't do that here, Onyx is nice and lightweight for simple games. Enough talk, let's get to it!

We can create a Lighting object and set it to the renderer's lighting in the constructor, and that's all we have to do. Include Onyx/Lighting.h and create a Lighting object. The first argument is the color of the light. The color of the light is multiplied by the object's color, so if any channel (R, G, and B individually are called channels) is 0, then that channel will not be visible on the object, period. So, if you want colored light, try setting the channels you don't want to something like 0.8, or at least not 0. You should also do the same for the colors of your objects if you want colored light, since you'll encounter the same thing if a color channel of your object is 0. Obviously, if you're using a texture, you probably don't have to worry about this. But I'm just going to use white light, simulating the sun (no, it's not yellow, just maybe a little orange at sunrise/set). The second argument is the ambient strength of the light. This is the brightness of a face of an object that is receiving no light at all, i.e. facing completely away from the light. You can play around with this, but I like something like 0.3 (it should range 0-1). The third argument is the direction vector of the light. If you don't know much about vectors representing directions (that is what they are meant to represent, but we have been using them as coordinates or RGB values), you can just copy the direction I use: <0.2, -1.0, -0.3>. (Directional vector notation uses carets <>, don't ask me why.)

// replace renderer creation
Onyx::Lighting lighting(Vec3(1.0f, 1.0f, 1.0f), 0.3f, Vec3(0.2f, -1.0f, -0.3f));
Onyx::Renderer renderer(window, cam, lighting);

If you move a little to the right after starting the program, you should see this:
Image 8
Now it actually looks like a cube! It still doesn't look too special, but don't worry, you'll get HUGE results from lighting when we get into model loading.

Just for fun, let's put the texture back on the cube, and maybe make the light color change over time. Onyx::GetTime() gives us the time in seconds since the window was initialized, so we can use some trig on the time for some color changing results. To actually update the renderer's lighting, we need to call refreshLighting() on it.

// replace cube creation
Onyx::Texture container = Onyx::Texture::Load(Onyx::Resources("textures/container.jpg"));
Onyx::Renderable cube = Onyx::RenderablePresets::TexturedCube(1.0f, container);

// in mainloop
lighting.setColor(Vec3(std::max(sin(Onyx::GetTime()) * 2.0f, 0.5), std::max(cos(Onyx::GetTime()) * 0.7f, 0.5), std::max(tan(Onyx::GetTime()) * 1.3f, 0.5)));
renderer.refreshLighting();

Here's the reason we need to refresh the lighting: Onyx tries to save some performance by not updating the shader variables of renderables every frame, instead it only updates them when it needs to - in this case: when setLighting() is called, the shader variables relating to lighting of all renderables are updated to the new lighting, and when a renderable is added to the renderer, the shader variables relating to lighting of just that renderable are updated to the current lighting. However, in cases like this, we may need to override this behavior, and so the renderer provides the refreshLighting() function to do so.

You should see some wacky results from this:
Image 9

Custom Renderables

Let's get rid of that cube renderable and make our own from scratch! This is a hefty topic, so it'll be divided into some subsections.

What are Renderables?

Renderables consist of three parts: the mesh, the shader, and the texture. The mesh contains the position and shape of the object, the shader does a few different things that we'll talk about, and the texture is pretty self explanatory at this point (many renderables, as we've seen, do not use a texture).

The Mesh

The mesh specifies the individual vertices that make up the object, and the indices that tell OpenGL how to draw the object.

Vertices may contain positional information (the most important), normal information (used for lighting), texture coordinate information, and/or color information (probably the least common, not seen in models). The positional information consists of 3D points in space, hopefully you're familiar with that. The normal information consists of 3D directional vectors in space, specifically ones perpendicular to the surface of the object. The lighting on an object will be the strongest if the normal vector and the light direction vector are pointed straight at each other, that's how it's calculated. The texture coordinate information consists of what are called UV mappings, essentially just the XY mappings, ranging from 0 to 1, on a texture image. (0, 0) is the bottom left of a texture, (1, 1) is the top right. Lastly, the color information consists of the 3 RGB values we're used to, as well as a 4th 'alpha' value, AKA the opacity. All of this information is specified in one array of floats, on a per-vertex basis. This will become more clear when we make a mesh ourselves, by far the hardest part of making custom renderables.

Indices tell OpenGL what vertices to use to draw triangles that will eventually make up the object. Every object is rendered from triangles, connecting 3 of its vertices together. Each index (indices is the plural of index, to be clear) simply refers to one vertex, the first vertex specified being 0, the second 1, 2, and so on, like arrays.

If we are just drawing one triangle, we would have 3 vertices for the 3 points on the triangle, and the indices would simply be (0, 1, 2). Or (1, 2, 0), or (2, 0, 1), it doesn't matter.

If we are drawing a square, however, we would have 4 vertices for the 4 points on the triangle, but the indices would have to specify two triangles to make up the square. Let's say our vertices are laid out as follows: top-left, top-right, bottom-right, bottom-left. Our indices would be: (0, 1, 2), (2, 3, 0). Hopefully you can visualize that and make it make sense. Again, the indices of the triangles themselves can be flipped around and it won't matter (we could instead use (2, 1, 0), (0, 3, 2)), but you have to make sure the triangles you're drawing correctly make up the object.

Alright, that's the hardest part out of the way, let's move on.

The Shader

Shaders are programs that run on the GPU of your system. They are written in a language called GLSL, and while we could write and create our own completely custom shaders, we're going to use some of the many shader presets Onyx already has for us. If you're curious, you can take a peek at some shader code in the resources/shaders folder.

Shaders, at least how Onyx uses them, consist of two parts: the vertex shader and the fragment shader. All these details really aren't important, but are good to know. The vertex shader converts our positional coordinates, using the transformations we've applied to the renderable, the camera, and the projection of the camera, to coordinates on our screen. It displays these 3D objects onto our 2D screen. The fragment shader takes every individual fragment, essentially a pixel, and calculates its color. In the colored triangle preset we've used, the fragment shader is as simple as setting every pixel's color to the color we passed to the renderable preset function. In the textured cube preset, on the other hand, the fragment shader uses a factor calculated in the vertex shader specifying how direct the light is, and applies that brightness to the part of the texture specified by the texture coordinates in the vertices of the mesh. It's all complicated, not really necessary to understand it all.

The Texture

The texture is pretty simple, it's an image applied to a renderable. The UV mappings in the vertices we talked about earlier correspond to coordinates in the texture image, and the fragment shader interpolates these coordinates in each vertex to generate a coordinate for each fragment, and takes the color of the texture at that coordinate to generate the color of that pixel on the screen, along with any lighting calculations.

If the renderable doesn't have a texture, then it doesn't use a shader that uses a texture. If we try to use a shader that requires a texture but we don't give the renderable a texture, the color will be calculated incorrectly; it will probably just be black. Actually, this is a good quick note - we can use a mesh that provides more data than the shader requires, but we can't use a shader that requires more data than the mesh provides.

Let's Make a Renderable!

Let's make a tetrahedral. It sounds fancy, it's really just a shape made from 4 points - 3 points specifying the base triangle, and one point on the top. From this, we get a base triangle and 3 side triangles.
Image 10

Let's make the 3 points for our base. It will change on the X and Z axes, and stay constant on the Y axis, since it is flat. Or XZ coordinates, to center around the 0, 0 origin, should be: (-1, -1), (1, -1), (0, 1). Make sense? Then, our top coordinate will just be in the center of the XZ plane and our Y coordinate will be 1. So, our 3D XYZ positional coordinates are as follows: (-1, 0, -1), (1, 0, -1), (0, 0, 1), (0, 1, 0). Let's make an array of vertices to show this:

float vertices[] = {
    -1.0f, 0.0f, -1.0f,
     1.0f, 0.0f, -1.0f,
     0.0f, 0.0f,  1.0f,
     0.0f, 1.0f,  0.0f
};

Great, now what will our indices look like? Well, we'll start of making a triangle out of the base, which would just be (0, 1, 2). Now, we need 3 triangles made out of 2 consecutive vertices of the base, and the top vertex. So, for organization, our third index can always be the top vertex, and the first two can be the consecutive vertices of the base, starting at 0, 1. So here are those 3 sets of indices: (0, 1, 3), (1, 2, 3), (2, 0, 3). Note how we roll back to 0 for the second index of that last set. Let's code this:

uint indices[] = {
    0, 1, 2,
    0, 1, 3,
    1, 2, 3,
    2, 0, 3
};

Now we can make a mesh out of this! A mesh needs a VertexArray object and an IndexArray object - these objects are very simple, they just need the array data and the size of the arrays, and the VertexArray also needs the format of our vertices. Based on what data the vertices may contain, we may have positional information, known also as just vertices (V), normals (N), texture coords (T), and/or colors (C). So the available formats include V, VN, VC, VT, VNC, VNT, and VNCT. We just have positional information, so our format will be V.

Onyx::Mesh mesh(
    Onyx::VertexArray(vertices, sizeof(vertices), Onyx::VertexFormat::V),
    Onyx::IndexArray(indices, sizeof(indices))
);

We won't use a texture for now, and our shader will have to be one compatible with the V vertex format. The only fitting one is V_Color. Sorry if these names our confusing - I tried - but shader names consist of the vertex format they are compatible with (unless it's a UI shader), and then an underscore with any sort of extension they may have, this one being that it can take a color. We will need to include Onyx/ShaderPresets.h. Additionally, now that we now how to make colors using RGB coordinates 0-1 from the basic tutorial, we can be lazy and use some color presets in the vector class. The color will be a Vec4, this time, the fourth value being the alpha value. Let's add Vec4 to our using directive up top: using Onyx::Math::Vec4;.

Onyx::Shader shader = Onyx::ShaderPresets::V_Color(Vec4::Green());

Great, now we can create a renderable by simply passing in this mesh and shader, and add it to the renderer!

Onyx::Renderable tetrahedral(mesh, shader);
// ...
renderer.add(tetrahedral);

Now run the program, let's see what we've got!
Image 11
Well, it's kinda just a blob, even worse then the cube. We could add some normal vectors, but that would be too mathy. Instead, we're going to render in wireframe mode, which will show us the outlines of our triangles. All we have to do is use the renderer's static SetWireframe function:

Onyx::Renderer::SetWireframe(true);

Image 12
That's better. Let's add a texture to it.

But, there's a problem - each face needs its own 3 texture coordinates, which means we now need 3 vertices per face. This kind of defeats the whole point of the vertex and index system, but it's important to understand that when loading huge models, it does save lots of data.

Anyways, we now need 12 vertices for our textured tetrahedral, and now our indices will just be 0-11 in order. Each face will just use the bottom-left of the texture, the bottom-right, and then the middle-top, just like how our positional information is defined for our base triangle. So our texture coordinates for every face will just be (0, 0), (1, 0), (0.5, 1). Here's how all that vertex and index data would look:

float vertices[] = {
    // positions            // texture coords
    -1.0f, 0.0f, -1.0f,     0.0f, 0.0f,
     1.0f, 0.0f, -1.0f,     1.0f, 0.0f,
     0.0f, 0.0f,  1.0f,     0.5f, 1.0f,

    -1.0f, 0.0f, -1.0f,     0.0f, 0.0f,
     1.0f, 0.0f, -1.0f,     1.0f, 0.0f,
     0.0f, 1.0f,  0.0f,     0.5f, 1.0f,

     1.0f, 0.0f, -1.0f,     0.0f, 0.0f,
     0.0f, 0.0f,  1.0f,     1.0f, 0.0f,
     0.0f, 1.0f,  0.0f,     0.5f, 1.0f,

     0.0f, 0.0f,  1.0f,     0.0f, 0.0f,
    -1.0f, 0.0f, -1.0f,     1.0f, 0.0f,
     0.0f, 1.0f,  0.0f,     0.5f, 1.0f
};

uint indices[] = {
    0, 1, 2,
    3, 4, 5,
    6, 7, 8,
    9, 10, 11
};

Now, we need an actual texture to apply to the renderable. We can just use the container texture from before, or feel free to grab some different one from the web. We also need to change our vertex format to VT, since that is how our vertices are laid out, and we need to change our shader to the VT shader. This shader doesn't have any extras, so there's no underscore something, just the vertex format. Here's how all of that works out:

Onyx::Mesh mesh(
    Onyx::VertexArray(vertices, sizeof(vertices), Onyx::VertexFormat::VT),
    Onyx::IndexArray(indices, sizeof(indices))
);

Onyx::Shader shader = Onyx::ShaderPresets::VT();

Onyx::Texture container = Onyx::Texture::Load(Onyx::Resources("textures/container.jpg"));

Onyx::Renderable tetrahedral(mesh, shader, container);

And, if you turned wireframe off, you should get this:
Image 13

Let's spice it up one last time - we can assign colors to each vertex along with these texture coordinates! Let's assign red to the bottom-left of the base, yellow to the bottom-right, green to the middle-top, and blue to the top of the whole thing (the 4th value will always be 1.0f, that's just the opacity):

float vertices[] = {
    // positions            // colors                   // texture coords
    -1.0f, 0.0f, -1.0f,     1.0f, 0.0f, 0.0f, 1.0f,     0.0f, 0.0f,
     1.0f, 0.0f, -1.0f,     1.0f, 1.0f, 0.0f, 1.0f,     1.0f, 0.0f,
     0.0f, 0.0f,  1.0f,     0.0f, 1.0f, 0.0f, 1.0f,     0.5f, 1.0f,

    -1.0f, 0.0f, -1.0f,     1.0f, 0.0f, 0.0f, 1.0f,     0.0f, 0.0f,
     1.0f, 0.0f, -1.0f,     1.0f, 1.0f, 0.0f, 1.0f,     1.0f, 0.0f,
     0.0f, 1.0f,  0.0f,     0.0f, 0.0f, 1.0f, 1.0f,     0.5f, 1.0f,

     1.0f, 0.0f, -1.0f,     1.0f, 1.0f, 0.0f, 1.0f,     0.0f, 0.0f,
     0.0f, 0.0f,  1.0f,     0.0f, 1.0f, 0.0f, 1.0f,     1.0f, 0.0f,
     0.0f, 1.0f,  0.0f,     0.0f, 0.0f, 1.0f, 1.0f,     0.5f, 1.0f,

     0.0f, 0.0f,  1.0f,     0.0f, 1.0f, 0.0f, 1.0f,     0.0f, 0.0f,
    -1.0f, 0.0f, -1.0f,     1.0f, 0.0f, 0.0f, 1.0f,     1.0f, 0.0f,
     0.0f, 1.0f,  0.0f,     0.0f, 0.0f, 1.0f, 1.0f,     0.5f, 1.0f
};

And once we change the vertex format and the shader to VCT, we get this cool result:
Image 14
Remember when I said: we can use a mesh that provides more data than the shader requires, but we can't use a shader that requires more data than the mesh provides? Well, our mesh provides position, color, and tex coord data, meaning we can still use the V_Color shader and assign a color to all vertices, or the VC shader to ignore the texture, and it will still work perfectly. Try it out!

That's gonna be it for the renderable section. All of this knowledge may go away when we get to model loading, but I think it's good to know.

Model Loading

Clone this wiki locally