Grid system for editors and such

A place to show off your latest screenshots and for people to comment on them. Only start a new thread here if you have some nice images to show off!
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Re: Grid system for editors and such

Post by JeDi »

Hi,

I just copied the license from Ogre back when I posted this. I don't know a lot about these different licenses, but it should indeed be public domain. I would be glad if I knew were the code is used though, just to boost my ego a bit ;)

I will change the license headers to the current Ogre ones (so MIT) once I arrive at work in an hour or so, or someone can do it for me. You have my permission.

Greetz,
JeDi
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Re: Grid system for editors and such

Post by JeDi »

OK, I changed both the hosted files and the wiki to represent the new OGRE license.

Greetz,
JeDi
fralik
Gnoblar
Posts: 3
Joined: Fri Sep 28, 2012 9:21 am

Re: Grid system for editors and such

Post by fralik »

Hi!

I can see the grid, while using the perspective camera, but whenever I switch camera type to PT_ORTHOGRAPHIC I see no grid at all. Any clues what could be the cause?
I am using tutorial framework and my code to create the camera is:

Code: Select all

mCamera = mSceneMgr->createCamera("PlayerCam");
mCamera->setProjectionType(PT_ORTHOGRAPHIC);
mCamera->setPosition(Ogre::Vector3(0.01, 500, 0.01));
mCamera->setFixedYawAxis(true, Vector3(0, 0, -1));
mCamera->lookAt(Ogre::Vector3(0, 0, 0));
mCamera->setNearClipDistance(5);
mCamera->setFarClipDistance(10000);
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Re: Grid system for editors and such

Post by JeDi »

I'm sorry, I haven't used Ogre for almost three years now, so I have no idea if this code still works in the current version. Maybe someone else can confirm that?
User avatar
Mind Calamity
Ogre Magi
Posts: 1255
Joined: Sat Dec 25, 2010 2:55 pm
Location: Macedonia
x 81

Re: Grid system for editors and such

Post by Mind Calamity »

It did work in perspective the last time I used it, as for orthographic, I can't remember if and how I fixed it at the time. (I was using this a few months ago with the repo version of OGRE).
BitBucket username changed to iboshkov (from MindCalamity)
Do you need help? What have you tried?
- xavier
---------------------
HkOgre - a Havok Integration for OGRE | Simple SSAO | My Blog | My YouTube | My DeviantArt
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Re: Grid system for editors and such

Post by JeDi »

Have you tried setting up your camera as in my example on the first page?

Code: Select all

Ogre::Quaternion orientation;
orientation.FromAxes(Ogre::Vector3::UNIT_X, Ogre::Vector3::NEGATIVE_UNIT_Z, Ogre::Vector3::UNIT_Y);
pCamera->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
pCamera->setOrientation(orientation);
pCamera->setPosition(0,10000,0);

Code: Select all

// Reset the camera
pCamera->setPosition(Ogre::Vector3::ZERO);

// Avoid weird clipping plane problems
if(m_zoom < 0.01f) m_zoom = 0.01f;

pCamera->moveRelative(Ogre::Vector3(m_panX, m_panY, 10000));
pCamera->setNearClipDistance(m_zoom);
fralik
Gnoblar
Posts: 3
Joined: Fri Sep 28, 2012 9:21 am

Re: Grid system for editors and such

Post by fralik »

JeDi wrote:Have you tried setting up your camera as in my example on the first page?
Well, seems that the code works and I can see the grid.

Image

However, I am not sure where to put the second part of your code (related to zoom and pan). I tried to invoke it in keyPressed function, but got an exception.

Playing with FOV of the camera I was able to get grid for the full render window.
Image

I am using tutorial framework and thus OgreBites::SdkCameraMan to control the camera. When I move the camera along the Y axis the size of grid cells stays the same.
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Re: Grid system for editors and such

Post by JeDi »

