(BlizPorals) Prey Portals like OpenGL

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Blizzard
Posts: 71
Joined: Thu Apr 20, 2006 5:27 pm
Location: Italy

(BlizPorals) Prey Portals like OpenGL

Post by Blizzard »

8) Hi,
Sick and Tired of my portals??? I hope no ^_^
because I've implemented the same BlizPortals with OpenGL library


They are simply portable on many systems and the implementation is simple.

Here is the code:

Code: Select all

//----------------------------------------------
// File: BlizPortal.h v0.2.1
//
// Developer: Santostefano "Blizzard" Giovanni
//
// contact: idmgiovanni@libero.it
//
//
// BlizPortal is a class that implement 3dRealms Prey's like 
// portals using OPENGL
// This class is created for drawing portals
// in irrlicht 3d (engine developed by
// Nickolaus Ghebardt)
//----------------------------------------------


#ifndef BLIZPORTAL
#define BLIZPORTAL


#include <windows.h>
#include <irrlicht.h>
#include <iostream>

#include <gl\gl.h>			// File d'Intestazione per la libreria OpenGL32 

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;

#pragma comment(lib, "Irrlicht.lib")


class BlizPortal{

	public:
		IrrlichtDevice* device; //pointer to the irr device

		ICameraSceneNode* camera; //active camera of the scene
		ICameraSceneNode* cP1; //camera of the first portal
		ICameraSceneNode* cP2; //camera of the second portal

		core::vector3df dp1c; //deltaPosition between portal 1 and the camera
		core::vector3df dp2c; //deltaPosition between portal 2 and the camera

		IAnimatedMeshSceneNode* portal1; //scene node of the first portal
		float Radius1; //radius of the object that compose fist portal
		IAnimatedMeshSceneNode* portal2; //scene node of the second portal
		float Radius2; //radius of the object that compose second portal

		int is_p1_active; //it assumes 1 if the first portal il active
		int is_p2_active; //it assumes 1 if the second portal il active
		int passed; //this int contains the portal that you have passed


	public:

		//Constructor
		BlizPortal();
		BlizPortal(IrrlichtDevice* dev);


		//Destructor
		~BlizPortal();

		//Baically in this version the lots of initialization
		//is made by the user such as in the sample.
		//This procedure initialize only the d3d pointers
		//and the delta vectors.
		//Before using this function the user/programmer must define
		//the device, the cameras and the nodes of the portals
		void BP_init();

		//Baically in this version the lots of initialization
		//is made by the user such as in the sample.
		//This procedure initialize only the d3d pointers
		//and the delta vectors.
		//Before using this function the user/programmer must define
		//the device
		//
		//fileM1 is the filename of the mesh that you want to load in the
		//first portal node
		//fileM2 is the filename of the mesh that you want to load in the
		//second portal node
		void BP_init(char* fileM1, char* fileM2);

		//This procedure render both the portals! ^_^
		//It must be called after the smgr->drawAll()
		//in the scene
		void BP_render_portals();

		//This procedure render the portal! ^_^
		void BP_render_portal(IAnimatedMeshSceneNode* portal, ICameraSceneNode* cP );

		//This procedure verify if the camera pass through a portal
		//if true, she actives the transfer and set to 0 the active value
		//of the portal were you are transfered.
		//If you exit from the portal the portal is reactivated so you
		//can use it to go in the previous zone
		//
		//NOTE: it's better that you define your own procedure to use
		//portals. If, for example, you are programming a 3rd person shooter
		//this function is not correct, because she evaluates the translation
		//of the camera.
		void BP_use_portals();



};


#endif

//----------------------------------------------
// File: BlizPortal.h
//
// Developer: Santostefano "Blizzard" Giovanni
//
// contact: idmgiovanni@libero.it
//
//
// BlizPortal is a class that implement 3dRealms Prey's like 
// portals using OPENGL
// This class is created for drawing portals
// in irrlicht 3d (engine developed by
// Nickolaus Ghebardt)
//----------------------------------------------

