//! Sets if the window should be resizable in windowed mode.
void CIrrDeviceWinCE::setResizable(bool resize)
{
	if (ExternalWindow || !getVideoDriver() || CreationParams.Fullscreen)
		return;

	LONG style = WS_POPUP;

	if (!resize)
		style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
	else
		style = WS_THICKFRAME | WS_SYSMENU | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MAXIMIZEBOX;

	if (!SetWindowLong(HWnd, GWL_STYLE, style))
		os::Printer::log("Could not change window style.");

	RECT clientSize;
	clientSize.top = 0;
	clientSize.left = 0;
	clientSize.right = getVideoDriver()->getScreenSize().Width;
	clientSize.bottom = getVideoDriver()->getScreenSize().Height;

	AdjustWindowRectEx(&clientSize, style, FALSE, 0);

	const s32 realWidth = clientSize.right - clientSize.left;
	const s32 realHeight = clientSize.bottom - clientSize.top;

	const s32 windowLeft = (GetSystemMetrics(SM_CXSCREEN) - realWidth) / 2;
	const s32 windowTop = (GetSystemMetrics(SM_CYSCREEN) - realHeight) / 2;

	SetWindowPos(HWnd, HWND_TOP, windowLeft, windowTop, realWidth, realHeight,
		SWP_FRAMECHANGED | SWP_NOMOVE | SWP_SHOWWINDOW);
}
void CIrrDeviceWinCE::resizeIfNecessary()
{
	if (!Resized)
		return;

	RECT r;
	GetClientRect(HWnd, &r);

	char tmp[255];

	if (r.right < 2 || r.bottom < 2)
	{
		sprintf(tmp, "Ignoring resize operation to (%ld %ld)", r.right, r.bottom);
		os::Printer::log(tmp);
	}
	else
	{
		sprintf(tmp, "Resizing window (%ld %ld)", r.right, r.bottom);
		os::Printer::log(tmp);

		getVideoDriver()->OnResize(irr::core::dimension2d<irr::u32>(r.right, r.bottom));
		getWin32CursorControl()->OnResize(getVideoDriver()->getScreenSize());
	}

	Resized = false;
}
예제 #3
0
void AbstractHMDSequence::initScene()
{
	auto device = Lib::device;
	auto driver = device->getVideoDriver();
	auto smgr = device->getSceneManager();

	// Create world
	// fps camera node를 쓰면 StereoRenderer+HeadTracking 박살난다
	smgr->addCameraSceneNode();

	// load the quake map
	printf("%i\n", (int)device->getFileSystem()->addFileArchive("ext/irrlicht/media/map-20kdm2.pk3"));
	irr::scene::IAnimatedMesh *q3mesh = smgr->getMesh("20kdm2.bsp");
	irr::scene::ISceneNode *q3levelNode = smgr->addOctreeSceneNode(q3mesh->getMesh(0), 0, -1, 128);
	q3levelNode->setPosition(irr::core::vector3df(-1350,-90,-1400));

	// load a faerie
	irr::scene::IAnimatedMesh *faerie = smgr->getMesh("ext/irrlicht/media/faerie.md2");
	irr::scene::IAnimatedMeshSceneNode *faerieNode = smgr->addAnimatedMeshSceneNode(faerie);
	faerieNode->setMaterialTexture(0, driver->getTexture("ext/irrlicht/media/faerie2.bmp"));
	faerieNode->setMaterialFlag(irr::video::EMF_LIGHTING, false);
	faerieNode->setPosition(irr::core::vector3df(40,190,-1030));
	faerieNode->setRotation(irr::core::vector3df(0,-90,0));
	faerieNode->setMD2Animation(irr::scene::EMAT_SALUTE);

	// load a dwarf
	irr::scene::IAnimatedMesh *dwarf = smgr->getMesh("ext/irrlicht/media/dwarf.x");
	irr::scene::IAnimatedMeshSceneNode *dwarfNode = smgr->addAnimatedMeshSceneNode(dwarf);
	dwarfNode->setPosition(irr::core::vector3df(40,-25,20));
}
예제 #4
0
void CIrrDeviceWin32::resizeIfNecessary()
{
	if (!Resized)
		return;

	RECT r;
	GetClientRect(HWnd, &r);

	char tmp[255];

	if (r.right < 2 || r.bottom < 2)
	{
		sprintf(tmp, "Ignoring resize operation to (%d %d)", r.right, r.bottom);
		os::Printer::log(tmp);
	}
	else
	{
		sprintf(tmp, "Resizing window (%d %d)", r.right, r.bottom);
		os::Printer::log(tmp);

		if ( r.right % 2 )
			r.right += 1;

		if ( r.bottom % 2 )
			r.bottom += 1;

		getVideoDriver()->OnResize(irr::core::dimension2d<irr::s32>(r.right, r.bottom));
	}

	Resized = false;
}
예제 #5
0
파일: render.cpp 프로젝트: jammm/stk-code
void IrrDriver::generateSkyboxCubemap()
{
    glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);

    glGenTextures(1, &SkyboxCubeMap);
    glGenTextures(1, &ConvolutedSkyboxCubeMap);

    GLint w = 0, h = 0;
    for (unsigned i = 0; i < 6; i++)
    {
        w = MAX2(w, SkyboxTextures[i]->getOriginalSize().Width);
        h = MAX2(h, SkyboxTextures[i]->getOriginalSize().Height);
    }

    const unsigned texture_permutation[] = { 2, 3, 0, 1, 5, 4 };
    char *rgba[6];
    for (unsigned i = 0; i < 6; i++)
        rgba[i] = new char[w * h * 4];
    for (unsigned i = 0; i < 6; i++)
    {
        unsigned idx = texture_permutation[i];

        video::IImage* image = getVideoDriver()->createImageFromData(
                                   SkyboxTextures[idx]->getColorFormat(),
                                   SkyboxTextures[idx]->getSize(),
                                   SkyboxTextures[idx]->lock(),
                                   false
                               );
        SkyboxTextures[idx]->unlock();

        image->copyToScaling(rgba[i], w, h);
        image->drop();

        glBindTexture(GL_TEXTURE_CUBE_MAP, SkyboxCubeMap);
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)rgba[i]);
    }

    testSH(rgba, w, h, blueSHCoeff, greenSHCoeff, redSHCoeff);

    for (unsigned i = 0; i < 6; i++)
    {
        glBindTexture(GL_TEXTURE_CUBE_MAP, ConvolutedSkyboxCubeMap);
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)rgba[i]);
    }
    for (unsigned i = 0; i < 6; i++)
        delete[] rgba[i];
    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
