Jump to content

Shadow Problems


SpiderPig
 Share

Go to solution Solved by Josh,

Recommended Posts

Okay, it's because it's far away from the origin.  This test grid is 2048 vertically up, move around and you can see the dark shapes move with the camera.  I wouldn't have thought this was far enough for floating point problems, but it may be related?

#include "UltraEngine.h"

using namespace UltraEngine;

Vec3 mousepos = Vec3(0.0f);
float move_adjustment = 0.1f;
float move_speed = 1.0f;
float lookspeed = 0.1f;
float looksmoothing = 0.5f;

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);

    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetFov(70);
    camera->SetPosition(0, 0, -3);

    auto light = CreateDirectionalLight(world);
    light->SetRotation(35, 35, 0);

    auto box = CreateBox(world);
    box->SetPosition(10.0f, 2049.0f, 10.0f);

    auto custom_model = CreateModel(world);
    auto mesh = custom_model->AddMesh();

    int size = 256, index = 0;
    for (int z = 0; z < size; z++) {
        for (int x = 0; x < size; x++) {
            mesh->AddVertex(x, 0.0f, z, 0.0f, 1.0f, 0.0f);

            if (x != 0 && x != size - 1 && z != 0 && z != size - 1) {
                mesh->AddPrimitive(index, index - size - 1, index - 1);
                mesh->AddPrimitive(index, index - size, index - size - 1);
            }
            index++;
        }
    }

    custom_model->UpdateBounds();
    custom_model->SetPosition(0.0f, 2048.0f, 0.0f);
    camera->SetPosition(0.0f, 2048.0f, 0.0f);


    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        if (window->KeyHit(KEY_F2) == true) { camera->SetWireframe(!camera->GetWireframe()); }

        auto _displaySize = window->GetSize();
        float cx = Round((float)_displaySize.x / 2.0f);
        float cy = Round((float)_displaySize.y / 2.0f);

        auto mpos = Vec3(window->GetMousePosition().x, window->GetMousePosition().y, window->GetMousePosition().z);
        window->SetMousePosition(cx, cy);
        mpos = mpos * looksmoothing + mousepos * (1 - looksmoothing);
        auto dx = (mpos.x - cx) * lookspeed;
        auto dy = (mpos.y - cy) * lookspeed;


        auto camrot = camera->GetRotation();
        camrot.x += dy;
        camrot.y += dx;
        camera->SetRotation(camrot);
        mousepos = mpos;

        auto speed = 0.1f;
        if (window->KeyDown(KEY_SHIFT) == true) { speed = speed * 10.0f; }
        if (window->KeyDown(KEY_W) == true) {camera->Move(0, 0, speed);}
        else if (window->KeyDown(KEY_S) == true) { camera->Move(0, 0, -speed); }
        if (window->KeyDown(KEY_A) == true) { camera->Move(-speed, 0, 0); }
        else if (window->KeyDown(KEY_D) == true) { camera->Move(speed, 0, 0); }

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

Here is my modified version. At around height 361, it looks like the box is intersecting the light's near clipping plane...

#include "UltraEngine.h"

using namespace UltraEngine;

Vec3 mousepos = Vec3(0.0f);
float move_adjustment = 0.1f;
float move_speed = 1.0f;
float lookspeed = 0.1f;
float looksmoothing = 0.5f;

int main(int argc, const char* argv[])
{
    const float dist = 100;

    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);

    auto camera = CreateCamera(world);
    camera->SetClearColor(0,0,1,1);
    camera->SetFov(70);
    camera->SetPosition(3, 2, 0);
    camera->SetRotation(45, 90, 0);

    auto light = CreateDirectionalLight(world);
    light->SetRotation(35, 35, 0);

    auto box = CreateBox(world);
    box->SetPosition(10.0f, dist + 1.0f, 10.0f);

    auto custom_model = CreateModel(world);
    auto mesh = custom_model->AddMesh();

    int size = 256, index = 0;
    for (int z = 0; z < size; z++) {
        for (int x = 0; x < size; x++) {
            mesh->AddVertex(x, 0.0f, z, 0.0f, 1.0f, 0.0f);
            if (x != 0 && x != size - 1 && z != 0 && z != size - 1) {
                mesh->AddPrimitive(index, index - size - 1, index - 1);
                mesh->AddPrimitive(index, index - size, index - size - 1);
            }
            index++;
        }
    }

    custom_model->UpdateBounds();
    custom_model->SetPosition(0.0f, dist, 0.0f);
    camera->SetPosition(0.0f, dist + 1.0f, 0.0f);

    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        window->SetText(box->position.y);

        if (window->KeyDown(KEY_UP))
        {
            box->Translate(0, 1, 0);
            custom_model->Translate(0, 1, 0);
            camera->Translate(0, 1, 0);
        }
        if (window->KeyDown(KEY_DOWN))
        {
            box->Translate(0, -1, 0);
            custom_model->Translate(0, -1, 0);
            camera->Translate(0, -1, 0);
        }

        if (window->KeyHit(KEY_F2) == true) { camera->SetWireframe(!camera->GetWireframe()); }

        auto _displaySize = window->GetSize();
        float cx = Round((float)_displaySize.x / 2.0f);
        float cy = Round((float)_displaySize.y / 2.0f);

        auto mpos = Vec3(window->GetMousePosition().x, window->GetMousePosition().y, window->GetMousePosition().z);
        window->SetMousePosition(cx, cy);
        mpos = mpos * looksmoothing + mousepos * (1 - looksmoothing);
        auto dx = (mpos.x - cx) * lookspeed;
        auto dy = (mpos.y - cy) * lookspeed;


        auto camrot = camera->GetRotation();
        camrot.x += dy;
        camrot.y += dx;
        camera->SetRotation(camrot);
        mousepos = mpos;

        auto speed = 0.1f;
        if (window->KeyDown(KEY_SHIFT) == true) { speed = speed * 10.0f; }
        if (window->KeyDown(KEY_W) == true) { camera->Move(0, 0, speed); }
        else if (window->KeyDown(KEY_S) == true) { camera->Move(0, 0, -speed); }
        if (window->KeyDown(KEY_A) == true) { camera->Move(-speed, 0, 0); }
        else if (window->KeyDown(KEY_D) == true) { camera->Move(speed, 0, 0); }

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

Untitled.thumb.jpg.aa9a4949e8adef207f5de8386a291b3d.jpg

  • 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

  • Josh locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...