Jump to content

Simple Model Viewer.


reepblue
 Share

Recommended Posts

A quick app to preview models with environment probes. Throw these files within your project directory and load the models via a batch script.

Key Listings:

F5 - Reload model

G - Toggle Grid

F - Toggle Floor

W - Toggle Widget

1-7 - Change Skybox.

0 - Remove Skybox.

image.thumb.png.8a20f5def9077036a48e71e47a293b30.png

Windows only as it was just an experiment (Could have done more null checking..), but here's the full code. Use at your own risk.

#include "Leadwerks.h"
using namespace Leadwerks;

Model*	m_pModel;
Vec3	m_vCamRot;
Pivot*	m_pCamPivot;
bool	m_bShowWidget = false;
bool	m_bShowGrid = false;
int m_intShowCubemap = -1;
int m_intCurrentCubemap = -1;
const char* m_pszCubeMaps[7]
{
	"Materials/CubeMap/1SaintLazarusChurch.tex",
	"Materials/CubeMap/LancellottiChapel.tex",
	"Materials/CubeMap/MarriottMadisonWest.tex",
	"Materials/CubeMap/papermill.tex",
	"Materials/CubeMap/park.tex",
	"Materials/CubeMap/PereaBeach.tex",
	"Materials/CubeMap/Yokohama.tex"
};

