Jump to content

Generate an image for a model


Josh
 Share

Recommended Posts

This is how the editor creates thumbnails for models. @Andy90

Multi-threaded rendering needs to be disabled if you get the screenshot immediately, like this code does.

auto win = openwindows[size];
if (win == NULL)
{
    win = CreateWindow("", 0, 0, size, size, GetDisplays()[0], WINDOW_HIDDEN | WINDOW_CLIENTCOORDS);
    openwindows[size] = win;
    fb[size] = CreateFramebuffer(win);
}
if (world == NULL)
{
    world = CreateWorld();
    world->SetEnvironmentMap(LoadTexture(AppDir() + "/thumbnail_diffuse.dds"), ENVIRONMENTMAP_DIFFUSE);
    world->SetEnvironmentMap(LoadTexture(AppDir() + "/thumbnail_specular.dds"), ENVIRONMENTMAP_SPECULAR);
    world->SetAmbientLight(0);
    light = CreateDirectionalLight(world);
    light->SetShadows(false);                    
}

shared_ptr<Model> mdl;
if (packagepath.empty())
{
    mdl = LoadModel(world, path, LOAD_UNMANAGED | LOAD_NO_PACKAGES);
}
else
{
    auto pak = LoadPackage(packagepath);
    if (pak)
    {
        packages.push_back(pak);
        auto stream = pak->ReadFile(path);
        if (stream == NULL) return NULL;
        mdl = LoadModel(world, stream, LOAD_UNMANAGED);
    }
}
if (mdl)
{
    light->SetRotation(35, -35, 0);
    if (cam == NULL) cam = CreateCamera(world);
    cam->SetProjectionMode(PROJECTION_ORTHOGRAPHIC);
    cam->SetRotation(0,135,0);
    cam->Turn(22.5, 0, 0);
    auto bounds = mdl->GetBounds(BOUNDS_RECURSIVE);
    cam->SetPosition(bounds.center);
    cam->Move(0,0,-bounds.radius);
    cam->SetZoom(float(size) / (bounds.radius * 2.0f));
    cam->SetRange(0, bounds.radius * 2.0f);
    cam->Sync();
    fb[size]->Capture();
    cam->SetHidden(false);
    world->Render(fb[size]);
    cam->SetHidden(true);
    auto caps = fb[size]->GetCaptures();
    if (not caps.empty())
    {
        pixmap = caps[0];
        //pixmap->Save(GetPath(PATH_DESKTOP) + "/" + String(size) + ".dds");
        Assert(pixmap->size == fb[size]->GetSize());
    }
}

 

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...