Need help with my camera snippet. - Solved

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
FlyingSauce
Posts: 18
Joined: Thu Apr 24, 2014 3:01 am

Need help with my camera snippet. - Solved

Post by FlyingSauce »

I'm new to using graphical API's and such in C++, and I've been exploring C++ libraries for years until I finally found something I'm a little more comfortable with. Well, after finishing the 2nd tutorial in the official Irrlicht documentation, I thought I would do a little bit of exercise, and create a camera class.

Code: Select all

 
class camera
{
private:
    float x,y,z;
    bool active;
public:
    void Init(float t, float i, float u, bool a)
    {
        x = t;
        y = i;
        z = u;
        active = a;
    }
    void Activate()
    {
        active = true;
        while(active)
        {
            setActiveCamera(camera);
        }
    }
};
 



Basically, this is supposed to be a camera I can add to the map, and when activated, the player will be forced to view through said instance of camera. Well, thing is, it won't let me call setActiveCamera, because it "wasn't defined in this scope". Any solutions would be greatly appreciated.


Thanks.

-FlyingSauce
Last edited by FlyingSauce on Thu Apr 24, 2014 11:19 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9682
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Need help with my camera snippet.

Post by CuteAlien »

There's a lot you're missing here...

First you have to understand the difference between a class and an object. The best analogy I could think of so far is - think of the class as a stamp - and an object as the picture printed by the stamp on a piece of paper. Or in the case of classes - objects are classes which got printed (or impressed) into parts of your computer memory. When you try to pass a class in setActiveCamera that makes no sense. Because a class is just a description - it's not a camera object in memory. You have to create objects to work with them. If you just want to say - I want the current object of this class - then you can use the "this" pointer. Any object in memory knows where starts itself that way.

The next thing you are having trouble is the cause of your error. When you call setActiveCamera the compiler looks for a function called setActiveCamera. It first searchs in your class (and it's base-classes) - where no such function exists. Then it searchs the global namespace - where also no such function exists. My guess is that you want to call setActiveCamera from the scenemanager. But that function is only valid if you have a scenemanager object. If you check the examples you will see that everywhere - it's never called without an object. So you need to pass a scenemanager object to your class and call the function then from that.

edit: Also forgot: You got an endless loop. while(true)... you can run that until your computer burns out ;-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
FlyingSauce
Posts: 18
Joined: Thu Apr 24, 2014 3:01 am

Re: Need help with my camera snippet.

Post by FlyingSauce »

Hmm... Where should I define Scene Manager?
CuteAlien
Admin
Posts: 9682
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Need help with my camera snippet.

Post by CuteAlien »

Sorry, I can't teach c++ from scratch over a forum. You have to work with some more c++ tutorials/books to get the basics. This code is still far, far away from doing anything useful. There are more errors - like you can only pass cameras which are derived from the irr::scene:: ICameraSceneNode interface. Or well - the code doesn't really _do_ anything - it just sets a few variables. That does approximately as much as if you just write some numbers on your wall at home. It doesn't have any meaning.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
FlyingSauce
Posts: 18
Joined: Thu Apr 24, 2014 3:01 am

Re: Need help with my camera snippet.

Post by FlyingSauce »

I just added a few tweaks, still haven't passed Scene Manager.

Code: Select all

 
 
class camera
{
private:
    float x,y,z;
    bool active;
    char name;
    ICameraSceneNode *viewcam;
public:
    void Init(float t, float i, float u, bool a)
    {
        x = t;
        y = i;
        z = u;
        active = a;
        viewcam = addCameraSceneNode(0, vector3df(x,y,z), vector3df(0,0,0),name);
    }
    void Activate()
    {
        active = true;
        while(active)
        {
            setActiveCamera(viewcam);
        }
    }
};
 
 

I understand, I just needed a little help with passing the SceneManager from my game class to the camera. Sorry if this came across as a "code this for me" thread.
CuteAlien
Admin
Posts: 9682
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Need help with my camera snippet.

Post by CuteAlien »

It doesn't come across as a "code this for me" thread. The problem is that you can't write anything meaningful before you have learned to read. And that code shows you don't understand yet what those c++ code-lines do mean. Not a problem - there are lots of tutorials out there which can help you. But you have to work on understanding c++ basics before we can help you with writing programs.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
FlyingSauce
Posts: 18
Joined: Thu Apr 24, 2014 3:01 am

Re: Need help with my camera snippet.

Post by FlyingSauce »

I finally got it to compile without compiler errors, and you were right, the code does nothing.

Code: Select all

 
class camera
{
private:
    float x,y,z;
    bool active;
    char name;
    ICameraSceneNode *viewcam;
    ISceneManager* smgr;
public:
    void Init(float t, float i, float u, bool a, ISceneManager* scmgr)
    {
        x = t;
        y = i;
        z = u;
        active = a;
        smgr = scmgr;
        viewcam = smgr->addCameraSceneNode(0, vector3df(x,y,z), vector3df(0,0,0),name);
    }
    void Activate()
    {
        active = true;
        smgr->setActiveCamera(viewcam);
    }
    void DeActivate()
    {
        active = false;
        //Will do the rest, later.
    }
};
 
I don't understand. It seemed to me that I had already declared a CamerSceneNode and passed my smgr to the Camera class.


I created a camera object and initialized it in my Game class. Here's the snippet:

Code: Select all

 
    void SetupCameras()
    {
        camera camera;
        camera.Init(5,5,5,true,smgr);
        camera.Activate();
    }
 
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Need help with my camera snippet.

Post by Seven »

camera camera; is not valid and should not compile
FlyingSauce
Posts: 18
Joined: Thu Apr 24, 2014 3:01 am

Re: Need help with my camera snippet.

Post by FlyingSauce »

Changing the name of the instance didn't change anything.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Need help with my camera snippet.

Post by Seven »

also, camera is a local variable that goes out of scope.......

if your testbed is all one file, can you post the entire thing here?
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Need help with my camera snippet.

Post by Seven »

FlyingSauce wrote:Changing the name of the instance didn't change anything.
but in the post above you wrote "I finally got it to compile without compiler errors, and you were right, the code does nothing."

I dont see how it compiled with camera camera; so maybe it was running an old compiled exe but you didnt know that.......
FlyingSauce
Posts: 18
Joined: Thu Apr 24, 2014 3:01 am

Re: Need help with my camera snippet.

Post by FlyingSauce »

game.cpp

Code: Select all

 
 
#include <irrlicht.h>
#include "camera.cpp"
 
using namespace irr;
 
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
#ifdef _MSC_VER
// We'll also define this to stop MSVC complaining about sprintf().
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "Irrlicht.lib")
#endif
 
 
class CGame
{
private:
    ISceneManager* smgr;
public:
    int InitGame()
    {
        IrrlichtDevice *device;
 
        IVideoDriver* driver;
 
        IGUIEnvironment* guienv;
 
 
        device = createDevice(EDT_OPENGL, dimension2d<u32>(640, 480), 16,
                false, false, false, 0);
        device->setWindowCaption(L"Irrlicht Test");
        driver = device->getVideoDriver();
        smgr = device->getSceneManager();
        guienv = device->getGUIEnvironment();
 
        device->getFileSystem()->addFileArchive("../../media/map-20kdm2.pk3");
 
        scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
        scene::ISceneNode* node = 0;
 
 
        if (mesh)
        {
            node = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
        }
        if (node)
        {
            node->setPosition(core::vector3df(-1300,-144,-1249));
        }
 
 
        smgr->addCameraSceneNodeFPS();
        device->getCursorControl()->setVisible(false);
 
 
        while(device->run())
        {
            if (device->isWindowActive())
            {
                driver->beginScene(true, true, video::SColor(255,200,200,200));
                smgr->drawAll();
                driver->endScene();
 
            }
            else
                device->yield();
        }
 
        device->drop();
 
        return 0;
    }
    void SetupCameras()
    {
        camera gcamera;
        gcamera.Init(5,5,5,true,smgr);
        gcamera.Activate();
    }
 
};
 
 
 
 