The dynamic grid system (with fading minor and major grid lines) only works for orthogonal cameras. Using a perspective camera, the grid is a fixed size 3D grid on the XZ plane, and that is what you are seeing in the first screenshot. I'm not familiar with the camera system you are working with, but it probably sets the camera to perspective.
fralik
Gnoblar
Posts: 3
Joined: Fri Sep 28, 2012 9:21 am

Re: Grid system for editors and such

Post by fralik »

Yes, you are right. Probably camera management sets it to perspective. I created a small example in which I am trying to add zoom capabilities. I move the camera around with W,S,A,D keys and can zoom through SHIFT+W/SHIFT+S:
Image

I wonder if it is possible to zoom without image cropping?

MinGrid.cpp (the interesting function is processUnbufferedInput):

Code: Select all

#include "minGrid.h"

#define WIN32_LEAN_AND_MEAN
#include "windows.h"

using namespace Ogre;

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{
	minGrid app;

	try {
		app.go();
	} catch( Ogre::Exception& e ) {
		MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
	}

	return 0;
}

minGrid::minGrid(void)
	: mRoot(0),
	mCamera(0),
	mSceneMgr(0),
	mWindow(0),
	mResourcesCfg(Ogre::StringUtil::BLANK),
	mPluginsCfg(Ogre::StringUtil::BLANK),
	mShutDown(false),
	mTrayMgr(0),
	mInputManager(0),
	mKeyboard(0),
	mMouse(0),
	mDetailsPanel(0)
{
	m_panX = 0.1;
	m_panY = 350;
	m_panZ = 0.1;
	_viewportGrid = 0;
}

minGrid::~minGrid(void)
{
	if (mTrayMgr) 
		delete mTrayMgr;

	if (_viewportGrid)
		delete _viewportGrid;
	
	//Remove ourself as a Window listener
	Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
	windowClosed(mWindow);
	delete mRoot;
}

bool minGrid::processUnbufferedInput( const Ogre::FrameEvent& evt )
{
	Ogre::Real mMove = 5;      // The movement constant

	if (mKeyboard->isKeyDown(OIS::KC_W)) // Forward
	{
		if (mKeyboard->isKeyDown(OIS::KC_LSHIFT) || mKeyboard->isKeyDown(OIS::KC_RSHIFT))
		{
			m_zoom -= 2;
		}
		else
		{
			m_panY -= mMove;
		}
	}
	if (mKeyboard->isKeyDown(OIS::KC_S)) // Backward
	{
		if (mKeyboard->isKeyDown(OIS::KC_LSHIFT) || mKeyboard->isKeyDown(OIS::KC_RSHIFT))
		{
			m_zoom += 2;
		}
		else
		{
			m_panY += mMove;
		}
	}
	if (mKeyboard->isKeyDown(OIS::KC_A)) // left
	{
		m_panX -= mMove;
	}
	if (mKeyboard->isKeyDown(OIS::KC_D)) // right
	{
		m_panX += mMove;
	}

	// Reset the camera
	mCamera->setPosition(Ogre::Vector3::ZERO);

	// Avoid weird clipping plane problems
	if (m_zoom < 0.01f) 
		m_zoom = 0.01f;

	mCamera->moveRelative(Ogre::Vector3(m_panX, m_panY, 350));
	mCamera->setNearClipDistance(m_zoom);

	return true;
}

void minGrid::createCamera( void )
{
	mCamera = mSceneMgr->createCamera("PlayerCam");
	Ogre::Quaternion orientation;
	orientation.FromAxes(Ogre::Vector3::UNIT_X, Ogre::Vector3::NEGATIVE_UNIT_Z, Ogre::Vector3::UNIT_Y);
	mCamera->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
	mCamera->setOrientation(orientation);
	mCamera->setPosition(m_panX, m_panY, m_panZ);
	mCamera->setFOVy(Radian(Degree(160)));
	m_zoom = mCamera->getNearClipDistance();
}