Code: Select all

//----------------------------------------------
// File: BlizPortal.cpp v0.2.1
//
// Developer: Santostefano "Blizzard" Giovanni
//
// contact: idmgiovanni@libero.it
//
//
// BlizPortal is a class that implement 3dRealms Prey's like 
// portals using OPENGL
// This class is created for drawing portals
// in irrlicht 3d (engine developed by
// Nickolaus Ghebardt)
//----------------------------------------------


#include "BlizPortal.h"


//Constructor
BlizPortal::BlizPortal(){
	device=0;
//	hRC=0; 
//	hDC=0; 
	camera=0; 
	cP1=0; 
	cP2=0; 
	portal1=0; 
	portal2=0;
}

BlizPortal::BlizPortal(IrrlichtDevice* dev){
	device=dev;
//	hRC=0; 
//	hDC=0; 
	camera=0; 
	cP1=0; 
	cP2=0; 
	portal1=0; 
	portal2=0;
}

//Destructor
BlizPortal::~BlizPortal(){
	camera->drop(); 
	cP1->drop(); 
	cP2->drop(); 
	portal1->drop(); 
	portal2->drop();
}

//Baically in this version the lots of initialization
//is made by the user such as in the sample.
//This procedure initialize only the d3d pointers
//and the delta vectors.
//Before using this function the user/programmer must define
//the device, the cameras and the nodes of the portals
void BlizPortal::BP_init(){
	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();

	//hDC=reinterpret_cast<HDC>((driver->getExposedVideoData()).OpenGLWin32.HDc);
	//hRC=reinterpret_cast<HGLRC>((driver->getExposedVideoData()).OpenGLWin32.HRc)

	//d3d=(driver->getExposedVideoData()).D3D9.D3D9;
	//d3ddev=(driver->getExposedVideoData()).D3D9.D3DDev9;

	//set the portals unvisible
	//NOTE: PORTALS MUST BE UNVISIBLE!!!!!!
	portal1->setVisible(false);
	portal2->setVisible(false);
}


//Baically in this version the lots of initialization
//is made by the user such as in the sample.
//This procedure initialize only the d3d pointers
//and the delta vectors.
//Before using this function the user/programmer must define
//the device
//
//fileM1 is the filename of the mesh that you want to load in the
//first portal node
//fileM2 is the filename of the mesh that you want to load in the
//second portal node
void BlizPortal::BP_init(char* fileM1, char* fileM2){
	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();

//	d3d=(driver->getExposedVideoData()).D3D9.D3D9;
//	d3ddev=(driver->getExposedVideoData()).D3D9.D3DDev9;

	//setting cameras
	camera=smgr->addCameraSceneNodeFPS();
	cP1=smgr->addCameraSceneNodeFPS(0,0,0);
	cP2=smgr->addCameraSceneNodeFPS(0,0,0);
	//activating main camera
	smgr->setActiveCamera(camera);

	//creating portals
	portal1=smgr->addAnimatedMeshSceneNode(smgr->getMesh(fileM1));
	portal2=smgr->addAnimatedMeshSceneNode(smgr->getMesh(fileM2));
	portal1->setMaterialFlag(EMF_LIGHTING,false);
	portal2->setMaterialFlag(EMF_LIGHTING,false);

	//set the portals unvisible
	//NOTE: PORTALS MUST BE UNVISIBLE!!!!!!
	portal1->setVisible(false);
	portal2->setVisible(false);
}

//This procedure render both the portals! ^_^
//It must be called after the smgr->drawAll()
//in the scene
void BlizPortal::BP_render_portals(){

	//Setting of the cameras
	//--------------------------------------------------------
	dp2c=camera->getPosition() - portal1->getPosition();
	dp1c=camera->getPosition() - portal2->getPosition();

	cP1->setPosition(portal1->getPosition()+dp1c);
	cP1->setRotation(camera->getRotation());

	cP2->setPosition(portal2->getPosition()+dp2c);
	cP2->setRotation(camera->getRotation());
	//---------------------------------------------------------

	if(is_p1_active==1){
		portal2->setVisible(false);
		BP_render_portal(portal1,cP2);
	}
	if(is_p2_active==1){
		portal1->setVisible(false);
		BP_render_portal(portal2,cP1);
	}


}