camera.cpp

Code: Select all

#include <irrlicht.h>
 
using namespace irr;
using namespace core;
using namespace video;
using namespace scene;
 
 
 
class camera
{
private:
    float x,y,z;
    bool active;
    char name;
    ICameraSceneNode *viewcam;
    ISceneManager* smgr;
public:
    void Init(float t, float i, float u, bool a, ISceneManager* scmgr)
    {
        x = t;
        y = i;
        z = u;
        active = a;
        smgr = scmgr;
        viewcam = smgr->addCameraSceneNode(0, vector3df(x,y,z), vector3df(0,0,0),name);
    }
    void Activate()
    {
        active = true;
        smgr->setActiveCamera(viewcam);
    }
    void DeActivate()
    {
        active = false;
        //Will do the rest, later.
    }
};
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Need help with my camera snippet.

Post by Seven »

this all looks ok. what is your main() function?
FlyingSauce
Posts: 18
Joined: Thu Apr 24, 2014 3:01 am

Re: Need help with my camera snippet.

Post by FlyingSauce »

Seven wrote:this all looks ok. what is your main() function?

Code: Select all

int main(int argc, char** argv)
{
    CGame cgame;
    cgame.InitGame();
    cgame.SetupCameras();
    return 0;
}
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Need help with my camera snippet.

Post by Seven »

FlyingSauce wrote:
Seven wrote:this all looks ok. what is your main() function?

Code: Select all

int main(int argc, char** argv)
{
    CGame cgame;
    cgame.InitGame();                         ( this doesnt return until the program is done)
    cgame.SetupCameras();                 ( this gets called once just as the program terminates and should be a memory issue since smgr is not valid anymore)
    return 0;
}

Code: Select all

 
while(device->run())
        {
            if (device->isWindowActive())
            {
                driver->beginScene(true, true, video::SColor(255,200,200,200));
                smgr->drawAll();
                driver->endScene();
 
            }
            else
                device->yield();
        }
 
since InitGame() starts the message pump and doesnt return until the program is over (while device->run()) keeps looping over and over, the setupcameras will never be called........
Post Reply