void minGrid::createScene(void)
{
	Ogre::Entity* ninjaEnt = mSceneMgr->createEntity("Ninja", "ninja.mesh");

	Ogre::SceneNode* ninjaNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	ninjaNode->attachObject(ninjaEnt);

	// Set ambient light
	mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));

	// Create a light
	Ogre::Light* l = mSceneMgr->createLight("MainLight");
	l->setPosition(20,80,50);

	_viewportGrid = new ViewportGrid(mSceneMgr, mWindow->getViewport(0));
	_viewportGrid->enable();
}

bool minGrid::configure(void)
{
	// Show the configuration dialog and initialise the system
	// You can skip this and use root.restoreConfig() to load configuration
	// settings if you were sure there are valid ones saved in ogre.cfg
	if(mRoot->restoreConfig() || mRoot->showConfigDialog())
	{
		// If returned true, user clicked OK so initialise
		// Here we choose to let the system create a default rendering window by passing 'true'
		mWindow = mRoot->initialise(true, "minGrid Render Window");

		// Let's add a nice window icon
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
		HWND hwnd;
		mWindow->getCustomAttribute("WINDOW", (void*)&hwnd);
		LONG iconID   = (LONG)LoadIcon( GetModuleHandle(0), MAKEINTRESOURCE(IDI_APPICON) );
		SetClassLong( hwnd, GCL_HICON, iconID );
#endif
		return true;
	}
	else
	{
		return false;
	}
}

void minGrid::chooseSceneManager(void)
{
	// Get the SceneManager, in this case a generic one
	mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
}

bool minGrid::frameRenderingQueued( const Ogre::FrameEvent& evt )
{
	if (mWindow->isClosed())
		return false;

	if (mShutDown)
		return false;

	//Need to capture/update each device
	mKeyboard->capture();

	mTrayMgr->frameRenderingQueued(evt);

	if (!mTrayMgr->isDialogVisible())
	{
		if (mDetailsPanel->isVisible())   // if details panel is visible, then update its contents
		{
			mDetailsPanel->setParamValue(0, Ogre::StringConverter::toString(mCamera->getDerivedPosition().x));
			mDetailsPanel->setParamValue(1, Ogre::StringConverter::toString(mCamera->getDerivedPosition().y));
			mDetailsPanel->setParamValue(2, Ogre::StringConverter::toString(mCamera->getDerivedPosition().z));
			mDetailsPanel->setParamValue(4, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().w));
			mDetailsPanel->setParamValue(5, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().x));
			mDetailsPanel->setParamValue(6, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().y));
			mDetailsPanel->setParamValue(7, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().z));
		}
	}

	processUnbufferedInput(evt);
	return true;
}

void minGrid::go( void )
{
#ifdef _DEBUG
	mResourcesCfg = "resources_d.cfg";
	mPluginsCfg = "plugins_d.cfg";
#else
	mResourcesCfg = "resources.cfg";
	mPluginsCfg = "plugins.cfg";
#endif

	if (!setup())
		return;

	mRoot->startRendering();

	// clean up
	destroyScene();
}

void minGrid::createFrameListener(void)
{
	Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
	OIS::ParamList pl;
	size_t windowHnd = 0;
	std::ostringstream windowHndStr;

	mWindow->getCustomAttribute("WINDOW", &windowHnd);
	windowHndStr << windowHnd;
	pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));

	mInputManager = OIS::InputManager::createInputSystem( pl );

	mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));
	mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));

	mKeyboard->setEventCallback(this);

	//Set initial mouse clipping size
	windowResized(mWindow);

	//Register as a Window listener
	Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);

	mTrayMgr = new OgreBites::SdkTrayManager("InterfaceName", mWindow, mMouse, this);
	mTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
	mTrayMgr->hideCursor();

	// create a params panel for displaying sample details
	Ogre::StringVector items;
	items.push_back("cam.pX");
	items.push_back("cam.pY");
	items.push_back("cam.pZ");
	items.push_back("");
	items.push_back("cam.oW");
	items.push_back("cam.oX");
	items.push_back("cam.oY");
	items.push_back("cam.oZ");
	items.push_back("");
	items.push_back("Filtering");
	items.push_back("Poly Mode");

	mDetailsPanel = mTrayMgr->createParamsPanel(OgreBites::TL_NONE, "DetailsPanel", 200, items);
	mDetailsPanel->setParamValue(9, "Bilinear");
	mDetailsPanel->setParamValue(10, "Solid");
	mDetailsPanel->hide();

	mRoot->addFrameListener(this);
}