int main(int argc, const char *argv[])
{
	Leadwerks::logstream__ = NULL;
#ifndef DEBUG
	Leadwerks::logmode = false;
#endif

	System::ParseCommandLine(argc, argv);
	std::string m_strModelPath = System::GetProperty("model", FileSystem::FixPath("Models/ModelViewer/error.mdl"));

	// Load any zip files in main directory
	System::AppPath = FileSystem::ExtractDir(argv[0]);
	Leadwerks::Directory* dir = Leadwerks::FileSystem::LoadDir(".");
	if (dir)
	{
		for (size_t i = 0; i < dir->files.size(); i++)
		{
			std::string file = dir->files[i];
			std::string ext = Leadwerks::String::Lower(Leadwerks::FileSystem::ExtractExt(file));
			if (ext == "zip" || ext == "pak")
			{
				Leadwerks::Package::Load(file);
			}
		}
		delete dir;
	}

	Window* window = Window::Create("ModelViewer",0,0,1024,768,Leadwerks::Window::Titlebar+Leadwerks::Window::Center+ Leadwerks::Window::Resizable);
	Context* context = Context::Create(window);
	Font* font = Font::Load("Fonts/arial.ttf", 8); // <- Do a null check  here.
	context->SetFont(font);
	font->Release();

	World* world = World::Create();
	world->SetSkybox(NULL);

	Light* light = DirectionalLight::Create();
	light->SetShadowMode(Light::Static);
	light->SetRotation(45, 45, 0);

	m_pCamPivot = Pivot::Create();

	m_pModel = Model::Load(m_strModelPath);
	if (m_pModel == NULL) m_pModel = Model::Load("Models/ModelViewer/error.mdl");
	m_pModel->SetShadowMode(Light::Static);
	m_vCamRot = Vec3(0, 0, -4);

	Camera* camera = Camera::Create();
	camera->SetGridMode(m_bShowGrid);
	if (m_pModel != NULL)
	{
		camera->Move(0, 2, -m_pModel->GetAABB().size.z * 4);
	}
	else
	{
		camera->Move(0, 2, -5);
	}

	Model* m_pFloor = Model::Box(10.24, .32, 10.24);
	m_pFloor->SetColor(0.25, 0.25, 0.25);
	m_pFloor->Move(0, -.16, 0);
	m_pFloor->SetShadowMode(0);
	m_pFloor->Hide();

	Probe* probe = Probe::Create();
	probe->SetScale(10.24, 10.24, 10.24);
	probe->UpdateAABB(Entity::GlobalAABB);
	probe->needsupdate = true;
	probe->Render();

	bool mousedown;
	while (window->Closed() == false)
	{
		mousedown = false;

		if (window->MouseDown(1))
		{
			m_vCamRot.y += (window->MouseX() - (window->GetWidth() / 2)) * Time::GetSpeed() * 0.2;
			m_vCamRot.x += (window->MouseY() - (window->GetHeight() / 2)) * Time::GetSpeed() * 0.2;
			mousedown = true;
		}

		if (window->MouseDown(2))
		{
			if (m_pModel != NULL)
			{
				float current = m_vCamRot.z;
				m_vCamRot.z = Math::Clamp(current + (window->MouseY() - (window->GetHeight() / 2)) * Time::GetSpeed() * 0.1, -20.0f, -m_pModel->GetAABB().size.z * 2);
			}
			else
			{
				m_vCamRot.z += (window->MouseY() - (window->GetHeight() / 2)) * Time::GetSpeed() * 0.1;
			}

			mousedown = true;
		}

		if (window->KeyHit(Key::F))
		{
			if (m_pFloor->Hidden())
			{
				m_pFloor->Show();
			}
			else
			{
				m_pFloor->Hide();
			}			
		}

		if (window->KeyHit(Key::W))
		{
			m_bShowWidget = !m_bShowWidget;
		}

		if (window->KeyHit(Key::G))
		{
			m_bShowGrid = !m_bShowGrid;
			camera->SetGridMode(m_bShowGrid);
		}

		if (window->KeyHit(Key::F5))
		{
			m_pModel->Release();
			m_pModel = NULL;

			m_pModel = Model::Load(m_strModelPath);
			m_pModel->SetShadowMode(Light::Static);
		}

		if (mousedown)
		{
			window->HideMouse();
			window->SetMousePosition(window->GetWidth() / 2, window->GetHeight() / 2);
		}
		else
		{
			window->ShowMouse();
		}

		Vec3 pos = m_pCamPivot->GetPosition();
		camera->SetPosition(pos.x, pos.y + 1, pos.z);
		camera->SetRotation(m_vCamRot.x, m_vCamRot.y, 0);
		camera->Move(0, 0, m_vCamRot.z);

		if (window->KeyHit(Key::D0)) m_intShowCubemap = -1;
		if (window->KeyHit(Key::D1)) m_intShowCubemap = 0;
		if (window->KeyHit(Key::D2)) m_intShowCubemap = 1;
		if (window->KeyHit(Key::D3)) m_intShowCubemap = 2;
		if (window->KeyHit(Key::D4)) m_intShowCubemap = 3;
		if (window->KeyHit(Key::D5)) m_intShowCubemap = 4;
		if (window->KeyHit(Key::D6)) m_intShowCubemap = 5;
		if (window->KeyHit(Key::D7)) m_intShowCubemap = 6;

		if (m_intShowCubemap > -1)
		{
			if (m_intCurrentCubemap != m_intShowCubemap)
			{
				world->SetSkybox(m_pszCubeMaps[m_intShowCubemap]);
				probe->Render();
				m_intCurrentCubemap = m_intShowCubemap;
			}
		}
		else
		{
			if (world->GetSkybox() != NULL)
			{
				world->SetSkybox(NULL);
				m_intCurrentCubemap = m_intShowCubemap;
			}
		}


		Time::Update();
		world->Update();
		world->Render();

		context->SetBlendMode(Blend::Alpha);
		context->SetColor(1, 1, 1, 1);
		context->DrawStats(2, 2, true);

		if (m_bShowWidget)
		{
			auto dis = 1;
			Vec3 c = camera->Project(Vec3(0, 0, 0));
			Vec3 x = camera->Project(Vec3(dis, 0, 0));
			Vec3 y = camera->Project(Vec3(0, dis, 0));
			Vec3 z = camera->Project(Vec3(0, 0, dis));

			context->SetColor(1, 0, 0, 1);
			context->DrawLine(c.x, c.y, x.x, x.y);

			context->SetColor(0, 1, 0, 1);
			context->DrawLine(c.x, c.y, y.x, y.y);

			context->SetColor(0, 0, 1, 1);
			context->DrawLine(c.x, c.y, z.x, z.y);
		}

		context->SetColor(1, 1, 1, 1);
		context->SetBlendMode(Blend::Solid);

		context->Sync(true);
	}
}

modelviewer.zip

modelviewer.zip

  • Like 3
  • Thanks 1

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

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