//This procedure render the portal! ^_^
void BlizPortal::BP_render_portal(IAnimatedMeshSceneNode* portal, ICameraSceneNode* cP ){

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


	//SETTING THE STENCIL BUFFER
	//------------------------------------------------------------------
   	glClear(GL_STENCIL_BUFFER_BIT);
	glEnable(GL_STENCIL_TEST);
	glStencilFunc(GL_ALWAYS, 0x1, 0xffffffff);
	glStencilMask(0xffffffff);
	glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
	//-------------------------------------------------------------------

	//rendering the portal in the stencil buffer
	//-------------------------------------------
	portal->setVisible(true);
	camera->render();
	portal->render();
	portal->setVisible(true);
	//-------------------------------------------

	//rendering the scene connected to the portal
	//-------------------------------------------------------------------

	
	glStencilFunc(GL_EQUAL, 0x1, 0xffffffff);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
	glEnable(GL_DEPTH_TEST);
	driver->clearZBuffer();


   	smgr->setActiveCamera(cP);
	smgr->drawAll(); 
	//--------------------------------------------------------------------


	// Restore render states
	//---------------------------------------------------------------------
	glDisable(GL_STENCIL_TEST);
 
    
	smgr->setActiveCamera(camera);
	//----------------------------------------------------------------------
}

//This procedure verify if the camera pass through a portal
//if true, she actives the transfer and set to 0 the active value
//of the portal were you are transfered.
//If you exit from the portal the portal is reactivated so you
//can use it to go in the previous zone
//
//NOTE: it's better that you define your own procedure to use
//portals. If, for example, you are programming a 3rd person shooter
//this function is not correct, because she evaluates the translation
//of the camera.
void BlizPortal::BP_use_portals(){

	core::line3d<f32> line;

	if(is_p1_active==1){
		line.start = camera->getPosition();
		line.end=portal1->getPosition();

		//player is in the portal
		if(line.getLength()<Radius1){
			camera->setPosition(cP2->getPosition());

			is_p1_active=0;
			is_p2_active=0;

			passed=1;

			return;

		}

	}
	else if(is_p2_active==1){
		line.start = camera->getPosition();
		line.end=portal2->getPosition();

		//player is in the portal
		if(line.getLength()<Radius2){
			camera->setPosition(cP1->getPosition());

			is_p1_active=0;
			is_p2_active=0;

			passed=2;

			return;
		}
	}




	//active portals
	if(is_p1_active==0 && passed==2){
		line.start = camera->getPosition();
		line.end=portal1->getPosition();

		//player is in the portal
		if(line.getLength()>Radius1){
			is_p1_active=1;
		}

	}
	
	if(is_p2_active==0 && passed==1){
		line.start = camera->getPosition();
		line.end=portal2->getPosition();

		//player is in the portal
		if(line.getLength()>Radius2){
			is_p2_active=1;

		}
	}



}

//----------------------------------------------
// File: BlizPortal.cpp
//
// Developer: Santostefano "Blizzard" Giovanni
//
// contact: idmgiovanni@libero.it
//
//
// BlizPortal is a class that implement 3dRealms Prey's like 
// portals using OPENGL
// This class is created for drawing portals
// in irrlicht 3d (engine developed by
// Nickolaus Ghebardt)
//----------------------------------------------
Everyone for the game and the game for everyone
tjuhzj
Posts: 44
Joined: Mon Mar 27, 2006 7:00 am

any tutorial

Post by tjuhzj »

hi blizzard.
i think you'd better to update the blizportal tutorial for OPenGL version.
________
Washington Marijuana Dispensaries
Post Reply