void minGrid::destroyScene(void)
{
}

void minGrid::createViewports(void)
{
	// Create one viewport, entire window
	Ogre::Viewport* vp = mWindow->addViewport(mCamera);
	vp->setBackgroundColour(Ogre::ColourValue(0,0,0));

	// Alter the camera aspect ratio to match the viewport
	mCamera->setAspectRatio(
		Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
}

void minGrid::setupResources(void)
{
	// Load resource paths from config file
	Ogre::ConfigFile cf;
	cf.load(mResourcesCfg);

	// Go through all sections & settings in the file
	Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();

	Ogre::String secName, typeName, archName;
	while (seci.hasMoreElements())
	{
		secName = seci.peekNextKey();
		Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
		Ogre::ConfigFile::SettingsMultiMap::iterator i;
		for (i = settings->begin(); i != settings->end(); ++i)
		{
			typeName = i->first;
			archName = i->second;
			Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
				archName, typeName, secName);
		}
	}
}

void minGrid::createResourceListener(void)
{

}

void minGrid::loadResources(void)
{
	Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}

bool minGrid::setup(void)
{
	mRoot = new Ogre::Root(mPluginsCfg);

	setupResources();

	bool carryOn = configure();
	if (!carryOn) return false;

	chooseSceneManager();
	createCamera();
	createViewports();

	// Set default mipmap level (NB some APIs ignore this)
	Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);

	// Create any resource listeners (for loading screens)
	createResourceListener();
	// Load resources
	loadResources();

	// Create the scene
	createScene();

	createFrameListener();

	return true;
};

bool minGrid::keyPressed( const OIS::KeyEvent &arg )
{
	if (mTrayMgr->isDialogVisible()) return true;   // don't process any more keys if dialog is up

	if (arg.key == OIS::KC_F)   // toggle visibility of advanced frame stats
	{
		mTrayMgr->toggleAdvancedFrameStats();
	}
	else if (arg.key == OIS::KC_G)   // toggle visibility of even rarer debugging details
	{
		if (mDetailsPanel->getTrayLocation() == OgreBites::TL_NONE)
		{
			mTrayMgr->moveWidgetToTray(mDetailsPanel, OgreBites::TL_TOPRIGHT, 0);
			mDetailsPanel->show();
		}
		else
		{
			mTrayMgr->removeWidgetFromTray(mDetailsPanel);
			mDetailsPanel->hide();
		}
	}
	else if (arg.key == OIS::KC_T)   // cycle polygon rendering mode
	{
		Ogre::String newVal;
		Ogre::TextureFilterOptions tfo;
		unsigned int aniso;

		switch (mDetailsPanel->getParamValue(9).asUTF8()[0])
		{
		case 'B':
			newVal = "Trilinear";
			tfo = Ogre::TFO_TRILINEAR;
			aniso = 1;
			break;
		case 'T':
			newVal = "Anisotropic";
			tfo = Ogre::TFO_ANISOTROPIC;
			aniso = 8;
			break;
		case 'A':
			newVal = "None";
			tfo = Ogre::TFO_NONE;
			aniso = 1;
			break;
		default:
			newVal = "Bilinear";
			tfo = Ogre::TFO_BILINEAR;
			aniso = 1;
		}

		Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(tfo);
		Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(aniso);
		mDetailsPanel->setParamValue(9, newVal);
	}
	else if (arg.key == OIS::KC_R)   // cycle polygon rendering mode
	{
		Ogre::String newVal;
		Ogre::PolygonMode pm;

		switch (mCamera->getPolygonMode())
		{
		case Ogre::PM_SOLID:
			newVal = "Wireframe";
			pm = Ogre::PM_WIREFRAME;
			break;
		case Ogre::PM_WIREFRAME:
			newVal = "Points";
			pm = Ogre::PM_POINTS;
			break;
		default:
			newVal = "Solid";
			pm = Ogre::PM_SOLID;
		}

		mCamera->setPolygonMode(pm);
		mDetailsPanel->setParamValue(10, newVal);
	}
	else if(arg.key == OIS::KC_F5)   // refresh all textures
	{
		Ogre::TextureManager::getSingleton().reloadAll();
	}
	else if (arg.key == OIS::KC_SYSRQ)   // take a screenshot
	{
		mWindow->writeContentsToTimestampedFile("screenshot", ".jpg");
	}
	else if (arg.key == OIS::KC_ESCAPE)
	{
		mShutDown = true;
	}

	return true;
}

