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

 Irrlicht "сфера и куб"



#include <irrlicht.h>
#include <driverChoice.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

class MyReceiver : public IEventReceiver//обработчик клавы
{
public:
        virtual bool OnEvent(const SEvent& event)
        {
                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()
        {
                for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
                        KeyIsDown[i]= false;
        }

private:
        bool KeyIsDown[KEY_KEY_CODES_COUNT];
};

extern int main(int argc, char** argv)
{
        double angle=3.14159265;
        video::E_DRIVER_TYPE driverType=driverChoiceConsole();
        if (driverType==video::EDT_COUNT)  return 1;
        IrrlichtDevice *device = createDevice(driverType, core::dimension2d< u32 >(640, 480), 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);
        IMeshSceneNode* cube = smgr->addCubeSceneNode();
        cube->setPosition(vector3df(0,0,0));
        cube->setMaterialFlag(video::EMF_LIGHTING, false);
        //cube->setRotation(vector3df(150,50,50));

        cube->setMaterialTexture(0, driver->getTexture("1.jpg"));

        IMeshSceneNode* sphere = smgr->addSphereSceneNode(5,16,cube);
        sphere ->setMaterialFlag(video::EMF_LIGHTING, false);
        sphere ->setPosition(vector3df(10,10,10));
        sphere ->setMaterialTexture(0, driver->getTexture("1.jpg"));

        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));
        while(device->run())
        if (device->isWindowActive())
        {
            if(myRec.IsKeyDown(KEY_UP)) cube->setPosition(cube->getPosition() + vector3df(0.1,0,0));
            if(myRec.IsKeyDown(KEY_DOWN)) cube->setPosition(cube->getPosition() + vector3df(-0.1,0,0));
            if(myRec.IsKeyDown(KEY_LEFT)) cube->setPosition(cube->getPosition() + vector3df(0,0,0.1));
            if(myRec.IsKeyDown(KEY_RIGHT)) cube->setPosition(cube->getPosition() + vector3df(0,0,-0.1));
            sphere->setPosition(vector3df(10*2*cos(angle)/sqrt(2),10,10*sin(angle)*2/sqrt(2)));
            angle+=3.14159265/360;
            camera->setTarget(cube->getPosition());
                driver->beginScene(true, true, SColor(0,3,243,250));
                smgr->drawAll();
                driver->endScene();
        }

        device->drop();

        return 0;
}

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

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