Browse Source

Ogre load mesh, main loop, shutdown

* Updated Cakelisp for on-call-ptr
SDLAttempt
Macoy Madson 1 month ago
parent
commit
cbef228389
6 changed files with 146 additions and 32 deletions
  1. +2
    -2
      Build_Debug.sh
  2. +1
    -1
      Dependencies/cakelisp
  3. +58
    -3
      src/OgreCore.cake
  4. +54
    -24
      src/OgreInitialize.cpp
  5. +13
    -1
      src/OgreInitialize.hpp
  6. +18
    -1
      test/OgreApp.cake

+ 2
- 2
Build_Debug.sh View File

@@ -1,8 +1,8 @@
#!/bin/sh

./Dependencies/cakelisp/bin/cakelisp src/OgreCore.cake \
&& jam -j4 -sDEBUG_BUILD=true libGameLib.a
&& jam -j4 -sDEBUG_BUILD=true libGameLib.a || exit $?

# TODO: Figure out how to remove clean by teaching jam about libGameLib.a (dyn link instead?)
./Dependencies/cakelisp/bin/cakelisp test/OgreApp.cake && cd test \
&& jam clean && jam -j4 -sDEBUG_BUILD=true
&& jam clean && jam -j4 -sDEBUG_BUILD=true || exit $?

+ 1
- 1
Dependencies/cakelisp

@@ -1 +1 @@
Subproject commit 59b5e0e5aa3bda8ecea90060673a3fbaad01a7b4
Subproject commit 5ce4976faec4de4d562a9621b4e69f6dc2225d04

+ 58
- 3
src/OgreCore.cake View File

@@ -1,7 +1,62 @@
(c-import "<stdio.h>"
;; Ogre dependencies
"OgreItem.h"
"OgreMesh.h"
"OgreMesh2.h"
"OgreMeshManager.h"
"OgreMeshManager2.h"
"OgreRoot.h"
"OgreSceneManager.h"
"OgreWindowEventUtilities.h"
;; Not ported over to Cakelisp yet
"OgreInitialize.hpp")

(defun ogre-initialize (&return bool)
(OgreInitialize)
(printf "Ogre initialized\n")
(return true))
(return (OgreInitialize)))

(defun ogre-shutdown ()
(OgreShutdown))

(defun ogre-load-mesh (name (* (const char)))
(var sceneManager (* (in Ogre SceneManager)) (ogreGetSceneManager))
;; Load the v1 mesh. Notice the v1 namespace
;; Also notice the HBU_STATIC flag; since the HBU_WRITE_ONLY
;; bit would prohibit us from reading the data for importing.
(var v1Mesh Ogre::v1::MeshPtr
(on-call (call (in Ogre v1 MeshManager getSingleton)) load
name Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME
Ogre::v1::HardwareBuffer::HBU_STATIC Ogre::v1::HardwareBuffer::HBU_STATIC))

(var halfPosition bool true)
(var halfUVs bool true)
(var useQtangents bool true)

;; Create a v2 mesh to import to, with a different name (arbitrary).
(var mesh-name-v2 (const Ogre::String) (+ name (Ogre::String " Imported")))
;; Import the v1 mesh to v2
(var v2Mesh Ogre::MeshPtr
(on-call (call (in Ogre MeshManager getSingleton)) createByImportingV1
mesh-name-v2 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
(on-call v1Mesh get)
halfPosition halfUVs useQtangents))

;; We don't need the v1 mesh. Free CPU memory, get it out of the GPU.
;; Leave it loaded if you want to use athene with v1 Entity.
(on-call-ptr v1Mesh unload)

;; Create an Item with the model we just imported.
;; Notice we use the name of the imported model. We could also use the overload
;; with the mesh pointer:
;; item = sceneManager->createItem( v2Mesh, Ogre::SCENE_DYNAMIC );
(var item (* Ogre::Item) (on-call-ptr sceneManager createItem
mesh-name-v2 Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME
Ogre::SCENE_DYNAMIC)))

;; If false is returned, you should break from the main loop (the window has been closed)
(defun ogre-handle-window-events (&return bool)
(call (in Ogre WindowEventUtilities messagePump))
(return (not g_ogreWindowShouldQuit)))