bool minGrid::keyReleased( const OIS::KeyEvent &arg )
{
	return true;
}

//Adjust mouse clipping area
void minGrid::windowResized(Ogre::RenderWindow* rw)
{
	unsigned int width, height, depth;
	int left, top;
	rw->getMetrics(width, height, depth, left, top);
}

//Unattach OIS before window shutdown (very important under Linux)
void minGrid::windowClosed(Ogre::RenderWindow* rw)
{
	//Only close for window that created OIS (the main window in these demos)
	if( rw == mWindow )
	{
		if( mInputManager )
		{
			mInputManager->destroyInputObject( mMouse );
			mInputManager->destroyInputObject( mKeyboard );

			OIS::InputManager::destroyInputSystem(mInputManager);
			mInputManager = 0;
		}
	}
}
MinGrid.h

Code: Select all

#ifndef __minGrid_h_
#define __minGrid_h_

#include "BaseApplication.h"
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#include "../res/resource.h"
#endif

#include <OgreCamera.h>
#include <OgreEntity.h>
#include <OgreLogManager.h>
#include <OgreRoot.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreConfigFile.h>

#include <OISEvents.h>
#include <OISInputManager.h>
#include <OISKeyboard.h>
#include <OISMouse.h>

#include <SdkTrays.h>
#include <SdkCameraMan.h>

#include "ViewportGrid.h"

class minGrid : public Ogre::FrameListener, public OIS::KeyListener, public Ogre::WindowEventListener, OgreBites::SdkTrayListener
{
public:
    minGrid(void);
    virtual ~minGrid(void);

	virtual void go(void);

protected:
	virtual bool setup();
	virtual bool configure(void);
	virtual void chooseSceneManager(void);
	virtual void createCamera(void);
	virtual void createFrameListener(void);
	virtual void createScene(void);
	virtual void destroyScene(void);
	virtual void createViewports(void);
	virtual void setupResources(void);
	virtual void createResourceListener(void);
	virtual void loadResources(void);
	
	// Ogre::FrameListener
	virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
	bool processUnbufferedInput(const Ogre::FrameEvent& evt);

	// OIS::KeyListener
	virtual bool keyPressed( const OIS::KeyEvent &arg );
	virtual bool keyReleased( const OIS::KeyEvent &arg );
	
	// Ogre::WindowEventListener
	//Adjust mouse clipping area
	virtual void windowResized(Ogre::RenderWindow* rw);
	//Unattach OIS before window shutdown (very important under Linux)
	virtual void windowClosed(Ogre::RenderWindow* rw);

	Ogre::Root *mRoot;
	Ogre::Camera* mCamera;
	Ogre::SceneManager* mSceneMgr;
	Ogre::RenderWindow* mWindow;
	Ogre::String mResourcesCfg;
	Ogre::String mPluginsCfg;

	//OIS Input devices
	OIS::InputManager* mInputManager;
	OIS::Mouse*    mMouse;
	OIS::Keyboard* mKeyboard;

	// OgreBites
	OgreBites::SdkTrayManager* mTrayMgr;
	OgreBites::ParamsPanel* mDetailsPanel;   // sample details panel

	Ogre::ViewportGrid* _viewportGrid;
	double m_zoom, m_panX, m_panY, m_panZ;
	bool mShutDown;
};
#endif // #ifndef __minGrid_h_
Post Reply