Repository navigation
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, 720.0f - 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, 720.0f - 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!
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:

Now it actually looks like a cube!
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:

