Jump to content

3D GUI


Josh
 Share

Recommended Posts

This code shows how to set up a GUI with 3D graphics. This requires an update I will upload today:

#include "UltraEngine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = GetDisplays();

    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);

    //Create a world
    auto world = CreateWorld();

    //Create a framebuffer
    auto framebuffer = CreateFramebuffer(window);

    //Create a camera
    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetPosition(0, 0.1, -1);

    auto specmap = LoadTexture("Materials/Environment/Storm/specular.dds");
    auto diffmap = LoadTexture("Materials/Environment/Storm/diffuse.dds");

    world->SetEnvironmentMap(specmap, ENVIRONMENTMAP_BACKGROUND);
    world->SetEnvironmentMap(specmap, ENVIRONMENTMAP_SPECULAR);
    world->SetEnvironmentMap(diffmap, ENVIRONMENTMAP_DIFFUSE);

    auto mtl = CreateMaterial();
    mtl->SetRoughness(0.25);
    mtl->SetMetalness(0.5);

    auto ball = CreateSphere(world,0.5,32);
    ball->SetMaterial(mtl);

    camera->Point(ball);

    auto cam2d = CreateCamera(world);
    cam2d->SetProjectionMode(PROJECTION_ORTHOGRAPHIC);
    cam2d->SetRange(-1, 1);
    cam2d->SetRenderLayers(RENDERLAYER_1);
    cam2d->SetClearMode(CLEAR_DEPTH);
    cam2d->SetPosition(float(framebuffer->size.x)*0.5f, float(framebuffer->size.y)*0.5f);

    const int SIDEPANELWIDTH = 200;

    auto ui = CreateInterface(world, framebuffer->size);
    ui->background->SetShape(0, 0, SIDEPANELWIDTH, ui->background->size.y);
    ui->background->SetLayout(1, 0, 1, 1);
    ui->SetRenderLayers(RENDERLAYER_1);
    
    int x = 10, y = 10;
    CreateLabel("Rougness:", x, y, ui->background->size.x - 2 * x, 30, ui->background);
    y += 40;
    auto roughnessslider = CreateSlider(x, y, ui->background->size.x - 2 * x, 30, ui->background, SLIDER_HORIZONTAL | SLIDER_TRACKBAR);
    roughnessslider->SetLayout(1, 1, 1, 0);
    roughnessslider->SetRange(0, 10);
    roughnessslider->SetValue(0);
    y += 40;
    CreateLabel("Metallic:", x, y, ui->background->size.x - 2 * x, 30, ui->background);
    y += 40;
    auto metallicslider = CreateSlider(x, y, ui->background->size.x - 2 * x, 30, ui->background, SLIDER_HORIZONTAL | SLIDER_TRACKBAR);
    metallicslider->SetRange(0, 10);
    metallicslider->SetValue(0);
    metallicslider->SetLayout(1, 1, 1, 0);
    y += 40;

    ui->SetScale(displays[0]->scale);

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        while (PeekEvent())
        {
            const auto& e = WaitEvent();
            switch (e.id)
            {

            //Pass these events to the GUI:
            case EVENT_MOUSEWHEEL:
            case EVENT_MOUSEDOWN:
            case EVENT_MOUSEUP:
            case EVENT_MOUSEMOVE:
            case EVENT_KEYDOWN:
            case EVENT_KEYUP:
            case EVENT_KEYREPEAT:
            case EVENT_KEYCHAR:
                ui->ProcessEvent(e);
                break;

            //Response to GUI events:
            case EVENT_WIDGETACTION:
                if (e.source == roughnessslider)
                {
                    mtl->SetRoughness(float(e.data) * 0.1f);
                }
                else if (e.source == metallicslider)
                {
                    mtl->SetMetalness(float(e.data) * 0.1f);
                }
                break;
            }
        }

        //camera->UpdateControls(window);
        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

Here is the result:

Untitled.thumb.jpg.9da046976c2037656cf395e4c92b03b2.jpg

  • Like 1
  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Okay, the update is available now and this example works.

I noticed the project was not showing as requiring an update, even though new shaders were downloaded. There may be an issue with the time stamps on zip-extracted files, so you may need to uninstall and reinstall the SDK before the project will show that it is outdated.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...