I play around with GLUT for testing out some stuff, so this is just a rough attempt to run Ogre3D with it.
The basic freelook mouse camera, no warping yet. Drag to look around with WASD controls or change to arrow key codes below.
needs freeglut.dll or glut compatible library to link along with the rest required (no need for OIS).
Code: Select all
#include <Ogre.h>
#include <OgreConfigFile.h>
#include <OgreRenderWindow.h>
#include <OgreEntity.h>
#include <OgreWindowEventUtilities.h>
#include <GL/glut.h>
Ogre::RenderWindow* window;
Ogre::Camera* camera;
Ogre::Root* root;
bool isKeyPressed[256] = { false };
int lastMouseX = 0;
int lastMouseY = 0;
bool isMousePressed = false;
class OgreFrameListener : public Ogre::FrameListener {
public:
OgreFrameListener(Ogre::RenderWindow* window) : mWindow(window) {}
bool frameRenderingQueued(const Ogre::FrameEvent& evt) {
mWindow->update();
return true;
}
private:
Ogre::RenderWindow* mWindow;
};
void updateCameraPosition() {
const float moveSpeed = 5.0f;
Ogre::Vector3 camPosition = camera->getPosition();
Ogre::Quaternion camOrientation = camera->getOrientation();
Ogre::Vector3 camDirection = camOrientation * Ogre::Vector3::UNIT_Z;
Ogre::Vector3 camRight = camOrientation * Ogre::Vector3::UNIT_X;
Ogre::Vector3 camUp = camOrientation * Ogre::Vector3::UNIT_Y;
float x = 0, y = 0, z = 0;
if (isKeyPressed['w'])
z -= moveSpeed;
if (isKeyPressed['s'])
z += moveSpeed;
if (isKeyPressed['a'])
x -= moveSpeed;
if (isKeyPressed['d'])
x += moveSpeed;
if (isKeyPressed['q'])
y += moveSpeed;
if (isKeyPressed['z'])
y -= moveSpeed;
Ogre::Vector3 moveDirection = (camDirection * z) + (camRight * x);
camPosition += moveDirection;
camPosition.y += y;
camera->setPosition(camPosition);
}
void keyboardDown(unsigned char key, int x, int y) {
isKeyPressed[key] = true;
if (key == 27) {
delete root;
exit(0);
}
}
void keyboardUp(unsigned char key, int x, int y) {
isKeyPressed[key] = false;
}
void mouseMove(int x, int y) {
if (isMousePressed) {
int deltaX = x - lastMouseX;
int deltaY = y - lastMouseY;
const float sensitivity = 0.1f;
Ogre::Quaternion camOrientation = camera->getOrientation();
Ogre::Vector3 camRight = camOrientation * Ogre::Vector3::UNIT_X;
Ogre::Vector3 camUp = camOrientation * Ogre::Vector3::UNIT_Y;
Ogre::Quaternion pitchRotation(Ogre::Radian(Ogre::Degree(-deltaY * sensitivity)), camRight);
camOrientation = pitchRotation * camOrientation;
Ogre::Quaternion yawRotation(Ogre::Radian(Ogre::Degree(-deltaX * sensitivity)), Ogre::Vector3::UNIT_Y);
camOrientation = camOrientation * yawRotation;
camera->setOrientation(camOrientation);
}
lastMouseX = x;
lastMouseY = y;
}
void mouseButton(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
isMousePressed = true;
}
else if (state == GLUT_UP) {
isMousePressed = false;
}
}
}
void renderScene() {
Ogre::WindowEventUtilities::messagePump();
Ogre::Root::getSingletonPtr()->renderOneFrame();
}
void display() {
updateCameraPosition();
renderScene();
glutSwapBuffers();
glutPostRedisplay();
}
void reshape(int width, int height) {
if (window) {
window->resize(width, height);
window->windowMovedOrResized();
camera->setAspectRatio(Ogre::Real(width) / Ogre::Real(height));
}
}
int main(int argc, char* argv[]) {
// Initialize Ogre3D
Ogre::Root* root = new Ogre::Root();
root->loadPlugin("RenderSystem_GL.dll");
root->showConfigDialog();
Ogre::RenderSystem* rs = root->getRenderSystemByName("OpenGL Rendering Subsystem");
rs->setConfigOption("Full Screen", "No");
rs->setConfigOption("Video Mode", "1024 x 768");
window = root->initialise(true, "Ogre3D GLUT");
// Set up your scene (this part can be specific to your application)
Ogre::SceneManager* sceneManager = root->createSceneManager(Ogre::ST_GENERIC);
camera = sceneManager->createCamera("MainCamera");
camera->setPosition(Ogre::Vector3(0, 0, 500));
camera->lookAt(Ogre::Vector3(0, 0, 0));
camera->setNearClipDistance(5);
Ogre::Viewport* viewport = window->addViewport(camera);
viewport->setBackgroundColour(Ogre::ColourValue(0.392f, 0.584f, 0.929f));
// Load resources
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("media", "FileSystem");
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
// Create the Ogre frame listener
OgreFrameListener frameListener(window);
root->addFrameListener(&frameListener);
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(1024, 768);
glutCreateWindow("Ogre3D GLUT");
Ogre::Entity* ent = sceneManager->createEntity("MyEntity", "Sinbad.mesh");
sceneManager->getRootSceneNode()->attachObject(ent);
camera->setPosition(Ogre::Vector3(0, 0, 50));
// GLUT callbacks
glutKeyboardFunc(keyboardDown);
glutKeyboardUpFunc(keyboardUp);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMotionFunc(mouseMove);
glutPassiveMotionFunc(mouseMove);
glutMouseFunc(mouseButton);
// Main loop
glutMainLoop();
// Clean up
delete root;
return 0;
}