воскресенье, 20 апреля 2014 г.

Irrlicht "обзор мышью"



#define FRAME_W 640
#define FRAME_H 480

#include <irrlicht.h>
#include <driverChoice.h>
#include <iostream>
#include <cmath>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

using namespace std;

class MyReceiver : public IEventReceiver
{
public:
        virtual bool OnEvent(const SEvent& event)
        {
            if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
            {
                 switch(event.MouseInput.Event)
                {
                    case EMIE_LMOUSE_PRESSED_DOWN:
                    MouseState.LeftButtonDown = true;
                    break;

                    case EMIE_LMOUSE_LEFT_UP:
                    MouseState.LeftButtonDown = false;
                    break;

                    case EMIE_MOUSE_MOVED:
                    MouseState.Position.X = event.MouseInput.X;
                    MouseState.Position.Y = event.MouseInput.Y;
                    break;

                    default:
                    // Мы не будем обрабатывать колёсико мыши
                    break;
                }
            }
                if (event.EventType == irr::EET_KEY_INPUT_EVENT)
                        KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
                return false;
        }

        virtual bool IsKeyDown(EKEY_CODE keyCode) const
        {
                return KeyIsDown[keyCode];
        }

        MyReceiver()
        {

                mx=0;my=0;
                for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
                        KeyIsDown[i]= false;
        }
        double getMousedX(){
            double buf=MouseState.Position.X-FRAME_W/2;

            return buf;
        }
        double getMousedY(){
            double buf=MouseState.Position.Y-FRAME_H/2;

            return buf;
        }

        // Это единственный метод, который мы должны переопределить
private:
        double mx,my;
        struct SMouseState
        {
        core::position2di Position;
        bool LeftButtonDown;
        SMouseState() : LeftButtonDown(false) { }
        } MouseState;
        bool KeyIsDown[KEY_KEY_CODES_COUNT];
};

extern int main(int argc, char** argv)
{
        double angle=3.14159265;
        video::E_DRIVER_TYPE driverType=video::EDT_OPENGL;//driverChoiceConsole();
        if (driverType==video::EDT_COUNT)  return 1;
        IrrlichtDevice *device = createDevice(driverType, core::dimension2d< u32 >(FRAME_W, FRAME_H), 16, false);
        if (device == 0) return 1;

        IVideoDriver* driver = device->getVideoDriver();
        ISceneManager* smgr = device->getSceneManager();

        ICameraSceneNode *camera = smgr->addCameraSceneNode(0, vector3df(0,40,0), vector3df(0,0,0));
        MyReceiver myRec;
        device->setEventReceiver(&myRec);
        IAnimatedMesh* plane = smgr->addHillPlaneMesh(
                        "plane",
                        dimension2d< f32 >(10.0f, 10.0f),
                        dimension2d < u32 > (100, 100),
                        0,0,
                        core::dimension2d< f32 >(0.0f, 0.0f),
                        core::dimension2d< f32 >(50.0f, 50.0f)
                    );
                    IAnimatedMeshSceneNode* nplane = smgr->addAnimatedMeshSceneNode(plane);
                    nplane->setMaterialTexture(0, driver->getTexture("2.jpg"));
                    nplane->setMaterialFlag(video::EMF_LIGHTING, false);

        nplane->setPosition(vector3df(0,-20,0));

        double a=-M_PI/4,fi=0;
        double x=camera->getPosition().X+cos(fi);
        double y=camera->getPosition().Y+sin(a);
        double z=camera->getPosition().Z+sin(fi);
        /*cout<< x<< " ";
        cout<< y<< " ";
        cout<< x<< " ";
        cout<< a<< " ";
        cout<< fi<< "\n";*/
        device->getCursorControl()->setVisible(false);
        while(device->run())
        if (device->isWindowActive())
        {
/*
            if(myRec.IsKeyDown(KEY_KEY_W))a+=M_PI/190;
            if(myRec.IsKeyDown(KEY_KEY_S))a+=-M_PI/90;
            if(myRec.IsKeyDown(KEY_KEY_A))fi+=M_PI/90;
            if(myRec.IsKeyDown(KEY_KEY_D))fi+=-M_PI/90;
            */
            fi-=myRec.getMousedX()*M_PI/180;
            a-=myRec.getMousedY()*M_PI/180;
            device->getCursorControl()->setPosition(FRAME_W/2,FRAME_H/2);
            if(a>=M_PI*0.5-0.01){a=M_PI*0.5-0.01;}
            if(a<=-M_PI*0.5+0.01){a=-M_PI*0.5+0.01;}

            x=camera->getPosition().X+cos(fi)*(fabs(cos(a)));
            y=camera->getPosition().Y+sin(a);
            z=camera->getPosition().Z+sin(fi)*(fabs(cos(a)));
            camera->setTarget(vector3df(x,y,z));
            //angle+=3.14159265/360;
            //cout<< myRec.getMousedX()<<" "<<myRec.getMousedY()<<endl;

            //camera->setTarget(cube->getPosition());
                driver->beginScene(true, true, SColor(0,3,243,250));
                smgr->drawAll();
                driver->endScene();
        }

        device->drop();

        return 0;
}

Комментариев нет:

Отправить комментарий