;; If false is returned, you should break from the main loop
(defun ogre-render-frame (&return bool)
(return (on-call-ptr g_ogreRoot renderOneFrame)))

+ 54
- 24
src/OgreInitialize.cpp View File

@@ -19,11 +19,26 @@
#include "OgreMeshManager2.h"

#include "Compositor/OgreCompositorManager2.h"
#include "OgreWindowEventUtilities.h"
#pragma GCC diagnostic pop

static Ogre::Root* root = nullptr;
Ogre::Root* g_ogreRoot = nullptr;
static Ogre::SceneManager* g_sceneManager = nullptr;
static Ogre::Window* g_window = nullptr;

bool g_graphicsIntialized = false;
bool g_ogreWindowShouldQuit = false;

class MyWindowEventListener : public Ogre::WindowEventListener
{
public:
virtual void windowClosed(Ogre::Window* window)
{
g_ogreWindowShouldQuit = true;
}
};

MyWindowEventListener g_myWindowEventListener;

static void registerHlms(void)
{
@@ -118,7 +133,7 @@ static void registerHlms(void)
}
}

void OgreInitialize()
bool OgreInitialize()
{
using namespace Ogre;

@@ -132,21 +147,21 @@ void OgreInitialize()
const char* pluginsFile = "plugins.cfg";
#endif
#endif
root = OGRE_NEW Root(pluginsFolder + pluginsFile, //
writeAccessFolder + "ogre.cfg", //
writeAccessFolder + "Ogre.log");
g_ogreRoot = OGRE_NEW Root(pluginsFolder + pluginsFile, //
writeAccessFolder + "ogre.cfg", //
writeAccessFolder + "Ogre.log");

// This allows the user to configure the graphics. It's damn annoying during dev though
// TODO: Make this return false and quit the app
// if (!root->showConfigDialog())
// return; // false
// if (!g_ogreRoot->showConfigDialog())
// return false;

// Initialize Root
RenderSystem* renderSystem = root->getRenderSystemByName("OpenGL 3+ Rendering Subsystem");
RenderSystem* renderSystem = g_ogreRoot->getRenderSystemByName("OpenGL 3+ Rendering Subsystem");
if (!renderSystem)
{
printf("Render system not found!\n");
return;
return false;
}

// renderSystem->setConfigOption("Display Frequency", "N/A");
@@ -154,8 +169,8 @@ void OgreInitialize()
// renderSystem->setConfigOption("VSync", "Yes");
renderSystem->setConfigOption("Video Mode", "1920 x 1080");
renderSystem->setConfigOption("sRGB Gamma Conversion", "Yes");
root->setRenderSystem(renderSystem);
Window* window = root->initialise(/*autoCreateWindow=*/true, "GameLib");
g_ogreRoot->setRenderSystem(renderSystem);
g_window = g_ogreRoot->initialise(/*autoCreateWindow=*/true, "GameLib");

// Old bad attempt at Ogre within SFML (only party worked)
{
@@ -183,20 +198,20 @@ void OgreInitialize()
// int winHeight = 1080;

// Window* window =
// // root->createRenderWindow("GameLib", winWidth, winHeight, true, &windowSettings);
// root->createRenderWindow("GameLib", winWidth, winHeight, true);
// // g_ogreRoot->createRenderWindow("GameLib", winWidth, winHeight, true, &windowSettings);
// g_ogreRoot->createRenderWindow("GameLib", winWidth, winHeight, true);

// window->_setVisible(true);
// g_window->_setVisible(true);
}

registerHlms();

// Create SceneManager
const size_t numThreads = 1u;
SceneManager* sceneManager = root->createSceneManager(ST_GENERIC, numThreads, "SceneManager");
g_sceneManager = g_ogreRoot->createSceneManager(ST_GENERIC, numThreads, "SceneManager");

// Create & setup camera
Camera* camera = sceneManager->createCamera("Main Camera");
Camera* camera = g_sceneManager->createCamera("Main Camera");

// Position it at 500 in Z direction
camera->setPosition(Vector3(0, 5, 15));
@@ -207,12 +222,12 @@ void OgreInitialize()
camera->setAutoAspectRatio(true);

// Setup a basic compositor with a blue clear colour
CompositorManager2* compositorManager = root->getCompositorManager2();
CompositorManager2* compositorManager = g_ogreRoot->getCompositorManager2();
const String workspaceName("Main Workspace");
// const IdString definitionNameId = workspaceName;
const ColourValue backgroundColour(0.2f, 0.4f, 0.6f);
compositorManager->createBasicWorkspaceDef(workspaceName, backgroundColour, IdString());
compositorManager->addWorkspace(sceneManager, window->getTexture(), camera, workspaceName,
compositorManager->addWorkspace(g_sceneManager, g_window->getTexture(), camera, workspaceName,
true);

// Mesh importing
@@ -247,15 +262,15 @@ void OgreInitialize()
// Create an Item with the model we just imported.
// Notice we use the name of the imported model. We could also use the overload
// with the mesh pointer:
// item = sceneManager->createItem( v2Mesh, Ogre::SCENE_DYNAMIC );
Ogre::Item* item = sceneManager->createItem(
// item = g_sceneManager->createItem( v2Mesh, Ogre::SCENE_DYNAMIC );
Ogre::Item* item = g_sceneManager->createItem(
"Chassis.mesh Imported", Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME,
Ogre::SCENE_DYNAMIC);
// Ogre::SceneNode* rootSceneNode = sceneManager->getRootSceneNode(Ogre::SCENE_DYNAMIC);
Ogre::SceneNode* rootSceneNode = sceneManager->getRootSceneNode();
// Ogre::SceneNode* rootSceneNode = g_sceneManager->getRootSceneNode(Ogre::SCENE_DYNAMIC);
Ogre::SceneNode* rootSceneNode = g_sceneManager->getRootSceneNode();
if (!rootSceneNode)
{
return;
return false;
}
// WTF - this should work, but it's almost like the root scene node's vtable is wrong
// Ogre::SceneNode* sceneNode = rootSceneNode->createChildSceneNode(Ogre::SCENE_DYNAMIC);
@@ -263,11 +278,26 @@ void OgreInitialize()
rootSceneNode->createChild()); // rootSceneNode->createChildSceneNode();
if (!sceneNode)
{
return;
return false;
}
sceneNode->attachObject(item);
// sceneNode->scale( 0.1f, 0.1f, 0.1f );
}

Ogre::WindowEventUtilities::addWindowEventListener(g_window, &g_myWindowEventListener);

g_graphicsIntialized = true;
return true;
}

