Skip to content

Instantly share code, notes, and snippets.

@paroj
Created September 23, 2022 20:50
Show Gist options
  • Save paroj/084eb0c847e2febf53a60248e48755f6 to your computer and use it in GitHub Desktop.
Save paroj/084eb0c847e2febf53a60248e48755f6 to your computer and use it in GitHub Desktop.
Ogre3D GTK integration
/*-------------------------------------------------------------------------
This source file is a part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2013 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE
-------------------------------------------------------------------------*/
//! [fullsource]
#include "Ogre.h"
#include "OgreApplicationContext.h"
#include "OgreInput.h"
#include "OgreRTShaderSystem.h"
#include <iostream>
#include "gtk/gtk.h"
#include "gdk/gdk.h"
#include "gdk/gdkx.h"
using namespace Ogre;
using namespace OgreBites;
GtkWidget *privHandle;
class BasicTutorial1
: public ApplicationContext
, public InputListener
{
public:
BasicTutorial1();
virtual ~BasicTutorial1() {}
void setup();
bool keyPressed(const KeyboardEvent& evt);
SceneNode *camNode;
const std::string getOgreHandle() const;
};
BasicTutorial1::BasicTutorial1()
: ApplicationContext("OgreTutorialApp")
{
}
void BasicTutorial1::setup()
{
// do not forget to call the base first
ApplicationContext::setup();
addInputListener(this);
// get a pointer to the already created root
Root* root = getRoot();
Ogre::NameValuePairList params;
params["externalWindowHandle"] = getOgreHandle();
//create the render window
//Ogre::RenderWindow* mRenderWindow = createWindow("Basic", 640, 480).render;
Ogre::RenderWindow* mRenderWindow = Ogre::Root::getSingleton().createRenderWindow("Basic", 640, 480, false, &params);
SceneManager* scnMgr = root->createSceneManager();
// register our scene with the RTSS
RTShader::ShaderGenerator* shadergen = RTShader::ShaderGenerator::getSingletonPtr();
shadergen->addSceneManager(scnMgr);
// -- tutorial section start --
//! [turnlights]
scnMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
//! [turnlights]
//! [newlight]
Light* light = scnMgr->createLight("MainLight");
SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
lightNode->attachObject(light);
//! [newlight]
//! [lightpos]
lightNode->setPosition(20, 80, 50);
//! [lightpos]
//! [camera]
camNode = scnMgr->getRootSceneNode()->createChildSceneNode();
// create the camera
Camera* cam = scnMgr->createCamera("myCam");
cam->setNearClipDistance(5); // specific to this sample
cam->setAutoAspectRatio(true);
camNode->attachObject(cam);
camNode->setPosition(0, 0, 140);
mRenderWindow->addViewport(cam);
// and tell it to render into the main window
getRenderWindow()->addViewport(cam);
//! [camera]
//! [entity1]
Entity* ogreEntity = scnMgr->createEntity("ogrehead.mesh");
//! [entity1]
//! [entity1node]
SceneNode* ogreNode = scnMgr->getRootSceneNode()->createChildSceneNode();
//! [entity1node]
//! [entity1nodeattach]
ogreNode->attachObject(ogreEntity);
//! [entity1nodeattach]
//! [cameramove]
camNode->setPosition(0, 47, 222);
//! [cameramove]
//! [entity2]
Entity* ogreEntity2 = scnMgr->createEntity("ogrehead.mesh");
SceneNode* ogreNode2 = scnMgr->getRootSceneNode()->createChildSceneNode(Vector3(14, 48, 0));
ogreNode2->attachObject(ogreEntity2);
//! [entity2]
//! [entity3]
Entity* ogreEntity3 = scnMgr->createEntity("ogrehead.mesh");
SceneNode* ogreNode3 = scnMgr->getRootSceneNode()->createChildSceneNode();
ogreNode3->setPosition(0, 40, 150);
ogreNode3->setScale(2, 1.2, 1);
ogreNode3->attachObject(ogreEntity3);
//! [entity3]
//! [entity4]
Entity* ogreEntity4 = scnMgr->createEntity("ogrehead.mesh");
SceneNode* ogreNode4 = scnMgr->getRootSceneNode()->createChildSceneNode();
ogreNode4->setPosition(-14, 48, -100);
ogreNode4->roll(Degree(-90));
ogreNode4->attachObject(ogreEntity4);
//! [entity4]
// -- tutorial section end --
}
bool BasicTutorial1::keyPressed(const KeyboardEvent& evt)
{
if (evt.keysym.sym == SDLK_ESCAPE)
{
getRoot()->queueEndRendering();
}
camNode->setPosition(1 + camNode->getPosition().x, 47, 222);
return true;
}
static int render_callback(void* data)
{
auto root = (Root*)data;
root->renderOneFrame();
return !root->endRenderingQueued();
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
privHandle = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (privHandle), "GTK Window");
gtk_window_set_default_size (GTK_WINDOW (privHandle), 640, 480);
gtk_widget_show (privHandle);
BasicTutorial1 app;
app.initApp();
g_idle_add(render_callback, app.getRoot());
gtk_main();
app.closeApp();
return 0;
}
const std::string BasicTutorial1::getOgreHandle() const
{
// Handle for GTK-based systems
gtk_widget_realize(privHandle);
// grab the window object
//GdkWindow* gdkWin = gtk_widget_get_window((GtkWidget*)window->GetHandle());
GdkWindow* gdkWin = gtk_widget_get_window(privHandle);
Display* display = GDK_WINDOW_XDISPLAY(gdkWin);
Window wid = GDK_WINDOW_XID(gdkWin);
return std::to_string(wid);
}
//! [fullsource]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment