コード例 #1
0
ファイル: lr1.c プロジェクト: shohoku11wrj/duck-lang
/* build a canonical LR(1) parse table */
int BuildLRParser(GRAMMAR_TABLE grammar,
                  LR_TABLE*     parser)
{
    FindNullableNonterminals(&grammar);
    BuildFirstSets(&grammar);
    BuildFollowSets(&grammar);
    
    // building the collection of item sets for an LR(1) grammar
    LR_ITEM_COLLECTION* C = CanonicalCollection(&grammar);
    //PrintCollection(C, &grammar);
    
    RemoveEpsilons(&grammar);
    
    *parser = ConstructTable(C, &grammar);
    FreeCollection(C);
    return 0;
}
コード例 #2
0
ファイル: PhongShading.cpp プロジェクト: m-hogue/JHU-Graphics
/**
 * Construct the scene
 */
void ConstructScene()
{
   // Construct the lighting shader node
   LightingShaderNode* lightingShader = new LightingShaderNode();
   if (!lightingShader->Create("phong.vert", "phong.frag") ||
       !lightingShader->GetLocations())
      exit(-1);

   int positionLoc = lightingShader->GetPositionLoc();
   int normalLoc = lightingShader->GetNormalLoc();

   // -------------------- Geometry -------------------- //

   // Construct a unit square - use less subdivisions to see how
   // phong shading improves the lighting
   UnitSquareSurface* unitSquare = new UnitSquareSurface(2, positionLoc, normalLoc);

   // Construct a unit box
   SceneNode* box = ConstructUnitBox(unitSquare);

   // Construct a unit cylinder surface
   ConicSurface* cylinder = new ConicSurface(1.0f, 1.0f, 18, 4, positionLoc, normalLoc);

   // Construct a torus
   TorusSurface* torus = new TorusSurface(20.0f, 5.0f, 18, 18, positionLoc, normalLoc);

   // Teapot
   MeshTeapot* teapot = new MeshTeapot(3, positionLoc, normalLoc);

   // Sphere
   SphereSection* sphere = new SphereSection(-90.0f, 90.0f, 18, 
               -180.0f, 180.0f, 36, 1.0f, positionLoc, normalLoc);

   //-------------------- Materials ------------------------- //

   // Wood
   PresentationNode* wood = new PresentationNode;
   wood->SetMaterialAmbientAndDiffuse(Color4(0.55f, 0.45f, 0.15f));
   wood->SetMaterialSpecular(Color4(0.3f, 0.3f, 0.3f));
   wood->SetMaterialShininess(64.0f);

   // Silver
   PresentationNode* silver = new PresentationNode;
   silver->SetMaterialAmbient(Color4(0.19225f, 0.19225f, 0.19225f));
   silver->SetMaterialDiffuse(Color4(0.50754f, 0.50754f, 0.50754f));
   silver->SetMaterialSpecular(Color4(0.508273f, 0.508273f, 0.508273f));
   silver->SetMaterialShininess(51.2f);

   // Black, shiny
   PresentationNode* shinyBlack = new PresentationNode;
   shinyBlack->SetMaterialAmbient(Color4(0.0f, 0.0f, 0.0f));
   shinyBlack->SetMaterialDiffuse(Color4(0.01f, 0.01f, 0.01f));
   shinyBlack->SetMaterialSpecular(Color4(0.5f, 0.5f, 0.5f));
   shinyBlack->SetMaterialShininess(32.0f);

   // Shiny blue
   PresentationNode* shinyBlue = new PresentationNode;
   shinyBlue->SetMaterialAmbient(Color4(0.05f, 0.05f, 0.2f));
   shinyBlue->SetMaterialDiffuse(Color4(0.2f, 0.2f, 0.6f));
   shinyBlue->SetMaterialSpecular(Color4(0.75f, 0.75, 0.75f));
   shinyBlue->SetMaterialShininess(76.8f);

   // ------------------ Transformations ------------------- //

   // Position the table in the room
   TransformNode* tableTransform = new TransformNode;
   tableTransform->Translate(-50.0f, 50.0f, 0.0f);
   tableTransform->Rotate(30.0f, 0.0f, 0.0f, 1.0f);

   // Teapot transform
   TransformNode* teapotTransform = new TransformNode;
   teapotTransform->Translate(0.0f, 0.0f, 26.0f);
   teapotTransform->Scale(2.5f, 2.5f, 2.5f);

   // Torus
   TransformNode* torusTransform = new TransformNode;
   torusTransform->Translate(0.0f, 90.0f, 20.0f);
   torusTransform->Rotate(60.0f, 1.0f, 0.0f, 0.0f);

   // Sphere
   TransformNode* sphereTransform = new TransformNode;
   sphereTransform->Translate(80.0f, 20.0f, 10.0f);
   sphereTransform->Scale(10.0f, 10.0f, 10.0f);

   // --------------------------- Camera ----------------------- //
   MyCamera = new CameraNode;
   MyCamera->SetPosition(Point3(0.0f, -100.0f, 20.0f));
   MyCamera->SetLookAtPt(Point3(0.0f, 0.0f, 20.0f));
   MyCamera->SetViewUp(Vector3(0.0, 0.0, 1.0));
   MyCamera->SetPerspective(50.0, 1.0, 1.0, 300.0);

   // -------------------- Lighting --------------------------/

   // Set the global light ambient
   Color4 globalAmbient(0.4f, 0.4f, 0.4f, 1.0f);
   lightingShader->SetGlobalAmbient(globalAmbient);

   // Light 0 - point light source in back right corner
	LightNode* light0 = new LightNode(0);
	light0->SetDiffuse(Color4(0.5f, 0.5f, 0.5f, 1.0f));
	light0->SetSpecular(Color4(0.5f, 0.5f, 0.5f, 1.0f));
   light0->SetPosition(HPoint3(90.0f, 90.0f, 30.f, 1.0f));	
	light0->Enable();

   // Light1 - directional light from the ceiling
   LightNode* light1 = new LightNode(1);
	light1->SetDiffuse(Color4(0.7f, 0.7f, 0.7f, 1.0f ));
	light1->SetSpecular(Color4(0.7f, 0.7f, 0.7f, 1.0f));
   light1->SetPosition(HPoint3(0.0f, 0.0f, 1.0f, 0.0f));	
	light1->Enable();

   // Spotlight - reddish spotlight - we will place at the camera location
   // shining along -VPN
   Spotlight = new LightNode(2);
	Spotlight->SetDiffuse(Color4(0.5f, 0.1f, 0.1f, 1.0f ));
	Spotlight->SetSpecular(Color4(0.5f, 0.1f, 0.1f, 1.0f));
   Point3 pos = MyCamera->GetPosition();
   Spotlight->SetPosition(HPoint3(pos.x, pos.y, pos.z, 1.0f));
   Vector3 dir = MyCamera->GetViewPlaneNormal() * -1.0f;
   Spotlight->SetSpotlight(dir, 32.0f, 30.0f);
	Spotlight->Enable();

   // --------------------- Scene construction ----------------- //

   // Construct the scene root node
   SceneRoot = new SceneNode;
   SceneRoot->AddChild(lightingShader);
   lightingShader->AddChild(MyCamera);

   // Add the lights as the children of the camera
	MyCamera->AddChild(light0);
   light0->AddChild(light1);
   light1->AddChild(Spotlight);

   // Create a scene node to hold all scene objects (other than camera
   // and lights)
   SceneNode* myScene = new SceneNode;
   
   // Add the scene under the last light
   Spotlight->AddChild(myScene);

   // Construct the room (walls, floor, ceiling)
   ConstructRoom(myScene, unitSquare);

   // Construct the table
   SceneNode* table = ConstructTable(box, cylinder); 
   myScene->AddChild(wood);
   wood->AddChild(tableTransform);
   tableTransform->AddChild(table);

   // Place a teapot on the table
   tableTransform->AddChild(teapotTransform);
   teapotTransform->AddChild(silver);
   silver->AddChild(teapot);

   // Place a torus
   myScene->AddChild(shinyBlack);
   shinyBlack->AddChild(torusTransform);
   torusTransform->AddChild(torus);

    // Place a sphere
   myScene->AddChild(shinyBlue);
   shinyBlue->AddChild(sphereTransform);
   sphereTransform->AddChild(sphere);
}