Hello,

I am trying to add light to  the planetary system that is depicted in
the chapter titled "Node Cores" at  the tutorial section.

The scene graph of the planetary system is same as before, - the only
thing that is added now is that i have a NodePtr pLightNode as the
ancestor and set PointLightPtr as its core.

I have set the beacon to point to  the sun node.


But when i compile and run the program i do not see any effect .

I am attaching the code for it(even though it is bit long , it is well
well organized)


So i believe that some one would be kind enough to go through  the code
and let me know what did i miss or i miss the whole concept of lighting.



Regards,
Sajjad
#include "Basic.h"


//The SimpleSceneManager is a useful class which helps us to
//manage simple configurations. It will be discussed in detail later on
SimpleSceneManager *mgr;

//some global variable to simulate the water
NodePtr scene;


//the resolution of the mesh
const int N = 100;

//the 2D array that will store all the height
//values of the mesh  that we will be simulating
Real32 wMesh[N][N];
 

//the origin of the water mesh
Pnt3f wOrigin = Pnt3f(0,0,0);


//width and length of the mesh
UInt32 width = 100;
UInt32 height = 100;

	
//we have a forward declaration here, just to have a better order
//of codepieces
int  setupGLUT( int *argc, char *argv[] );
void moveNode(NodePtr, NodePtr, int);
void motion(int,int);
void mouse(int,int,int,int);
void reshape(int,int);
void display();
void keyboard(unsigned char, int,int);


void initWaterMesh(void);
void updateWaterMesh(Real32);

void updateWaterMesh(Real32 time)
{
	for(int x = 0;x < N; x++)
		for(int z = 0; z < N; z++)
			wMesh[x][z] = 10*cos(time/100.f + (x+z)/10.f); 
}



void initWaterMesh(void)
{
	for(int x =0;x < N; x++) // corresponds to row
		for(int z=0;z < N;z++)// correspods to column
			wMesh[x][z] = 0;
}

NodePtr createWater(void);


NodePtr createWater(void)
{
	//initialize  the mesh  that
	//we have just created
	//so that we do not end up with
	//any garbage value
	initWaterMesh();


	//now we start creating the geometry 
	//step by step and the first thing is to define the
	//primitives that we may want to use
	//and Quads is sufficient for us for that case
	//EVEN THOUGH IT IS POSSIBLE TO USE MORE THAN ONE PRIMITIVE
	GeoPTypesPtr type = GeoPTypesUI8::create();
		
	beginEditCP(type,GeoPTypesUI8::GeoPropDataFieldMask);
		//we want to use the quad only
		//but that is not restricted to only one quad
		type->addValue(GL_QUADS);
		//type->addValue(GL_TRIANGLES);
		//type->addValue(GL_LINES);
	endEditCP(type,GeoPTypesUI8::GeoPropDataFieldMask);

	//now we tell  th opensg how many vertices 
	//the primitives is going to have
	//the length of the single quad is reasonably 4
	//and we want to have more quads and therefore we multiply
	
	//GeoPLength will define the number of the the vertices 
	//of the used primitives
	GeoPLengthsPtr length = GeoPLengthsUI32::create();


	beginEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask);
		//the length of the single
		//quad is four
		length->addValue((N-1)*(N-1)*4);
	endEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask);


	//now we specify  the positions of the vertices
	//used in the specific geometry core
	GeoPositions3fPtr pos = GeoPositions3f::create();

	beginEditCP(pos,GeoPositions3f::GeoPropDataFieldMask);
		//here we put all  h position
		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);


	//now we assign the color of the geometry
	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);



	//Settig the normals as we have done that for colors
	GeoNormals3fPtr norms = GeoNormals3f::create();


	beginEditCP(norms, GeoNormals3f::GeoPropDataFieldMask);
		for(int x = 0;x < N; x++)
			for(int z = 0; z < N; z++)
				norms->addValue(Vec3f(0,1,0));
	endEditCP(norms, GeoNormals3f::GeoPropDataFieldMask);	

	
	//even though the material will be playing no role
	//we need that because without that the renderer 
	//stop doing its job of rendering leaving me with a blank screen
	SimpleMaterialPtr mat = SimpleMaterial::create();

	
	beginEditCP(mat);
		mat->setDiffuse(Color3f(1,0,0));
	endEditCP(mat);
	

	//set  the indices for the quads
	//GeoIndicesUI32 points to 
	//all the 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++)
			{
				//point to four vertices that will
				//define the 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);



	/*
 	As we have created all  the required data
	we can create a the geometry object  that we may need
	*/

	GeometryPtr geo = Geometry::create();
	
	/*
 	*Even though the fiels masks are not manadatory to specify
	*but it is a good practice to specify  them as they do save some of 
	*your time to load up  the geometry
 	*/
	beginEditCP(geo, 
		Geometry::TypesFieldMask    |
		Geometry::LengthsFieldMask  |
		Geometry::IndicesFieldMask  |
		Geometry::PositionsFieldMask |
		Geometry::NormalsFieldMask  |
		Geometry::ColorsFieldMask   |
		Geometry::MaterialFieldMask |
		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::ColorsFieldMask    |
		Geometry::MaterialFieldMask  |
	        Geometry::DlistCacheFieldMask);


	   //Let there be light!!!!!
		
		//start with  the point light
		PointLightPtr pLight = PointLight::create();

		//then i try  the directional light
		//DirectionalLightPtr dLight = DirectionalLight::create();

		NodePtr water = Node::create();
		NodePtr pLightTransformNode = Node::create();
		TransformPtr pLightTransform = Transform::create();
		NodePtr pLightNode = Node::create();
		

		beginEditCP(pLightNode, Node::CoreFieldMask);
			pLightNode->setCore(Group::create());
		endEditCP(pLightNode, Node::CoreFieldMask);


		Matrix m;
		m.setIdentity();
		m.setTranslate(50,25,50);


		beginEditCP(pLightTransform, Transform::MatrixFieldMask);
				pLightTransform->setMatrix(m);
		endEditCP(pLightTransform, Transform::MatrixFieldMask);


		//we add a little sphere that will represent the little light 
		//souce that we are trying to simulate
		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, Node::CoreFieldMask);
				sphereNode->setCore(sphere);
		endEditCP(sphereNode, Node::CoreFieldMask);


		beginEditCP(pLightTransformNode, Node::CoreFieldMask | 
													Node::ChildrenFieldMask);
				pLightTransformNode->setCore(pLightTransform);
				pLightTransformNode->addChild(pLightNode);
				pLightTransformNode->addChild(sphereNode);
		endEditCP(pLightTransformNode, Node::CoreFieldMask | 
												 Node::ChildrenFieldMask);												

	  
		beginEditCP(pLight, PointLight::PositionFieldMask						|
								  PointLight::ConstantAttenuationFieldMask		|
								  PointLight::LinearAttenuationFieldMask			|
								  PointLight::QuadraticAttenuationFieldMask		|
								  PointLight::DiffuseFieldMask						|
								  PointLight::AmbientFieldMask						|
								  PointLight::SpecularFieldMask						|
								  PointLight::BeaconFieldMask);
						//attenuation information
						pLight->setPosition(Pnt3f(0,0,0));
						pLight->setConstantAttenuation(1);
						pLight->setLinearAttenuation(0);
						pLight->setQuadraticAttenuation(0);


						//color information
						pLight->setDiffuse(Color4f(1,1,1,1));
						pLight->setDiffuse(Color4f(0.2,0.2,0.2,1));
						pLight->setSpecular(Color4f(1,1,1,1));


						//set beacon
						pLight->setBeacon(pLightNode);

		beginEditCP(pLight, PointLight::PositionFieldMask						|
								  PointLight::ConstantAttenuationFieldMask		|
								  PointLight::LinearAttenuationFieldMask			|
								  PointLight::QuadraticAttenuationFieldMask		|
								  PointLight::DiffuseFieldMask						|
								  PointLight::AmbientFieldMask						|
								  PointLight::SpecularFieldMask						|
								  PointLight::BeaconFieldMask);	
   

   /*
	beginEditCP(dLight, DirectionalLight::DiffuseFieldMask	|
							  DirectionalLight::AmbientFieldMask	|
							  DirectionalLight::SpecularFieldMask	|
							  DirectionalLight::BeaconFieldMask);
					dLight->setDiffuse(Color4f(1,1,1,1));
					dLight->setAmbient(Color4f(0.2,0.2,0.2,1));
					dLight->setSpecular(Color4f(1,1,1,1));
					dLight->setBeacon(pLightNode);
	endEditCP(dLight,   DirectionalLight::DiffuseFieldMask	|
							  DirectionalLight::AmbientFieldMask	|
							  DirectionalLight::SpecularFieldMask	|
							  DirectionalLight::BeaconFieldMask);
   */
	beginEditCP(water, Node::CoreFieldMask);
		water->setCore(geo);
	endEditCP(water, Node::CoreFieldMask);


	//finally we put the newly created core
	//to the nore and return
	NodePtr root = Node::create();
	
	beginEditCP(root, Node::CoreFieldMask |
							Node::ChildrenFieldMask);
				root->setCore(pLight);
				root->addChild(water);
				root->addChild(pLightTransformNode);
	beginEditCP(root, Node::CoreFieldMask |
							Node::ChildrenFieldMask);
	

	return root;
}