예제 #6
0
void ExtShaders::InitInstance( const IrrEngineUPtr& engine )
{
	auto& imp_ = *ExtShaders::GetInstance().ImpUPtr_;
	
	auto device = engine->GetDevice();

	{
		auto selectionCB = new LuminanceCB;
		auto material = device->getVideoDriver()->getGPUProgrammingServices()->addHighLevelShaderMaterial(LuminanceCB::GetVertexShader(), LuminanceCB::GetPixelShader(), selectionCB);
		selectionCB->drop();
		imp_.ShaderMap_.emplace(EST_LUMINANCE, material);
	}

	{
		auto blueLine = new LineColorCB;
		auto material = device->getVideoDriver()->getGPUProgrammingServices()->addHighLevelShaderMaterial(LineColorCB::GetVertexShader(), LineColorCB::GetPixelShader(), blueLine);
		blueLine->drop();
		imp_.ShaderMap_.emplace(EST_LINE, material);
	}

	{
		auto vertexAlpha = new VertexAlphaCB;
		auto material = device->getVideoDriver()->getGPUProgrammingServices()->addHighLevelShaderMaterial(VertexAlphaCB::GetVertexShader(), VertexAlphaCB::GetPixelShader(), vertexAlpha, irr::video::EMT_TRANSPARENT_VERTEX_ALPHA);
		vertexAlpha->drop();
		imp_.ShaderMap_.emplace(EST_VERTEX_ALPHA, material);
	}

	{
		auto font = new FontColorCB;
		auto material = device->getVideoDriver()->getGPUProgrammingServices()->addHighLevelShaderMaterial(FontColorCB::GetVertexShader(), FontColorCB::GetPixelShader(), font, irr::video::EMT_TRANSPARENT_VERTEX_ALPHA);
		font->drop();
		imp_.ShaderMap_.emplace(EST_FONT, material);
	}

	{
		auto ads = new ADSLightCB;
		auto material = device->getVideoDriver()->getGPUProgrammingServices()->addHighLevelShaderMaterial(ADSLightCB::GetVertexShader(), ADSLightCB::GetPixelShader(), ads);
		ads->drop();
		imp_.ShaderMap_.emplace(EST_ADS_LIGHT, material);

		auto ftr = [ads](const SRenderContextSPtr& rc)
		{
			ads->SetSmgr(rc->Smgr_.get());
		};

		engine->Register(ftr);
	}

	{
		auto picking = new PickingColorCB;
		auto material = device->getVideoDriver()->getGPUProgrammingServices()->addHighLevelShaderMaterial(PickingColorCB::GetVertexShader(), PickingColorCB::GetPixelShader(), picking);
		picking->drop();
		imp_.ShaderMap_.emplace(EST_PICKING, material);
	}
}
예제 #7
0
void IrrDriver::computeCameraMatrix(scene::ICameraSceneNode * const camnode, size_t width, size_t height)
{
    static_cast<scene::CSceneManager *>(m_scene_manager)->OnAnimate(os::Timer::getTime());
    camnode->render();
    irr_driver->setProjMatrix(irr_driver->getVideoDriver()->getTransform(video::ETS_PROJECTION));
    irr_driver->setViewMatrix(irr_driver->getVideoDriver()->getTransform(video::ETS_VIEW));
    irr_driver->genProjViewMatrix();

    m_current_screen_size = core::vector2df(float(width), float(height));

    const float oldfar = camnode->getFarValue();
    const float oldnear = camnode->getNearValue();
    float FarValues[] =
    {
        6.,
        21.,
        55.,
        150.,
    };
    float NearValues[] =
    {
        oldnear,
        5.,
        20.,
        50.,
    };


    float tmp[16 * 9 + 2];
    memcpy(tmp, irr_driver->getViewMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[16], irr_driver->getProjMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[32], irr_driver->getInvViewMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[48], irr_driver->getInvProjMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[64], irr_driver->getProjViewMatrix().pointer(), 16 * sizeof(float));

    const core::matrix4 &SunCamViewMatrix = m_suncam->getViewMatrix();
    for (unsigned i = 0; i < 4; i++)
    {
        if (!m_shadow_camnodes[i])
            m_shadow_camnodes[i] = (scene::ICameraSceneNode *) m_suncam->clone();
    }
    sun_ortho_matrix.clear();

    if (World::getWorld() && World::getWorld()->getTrack())
    {
        btVector3 btmin, btmax;
        if (World::getWorld()->getTrack()->getPtrTriangleMesh())
        {
            World::getWorld()->getTrack()->getTriangleMesh().getCollisionShape().getAabb(btTransform::getIdentity(), btmin, btmax);
        }
        const Vec3 vmin = btmin , vmax = btmax;

        // Build the 3 ortho projection (for the 3 shadow resolution levels)
        for (unsigned i = 0; i < 4; i++)
        {
            camnode->setFarValue(FarValues[i]);
            camnode->setNearValue(NearValues[i]);
            camnode->render();
            const scene::SViewFrustum *frustrum = camnode->getViewFrustum();
            float tmp[24] = {
                frustrum->getFarLeftDown().X,
                frustrum->getFarLeftDown().Y,
                frustrum->getFarLeftDown().Z,
                frustrum->getFarLeftUp().X,
                frustrum->getFarLeftUp().Y,
                frustrum->getFarLeftUp().Z,
                frustrum->getFarRightDown().X,
                frustrum->getFarRightDown().Y,
                frustrum->getFarRightDown().Z,
                frustrum->getFarRightUp().X,
                frustrum->getFarRightUp().Y,
                frustrum->getFarRightUp().Z,
                frustrum->getNearLeftDown().X,
                frustrum->getNearLeftDown().Y,
                frustrum->getNearLeftDown().Z,
                frustrum->getNearLeftUp().X,
                frustrum->getNearLeftUp().Y,
                frustrum->getNearLeftUp().Z,
                frustrum->getNearRightDown().X,
                frustrum->getNearRightDown().Y,
                frustrum->getNearRightDown().Z,
                frustrum->getNearRightUp().X,
                frustrum->getNearRightUp().Y,
                frustrum->getNearRightUp().Z,
            };
            memcpy(m_shadows_cam[i], tmp, 24 * sizeof(float));
            const core::aabbox3df smallcambox = camnode->
                getViewFrustum()->getBoundingBox();
            core::aabbox3df trackbox(vmin.toIrrVector(), vmax.toIrrVector() -
                core::vector3df(0, 30, 0));

            // Set up a nice ortho projection that contains our camera frustum
            core::aabbox3df box = smallcambox;
            box = box.intersect(trackbox);


            std::vector<vector3df> vectors;
            vectors.push_back(frustrum->getFarLeftDown());
            vectors.push_back(frustrum->getFarLeftUp());
            vectors.push_back(frustrum->getFarRightDown());
            vectors.push_back(frustrum->getFarRightUp());
            vectors.push_back(frustrum->getNearLeftDown());
            vectors.push_back(frustrum->getNearLeftUp());
            vectors.push_back(frustrum->getNearRightDown());
            vectors.push_back(frustrum->getNearRightUp());

/*            SunCamViewMatrix.transformBoxEx(trackbox);
            SunCamViewMatrix.transformBoxEx(box);

            core::vector3df extent = box.getExtent();
            const float w = fabsf(extent.X);
            const float h = fabsf(extent.Y);
            float z = box.MaxEdge.Z;

            // Snap to texels
            const float units_per_w = w / 1024;
            const float units_per_h = h / 1024;*/

            m_shadow_camnodes[i]->setProjectionMatrix(getTighestFitOrthoProj(SunCamViewMatrix, vectors) , true);
            m_shadow_camnodes[i]->render();

            sun_ortho_matrix.push_back(getVideoDriver()->getTransform(video::ETS_PROJECTION) * getVideoDriver()->getTransform(video::ETS_VIEW));
        }

        {
            core::aabbox3df trackbox(vmin.toIrrVector(), vmax.toIrrVector() -
                core::vector3df(0, 30, 0));
            if (trackbox.MinEdge.X != trackbox.MaxEdge.X &&
                trackbox.MinEdge.Y != trackbox.MaxEdge.Y &&
                // Cover the case where SunCamViewMatrix is null
                SunCamViewMatrix.getScale() != core::vector3df(0., 0., 0.))
            {
                SunCamViewMatrix.transformBoxEx(trackbox);
                core::matrix4 tmp_matrix;
                tmp_matrix.buildProjectionMatrixOrthoLH(trackbox.MinEdge.X, trackbox.MaxEdge.X,
                    trackbox.MaxEdge.Y, trackbox.MinEdge.Y,
                    30, trackbox.MaxEdge.Z);
                m_suncam->setProjectionMatrix(tmp_matrix, true);
                m_suncam->render();
            }
            rsm_matrix = getVideoDriver()->getTransform(video::ETS_PROJECTION) * getVideoDriver()->getTransform(video::ETS_VIEW);
        }
        rh_extend = core::vector3df(128, 64, 128);
        core::vector3df campos = camnode->getAbsolutePosition();
        core::vector3df translation(8 * floor(campos.X / 8), 8 * floor(campos.Y / 8), 8 * floor(campos.Z / 8));
        rh_matrix.setTranslation(translation);


        assert(sun_ortho_matrix.size() == 4);
        camnode->setNearValue(oldnear);
        camnode->setFarValue(oldfar);
        camnode->render();

        size_t size = irr_driver->getShadowViewProj().size();
        for (unsigned i = 0; i < size; i++)
            memcpy(&tmp[16 * i + 80], irr_driver->getShadowViewProj()[i].pointer(), 16 * sizeof(float));
    }

    tmp[144] = float(width);
    tmp[145] = float(height);
    glBindBuffer(GL_UNIFORM_BUFFER, SharedObject::ViewProjectionMatrixesUBO);
    glBufferSubData(GL_UNIFORM_BUFFER, 0, (16 * 9 + 2) * sizeof(float), tmp);
}
예제 #8
0
/** Generate View, Projection, Inverse View, Inverse Projection, ViewProjection and InverseProjection matrixes
and matrixes and cameras for the four shadow cascade and RSM.
*   \param camnode point of view used
*   \param width of the rendering viewport
*   \param height of the rendering viewport
*/
void IrrDriver::computeMatrixesAndCameras(scene::ICameraSceneNode * const camnode, size_t width, size_t height)
{
    if (CVS->isSDSMEnabled())
        UpdateSplitAndLightcoordRangeFromComputeShaders(width, height);
    static_cast<scene::CSceneManager *>(m_scene_manager)->OnAnimate(os::Timer::getTime());
    camnode->render();
    irr_driver->setProjMatrix(irr_driver->getVideoDriver()->getTransform(video::ETS_PROJECTION));
    irr_driver->setViewMatrix(irr_driver->getVideoDriver()->getTransform(video::ETS_VIEW));
    irr_driver->genProjViewMatrix();

    m_current_screen_size = core::vector2df(float(width), float(height));

    const float oldfar = camnode->getFarValue();
    const float oldnear = camnode->getNearValue();
    float FarValues[] =
    {
        shadowSplit[1],
        shadowSplit[2],
        shadowSplit[3],
        shadowSplit[4],
    };
    float NearValues[] =
    {
        shadowSplit[0],
        shadowSplit[1],
        shadowSplit[2],
        shadowSplit[3]
    };

    float tmp[16 * 9 + 2];
    memcpy(tmp, irr_driver->getViewMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[16], irr_driver->getProjMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[32], irr_driver->getInvViewMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[48], irr_driver->getInvProjMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[64], irr_driver->getProjViewMatrix().pointer(), 16 * sizeof(float));

    m_suncam->render();
    for (unsigned i = 0; i < 4; i++)
    {
        if (m_shadow_camnodes[i])
            delete m_shadow_camnodes[i];
        m_shadow_camnodes[i] = (scene::ICameraSceneNode *) m_suncam->clone();
    }
    sun_ortho_matrix.clear();
    const core::matrix4 &SunCamViewMatrix = m_suncam->getViewMatrix();

    if (World::getWorld() && World::getWorld()->getTrack())
    {
        // Compute track extent
        btVector3 btmin, btmax;
        if (World::getWorld()->getTrack()->getPtrTriangleMesh())
        {
            World::getWorld()->getTrack()->getTriangleMesh().getCollisionShape().getAabb(btTransform::getIdentity(), btmin, btmax);
        }
        const Vec3 vmin = btmin, vmax = btmax;
        core::aabbox3df trackbox(vmin.toIrrVector(), vmax.toIrrVector() -
            core::vector3df(0, 30, 0));

        // Shadow Matrixes and cameras
        for (unsigned i = 0; i < 4; i++)
        {
            core::matrix4 tmp_matrix;

            camnode->setFarValue(FarValues[i]);
            camnode->setNearValue(NearValues[i]);
            camnode->render();
            const scene::SViewFrustum *frustrum = camnode->getViewFrustum();
            float tmp[24] = {
                frustrum->getFarLeftDown().X,
                frustrum->getFarLeftDown().Y,
                frustrum->getFarLeftDown().Z,
                frustrum->getFarLeftUp().X,
                frustrum->getFarLeftUp().Y,
                frustrum->getFarLeftUp().Z,
                frustrum->getFarRightDown().X,
                frustrum->getFarRightDown().Y,
                frustrum->getFarRightDown().Z,
                frustrum->getFarRightUp().X,
                frustrum->getFarRightUp().Y,
                frustrum->getFarRightUp().Z,
                frustrum->getNearLeftDown().X,
                frustrum->getNearLeftDown().Y,
                frustrum->getNearLeftDown().Z,
                frustrum->getNearLeftUp().X,
                frustrum->getNearLeftUp().Y,
                frustrum->getNearLeftUp().Z,
                frustrum->getNearRightDown().X,
                frustrum->getNearRightDown().Y,
                frustrum->getNearRightDown().Z,
                frustrum->getNearRightUp().X,
                frustrum->getNearRightUp().Y,
                frustrum->getNearRightUp().Z,
            };
            memcpy(m_shadows_cam[i], tmp, 24 * sizeof(float));

            std::vector<vector3df> vectors = getFrustrumVertex(*frustrum);
            tmp_matrix = getTighestFitOrthoProj(SunCamViewMatrix, vectors, m_shadow_scales[i]);


            m_shadow_camnodes[i]->setProjectionMatrix(tmp_matrix, true);
            m_shadow_camnodes[i]->render();

            sun_ortho_matrix.push_back(getVideoDriver()->getTransform(video::ETS_PROJECTION) * getVideoDriver()->getTransform(video::ETS_VIEW));
        }

        // Rsm Matrix and camera
        if (!m_rsm_matrix_initialized)
        {
            if (trackbox.MinEdge.X != trackbox.MaxEdge.X &&
                trackbox.MinEdge.Y != trackbox.MaxEdge.Y &&
                // Cover the case where SunCamViewMatrix is null
                SunCamViewMatrix.getScale() != core::vector3df(0., 0., 0.))
            {
                SunCamViewMatrix.transformBoxEx(trackbox);
                core::matrix4 tmp_matrix;
                tmp_matrix.buildProjectionMatrixOrthoLH(trackbox.MinEdge.X, trackbox.MaxEdge.X,
                    trackbox.MaxEdge.Y, trackbox.MinEdge.Y,
                    30, trackbox.MaxEdge.Z);
                m_suncam->setProjectionMatrix(tmp_matrix, true);
                m_suncam->render();
            }
            rsm_matrix = getVideoDriver()->getTransform(video::ETS_PROJECTION) * getVideoDriver()->getTransform(video::ETS_VIEW);
            m_rsm_matrix_initialized = true;
            m_rsm_map_available = false;
        }
        rh_extend = core::vector3df(128, 64, 128);
        core::vector3df campos = camnode->getAbsolutePosition();
        core::vector3df translation(8 * floor(campos.X / 8), 8 * floor(campos.Y / 8), 8 * floor(campos.Z / 8));
        rh_matrix.setTranslation(translation);


        assert(sun_ortho_matrix.size() == 4);
        // reset normal camera
        camnode->setNearValue(oldnear);
        camnode->setFarValue(oldfar);
        camnode->render();

        size_t size = irr_driver->getShadowViewProj().size();
        for (unsigned i = 0; i < size; i++)
            memcpy(&tmp[16 * i + 80], irr_driver->getShadowViewProj()[i].pointer(), 16 * sizeof(float));
    }

    tmp[144] = float(width);
    tmp[145] = float(height);
    glBindBuffer(GL_UNIFORM_BUFFER, SharedObject::ViewProjectionMatrixesUBO);
    if (CVS->isSDSMEnabled())
    {
        glBufferSubData(GL_UNIFORM_BUFFER, 0, (16 * 5) * sizeof(float), tmp);
        glBufferSubData(GL_UNIFORM_BUFFER, (16 * 9) * sizeof(float), 2 * sizeof(float), &tmp[144]);
    }
    else
        glBufferSubData(GL_UNIFORM_BUFFER, 0, (16 * 9 + 2) * sizeof(float), tmp);
}
//! runs the device. Returns false if device wants to be deleted
bool CengineDeviceWin32::run()
{
	if (__engine_stop_execution) return false;

	os::Timer::tick();

	if(NetworkDriver)
		NetworkDriver->run();

	bool quit = false;
	engine::SEvent event;
	event.KeyInput.Control = false;

	// *------------------------------------------
	// tutte le operazioni per prendere il cursore
	// e i tasti premuti vanno qui
	SceCtrlData pad;
	sceCtrlPeekBufferPositive (&pad, 1);


	// la tastiera è attiva
	if (key_state == 1) {

		if (pad.Buttons & PSP_CTRL_UP)
			key_pos_y -= 5;

		if (pad.Buttons & PSP_CTRL_DOWN)
			key_pos_y += 5;

		if (pad.Buttons & PSP_CTRL_RIGHT)
			key_pos_x += 5;

		if (pad.Buttons & PSP_CTRL_LEFT)
			key_pos_x -= 5;

		// disabilita la keyboard
		if (pad.Buttons & PSP_CTRL_START)
		{
			if (wasStartDown == 0) {
				key_state = 0;
				wasStartDown = 1;
				return !quit;
			}
		} else {
			wasStartDown = 0;
		}



		/* aggiusta la posizione in caso */
		if (key_pos_x < 0) key_pos_x = 0;
		if (key_pos_x > 329) key_pos_x = 329;
		if (key_pos_y < 0) key_pos_y = 0;
		if (key_pos_y > 121) key_pos_y = 121;

		if (pad.Buttons & PSP_CTRL_LTRIGGER){
			if (wasLDown == 0){
				key_type--;
				wasLDown = 1;
			}
		} else wasLDown = 0;

		if (pad.Buttons & PSP_CTRL_RTRIGGER){
			if (wasRDown == 0) {
				key_type++;
				wasRDown = 1;
			}
		} else wasRDown = 0;

		if (key_type < 0) key_type = 3;
		if (key_type > 3) key_type = 0;





		int xpos = 0, ypos = 0;
		if ((pad.Lx > 170) || (pad.Lx < 100)) {
			xpos = (pad.Lx - 128);
		}
		if ((pad.Ly > 170) || (pad.Ly < 100)) {
			ypos = (pad.Ly - 128);
		}

		int currentindex = 4;
		if (xpos < 0) currentindex--;
		if (xpos > 0) currentindex++;
		if (ypos > 0) currentindex+=3;
		if (ypos < 0) currentindex-=3;
		key_sel = currentindex;

		int keycode = KEYMAP_NOTHING;

		if (pad.Buttons & PSP_CTRL_SQUARE) keycode = key_map_of_codes[key_type][key_sel][0];
		if (pad.Buttons & PSP_CTRL_TRIANGLE) keycode = key_map_of_codes[key_type][key_sel][1];
		if (pad.Buttons & PSP_CTRL_CIRCLE) keycode = key_map_of_codes[key_type][key_sel][2];
		if (pad.Buttons & PSP_CTRL_CROSS) keycode = key_map_of_codes[key_type][key_sel][3];


		if (keymapPressed == 0)
			switch (keycode) {
			case KEYMAP_NOTHING:
				break;
			case KEYMAP_DELETE:
				event.KeyInput.Key = engine::KEY_BACK;
				event.EventType = engine::EET_KEY_INPUT_EVENT;
				event.KeyInput.PressedDown = true;
				postEventFromUser(event);
				event.KeyInput.PressedDown = false;
				postEventFromUser(event);
				break;
			case KEYMAP_SPACE:
				event.KeyInput.Key = engine::KEY_SPACE;
				event.EventType = engine::EET_KEY_INPUT_EVENT;
				event.KeyInput.Char = 32;
				event.KeyInput.PressedDown = true;
				postEventFromUser(event);
				event.KeyInput.PressedDown = false;
				postEventFromUser(event);
				break;
			default:
				event.KeyInput.Key = (engine::EKEY_CODE)keycode;
				event.EventType = engine::EET_KEY_INPUT_EVENT;
				event.KeyInput.Char = keycode;
				event.KeyInput.PressedDown = true;
				postEventFromUser(event);
				event.KeyInput.PressedDown = false;
				postEventFromUser(event);
				break;
			}
		if ((pad.Buttons & PSP_CTRL_SQUARE) || (pad.Buttons & PSP_CTRL_TRIANGLE) ||
		    (pad.Buttons & PSP_CTRL_CIRCLE) || (pad.Buttons & PSP_CTRL_CROSS)) keymapPressed = 1;
		else
			keymapPressed = 0;



	} else {

		if (wasStartDown == 1 && (pad.Buttons & PSP_CTRL_START)) return !quit;
		else wasStartDown = 0;

		f32 sensibility = 0.2;
		int fpsc = getVideoDriver()->getFPS();
		if (fpsc == 0) fpsc = 60;

		if (CursorControl->isVisible() == true) sensibility = sensibility = (60/fpsc) *  ((CCursorControl*)CursorControl)->getSpeed();
		// if (CursorControl->isVisible() == true && (pad.Buttons  & PSP_CTRL_RTRIGGER)) sensibility = (60/fpsc) *  0.05;
		bool moved = false;
		const f32 speed = 0.05f;
		core::position2d<s32> CursorPos = CursorControl->getPosition();
		if ((pad.Lx > 160) || (pad.Lx < 110)) {
			f32 xpos = (pad.Lx - 128) * sensibility;
			CursorPos.X += (int)xpos;
			moved=true;
		}
		if ((pad.Ly > 160) || (pad.Ly < 110)) {
			f32 ypos = (pad.Ly - 128) * sensibility;
			CursorPos.Y += (int)ypos;
			moved=true;
		}

		if (CursorPos.Y < 0) CursorPos.Y = 0;
		if (CursorPos.Y > 272) CursorPos.Y = 272;

		if (CursorPos.X < 0) CursorPos.X = 0;
		if  (CursorPos.X > 480) CursorPos.X = 480;
		CursorControl->setPosition(CursorPos);

		if (CursorControl->isEnabled() == true)
		{
			((CCursorControl*)CursorControl)->setPosition2(CursorPos);

		}



		/* Il cursore del mouse è visibile quindi interpreta i pulsanti come mouse */
		if (CursorControl->isVisible() == true && CursorControl->isEnabled() == true)
		{

			if (moved == true)
			{


				event.MouseInput.Event = engine::EMIE_MOUSE_MOVED;
				event.MouseInput.X = CursorPos.X;
				event.MouseInput.Y = CursorPos.Y;
				postEventFromUser(event);


			}

			if (CursorControl->isXEnabled() == true)
			{

				if (wasMouseDown && !(pad.Buttons & PSP_CTRL_CROSS)) {
					event.EventType = engine::EET_MOUSE_INPUT_EVENT;
					event.MouseInput.Event = engine::EMIE_LMOUSE_LEFT_UP;
					event.MouseInput.X = CursorPos.X;
					event.MouseInput.Y = CursorPos.Y;
					postEventFromUser(event);
					wasMouseDown=0;
				}

				if (pad.Buttons & PSP_CTRL_CROSS)
				{
					if (wasMouseDown == 0) {

						event.MouseInput.Event = engine::EMIE_LMOUSE_PRESSED_DOWN;
						event.MouseInput.X = CursorPos.X;
						event.MouseInput.Y = CursorPos.Y;
						postEventFromUser(event);
						wasMouseDown = 1;

					}
				}
			}


		}
		/* Il cursore del mouse non è visibile quindi interpreta i pulsanti
		   come tastiera */


		event.KeyInput.Char = 0;
		event.EventType = engine::EET_KEY_INPUT_EVENT;
		event.KeyInput.PressedDown = true;

		event.KeyInput.Key = engine::KEY_SQUARE;
		if (pad.Buttons & PSP_CTRL_SQUARE){

			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;
			event.KeyInput.PressedOnce = false;



			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_TRIANGLE;
		if (pad.Buttons & PSP_CTRL_TRIANGLE){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);


			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_CIRCLE;
		if (pad.Buttons & PSP_CTRL_CIRCLE){

			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_CROSS;

		if (pad.Buttons & PSP_CTRL_CROSS){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;

			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_UP;
		if (pad.Buttons & PSP_CTRL_UP){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_DOWN;
		if (pad.Buttons & PSP_CTRL_DOWN){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_LEFT;
		if (pad.Buttons & PSP_CTRL_LEFT){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		}  else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_RIGHT;
		if (pad.Buttons & PSP_CTRL_RIGHT){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;

		event.KeyInput.Key = engine::KEY_START;

		if (pad.Buttons & PSP_CTRL_START){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;


		event.KeyInput.Key = engine::KEY_SELECT;
		if (pad.Buttons & PSP_CTRL_SELECT){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_LBUTTON;
		if (pad.Buttons & PSP_CTRL_LTRIGGER){


			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);
			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_HOME;
		if (pad.Buttons & PSP_CTRL_HOME){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;

		event.KeyInput.Key = engine::KEY_NOTE;
		if (pad.Buttons & PSP_CTRL_NOTE){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;
		event.KeyInput.Key = engine::KEY_RBUTTON;

		if (pad.Buttons & PSP_CTRL_RTRIGGER ){
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = true;

			if (__table_pressed_down[(int)event.KeyInput.Key] == 0)
				__lte_send_po(event.KeyInput.Key);

			__table_pressed_down[(int)event.KeyInput.Key]= 1;
			postEventFromUser(event);
		} else if (__table_pressed_down[(int)event.KeyInput.Key] == 1) __table_pressed_down[(int)event.KeyInput.Key] = -1;

		for (int x = 1; x < 0x19; x++)
		{

			event.KeyInput.Char = 0;
			event.EventType = engine::EET_KEY_INPUT_EVENT;
			event.KeyInput.PressedDown = false;
			event.KeyInput.Key = (engine::EKEY_CODE)x;
			if (__table_pressed_down[(int)event.KeyInput.Key] == -1) {
				__table_pressed_down[(int)event.KeyInput.Key] = 0;
				postEventFromUser(event);



			}

		}

	}





	// sposto il cursore del mouse

	// ------------------------------------------*

	return !quit;
}
예제 #10
0
void IrrDriver::computeCameraMatrix(scene::ICameraSceneNode * const camnode, size_t width, size_t height)
{
    if (irr_driver->supportsSDSM())
        UpdateSplitAndLightcoordRangeFromComputeShaders(width, height);
    static_cast<scene::CSceneManager *>(m_scene_manager)->OnAnimate(os::Timer::getTime());
    camnode->render();
    irr_driver->setProjMatrix(irr_driver->getVideoDriver()->getTransform(video::ETS_PROJECTION));
    irr_driver->setViewMatrix(irr_driver->getVideoDriver()->getTransform(video::ETS_VIEW));
    irr_driver->genProjViewMatrix();

    m_current_screen_size = core::vector2df(float(width), float(height));

    const float oldfar = camnode->getFarValue();
    const float oldnear = camnode->getNearValue();
    float FarValues[] =
    {
        shadowSplit[1],
        shadowSplit[2],
        shadowSplit[3],
        shadowSplit[4],
    };
    float NearValues[] =
    {
        shadowSplit[0],
        shadowSplit[1],
        shadowSplit[2],
        shadowSplit[3]
    };


    float tmp[16 * 9 + 2];
    memcpy(tmp, irr_driver->getViewMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[16], irr_driver->getProjMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[32], irr_driver->getInvViewMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[48], irr_driver->getInvProjMatrix().pointer(), 16 * sizeof(float));
    memcpy(&tmp[64], irr_driver->getProjViewMatrix().pointer(), 16 * sizeof(float));

    m_suncam->render();
    const core::vector3df &camdir = (camnode->getTarget() - camnode->getAbsolutePosition()).normalize();
    const core::vector3df &sundir = (m_suncam->getTarget() - m_suncam->getAbsolutePosition()).normalize();
    const core::vector3df &up = camdir.crossProduct(sundir).normalize();
    if (up.getLength())
        m_suncam->setUpVector(up);
    m_suncam->render();

    for (unsigned i = 0; i < 4; i++)
    {
        if (m_shadow_camnodes[i])
            delete m_shadow_camnodes[i];
        m_shadow_camnodes[i] = (scene::ICameraSceneNode *) m_suncam->clone();
    }
    const core::matrix4 &SunCamViewMatrix = m_suncam->getViewMatrix();

    sun_ortho_matrix.clear();

    if (World::getWorld() && World::getWorld()->getTrack())
    {
        btVector3 btmin, btmax;
        if (World::getWorld()->getTrack()->getPtrTriangleMesh())
        {
            World::getWorld()->getTrack()->getTriangleMesh().getCollisionShape().getAabb(btTransform::getIdentity(), btmin, btmax);
        }
        const Vec3 vmin = btmin , vmax = btmax;

        // Build the 3 ortho projection (for the 3 shadow resolution levels)
        for (unsigned i = 0; i < 4; i++)
        {
            if (!irr_driver->supportsSDSM())
            {
                camnode->setFarValue(FarValues[i]);
                camnode->setNearValue(NearValues[i]);
                camnode->render();
            }
            const scene::SViewFrustum *frustrum = camnode->getViewFrustum();
            float tmp[24] = {
                frustrum->getFarLeftDown().X,
                frustrum->getFarLeftDown().Y,
                frustrum->getFarLeftDown().Z,
                frustrum->getFarLeftUp().X,
                frustrum->getFarLeftUp().Y,
                frustrum->getFarLeftUp().Z,
                frustrum->getFarRightDown().X,
                frustrum->getFarRightDown().Y,
                frustrum->getFarRightDown().Z,
                frustrum->getFarRightUp().X,
                frustrum->getFarRightUp().Y,
                frustrum->getFarRightUp().Z,
                frustrum->getNearLeftDown().X,
                frustrum->getNearLeftDown().Y,
                frustrum->getNearLeftDown().Z,
                frustrum->getNearLeftUp().X,
                frustrum->getNearLeftUp().Y,
                frustrum->getNearLeftUp().Z,
                frustrum->getNearRightDown().X,
                frustrum->getNearRightDown().Y,
                frustrum->getNearRightDown().Z,
                frustrum->getNearRightUp().X,
                frustrum->getNearRightUp().Y,
                frustrum->getNearRightUp().Z,
            };
            memcpy(m_shadows_cam[i], tmp, 24 * sizeof(float));
            const core::aabbox3df smallcambox = camnode->
                getViewFrustum()->getBoundingBox();
            core::aabbox3df trackbox(vmin.toIrrVector(), vmax.toIrrVector() -
                core::vector3df(0, 30, 0));

            // Set up a nice ortho projection that contains our camera frustum
            core::aabbox3df box = smallcambox;
            box = box.intersect(trackbox);

            std::vector<vector3df> vectors;
            vectors.push_back(frustrum->getFarLeftDown());
            vectors.push_back(frustrum->getFarLeftUp());
            vectors.push_back(frustrum->getFarRightDown());
            vectors.push_back(frustrum->getFarRightUp());
            vectors.push_back(frustrum->getNearLeftDown());
            vectors.push_back(frustrum->getNearLeftUp());
            vectors.push_back(frustrum->getNearRightDown());
            vectors.push_back(frustrum->getNearRightUp());

            core::matrix4 tmp_matrix;

            if (irr_driver->supportsSDSM()){
                float left = float(CBB[currentCBB][i].xmin / 4 - 2);
                float right = float(CBB[currentCBB][i].xmax / 4 + 2);
                float up = float(CBB[currentCBB][i].ymin / 4 - 2);
                float down = float(CBB[currentCBB][i].ymax / 4 + 2);

                // Prevent Matrix without extend
                if (left != right && up != down)
                {
                    tmp_matrix.buildProjectionMatrixOrthoLH(left, right,
                        down, up,
                        float(CBB[currentCBB][i].zmin / 4 - 100),
                        float(CBB[currentCBB][i].zmax / 4 + 2));
                    m_shadow_scales[i] = std::make_pair(right - left, down - up);
                }
            }
            else
                tmp_matrix = getTighestFitOrthoProj(SunCamViewMatrix, vectors, m_shadow_scales[i]);

            m_shadow_camnodes[i]->setProjectionMatrix(tmp_matrix , true);
            m_shadow_camnodes[i]->render();

            sun_ortho_matrix.push_back(getVideoDriver()->getTransform(video::ETS_PROJECTION) * getVideoDriver()->getTransform(video::ETS_VIEW));
        }

        if (!m_rsm_matrix_initialized)
        {
            core::aabbox3df trackbox(vmin.toIrrVector(), vmax.toIrrVector() -
                core::vector3df(0, 30, 0));
            if (trackbox.MinEdge.X != trackbox.MaxEdge.X &&
                trackbox.MinEdge.Y != trackbox.MaxEdge.Y &&
                // Cover the case where SunCamViewMatrix is null
                SunCamViewMatrix.getScale() != core::vector3df(0., 0., 0.))
            {
                SunCamViewMatrix.transformBoxEx(trackbox);
                core::matrix4 tmp_matrix;
                tmp_matrix.buildProjectionMatrixOrthoLH(trackbox.MinEdge.X, trackbox.MaxEdge.X,
                    trackbox.MaxEdge.Y, trackbox.MinEdge.Y,
                    30, trackbox.MaxEdge.Z);
                m_suncam->setProjectionMatrix(tmp_matrix, true);
                m_suncam->render();
            }
            rsm_matrix = getVideoDriver()->getTransform(video::ETS_PROJECTION) * getVideoDriver()->getTransform(video::ETS_VIEW);
            m_rsm_matrix_initialized = true;
            m_rsm_map_available = false;
        }
        rh_extend = core::vector3df(128, 64, 128);
        core::vector3df campos = camnode->getAbsolutePosition();
        core::vector3df translation(8 * floor(campos.X / 8), 8 * floor(campos.Y / 8), 8 * floor(campos.Z / 8));
        rh_matrix.setTranslation(translation);


        assert(sun_ortho_matrix.size() == 4);
        camnode->setNearValue(oldnear);
        camnode->setFarValue(oldfar);
        camnode->render();

        size_t size = irr_driver->getShadowViewProj().size();
        for (unsigned i = 0; i < size; i++)
            memcpy(&tmp[16 * i + 80], irr_driver->getShadowViewProj()[i].pointer(), 16 * sizeof(float));
    }

    tmp[144] = float(width);
    tmp[145] = float(height);
    glBindBuffer(GL_UNIFORM_BUFFER, SharedObject::ViewProjectionMatrixesUBO);
    glBufferSubData(GL_UNIFORM_BUFFER, 0, (16 * 9 + 2) * sizeof(float), tmp);
}
예제 #11
0
void IrrDriver::renderShadows(//ShadowImportanceProvider * const sicb,
                              scene::ICameraSceneNode * const camnode,
                              //video::SOverrideMaterial &overridemat,
                              Camera * const camera)
{
    m_scene_manager->setCurrentRendertime(scene::ESNRP_SOLID);

    const Vec3 *vmin, *vmax;
    World::getWorld()->getTrack()->getAABB(&vmin, &vmax);

    const float oldfar = camnode->getFarValue();
    const float oldnear = camnode->getNearValue();
    float FarValues[] =
    {
        20.,
        50.,
        100.,
        oldfar,
    };
    float NearValues[] =
    {
        oldnear,
        20.,
        50.,
        100.,
    };

    const core::matrix4 &SunCamViewMatrix = m_suncam->getViewMatrix();
    sun_ortho_matrix.clear();

    // Build the 3 ortho projection (for the 3 shadow resolution levels)
    for (unsigned i = 0; i < 4; i++)
    {
        camnode->setFarValue(FarValues[i]);
        camnode->setNearValue(NearValues[i]);
        camnode->render();
        const core::aabbox3df smallcambox = camnode->
            getViewFrustum()->getBoundingBox();
        core::aabbox3df trackbox(vmin->toIrrVector(), vmax->toIrrVector() -
            core::vector3df(0, 30, 0));


        // Set up a nice ortho projection that contains our camera frustum
        core::aabbox3df box = smallcambox;
        box = box.intersect(trackbox);


        SunCamViewMatrix.transformBoxEx(trackbox);
        SunCamViewMatrix.transformBoxEx(box);

        core::vector3df extent = trackbox.getExtent();
        const float w = fabsf(extent.X);
        const float h = fabsf(extent.Y);
        float z = box.MaxEdge.Z;

        // Snap to texels
        const float units_per_w = w / 1024;
        const float units_per_h = h / 1024;

        float left = box.MinEdge.X;
        float right = box.MaxEdge.X;
        float up = box.MaxEdge.Y;
        float down = box.MinEdge.Y;

        left -= fmodf(left, units_per_w);
        right -= fmodf(right, units_per_w);
        up -= fmodf(up, units_per_h);
        down -= fmodf(down, units_per_h);
        z -= fmodf(z, 0.5f);

        // FIXME: quick and dirt (and wrong) workaround to avoid division by zero
        if (left == right) right += 0.1f;
        if (up == down) down += 0.1f;
        if (z == 30) z += 0.1f;

        core::matrix4 tmp_matrix;

        tmp_matrix.buildProjectionMatrixOrthoLH(left, right,
            up, down,
            30, z);
        m_suncam->setProjectionMatrix(tmp_matrix, true);
        m_scene_manager->setActiveCamera(m_suncam);
        m_suncam->render();

        sun_ortho_matrix.push_back(getVideoDriver()->getTransform(video::ETS_PROJECTION) * getVideoDriver()->getTransform(video::ETS_VIEW));
    }
    assert(sun_ortho_matrix.size() == 4);

    irr_driver->setPhase(SHADOW_PASS);
    glDisable(GL_BLEND);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);
    glBindFramebuffer(GL_FRAMEBUFFER, m_rtts->getShadowFBO());
    glViewport(0, 0, 1024, 1024);
    glClear(GL_DEPTH_BUFFER_BIT);
    glDrawBuffer(GL_NONE);
    m_scene_manager->drawAll(scene::ESNRP_SOLID);
    glCullFace(GL_BACK);

    camnode->setNearValue(oldnear);
    camnode->setFarValue(oldfar);
    camnode->render();
    camera->activate();
    m_scene_manager->drawAll(scene::ESNRP_CAMERA);


    //sun_ortho_matrix *= m_suncam->getViewMatrix();
/*    ((SunLightProvider *) m_shaders->m_callbacks[ES_SUNLIGHT])->setShadowMatrix(ortho);
    sicb->setShadowMatrix(ortho);

    overridemat.Enabled = 0;

    // Render the importance map
    m_video_driver->setRenderTarget(m_rtts->getRTT(RTT_COLLAPSE), true, true);

    m_shadow_importance->render();

    CollapseProvider * const colcb = (CollapseProvider *)
                                            m_shaders->
                                            m_callbacks[ES_COLLAPSE];
    ScreenQuad sq(m_video_driver);
    sq.setMaterialType(m_shaders->getShader(ES_COLLAPSE));
    sq.setTexture(m_rtts->getRTT(RTT_COLLAPSE));
    sq.getMaterial().setFlag(EMF_BILINEAR_FILTER, false);

    const TypeRTT oldh = tick ? RTT_COLLAPSEH : RTT_COLLAPSEH2;
    const TypeRTT oldv = tick ? RTT_COLLAPSEV : RTT_COLLAPSEV2;
    const TypeRTT curh = tick ? RTT_COLLAPSEH2 : RTT_COLLAPSEH;
    const TypeRTT curv = tick ? RTT_COLLAPSEV2 : RTT_COLLAPSEV;

    colcb->setResolution(1, m_rtts->getRTT(RTT_WARPV)->getSize().Height);
    sq.setTexture(m_rtts->getRTT(oldh), 1);
    sq.render(m_rtts->getRTT(RTT_WARPH));

    colcb->setResolution(m_rtts->getRTT(RTT_WARPV)->getSize().Height, 1);
    sq.setTexture(m_rtts->getRTT(oldv), 1);
    sq.render(m_rtts->getRTT(RTT_WARPV));

    sq.setTexture(0, 1);
    ((GaussianBlurProvider *) m_shaders->m_callbacks[ES_GAUSSIAN3H])->setResolution(
                m_rtts->getRTT(RTT_WARPV)->getSize().Height,
                m_rtts->getRTT(RTT_WARPV)->getSize().Height);

    sq.setMaterialType(m_shaders->getShader(ES_GAUSSIAN6H));
    sq.setTexture(m_rtts->getRTT(RTT_WARPH));
    sq.render(m_rtts->getRTT(curh));

    sq.setMaterialType(m_shaders->getShader(ES_GAUSSIAN6V));
    sq.setTexture(m_rtts->getRTT(RTT_WARPV));
    sq.render(m_rtts->getRTT(curv));*/

    // Convert importance maps to warp maps
    //
    // It should be noted that while they do repeated work
    // calculating the min, max, and total, it's several hundred us
    // faster to do that than to do it once in a separate shader
    // (shader switch overhead, measured).
    /*colcb->setResolution(m_rtts->getRTT(RTT_WARPV)->getSize().Height,
                            m_rtts->getRTT(RTT_WARPV)->getSize().Height);

    sq.setMaterialType(m_shaders->getShader(ES_SHADOW_WARPH));
    sq.setTexture(m_rtts->getRTT(curh));
    sq.render(m_rtts->getRTT(RTT_WARPH));

    sq.setMaterialType(m_shaders->getShader(ES_SHADOW_WARPV));
    sq.setTexture(m_rtts->getRTT(curv));
    sq.render(m_rtts->getRTT(RTT_WARPV));*/

    // Actual shadow map


/*    overridemat.Material.MaterialType = m_shaders->getShader(ES_SHADOWPASS);
    overridemat.EnableFlags = video::EMF_MATERIAL_TYPE | video::EMF_TEXTURE1 |
                                video::EMF_TEXTURE2;
    overridemat.EnablePasses = scene::ESNRP_SOLID;
    overridemat.Material.setTexture(1, m_rtts->getRTT(RTT_WARPH));
    overridemat.Material.setTexture(2, m_rtts->getRTT(RTT_WARPV));
    overridemat.Material.TextureLayer[1].TextureWrapU =
    overridemat.Material.TextureLayer[1].TextureWrapV =
    overridemat.Material.TextureLayer[2].TextureWrapU =
    overridemat.Material.TextureLayer[2].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
    overridemat.Material.TextureLayer[1].BilinearFilter =
    overridemat.Material.TextureLayer[2].BilinearFilter = true;
    overridemat.Material.TextureLayer[1].TrilinearFilter =
    overridemat.Material.TextureLayer[2].TrilinearFilter = false;
    overridemat.Material.TextureLayer[1].AnisotropicFilter =
    overridemat.Material.TextureLayer[2].AnisotropicFilter = 0;
    overridemat.Material.Wireframe = 1;
    overridemat.Enabled = true;*/



//    overridemat.EnablePasses = 0;
//    overridemat.Enabled = false;
}
예제 #12
0
void IrrDriver::generateDiffuseCoefficients()
{
    if (!m_SH_dirty)
        return;
    m_SH_dirty = false;
    const unsigned texture_permutation[] = { 2, 3, 0, 1, 5, 4 };

    if (SphericalHarmonicsTextures.size() == 6)
    {
        unsigned sh_w = 0, sh_h = 0;
        for (unsigned i = 0; i < 6; i++)
        {
            sh_w = MAX2(sh_w, SphericalHarmonicsTextures[i]->getOriginalSize().Width);
            sh_h = MAX2(sh_h, SphericalHarmonicsTextures[i]->getOriginalSize().Height);
        }

        unsigned char *sh_rgba[6];
        for (unsigned i = 0; i < 6; i++)
            sh_rgba[i] = new unsigned char[sh_w * sh_h * 4];
        for (unsigned i = 0; i < 6; i++)
        {
            unsigned idx = texture_permutation[i];

            video::IImage* image = getVideoDriver()->createImageFromData(
                SphericalHarmonicsTextures[idx]->getColorFormat(),
                SphericalHarmonicsTextures[idx]->getSize(),
                SphericalHarmonicsTextures[idx]->lock(),
                false
                );
            SphericalHarmonicsTextures[idx]->unlock();

            image->copyToScaling(sh_rgba[i], sh_w, sh_h);
            delete image;
        }

        testSH(sh_rgba, sh_w, sh_h, blueSHCoeff, greenSHCoeff, redSHCoeff);

        for (unsigned i = 0; i < 6; i++)
            delete[] sh_rgba[i];
    }
    else
    {
        int sh_w = 16;
        int sh_h = 16;

        video::SColor ambient = m_scene_manager->getAmbientLight().toSColor();

        unsigned char *sh_rgba[6];
        for (unsigned i = 0; i < 6; i++)
        {
            sh_rgba[i] = new unsigned char[sh_w * sh_h * 4];

            for (int j = 0; j < sh_w * sh_h * 4; j += 4)
            {
                sh_rgba[i][j] = ambient.getBlue();
                sh_rgba[i][j + 1] = ambient.getGreen();
                sh_rgba[i][j + 2] = ambient.getRed();
                sh_rgba[i][j + 3] = 255;
            }
        }

        testSH(sh_rgba, sh_w, sh_h, blueSHCoeff, greenSHCoeff, redSHCoeff);

        // Diffuse env map is x 0.25, compensate
        for (unsigned i = 0; i < 9; i++)
        {
            blueSHCoeff[i] *= 4;
            greenSHCoeff[i] *= 4;
            redSHCoeff[i] *= 4;
        }

        for (unsigned i = 0; i < 6; i++)
            delete[] sh_rgba[i];
    }

    /*for (unsigned i = 0; i < 6; i++)
    {
    glBindTexture(GL_TEXTURE_CUBE_MAP, ConvolutedSkyboxCubeMap);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_SRGB_ALPHA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)rgba[i]);
    }

    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);*/

}