Exemple #1
0
NodePtr createScenegraph(){
    // the scene must be created here
    for (int x = 0; x < N; x++)
        for (int z = 0; z < N; z++)
            wMesh[x][z] = 0;
            
    // GeoPTypes will define the types of primitives to be used
    GeoPTypesPtr type = GeoPTypesUI8::create();
    beginEditCP(type, GeoPTypesUI8::GeoPropDataFieldMask);
	// we want to use quads ONLY 
	type->addValue(GL_QUADS);
    endEditCP(type, GeoPTypesUI8::GeoPropDataFieldMask);
    
    // GeoPLength will define the number of vertices of
    // the used primitives
    GeoPLengthsPtr length = GeoPLengthsUI32::create();
    beginEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask);
	// the length of our quads is four ;-)
	length->addValue((N-1)*(N-1)*4);
    endEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask);

    // GeoPositions3f stores the positions of all vertices used in 
    // this specific geometry core
    GeoPositions3fPtr pos = GeoPositions3f::create();
    beginEditCP(pos, GeoPositions3f::GeoPropDataFieldMask);
	// here they all come
	for (int x = 0; x < N; x++)
            for (int z = 0; z < N; z++)
		pos->addValue(Pnt3f(x, wMesh[x][z], z));
    endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask);

    //GeoColors3f stores all color values that will be used
    GeoColors3fPtr colors = GeoColors3f::create();
    beginEditCP(colors, GeoColors3f::GeoPropDataFieldMask);
        for (int x = 0; x < N; x++)
            for (int z = 0; z < N; z++)
		colors->addValue(Color3f(0,0,1));
    endEditCP(colors, GeoColors3f::GeoPropDataFieldMask);
    
    GeoNormals3fPtr norms = GeoNormals3f::create();
    beginEditCP(norms, GeoNormals3f::GeoPropDataFieldMask);
        for (int x = 0; x < N; x++)
            for (int z = 0; z < N; z++)
		// As initially all heights are set to zero thus yielding a plane,
		// we set all normals to (0,1,0) parallel to the y-axis
		norms->addValue(Vec3f(0,1,0));
    endEditCP(norms, GeoNormals3f::GeoPropDataFieldMask);
    
    SimpleMaterialPtr mat = SimpleMaterial::create();
    beginEditCP(mat);
        mat->setDiffuse(Color3f(0,0,1));
    endEditCP(mat);
    
    // GeoIndicesUI32 points to all relevant data used by the 
    // provided primitives
    GeoIndicesUI32Ptr indices = GeoIndicesUI32::create();
    beginEditCP(indices, GeoIndicesUI32::GeoPropDataFieldMask);
        for (int x = 0; x < N-1; x++)
            for (int z = 0; z < N-1; z++){
		// points to four vertices that will
		// define a single quad
		indices->addValue(z*N+x);	
		indices->addValue((z+1)*N+x);
		indices->addValue((z+1)*N+x+1);
		indices->addValue(z*N+x+1);
            }
    endEditCP(indices, GeoIndicesUI32::GeoPropDataFieldMask);

        GeometryPtr geo = Geometry::create();
    beginEditCP(geo,
        Geometry::TypesFieldMask	|
	Geometry::LengthsFieldMask	|
	Geometry::IndicesFieldMask	|
	Geometry::PositionsFieldMask	|
	Geometry::NormalsFieldMask      |
        Geometry::MaterialFieldMask     |
	Geometry::ColorsFieldMask	|
        Geometry::DlistCacheFieldMask
	);
	
	geo->setTypes(type);
	geo->setLengths(length);
	geo->setIndices(indices);
	geo->setPositions(pos);
	geo->setNormals(norms);
        geo->setMaterial(mat);
	//geo->setColors(colors);
        geo->setDlistCache(false);

    endEditCP(geo,
        Geometry::TypesFieldMask	|
	Geometry::LengthsFieldMask	|
	Geometry::IndicesFieldMask	|
	Geometry::PositionsFieldMask	|
	Geometry::NormalsFieldMask	|
        Geometry::MaterialFieldMask     |
	Geometry::ColorsFieldMask	|
        Geometry::DlistCacheFieldMask
	);
    
    PointLightPtr pLight = PointLight::create();
    NodePtr root = Node::create();
    NodePtr water = Node::create();
    NodePtr pLightTransformNode = Node::create();
    TransformPtr pLightTransform = Transform::create();
    NodePtr pLightNode = Node::create();
    
    beginEditCP(pLightNode);
        pLightNode->setCore(Group::create());
    endEditCP(pLightNode);
    
    Matrix m;
    m.setIdentity();
    m.setTranslate(50,25,50);
    
    beginEditCP(pLightTransform);
        pLightTransform->setMatrix(m);
    endEditCP(pLightTransform);
    
    //we add a little spehere that will represent the light source
    GeometryPtr sphere = makeSphereGeo(2,2);

    SimpleMaterialPtr sm = SimpleMaterial::create();

    beginEditCP(sm, SimpleMaterial::DiffuseFieldMask |
                    SimpleMaterial::LitFieldMask);
    {
        sm->setLit(false);
        sm->setDiffuse(Color3f(1,1,1));
    }
    endEditCP  (sm, SimpleMaterial::DiffuseFieldMask |
                    SimpleMaterial::LitFieldMask);

    beginEditCP(sphere, Geometry::MaterialFieldMask);
    {
        sphere->setMaterial(sm);
    }
    endEditCP  (sphere, Geometry::MaterialFieldMask);
    
    NodePtr sphereNode = Node::create();
    beginEditCP(sphereNode);
        sphereNode->setCore(sphere);
    endEditCP(sphereNode);

    beginEditCP(pLightTransformNode);
        pLightTransformNode->setCore(pLightTransform);
        pLightTransformNode->addChild(pLightNode);
        pLightTransformNode->addChild(sphereNode);
    endEditCP(pLightTransformNode);

    beginEditCP(pLight);
        pLight->setPosition(Pnt3f(0,0,0));
    
        //Attenuation parameters
        pLight->setConstantAttenuation(1);
        pLight->setLinearAttenuation(0);
        pLight->setQuadraticAttenuation(0);
        
        //color information
        pLight->setDiffuse(Color4f(1,1,1,1));
        pLight->setAmbient(Color4f(0.2,0.2,0.2,1));
        pLight->setSpecular(Color4f(1,1,1,1));
        
        //set the beacon
        pLight->setBeacon(pLightNode);
    endEditCP  (pLight);

    
    beginEditCP(water);
        water->setCore(geo);
    endEditCP(water);
    
    beginEditCP(root);
        root->setCore(pLight);
        root->addChild(water);
        root->addChild(pLightTransformNode);
    endEditCP(root);    
    return root;
}
void SkyBackgroundPluginForm::setupSkyBackground( const QDir dir, const bool zUp )
{	
	try
	{
		skyBackground = NullFC;
		
		bool imageLoaded[6] = {false, false, false, false, false, false};

		const QFileInfoList* fileList = dir.entryInfoList();
		QFileInfoListIterator it(*fileList);
		QFileInfo* fileInfo;

		while ((fileInfo = it.current()) != 0)
		{
			QString qstr = fileInfo->absFilePath();
			const char *filename = qstr.latin1();

			if (((qstr.find("north") >= 0) || (qstr.find("front") >= 0)) && !imageLoaded[0])
			{
				beginEditCP(images[5]);
				if (images[5]->read(filename))
				{
					beginEditCP(textures[5]);
					textures[5]->setImage(images[5]);
					endEditCP(textures[5]);
					vrLog::info("Sky Background: Front/North image loaded.");
					imageLoaded[0] = true;
				}
				endEditCP(images[5]);
			}
			if (((qstr.find("south") >= 0) || (qstr.find("back") >= 0)) && !imageLoaded[1])
			{
				beginEditCP(images[4]);
				if (images[4]->read(filename))
				{
					if (zUp)
						vrImage::rotateImage180Degrees(images[4]);
					beginEditCP(textures[4]);
					textures[4]->setImage(images[4]);
					endEditCP(textures[4]);
					vrLog::info("Sky Background: Back/South image loaded.");
					imageLoaded[1] = true;
				}
				endEditCP(images[4]);
			}
			if (((qstr.find("down") >= 0) || (qstr.find("bottom") >= 0)) && !imageLoaded[2])
			{
				beginEditCP(images[3]);
				if (images[3]->read(filename))
				{
					beginEditCP(textures[3]);
					textures[3]->setImage(images[3]);
					endEditCP(textures[3]);
					vrLog::info("Sky Background: Bottom/Down image loaded.");
					imageLoaded[2] = true;
				}
				endEditCP(images[3]);
			}
			if (((qstr.find("up") >= 0) || (qstr.find("top") >= 0)) && !imageLoaded[3])
			{
				beginEditCP(images[2]);
				if (images[2]->read(filename))
				{
					if (zUp)
						vrImage::rotateImage180Degrees(images[2]);
					beginEditCP(textures[2]);
					textures[2]->setImage(images[2]);
					endEditCP(textures[2]);
					vrLog::info("Sky Background: Top/Up image loaded.");
					imageLoaded[3] = true;
				}
				endEditCP(images[2]);
			}
			if (((qstr.find("east") >= 0) || (qstr.find("right") >= 0)) && !imageLoaded[4])
			{
				beginEditCP(images[1]);
				if (images[1]->read(filename))
				{
					if (zUp)
						vrImage::rotate90Left(images[1]);
					beginEditCP(textures[1]);
					textures[1]->setImage(images[1]);
					endEditCP(textures[1]);
					vrLog::info("Sky Background: Right/East image loaded.");
					imageLoaded[4] = true;
				}
				endEditCP(images[1]);
			}
			if (((qstr.find("west") >= 0) || (qstr.find("left") >= 0)) && !imageLoaded[5])
			{
				beginEditCP(images[0]);
				if (images[0]->read(filename))
				{
					if (zUp)
						vrImage::rotate90Right(images[0]);
					beginEditCP(textures[0]);
					textures[0]->setImage(images[0]);
					endEditCP(textures[0]);
					vrLog::info("Sky Background: Left/West image loaded.");
					imageLoaded[5] = true;
				}
				endEditCP(images[0]);
			}

			++it;
		}

		skyBackground = SkyBackground::create();
		beginEditCP(skyBackground);
		if (!zUp)
		{
			skyBackground->setFrontTexture(textures[5]);
			skyBackground->setBackTexture(textures[4]);
			skyBackground->setBottomTexture(textures[3]);
			skyBackground->setTopTexture(textures[2]);
			skyBackground->setRightTexture(textures[1]);
			skyBackground->setLeftTexture(textures[0]);
		}
		else
		{
			skyBackground->setFrontTexture(textures[3]);
			skyBackground->setBackTexture(textures[2]);
			skyBackground->setBottomTexture(textures[4]);
			skyBackground->setTopTexture(textures[5]);
			skyBackground->setRightTexture(textures[1]);
			skyBackground->setLeftTexture(textures[0]);
		}
		endEditCP(skyBackground);

		vrOSGWidget *gl = vrOSGWidget::getMGLW(-1);
		ViewportPtr vredViewport = gl->getViewport();
		//oldBackground = vredViewport->getBackground();
		beginEditCP(vredViewport);
		vredViewport->setBackground(skyBackground);
		endEditCP(vredViewport);

		directoryLineEdit->setText(dir.absPath());

		// read light settings
		if (SetLightingCheckBox->isChecked())
		{
			string lightName = LightNameLineEdit->text().ascii();
			if (!QFile::exists(dir.absPath() + "/LightSettings.xml"))
				vrLog::warning("Light Settings not found.");
			else
			{
				QFile* file = new QFile(dir.absPath() + "/LightSettings.xml");
				if (file->open(IO_ReadOnly))
				{
					LightSettingsHandler handler;
					QXmlSimpleReader reader;
					reader.setContentHandler(&handler);
					reader.setErrorHandler(&handler);
					QXmlInputSource source(file);
					reader.parse(source);
					file->close();
					handler.direction.normalize();

					vector<NodePtr> lights;
					vrLights::getLights(lights);
					bool lightSet = false;
					for (vector<NodePtr>::const_iterator it = lights.begin();
						it != lights.end(); ++it)
					{
						LightPtr light = LightPtr::dcast((*it)->getCore());
						if (light != NullFC)
						{
							NodePtr node = *it;
							string name = getName(node);
							if (name.find(lightName) != string::npos)
							{
								setLightSettings(light, handler);
								lightSet = true;
							}
						}
					}

					if (!lightSet)
					{
						NodePtr rootNode = vrScenegraph::getRoot();

						TransformPtr beaconTransform = Transform::create();
						NodePtr beaconNode = Node::create();
						beginEditCP(beaconNode);
						beaconNode->setCore(beaconTransform);
						endEditCP(beaconNode);

						PointLightPtr light = PointLight::create();
						beginEditCP(light);
						light->setAttenuation(1,0,0);
						light->setBeacon(beaconNode);
						endEditCP(light);
						NodePtr lightNode = Node::create();
						beginEditCP(lightNode);
						lightNode->setCore(light);
						endEditCP(lightNode);
						OSG::setName(lightNode, lightName);

						beginEditCP(rootNode);
						rootNode->addChild(lightNode);
						rootNode->addChild(beaconNode);
						endEditCP(rootNode);

						setLightSettings(light, handler);

						vrScenegraph::update(true);
					}

				}
			}
		}
	}
	catch (std::exception const& e)
	{
		cout << "type: " << typeid(e).name() << endl;
		cout << "message: " << e.what() << endl << endl;
	}
}
FBOViewportPtr createSceneFBO(void)
{
    //Create Camera Beacon
    Matrix CameraMat;
    CameraMat.setTranslate(0.0f,0.0f,4.0f);
    TransformPtr CameraBeconCore = Transform::create();
    beginEditCP(CameraBeconCore, Transform::MatrixFieldMask);
        CameraBeconCore->setMatrix(CameraMat);
    endEditCP(CameraBeconCore, Transform::MatrixFieldMask);

    NodePtr CameraBeconNode = Node::create();
    beginEditCP(CameraBeconNode, Node::CoreFieldMask);
        CameraBeconNode->setCore(CameraBeconCore);
    endEditCP(CameraBeconNode, Node::CoreFieldMask);

    //Create Camera
    PerspectiveCameraPtr TheCamera = PerspectiveCamera::create();
    beginEditCP(TheCamera);
        TheCamera->setFov(deg2rad(60.0f));
        TheCamera->setAspect(1.0f);
        TheCamera->setNear(0.1f);
        TheCamera->setFar(100.0f);
        TheCamera->setBeacon(CameraBeconNode);
    endEditCP(TheCamera);
    
    //Make the Material
    BlinnMaterialPtr TheMaterial = BlinnMaterial::create();
    beginEditCP(TheMaterial);
        TheMaterial->setDiffuse(0.8);
        TheMaterial->setColor(Color3f(1.0,1.0,1.0));
        TheMaterial->setAmbientColor(Color3f(1.0,1.0,1.0));
        TheMaterial->setNumLights(1);
    endEditCP(TheMaterial);

										
    // Make Torus Node (creates Torus in background of scene)
    NodePtr TorusGeometryNode = makeTorus(.5, 2, 24, 48);

    beginEditCP(TorusGeometryNode->getCore());
        GeometryPtr::dcast(TorusGeometryNode->getCore())->setMaterial(TheMaterial);
    endEditCP(TorusGeometryNode->getCore());
    calcVertexNormals(GeometryPtr::dcast(TorusGeometryNode->getCore()));
    calcVertexTangents(GeometryPtr::dcast(TorusGeometryNode->getCore()),0,Geometry::TexCoords7FieldId, Geometry::TexCoords6FieldId);

    RootTransformCore = Transform::create();

    NodePtr TorusTransformNode = Node::create();
    beginEditCP(TorusTransformNode, Node::CoreFieldMask);
        TorusTransformNode->setCore(RootTransformCore);
        TorusTransformNode->addChild(TorusGeometryNode);
    endEditCP(TorusTransformNode, Node::CoreFieldMask);

    //Create Light Beacon
    Matrix LightMat;
    LightMat.setTranslate(0.0f,10.0f,1.0f);
    TransformPtr LightBeconCore = Transform::create();
    beginEditCP(LightBeconCore, Transform::MatrixFieldMask);
        LightBeconCore->setMatrix(LightMat);
    endEditCP(LightBeconCore, Transform::MatrixFieldMask);

    NodePtr LightBeconNode = Node::create();
    beginEditCP(LightBeconNode, Node::CoreFieldMask);
        LightBeconNode->setCore(LightBeconCore);
    endEditCP(LightBeconNode, Node::CoreFieldMask);

    //Create Light
    TheLight = PointLight::create();
    beginEditCP(TheLight);
        TheLight->setBeacon(LightBeconNode);
    endEditCP(TheLight);

    NodePtr LightNode = Node::create();
    beginEditCP(LightNode, Node::CoreFieldMask);
        LightNode->setCore(TheLight);
        LightNode->addChild(TorusTransformNode);
    endEditCP(LightNode, Node::CoreFieldMask);


    //Create Root

    NodePtr TheRoot = Node::create();
    beginEditCP(TheRoot);
        TheRoot->setCore(Group::create());
        TheRoot->addChild(CameraBeconNode);
        TheRoot->addChild(LightNode);
        TheRoot->addChild(LightBeconNode);
    endEditCP(TheRoot);

    //Create Background
    SolidBackgroundPtr TheBackground = SolidBackground::create();
    TheBackground->setColor(Color3f(1.0,0.0,0.0));

    //DepthClearBackgroundPtr TheBackground = DepthClearBackground::create();

    //Create the Image
    ImagePtr TheColorImage = Image::create();
    TheColorImage->set(Image::OSG_RGB_PF,2,2,1,1,1,0.0f,0,Image::OSG_FLOAT16_IMAGEDATA);

    //Create the texture
    TextureChunkPtr TheColorTextureChunk = TextureChunk::create();
    beginEditCP(TheColorTextureChunk);
        TheColorTextureChunk->setImage(TheColorImage);

        TheColorTextureChunk->setMinFilter(GL_NEAREST);
        TheColorTextureChunk->setMagFilter(GL_NEAREST);

        TheColorTextureChunk->setWrapS(GL_CLAMP_TO_EDGE);
        TheColorTextureChunk->setWrapR(GL_CLAMP_TO_EDGE);

        TheColorTextureChunk->setScale(false);
        TheColorTextureChunk->setNPOTMatrixScale(true);
        
        TheColorTextureChunk->setEnvMode(GL_REPLACE);
        TheColorTextureChunk->setInternalFormat(GL_RGB16F);

    endEditCP(TheColorTextureChunk);


    //Create FBO
    FBOViewportPtr TheFBO = FBOViewport::create();
    beginEditCP(TheFBO);
        TheFBO->setBackground(TheBackground);
        TheFBO->setRoot(TheRoot);
        TheFBO->setCamera(TheCamera);

        TheFBO->setEnabled(true);
        TheFBO->getTextures().push_back(TheColorTextureChunk);

        TheFBO->setStorageWidth(TheColorTextureChunk->getImage()->getWidth());
        TheFBO->setStorageHeight(TheColorTextureChunk->getImage()->getHeight());
        
        TheFBO->setSize(0,0,TheColorTextureChunk->getImage()->getWidth()-1, TheColorTextureChunk->getImage()->getHeight()-1);
    endEditCP(TheFBO);
    return TheFBO;
}
Exemple #4
0
// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    // GLUT init
    int winid = setupGLUT(&argc, argv);
    gwin = GLUTWindow::create();

    // create root
    rootNode = makeCoredNode<Group>();
    NodePtr scene = makeCoredNode<Group>();

    // create lights
    TransformPtr point1_trans;
    NodePtr point1 = makeCoredNode<PointLight>(&_point1_core);
    NodePtr point1_beacon = makeCoredNode<Transform>(&point1_trans);
    beginEditCP(point1_trans);
        point1_trans->editMatrix().setTranslate(0.0, 0.0, 25.0);
    endEditCP(point1_trans);

    beginEditCP(_point1_core);
        _point1_core->setAmbient(0.15,0.15,0.15,1);
        _point1_core->setDiffuse(0.4,0.4,0.4,1);
        _point1_core->setSpecular(0.0,0.0,0.0,1);
        _point1_core->setBeacon(point1_beacon);
        _point1_core->setOn(true);
    endEditCP(_point1_core);

    TransformPtr point2_trans;
    NodePtr point2 = makeCoredNode<PointLight>(&_point2_core);
    NodePtr point2_beacon = makeCoredNode<Transform>(&point2_trans);
    beginEditCP(point2_trans);
        point2_trans->editMatrix().setTranslate(5.0, 5.0, 20.0);
    endEditCP(point2_trans);

    beginEditCP(_point2_core);
        _point2_core->setAmbient(0.15,0.15,0.15,1);
        _point2_core->setDiffuse(0.4,0.4,0.4,1);
        _point2_core->setSpecular(0.0,0.0,0.0,1);
        _point2_core->setBeacon(point2_beacon);
        _point2_core->setOn(true);
    endEditCP(_point2_core);

    beginEditCP(point1);
        point1->addChild(point2);
    endEditCP(point1);

    beginEditCP(point2);
        point2->addChild(scene);
    endEditCP(point2);

    // create scene
    
    // bottom
    NodePtr plane = makePlane(25.0, 25.0, 128, 128);
    
    int size = imageWinWidth*imageWinHeight*256;

    ImagePtr plane_img = Image::create();

    beginEditCP(plane_img);
    plane_img->set(Image::OSG_RGBA_PF, imageWinWidth, imageWinHeight, 1, 1, 1, 0, NULL);
    endEditCP(plane_img);

    TextureChunkPtr plane_tex = TextureChunk::create();
    beginEditCP(plane_tex);
        plane_tex->setImage(plane_img);
        plane_tex->setMinFilter(GL_LINEAR);
        plane_tex->setMagFilter(GL_LINEAR);
        plane_tex->setTarget(GL_TEXTURE_2D);
        plane_tex->setInternalFormat(GL_RGBA16F_ARB);
    endEditCP(plane_tex);

    SHLChunkPtr shl = SHLChunk::create();
    beginEditCP(shl);
        shl->setVertexProgram(_vp_program);
        shl->setFragmentProgram(_fp_program);
        shl->setUniformParameter("tex0", 0);
    endEditCP(shl);

    SimpleMaterialPtr plane_mat = SimpleMaterial::create();
    beginEditCP(plane_mat);
        plane_mat->setAmbient(Color3f(0.3,0.3,0.3));
        plane_mat->setDiffuse(Color3f(1.0,1.0,1.0));
        plane_mat->addChunk(plane_tex);
        plane_mat->addChunk(shl);
    endEditCP(plane_mat);

    GeometryPtr plane_geo = GeometryPtr::dcast(plane->getCore());
    beginEditCP(plane_geo);
        plane_geo->setMaterial(plane_mat);
    beginEditCP(plane_geo);
    
    // box
    box_trans_node = makeCoredNode<Transform>(&_box_trans);
    beginEditCP(_box_trans);
        _box_trans->editMatrix().setTranslate(0.0, 0.0, 12.0);
    endEditCP(_box_trans);
    NodePtr box = makeBox(4.0, 4.0, 0.8, 10, 10 , 10);
    beginEditCP(box_trans_node);
        box_trans_node->addChild(box);
    endEditCP(box_trans_node);
    
    PolygonChunkPtr pchunk = osg::PolygonChunk::create();
    beginEditCP(pchunk);
        pchunk->setCullFace(GL_BACK);
    endEditCP(pchunk);

    SimpleMaterialPtr box_mat = SimpleMaterial::create();
    beginEditCP(box_mat);
        box_mat->setAmbient(Color3f(0.0,0.0,0.0));
        box_mat->setDiffuse(Color3f(0.0,0.0,1.0));
        box_mat->addChunk(pchunk);
    endEditCP(box_mat);

    GeometryPtr box_geo = GeometryPtr::dcast(box->getCore());
    beginEditCP(box_geo);
        box_geo->setMaterial(box_mat);
    beginEditCP(box_geo);

    // cylinder1
    NodePtr cylinder1_trans_node = makeCoredNode<Transform>(&_cylinder1_trans);
    beginEditCP(_cylinder1_trans);
        _cylinder1_trans->editMatrix().setTranslate(0.0, 0.0, 5.0);
    endEditCP(_cylinder1_trans);
    NodePtr cylinder1 = OSG::makeCylinder(10.0, 0.4, 32, true, true ,true);
    beginEditCP(cylinder1_trans_node);
        cylinder1_trans_node->addChild(cylinder1);
    endEditCP(cylinder1_trans_node);

    SimpleMaterialPtr cylinder1_mat = SimpleMaterial::create();
    beginEditCP(cylinder1_mat);
        cylinder1_mat->setAmbient(Color3f(0.0,0.0,0.0));
        cylinder1_mat->setDiffuse(Color3f(1.0,0.0,0.0));
        cylinder1_mat->addChunk(pchunk);
    endEditCP(cylinder1_mat);

    GeometryPtr cylinder1_geo = GeometryPtr::dcast(cylinder1->getCore());
    beginEditCP(cylinder1_geo);
        cylinder1_geo->setMaterial(cylinder1_mat);
    beginEditCP(cylinder1_geo);
    
    // cylinder2
    NodePtr cylinder2_trans_node = makeCoredNode<Transform>(&_cylinder2_trans);
    beginEditCP(_cylinder2_trans);
        _cylinder2_trans->editMatrix().setTranslate(0.0, 0.0, 8.0);
    endEditCP(_cylinder2_trans);
    NodePtr cylinder2 = OSG::makeCylinder(10.0, 0.4, 32, true, true ,true);
    beginEditCP(cylinder2_trans_node);
        cylinder2_trans_node->addChild(cylinder2);
    endEditCP(cylinder2_trans_node);

    SimpleMaterialPtr cylinder2_mat = SimpleMaterial::create();
    beginEditCP(cylinder2_mat);
        cylinder2_mat->setAmbient(Color3f(0.0,0.0,0.0));
        cylinder2_mat->setDiffuse(Color3f(0.0,1.0,0.0));
        cylinder2_mat->addChunk(pchunk);
    endEditCP(cylinder2_mat);

    GeometryPtr cylinder2_geo = GeometryPtr::dcast(cylinder2->getCore());
    beginEditCP(cylinder2_geo);
        cylinder2_geo->setMaterial(cylinder2_mat);
    beginEditCP(cylinder2_geo);

    // scene
    beginEditCP(scene);
        scene->addChild(plane);
        scene->addChild(box_trans_node);
        scene->addChild(cylinder1_trans_node);
        scene->addChild(cylinder2_trans_node);
    endEditCP(scene);

    vp = ShadowViewport::create();
    
    GradientBackgroundPtr gbg = GradientBackground::create();
    SolidBackgroundPtr sbg = SolidBackground::create();

    UChar8 imgdata[] = {  255,0,0,  0,255,0,  0,0,255, 255,255,0 };
    ImagePtr img1 = Image::create();
    img1->set(Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, imgdata);

    TextureChunkPtr tcPtr = TextureChunk::create();
    beginEditCP(tcPtr);
        tcPtr->setImage(img1);
    endEditCP(tcPtr);
    TextureBackgroundPtr bkg = TextureBackground::create();
    beginEditCP(bkg);
        bkg->setTexture(tcPtr);
    endEditCP(bkg);
    
    beginEditCP(sbg);
        sbg->setColor(Color3f(0.2,0.4,0.6));
    endEditCP(sbg);
    
    beginEditCP(gbg);
        gbg->addLine(Color3f(0.7, 0.7, 0.8), 0);
        gbg->addLine(Color3f(0.1, 0.1, 0.3), 1);
    endEditCP(gbg);

    beginEditCP(rootNode);
        rootNode->addChild(point1);
        rootNode->addChild(point1_beacon);
        rootNode->addChild(point2_beacon);
    endEditCP(rootNode);

    //FBOViewportPtr
    fbo_vp = FBOViewport::create();
    addRefCP(fbo_vp);

    // the Camera for the map
    cam = PerspectiveCamera::create();
    beginEditCP(cam);
        cam->setFov(osgdegree2rad(90));
        cam->setAspect(1);
        cam->setNear(0.001);
        cam->setFar(10000);
        cam->setBeacon(box_trans_node);
    endEditCP(cam);

    beginEditCP(fbo_vp);
        fbo_vp->setBackground(bkg);
        fbo_vp->setRoot(rootNode);
        fbo_vp->setCamera(cam);
        fbo_vp->setSize(0,0,imageWinWidth-1, imageWinHeight-1);
        fbo_vp->setStorageWidth(imageWinWidth);
        fbo_vp->setStorageHeight(imageWinHeight);
        fbo_vp->setDirty(true);
        fbo_vp->editMFTextures    ()->push_back(plane_tex);
        fbo_vp->editMFExcludeNodes()->push_back(plane);
        fbo_vp->setFboOn(true);
    endEditCP(fbo_vp);

    // normal shadow viewport
    beginEditCP(vp);
        vp->setBackground(gbg);
        vp->setRoot(rootNode);
        vp->setSize(0,0,1,1);
    endEditCP(vp);

    beginEditCP(gwin); //Window
        gwin->setId(winid);
        gwin->addPort(vp);
        gwin->init();
    endEditCP(gwin);

    Vec3f min,max;
    rootNode->updateVolume();
    rootNode->getVolume().getBounds( min, max );

    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    mgr->setWindow(gwin);
    mgr->setRoot(rootNode);

    //Viewport
    beginEditCP(vp);
        vp->setCamera(mgr->getCamera());
    endEditCP(vp);

    mgr->turnHeadlightOff();

    mgr->showAll();

    // GLUT main loop
    glutMainLoop();

    return 0;
}
// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    // Set up Window
    TutorialWindowEventProducer = createDefaultWindowEventProducer();
    WindowPtr MainWindow = TutorialWindowEventProducer->initWindow();

    TutorialWindowEventProducer->setDisplayCallback(display);
    TutorialWindowEventProducer->setReshapeCallback(reshape);

    //Add Window Listener
    TutorialKeyListener TheKeyListener;
    TutorialWindowEventProducer->addKeyListener(&TheKeyListener);
    TutorialMouseListener TheTutorialMouseListener;
    TutorialMouseMotionListener TheTutorialMouseMotionListener;
    TutorialWindowEventProducer->addMouseListener(&TheTutorialMouseListener);
    TutorialWindowEventProducer->addMouseMotionListener(&TheTutorialMouseMotionListener);

    // Create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

	
    // Tell the Manager what to manage
    mgr->setWindow(TutorialWindowEventProducer->getWindow());

    //Make a SphereNode for the point light
    LambertMaterialPtr TheLightMat = LambertMaterial::create();
    beginEditCP(TheLightMat, LambertMaterial::IncandescenceFieldMask);
        TheLightMat->setIncandescence(Color3f(1.0,1.0,1.0));
    endEditCP(TheLightMat, LambertMaterial::IncandescenceFieldMask);

    GeometryPtr LightSphereGeo = makeSphereGeo(2,2.0);
    beginEditCP(LightSphereGeo, Geometry::MaterialFieldMask);
        LightSphereGeo->setMaterial(TheLightMat);
    endEditCP  (LightSphereGeo, Geometry::MaterialFieldMask);

    NodePtr LightSphereNode = Node::create();
    beginEditCP(LightSphereNode, Node::CoreFieldMask);
		LightSphereNode->setCore(LightSphereGeo);
    endEditCP  (LightSphereNode, Node::CoreFieldMask);

    //Create the beacon for the Point Light
    Matrix ThePointLightMat;
    ThePointLightMat.setTranslate(Vec3f(0.0,100.0,0.0));
    
    ThePointLightBeaconTransform = Transform::create();
    beginEditCP(ThePointLightBeaconTransform);
        ThePointLightBeaconTransform->setMatrix(ThePointLightMat);
    endEditCP(ThePointLightBeaconTransform);

    NodePtr ThePointLightBeaconNode = Node::create();
    beginEditCP(ThePointLightBeaconNode);
        ThePointLightBeaconNode->setCore(ThePointLightBeaconTransform);
        ThePointLightBeaconNode->addChild(LightSphereNode);
    endEditCP(ThePointLightBeaconNode);

    //Set the light properties desired
    PointLightPtr ThePointLight = PointLight::create();
    beginEditCP(ThePointLight);
        ThePointLight->setAmbient(0.3,0.3,0.3,0.3);
        ThePointLight->setDiffuse(1.0,1.0,1.0,1.0);
        ThePointLight->setSpecular(1.0,1.0,1.0,1.0);
        ThePointLight->setBeacon(ThePointLightBeaconNode);
    endEditCP(ThePointLight);

    NodePtr ThePointLightNode = Node::create();
    beginEditCP(ThePointLightNode);
        ThePointLightNode->setCore(ThePointLight);
    endEditCP(ThePointLightNode);
    
    //Set the light properties desired
    SpotLightPtr TheSpotLight = SpotLight::create();
    beginEditCP(TheSpotLight);
        TheSpotLight->setAmbient(0.3,0.3,0.3,0.3);
        TheSpotLight->setDiffuse(1.0,1.0,1.0,1.0);
        TheSpotLight->setSpecular(1.0,1.0,1.0,1.0);
        TheSpotLight->setBeacon(ThePointLightBeaconNode);
        TheSpotLight->setDirection(Vec3f(0.0,-1.0,0.0));
        TheSpotLight->setSpotExponent(5.0);
        TheSpotLight->setSpotCutOff(1.1);
    endEditCP(TheSpotLight);

    NodePtr TheSpotLightNode = Node::create();
    beginEditCP(TheSpotLightNode);
        TheSpotLightNode->setCore(TheSpotLight);
    endEditCP(TheSpotLightNode);

	//Load in the Heightmap Image
	ImagePtr PerlinNoiseImage = createPerlinImage(Vec2s(256,256), Vec2f(10.0f,10.0f),0.5f,1.0f,Vec2f(0.0f,0.0f),0.25f,6,PERLIN_INTERPOLATE_COSINE,false,Image::OSG_L_PF, Image::OSG_UINT8_IMAGEDATA);

    TextureChunkPtr TheTextureChunk = TextureChunk::create();
    beginEditCP(TheTextureChunk);
        TheTextureChunk->setImage(PerlinNoiseImage);
    endEditCP(TheTextureChunk);

    //Lambert Material
    LambertMaterialPtr TheLambertMat = LambertMaterial::create();
    beginEditCP(TheLambertMat, LambertMaterial::ColorFieldMask | LambertMaterial::AmbientColorFieldMask | LambertMaterial::DiffuseFieldMask
                              | LambertMaterial::NumLightsFieldMask | LambertMaterial::DiffuseTextureFieldMask);
        TheLambertMat->setColor(Color3f(0.0,1.0,0.0));
        TheLambertMat->setAmbientColor(Color3f(1.0,0.0,0.0));
        TheLambertMat->setDiffuse(0.5);
        TheLambertMat->setNumLights(1);
    endEditCP(TheLambertMat, LambertMaterial::ColorFieldMask | LambertMaterial::AmbientColorFieldMask | LambertMaterial::DiffuseFieldMask
                              | LambertMaterial::NumLightsFieldMask | LambertMaterial::DiffuseTextureFieldMask);
    

    //Blinn Material
    TheBlinnMat = BlinnMaterial::create();
    beginEditCP(TheBlinnMat, BlinnMaterial::ColorFieldMask | BlinnMaterial::AmbientColorFieldMask | BlinnMaterial::DiffuseFieldMask
         | BlinnMaterial::SpecularColorFieldMask | BlinnMaterial::SpecularEccentricityFieldMask | BlinnMaterial::SpecularRolloffFieldMask | BlinnMaterial::DiffuseTextureFieldMask);
        TheBlinnMat->setColor(Color3f(1.0,0.0,0.0));
        TheBlinnMat->setAmbientColor(Color3f(0.0,0.0,0.0));
        TheBlinnMat->setSpecularColor(Color3f(0.0,0.0,1.0));
        TheBlinnMat->setSpecularEccentricity(0.35);
        TheBlinnMat->setSpecularRolloff(0.85);
        TheBlinnMat->setDiffuse(0.65);
        TheBlinnMat->setDiffuseTexture(TheTextureChunk);
    endEditCP(TheBlinnMat, BlinnMaterial::ColorFieldMask | BlinnMaterial::AmbientColorFieldMask | BlinnMaterial::DiffuseFieldMask
         | BlinnMaterial::SpecularColorFieldMask | BlinnMaterial::SpecularEccentricityFieldMask | BlinnMaterial::SpecularRolloffFieldMask | BlinnMaterial::DiffuseTextureFieldMask);
    
    //Phong Material
    Phong2MaterialPtr ThePhongMat = Phong2Material::create();
    beginEditCP(ThePhongMat, Phong2Material::ColorFieldMask | Phong2Material::AmbientColorFieldMask | Phong2Material::DiffuseFieldMask
         | Phong2Material::SpecularColorFieldMask | Phong2Material::SpecularCosinePowerFieldMask);
        ThePhongMat->setColor(Color3f(1.0,0.0,0.0));
        ThePhongMat->setAmbientColor(Color3f(0.0,0.0,0.0));
        ThePhongMat->setSpecularColor(Color3f(0.0,0.0,1.0));
        ThePhongMat->setSpecularCosinePower(50.0);
        ThePhongMat->setDiffuse(0.65);
    endEditCP(ThePhongMat, Phong2Material::ColorFieldMask | Phong2Material::AmbientColorFieldMask | Phong2Material::DiffuseFieldMask
         | Phong2Material::SpecularColorFieldMask | Phong2Material::SpecularCosinePowerFieldMask);

    //Anisotropic Material
    AnisotropicMaterialPtr TheAnisotropicMat = AnisotropicMaterial::create();
    beginEditCP(TheAnisotropicMat, AnisotropicMaterial::ColorFieldMask | AnisotropicMaterial::AmbientColorFieldMask | AnisotropicMaterial::DiffuseFieldMask
         | AnisotropicMaterial::SpecularColorFieldMask | AnisotropicMaterial::SpecularRoughnessFieldMask | AnisotropicMaterial::SpecularFresnelIndexFieldMask
          | AnisotropicMaterial::SpecularSpreadXFieldMask | AnisotropicMaterial::SpecularSpreadYFieldMask);
        TheAnisotropicMat->setColor(Color3f(1.0,0.0,0.0));
        TheAnisotropicMat->setAmbientColor(Color3f(0.0,0.0,0.0));
        TheAnisotropicMat->setDiffuse(0.65);
        TheAnisotropicMat->setSpecularColor(Color3f(0.0,0.0,1.0));
        TheAnisotropicMat->setSpecularRoughness(32.0);
        TheAnisotropicMat->setSpecularFresnelIndex(0.85);
        TheAnisotropicMat->setSpecularSpreadX(1.0);
        TheAnisotropicMat->setSpecularSpreadY(1.0);
    endEditCP(TheAnisotropicMat, AnisotropicMaterial::ColorFieldMask | AnisotropicMaterial::AmbientColorFieldMask | AnisotropicMaterial::DiffuseFieldMask
         | AnisotropicMaterial::SpecularColorFieldMask | AnisotropicMaterial::SpecularRoughnessFieldMask | AnisotropicMaterial::SpecularFresnelIndexFieldMask
          | AnisotropicMaterial::SpecularSpreadXFieldMask | AnisotropicMaterial::SpecularSpreadYFieldMask);

    PointChunkPtr TempChunk = PointChunk::create();
    //addRefCP(TempChunk);

    //Anisotropic Material
    TheRampMat = RampMaterial::create();
    beginEditCP(TheRampMat);
        //Color
        TheRampMat->setRampSource(RampMaterial::RAMP_SOURCE_FACING_ANGLE);
        TheRampMat->getColors().push_back(Color3f(1.0,0.0,0.0));
        TheRampMat->getColorPositions().push_back(0.4);
        TheRampMat->getColorInterpolations().push_back(RampMaterial::RAMP_INTERPOLATION_SMOOTH);
        TheRampMat->getColors().push_back(Color3f(0.0,1.0,0.0));
        TheRampMat->getColorPositions().push_back(1.0);
        
        //Transparency
        TheRampMat->getTransparencies().push_back(Color3f(0.0,0.0,0.0));
        TheRampMat->getTransparencyPositions().push_back(0.83);
        TheRampMat->getTransparencyInterpolations().push_back(RampMaterial::RAMP_INTERPOLATION_SMOOTH);
        TheRampMat->getTransparencies().push_back(Color3f(1.0,1.0,1.0));
        TheRampMat->getTransparencyPositions().push_back(1.0);

        TheRampMat->setAmbientColor(Color3f(0.0,0.0,0.0));
        TheRampMat->setSpecularity(1.0);
        TheRampMat->setSpecularEccentricity(0.8);
        TheRampMat->getSpecularColors().push_back(Color3f(1.0,1.0,1.0));
        TheRampMat->getSpecularColorPositions().push_back(0.95);
        TheRampMat->getSpecularColorInterpolations().push_back(RampMaterial::RAMP_INTERPOLATION_SMOOTH);
        TheRampMat->getSpecularColors().push_back(Color3f(0.0,0.0,1.0));
        TheRampMat->getSpecularColorPositions().push_back(1.0);
        TheRampMat->getSpecularRolloffs().push_back(1.0);
        TheRampMat->getExtraChunks().push_back(TempChunk);
    endEditCP(TheRampMat);



	//Make the Heightmap Geometry
	HeightmapGeometryPtr TutorialHeightmapGeo = HeightmapGeometry::create();
	beginEditCP(TutorialHeightmapGeo, HeightmapGeometry::HeightImageFieldMask | HeightmapGeometry::DimensionsFieldMask | HeightmapGeometry::SegmentsFieldMask | HeightmapGeometry::ScaleFieldMask | HeightmapGeometry::OffsetFieldMask | HeightmapGeometry::MaterialFieldMask);
		TutorialHeightmapGeo->setHeightImage(PerlinNoiseImage);
		TutorialHeightmapGeo->setDimensions(Vec2f(200.0,200.0));
		TutorialHeightmapGeo->setSegments(Vec2f(150.0,150.0));
		TutorialHeightmapGeo->setScale(30.0);
		TutorialHeightmapGeo->setOffset(0.0);
		TutorialHeightmapGeo->setMaterial( TheBlinnMat );
	endEditCP(TutorialHeightmapGeo, HeightmapGeometry::HeightImageFieldMask | HeightmapGeometry::DimensionsFieldMask | HeightmapGeometry::SegmentsFieldMask | HeightmapGeometry::ScaleFieldMask | HeightmapGeometry::OffsetFieldMask | HeightmapGeometry::MaterialFieldMask);

    calcVertexNormals(TutorialHeightmapGeo);
    calcVertexTangents(TutorialHeightmapGeo,0,Geometry::TexCoords7FieldId, Geometry::TexCoords6FieldId);

    //Make the Heightmap Node
    NodePtr TutorialHeightmapNode = Node::create();
    beginEditCP(TutorialHeightmapNode, Node::CoreFieldMask);
		TutorialHeightmapNode->setCore(TutorialHeightmapGeo);
    endEditCP  (TutorialHeightmapNode, Node::CoreFieldMask);

    //Make a SphereNode
    GeometryPtr SphereGeo = makeSphereGeo(2,50.0);
    //GeometryPtr SphereGeo = makeCylinderGeo(50,20.0, 16,true,true,true);
    beginEditCP(SphereGeo, Geometry::MaterialFieldMask);
		SphereGeo->setMaterial(TheLambertMat);
    endEditCP  (SphereGeo, Geometry::MaterialFieldMask);
    calcVertexNormals(SphereGeo);
    calcVertexTangents(SphereGeo,0,Geometry::TexCoords7FieldId, Geometry::TexCoords6FieldId);

    NodePtr SphereNode = Node::create();
    beginEditCP(SphereNode, Node::CoreFieldMask);
		SphereNode->setCore(SphereGeo);
    endEditCP  (SphereNode, Node::CoreFieldMask);

    //Make Main Scene Node
    NodePtr scene = Node::create();
    beginEditCP(scene, Node::CoreFieldMask | Node::ChildrenFieldMask);
		scene->setCore(Group::create());
 
        // add the torus as a child
        scene->addChild(TutorialHeightmapNode);
        //scene->addChild(SphereNode);
        scene->addChild(ThePointLightBeaconNode);
    endEditCP  (scene, Node::CoreFieldMask | Node::ChildrenFieldMask);

    //Add the scene to the Light Nodes
    //beginEditCP(ThePointLightNode, Node::ChildrenFieldMask);
        //ThePointLightNode->addChild(scene);
    //endEditCP(ThePointLightNode, Node::ChildrenFieldMask);


    //// tell the manager what to manage
    //mgr->setRoot  (ThePointLightNode);

    beginEditCP(TheSpotLightNode, Node::ChildrenFieldMask);
        TheSpotLightNode->addChild(scene);
    endEditCP(TheSpotLightNode, Node::ChildrenFieldMask);


    // tell the manager what to manage
    mgr->setRoot  (TheSpotLightNode);
    mgr->turnHeadlightOff();

    // show the whole scene
    mgr->showAll();
    
    //Open Window
    Vec2f WinSize(TutorialWindowEventProducer->getDesktopSize() * 0.85f);
    Pnt2f WinPos((TutorialWindowEventProducer->getDesktopSize() - WinSize) *0.5);
    TutorialWindowEventProducer->openWindow(WinPos,
                        WinSize,
                                        "06Heightmap");

    //Main Loop
    TutorialWindowEventProducer->mainLoop();

    osgExit();

    return 0;
}