int main(int argc, char **argv)
{
	osgInit(argc,argv);

	//create the GLUTWindow which is same for most of the application
	int winid = setupGLUT(&argc,argv);	


	GLUTWindowPtr gwin = GLUTWindow::create();


	gwin->setId(winid);
	gwin->init();
	
	scene = createWater();


	//Create and setup the SSM
	mgr = new SimpleSceneManager();
	mgr->setWindow(gwin);
	mgr->setRoot(scene);
	mgr->setHeadlight(true);
	mgr->showAll();


	/*
 	without the following code i have to rotate to get  
	a view of the quad. To solve that problem we will be adding 
	value to  the camera position during the setup. 
 	*/
	Navigator *nav = mgr->getNavigator();
	nav->setFrom(nav->getFrom() + Vec3f(0,50,0));
		

	//give control to  the glut mainloop
	glutMainLoop();

}



//react to  the keys
void keyboard(unsigned char k,int x,int y)
{
	switch(k)
	{
		case 27:
		{
			OSG::osgExit();
			exit(0);
		}
		break;
	}
}


//react the mouse motions with pressed buttons
void motion(int x, int y)
{
	mgr->mouseMove(x,y);
	glutPostRedisplay();
}

//react to mouse button presses
void mouse(int button,int state, int x,int y)
{
	if(state)
		mgr->mouseButtonRelease(button,x,y);
	else
		mgr->mouseButtonPress(button,x,y);

	glutPostRedisplay();
}



// react to size changes
void reshape(int w, int h)
{
    mgr->resize(w, h);
    glutPostRedisplay();
}
// just redraw our scene if this GLUT callback is invoked
void display(void)
{
    Real32 time = glutGet(GLUT_ELAPSED_TIME);

    updateWaterMesh(time);

    //even though we update the data structure of the mesh
    //the scenegraph is still the same so we have to extract 
    //the geometry to reflect  the change
    //now we extract  the core out of the root node
    GeometryPtr geo = GeometryPtr::dcast(scene->getChild(0)->getCore());

    //now we modify  the content
    //and first we need a pointer 
    //to  the position of the data field
    GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions());
       
    /* 
    beginEditCP(pos, GeoPositions3f::GeoPropDataFieldMask);
		for(int x = 0;x < N; x++)
			for(int z = 0; z < N; z++)
				pos->setValue(Pnt3f(x,wMesh[x][z],z),N*x+z); 
    endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask);
    */

    //get  the data field pointer that 
    //is usde to store the positions
    GeoPositions3f::StoredFieldType *posField = pos->getFieldPtr();
   
    //get some iterators
    GeoPositions3f::StoredFieldType::iterator last, it;

    //set  the iterator to  the first of the 
    //data element
    it = posField->begin();

    beginEditCP(pos, GeoPositions3f::GeoPropDataFieldMask);
		for(int x = 0; x < N; x++)
			for(int z =0;z < N; z++)
			{
				(*it) = Pnt3f(x,wMesh[x][z],z);
				++it;
			}

    endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask);


    mgr->redraw();
}
//The GLUT subsystem is set up here. This is very similar to other GLUT
//applications. If you have worked with GLUT before, you may have the
//feeling of meeting old friends again, if you have not used GLUT before
//that is no problem. GLUT will be introduced briefly on the next section
int setupGLUT(int *argc, char *argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    int winid = glutCreateWindow("de panaaaaaaaaaaa......");

    // register the GLUT callback functions
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    return winid;

}


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Opensg-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to