forked from teamnwah/openmw-tes3coop
item selection working on the preview
parent
eef750e6b0
commit
6ac2a12296
@ -1 +1 @@
|
||||
Subproject commit 7168415233905de2864eec71ed4312cb8f83059b
|
||||
Subproject commit 4750676ac46a7aaa86bca53dc68c5a1ba11f3bc1
|
@ -0,0 +1,8 @@
|
||||
material SelectionColour
|
||||
{
|
||||
pass
|
||||
{
|
||||
vertex_program selection_vertex
|
||||
fragment_program selection_fragment
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
#include "core.h"
|
||||
|
||||
#ifdef SH_VERTEX_SHADER
|
||||
|
||||
SH_BEGIN_PROGRAM
|
||||
shUniform(float4x4, wvp) @shAutoConstant(wvp, worldviewproj_matrix)
|
||||
|
||||
SH_START_PROGRAM
|
||||
{
|
||||
shOutputPosition = shMatrixMult(wvp, shInputPosition);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
SH_BEGIN_PROGRAM
|
||||
shUniform(float4, colour) @shAutoConstant(colour, custom, 1)
|
||||
|
||||
SH_START_PROGRAM
|
||||
{
|
||||
shOutputColour(0) = colour;
|
||||
//shOutputColour(0) = float4(1,0,0,1);
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,15 @@
|
||||
shader_set selection_vertex
|
||||
{
|
||||
source selection.shader
|
||||
type vertex
|
||||
profiles_cg vs_2_0 arbvp1
|
||||
profiles_hlsl vs_2_0
|
||||
}
|
||||
|
||||
shader_set selection_fragment
|
||||
{
|
||||
source selection.shader
|
||||
type fragment
|
||||
profiles_cg ps_2_x ps_2_0 ps arbfp1
|
||||
profiles_hlsl ps_2_0
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
#include "selectionbuffer.hpp"
|
||||
|
||||
#include <OgreHardwarePixelBuffer.h>
|
||||
#include <OgreRenderTexture.h>
|
||||
#include <OgreSubEntity.h>
|
||||
#include <OgreEntity.h>
|
||||
|
||||
#include <extern/shiny/Main/Factory.hpp>
|
||||
|
||||
namespace OEngine
|
||||
{
|
||||
namespace Render
|
||||
{
|
||||
|
||||
SelectionBuffer::SelectionBuffer(Ogre::Camera *camera, int sizeX, int sizeY, int visibilityFlags)
|
||||
{
|
||||
mTexture = Ogre::TextureManager::getSingleton().createManual("SelectionBuffer",
|
||||
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, sizeX, sizeY, 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET);
|
||||
|
||||
mRenderTarget = mTexture->getBuffer()->getRenderTarget();
|
||||
Ogre::Viewport* vp = mRenderTarget->addViewport(camera);
|
||||
vp->setOverlaysEnabled(false);
|
||||
vp->setBackgroundColour(Ogre::ColourValue(0, 0, 0, 0));
|
||||
vp->setShadowsEnabled(false);
|
||||
vp->setMaterialScheme("selectionbuffer");
|
||||
vp->setVisibilityMask (visibilityFlags);
|
||||
mRenderTarget->setActive(true);
|
||||
mRenderTarget->setAutoUpdated (false);
|
||||
|
||||
mCurrentColour = Ogre::ColourValue(0.3, 0.3, 0.3);
|
||||
}
|
||||
|
||||
SelectionBuffer::~SelectionBuffer()
|
||||
{
|
||||
Ogre::TextureManager::getSingleton ().remove("SelectionBuffer");
|
||||
}
|
||||
|
||||
void SelectionBuffer::update ()
|
||||
{
|
||||
Ogre::MaterialManager::getSingleton ().addListener (this);
|
||||
|
||||
mRenderTarget->update();
|
||||
|
||||
Ogre::MaterialManager::getSingleton ().removeListener (this);
|
||||
|
||||
mTexture->convertToImage(mBuffer);
|
||||
}
|
||||
|
||||
int SelectionBuffer::getSelected(int xPos, int yPos)
|
||||
{
|
||||
Ogre::ColourValue clr = mBuffer.getColourAt (xPos, yPos, 0);
|
||||
clr.a = 1;
|
||||
if (mColourMap.find(clr) != mColourMap.end())
|
||||
return mColourMap[clr];
|
||||
else
|
||||
return -1; // nothing selected
|
||||
}
|
||||
|
||||
Ogre::Technique* SelectionBuffer::handleSchemeNotFound (
|
||||
unsigned short schemeIndex, const Ogre::String &schemeName, Ogre::Material *originalMaterial,
|
||||
unsigned short lodIndex, const Ogre::Renderable *rend)
|
||||
{
|
||||
if (schemeName == "selectionbuffer")
|
||||
{
|
||||
sh::Factory::getInstance ()._ensureMaterial ("SelectionColour", "Default");
|
||||
|
||||
Ogre::MaterialPtr m = Ogre::MaterialManager::getSingleton ().getByName("SelectionColour");
|
||||
|
||||
|
||||
if(typeid(*rend) == typeid(Ogre::SubEntity))
|
||||
{
|
||||
const Ogre::SubEntity *subEntity = static_cast<const Ogre::SubEntity *>(rend);
|
||||
int id = subEntity->getParent ()->getUserObjectBindings().getUserAny().get<int>();
|
||||
std::cout << "found ID:" << id << " entity name is " << subEntity->getParent()->getName() << std::endl;
|
||||
bool found = false;
|
||||
Ogre::ColourValue colour;
|
||||
for (std::map<Ogre::ColourValue, int>::iterator it = mColourMap.begin(); it != mColourMap.end(); ++it)
|
||||
{
|
||||
if (it->second == id)
|
||||
{
|
||||
found = true;
|
||||
colour = it->first;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!found)
|
||||
{
|
||||
getNextColour();
|
||||
const_cast<Ogre::SubEntity *>(subEntity)->setCustomParameter(1, Ogre::Vector4(mCurrentColour.r, mCurrentColour.g, mCurrentColour.b, 1.0));
|
||||
mColourMap[mCurrentColour] = id;
|
||||
}
|
||||
else
|
||||
{
|
||||
const_cast<Ogre::SubEntity *>(subEntity)->setCustomParameter(1, Ogre::Vector4(colour.r, colour.g, colour.b, 1.0));
|
||||
}
|
||||
|
||||
assert(m->getTechnique(1));
|
||||
return m->getTechnique(1);
|
||||
}
|
||||
else
|
||||
throw std::runtime_error("selectionbuffer only works with entities");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void SelectionBuffer::getNextColour ()
|
||||
{
|
||||
Ogre::ARGB color = (float(rand()) / float(RAND_MAX)) * std::numeric_limits<Ogre::uint32>::max();
|
||||
|
||||
if (mCurrentColour.getAsARGB () == color)
|
||||
{
|
||||
getNextColour();
|
||||
return;
|
||||
}
|
||||
|
||||
mCurrentColour.setAsARGB(color);
|
||||
mCurrentColour.a = 1;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
#ifndef OENGINE_SELECTIONBUFFER_H
|
||||
#define OENGINE_SELECTIONBUFFER_H
|
||||
|
||||
|
||||
#include <OgreTexture.h>
|
||||
#include <OgreRenderTarget.h>
|
||||
#include <OgreMaterialManager.h>
|
||||
|
||||
namespace OEngine
|
||||
{
|
||||
namespace Render
|
||||
{
|
||||
|
||||
struct cmp_ColourValue
|
||||
{
|
||||
bool operator()(const Ogre::ColourValue &a, const Ogre::ColourValue &b) const
|
||||
{
|
||||
return a.getAsBGRA() < b.getAsBGRA();
|
||||
}
|
||||
};
|
||||
|
||||
class SelectionBuffer : public Ogre::MaterialManager::Listener
|
||||
{
|
||||
public:
|
||||
SelectionBuffer(Ogre::Camera* camera, int sizeX, int sizeY, int visibilityFlags);
|
||||
virtual ~SelectionBuffer();
|
||||
|
||||
int getSelected(int xPos, int yPos);
|
||||
///< @return ID of the selected object
|
||||
|
||||
void update();
|
||||
|
||||
virtual Ogre::Technique* handleSchemeNotFound (
|
||||
unsigned short schemeIndex, const Ogre::String &schemeName, Ogre::Material *originalMaterial,
|
||||
unsigned short lodIndex, const Ogre::Renderable *rend);
|
||||
|
||||
|
||||
private:
|
||||
Ogre::TexturePtr mTexture;
|
||||
Ogre::RenderTexture* mRenderTarget;
|
||||
|
||||
Ogre::Image mBuffer;
|
||||
|
||||
std::map<Ogre::ColourValue, int, cmp_ColourValue> mColourMap;
|
||||
|
||||
Ogre::ColourValue mCurrentColour;
|
||||
|
||||
void getNextColour();
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue