Treasure Island - Source Preview


#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
 
using namespace std;
#pragma comment (lib , "winmm.lib")
 
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
#pragma comment(lib, "Irrlicht.lib")
 
#include <Cg/cg.h>
#include "IrrCg.h"
using namespace IrrCg;
 
const s32 winWidth = 1244;
const s32 winHeight = 700;
IrrlichtDevice* device = 0;
IVideoDriver* driver = 0;
ICameraSceneNode* camera = 0;
 
class ShaderCallBack : public ICgShaderConstantSetCallBack {
public:
	CGparameter	WorldViewProjection;
	CGparameter	WorldInverseTrans;
	CGparameter	TimerPixel;
	CGparameter	TimerVertex;
	virtual void OnSetConstants(ICgServices* services, CGprogram Vertex, CGprogram Pixel, const SMaterial &Material) {
		WorldViewProjection = cgGetNamedParameter(Vertex, "WorldViewProj");
		matrix4 worldViewProj;
		worldViewProj = driver->getTransform(ETS_PROJECTION);
		worldViewProj *= driver->getTransform(ETS_VIEW);
		worldViewProj *= driver->getTransform(ETS_WORLD);
		services->setMatrix(WorldViewProjection, ICGT_MATRIX_IDENTITY, worldViewProj);
		matrix4 World = driver->getTransform(ETS_WORLD);
		matrix4 WorldInverseTranspose;
		World.getInverse(WorldInverseTranspose);
		WorldInverseTranspose.getTransposed(WorldInverseTranspose);
		WorldInverseTrans = cgGetNamedParameter(Vertex, "WorldInverseTranspose");
		services->setMatrix(WorldInverseTrans, ICGT_MATRIX_IDENTITY, WorldInverseTranspose);
		float time = (float)device->getTimer()->getRealTime();
		TimerPixel = cgGetNamedParameter(Pixel, "Timer");
		TimerVertex = cgGetNamedParameter(Vertex, "Timer");
		services->setParameter3f(TimerPixel, time, 1.0f, 1.0f);
		services->setParameter3f(TimerVertex, time, 1.0f, 1.0f);
	}
};
 
class InputEventReceiver : public IEventReceiver {
public:
	virtual bool OnEvent(const SEvent& event) {
		if (event.EventType == EET_KEY_INPUT_EVENT)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
		if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key == KEY_ESCAPE && !event.KeyInput.PressedDown) {
			device->closeDevice();
			return true;
		}
		if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key == KEY_KEY_Q && !event.KeyInput.PressedDown) {
			device->closeDevice();
			return true;
		}
		return false;
	}
	virtual bool IsKeyDown(EKEY_CODE keyCode) const {
		return KeyIsDown[keyCode];
	}
	InputEventReceiver() {
		for (u32 i = 0; i < KEY_KEY_CODES_COUNT; ++i) KeyIsDown[i] = false;
	}
private:
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
int main(int argc, char** argv) {
	InputEventReceiver receiver;
	SIrrlichtCreationParameters param;
	param.AntiAlias = 0;
	param.Bits = 32;
	param.DriverType = EDT_DIRECT3D9;
	param.EventReceiver = &receiver;
	if (argc > 1) {
		IrrlichtDevice *nullDevice = createDevice(EDT_NULL);
		dimension2d<u32> res = nullDevice->getVideoModeList()->getDesktopResolution();
		nullDevice->drop();
		param.WindowSize.Width = res.Width;
		param.WindowSize.Height = res.Height;
		param.Fullscreen = true;
	} else {
		param.WindowSize.Width = winWidth;
		param.WindowSize.Height = winHeight;
		param.Fullscreen = false;
	}
	param.HighPrecisionFPU = true;
	param.Stencilbuffer = true;
	param.Vsync = false;
	device = createDeviceEx(param);
	driver = device->getVideoDriver();
	ISceneManager* sceneManager = device->getSceneManager();
 
	SKeyMap keyMap[8];
	keyMap[0].Action = EKA_MOVE_FORWARD;
	keyMap[0].KeyCode = KEY_UP;
	keyMap[1].Action = EKA_MOVE_FORWARD;
	keyMap[1].KeyCode = KEY_KEY_W;
	keyMap[2].Action = EKA_MOVE_BACKWARD;
	keyMap[2].KeyCode = KEY_DOWN;
	keyMap[3].Action = EKA_MOVE_BACKWARD;
	keyMap[3].KeyCode = KEY_KEY_S;
	keyMap[4].Action = EKA_STRAFE_LEFT;
	keyMap[4].KeyCode = KEY_LEFT;
	keyMap[5].Action = EKA_STRAFE_LEFT;
	keyMap[5].KeyCode = KEY_KEY_A;
	keyMap[6].Action = EKA_STRAFE_RIGHT;
	keyMap[6].KeyCode = KEY_RIGHT;
	keyMap[7].Action = EKA_STRAFE_RIGHT;
	keyMap[7].KeyCode = KEY_KEY_D;
	camera = sceneManager->addCameraSceneNodeFPS(0, 40.0f, 0.4f, -1, keyMap, 8, false);
	camera->setPosition(vector3df(0.0f, 120.0f, 0.0f));
	vector3df floor = vector3df(0.0f, 10.0f, 0.0f);
	ICgProgrammingServices* gpu = new ICgProgrammingServices(device);
 
	// palmLeafsBark
	c8* palmLeafsBarkFileName = "shaders/palmLeafsBark.cg"; s32 palmLeafsBark = -1;
	ShaderCallBack* shaderCallBackpalmLeafsBark = new ShaderCallBack();
	palmLeafsBark = gpu->addCgShaderMaterialFromFiles(CG_SOURCE,palmLeafsBarkFileName,"main_v","arbvp1","vs_2_0",palmLeafsBarkFileName,"main_f","arbfp1","ps_2_0",shaderCallBackpalmLeafsBark,EMT_SOLID);
	IAnimatedMesh* palmLeafsBarkMesh = sceneManager->getMesh("obj/palmLeafsBark.obj");
	ISceneNode* palmLeafsBarkSceneNode = 0;
	IMesh* palmLeafsBarkTangentMesh = sceneManager->getMeshManipulator()->createMeshWithTangents(palmLeafsBarkMesh->getMesh(0));
	palmLeafsBarkSceneNode= sceneManager->addMeshSceneNode(palmLeafsBarkTangentMesh);
	palmLeafsBarkTangentMesh->drop();
	// palmLeafsBark Material
	palmLeafsBarkSceneNode->setMaterialType((E_MATERIAL_TYPE)palmLeafsBark);
	palmLeafsBarkSceneNode->getMaterial(0).Lighting = false;
	// palmLeafsBark Maps
	ITexture* palmLeafsBarkMap1 = driver->getTexture("maps/palmLeafsBark.png");
	palmLeafsBarkSceneNode->setMaterialTexture(0, palmLeafsBarkMap1);
	ITexture* palmLeafsBarkMap2 = driver->getTexture("maps/white.png");
	palmLeafsBarkSceneNode->setMaterialTexture(1, palmLeafsBarkMap2);
	ITexture* palmLeafsBarkMap3 = driver->getTexture("maps/palmLeafsBarkNormal.png");
	driver->makeNormalMapTexture(palmLeafsBarkMap3, 9.0f);
	palmLeafsBarkSceneNode->setMaterialTexture(2, palmLeafsBarkMap3);
 
	// Rox
	c8* roxFileName = "shaders/rox.cg"; s32 rox = -1;
	ShaderCallBack* shaderCallBackRox = new ShaderCallBack();
	rox = gpu->addCgShaderMaterialFromFiles(CG_SOURCE,roxFileName,"main_v","arbvp1","vs_2_0",roxFileName,"main_f","arbfp1","ps_2_0",shaderCallBackRox,EMT_SOLID);
	IAnimatedMesh* roxMesh = sceneManager->getMesh("obj/rox.obj");
	ISceneNode* roxSceneNode = 0;
	IMesh* roxTangentMesh = sceneManager->getMeshManipulator()->createMeshWithTangents(roxMesh->getMesh(0));
	roxSceneNode= sceneManager->addMeshSceneNode(roxTangentMesh);
	roxTangentMesh->drop();
	// Rox Material
	roxSceneNode->setMaterialType((E_MATERIAL_TYPE)rox);
	roxSceneNode->getMaterial(0).Lighting = false;
	// Rox Maps
	ITexture* roxMap1 = driver->getTexture("maps/roxDiffuse.png");
	roxSceneNode->setMaterialTexture(0, roxMap1);
	ITexture* roxMap2 = driver->getTexture("maps/white.png");
	roxSceneNode->setMaterialTexture(1, roxMap2);
	ITexture* roxMap3 = driver->getTexture("maps/roxNormal.png");
	driver->makeNormalMapTexture(roxMap3, 9.0f);
	roxSceneNode->setMaterialTexture(2, roxMap3);
 
	// roxSmall
	c8* roxSmallFileName = "shaders/rox.cg"; s32 roxSmall = -1;
	ShaderCallBack* shaderCallBackroxSmall = new ShaderCallBack();
	roxSmall = gpu->addCgShaderMaterialFromFiles(CG_SOURCE,roxSmallFileName,"main_v","arbvp1","vs_2_0",roxSmallFileName,"main_f","arbfp1","ps_2_0",shaderCallBackroxSmall,EMT_SOLID);
	IAnimatedMesh* roxSmallMesh = sceneManager->getMesh("obj/roxSmall.obj");
	ISceneNode* roxSmallSceneNode = 0;
	IMesh* roxSmallTangentMesh = sceneManager->getMeshManipulator()->createMeshWithTangents(roxSmallMesh->getMesh(0));
	roxSmallSceneNode= sceneManager->addMeshSceneNode(roxSmallTangentMesh);
	roxSmallTangentMesh->drop();
	// roxSmall Material
	roxSmallSceneNode->setMaterialType((E_MATERIAL_TYPE)roxSmall);
	roxSmallSceneNode->getMaterial(0).Lighting = false;
	// roxSmall Maps
	ITexture* roxSmallMap1 = driver->getTexture("maps/roxDiffuse.png");
	roxSmallSceneNode->setMaterialTexture(0, roxSmallMap1);
	ITexture* roxSmallMap2 = driver->getTexture("maps/white.png");
	roxSmallSceneNode->setMaterialTexture(1, roxSmallMap2);
	ITexture* roxSmallMap3 = driver->getTexture("maps/roxNormal.png");
	driver->makeNormalMapTexture(roxSmallMap3, 9.0f);
	roxSmallSceneNode->setMaterialTexture(2, roxSmallMap3);
 
	// Water
	c8* waterFileName = "shaders/water.cg"; s32 water = -1;
	ShaderCallBack* waterCallBack = new ShaderCallBack();
	water = gpu->addCgShaderMaterialFromFiles(CG_SOURCE,waterFileName,"main_v","arbvp1","vs_3_0",waterFileName,"main_f","arbfp1","ps_3_0",waterCallBack,EMT_TRANSPARENT_ALPHA_CHANNEL);
	IAnimatedMesh* waterMesh = sceneManager->getMesh("obj/water.obj");
	ISceneNode* waterSceneNode = 0;
	IMesh* waterTangentMesh = sceneManager->getMeshManipulator()->createMeshWithTangents(waterMesh->getMesh(0));
	waterSceneNode = sceneManager->addMeshSceneNode(waterTangentMesh);
	waterTangentMesh->drop();
	// Water Material
	waterSceneNode->setMaterialType((E_MATERIAL_TYPE)water);
	waterSceneNode->getMaterial(0).Lighting = false;
	waterSceneNode->getMaterial(0).BlendOperation = EBO_ADD;
	waterSceneNode->getMaterial(0).MaterialTypeParam = pack_textureBlendFunc(EBF_SRC_ALPHA, EBF_ONE_MINUS_SRC_ALPHA, EMFN_MODULATE_1X, EAS_TEXTURE | EAS_VERTEX_COLOR);
	waterSceneNode->setMaterialFlag(EMF_BLEND_OPERATION, true);
	waterSceneNode->setMaterialFlag(EMF_COLOR_MATERIAL, true);
	// Water Maps
	ITexture* waterMap1 = driver->getTexture("maps/waterSand.png");
	waterSceneNode->setMaterialTexture(0, waterMap1);
	ITexture* waterMap2 = driver->getTexture("maps/waterCaustics.png");
	waterSceneNode->setMaterialTexture(1, waterMap2);
	ITexture* waterMap3 = driver->getTexture("maps/waterFoam.png");
	waterSceneNode->setMaterialTexture(2, waterMap3);
	ITexture* waterMap4 = driver->getTexture("maps/waterMap.png");
	waterSceneNode->setMaterialTexture(3, waterMap4);
	ITexture* waterMap5 = driver->getTexture("maps/waterNormal.png");
	driver->makeNormalMapTexture(waterMap5, 9.0f);
	waterSceneNode->setMaterialTexture(4, waterMap5);
 
	// [...]
 
	// skyBox
	ISceneNode* skyBox = sceneManager->addSkyBoxSceneNode(
		driver->getTexture("maps/cubeMapposy.png"),
		driver->getTexture("maps/cubeMapnegy.png"),
		driver->getTexture("maps/cubeMapposz.png"),
		driver->getTexture("maps/cubeMapnegz.png"),
		driver->getTexture("maps/cubeMapposx.png"),
		driver->getTexture("maps/cubeMapnegx.png"));
 
	device->getCursorControl()->setVisible(false);
	PlaySound(".\audio\water.wav", NULL, SND_ASYNC | SND_LOOP | SND_FILENAME);
	printf("\n  Treasure Island\n  Realtime3D Demo DirectX9\n" \
		"\n  http://codewelt.com\n");
	stringw str = "Treasure Island - Realtime3D Demo DirectX9 - http://codewelt.com";
	device->setWindowCaption(str.c_str());
	while(device->run()) {
		if(device->isWindowActive()) {
			driver->beginScene(true, true, SColor(255, 255, 255, 255));
			sceneManager->drawAll();
			driver->endScene();
		}
	}
	delete gpu;
	device->drop();
	return 0;
}
 
 
back