void OgreShutdown()
{
Ogre::WindowEventUtilities::removeWindowEventListener(g_window, &g_myWindowEventListener);
OGRE_DELETE g_ogreRoot;
g_ogreRoot = nullptr;
}

Ogre::SceneManager* ogreGetSceneManager()
{
return g_sceneManager;
}

+ 13
- 1
src/OgreInitialize.hpp View File

@@ -1,3 +1,15 @@
#pragma once

void OgreInitialize();
bool OgreInitialize();
void OgreShutdown();

namespace Ogre
{
class Root;
class SceneManager;
} // namespace Ogre

Ogre::SceneManager* ogreGetSceneManager();

extern Ogre::Root* g_ogreRoot;
extern bool g_ogreWindowShouldQuit;

+ 18
- 1
test/OgreApp.cake View File

@@ -4,6 +4,23 @@
(c-import "<stdio.h>")

(defun main (&return int)
(printf "Hello, world!\n")
;; Ogre uses exceptions for error handling, so we can't gracefully close without getting all that
;; stuff set up (which I don't really want to do; it belongs in Gamelib)
(ogre-initialize)
;; (ogre-load-mesh "Test.mesh")

(var exit-reason (* (const char)) nullptr)

;; Main loop
(while true
(unless (ogre-handle-window-events)
(set exit-reason "Window closed")
(break))
(unless (ogre-render-frame)
(set exit-reason "Failed to render frame")
(break)))

(ogre-shutdown)
(when exit-reason
(printf "Exit reason: %s\n" exit-reason))
(return 0))

Loading…
Cancel
Save