Esempio n. 1
0
void CityOnPlanet::PutCityBit(MTRand &rand, const matrix4x4d &rot, vector3d p1, vector3d p2, vector3d p3, vector3d p4)
{
	double rad = (p1-p2).Length()*0.5;
	LmrModel *model(0);
	double modelRadXZ(0);
	const LmrCollMesh *cmesh(0);
	vector3d cent = (p1+p2+p3+p4)*0.25;

	cityflavourdef_t *flavour(0);
	citybuildinglist_t *buildings(0);

	// pick a building flavour (city, windfarm, etc)
	for (int flv=0; flv<CITYFLAVOURS; flv++) {
		flavour = &cityflavour[flv];
		buildings = &s_buildingLists[flavour->buildingListIdx];
       
		int tries;
		for (tries=20; tries--; ) {
			const citybuilding_t &bt = buildings->buildings[rand.Int32(buildings->numBuildings)];
			model = bt.resolvedModel;
			modelRadXZ = bt.xzradius;
			cmesh = bt.collMesh;
			if (modelRadXZ < rad) break;
			if (tries == 0) return;
		}
		
		bool tooDistant = ((flavour->center - cent).Length()*(1.0/flavour->size) > rand.Double());
		if (!tooDistant) break;
		else flavour = 0;
	}

	if (flavour == 0) {
		if (rad > MIN_SEG_SIZE) goto always_divide;
		else return;
	}

	if (rad > modelRadXZ*2.0) {
always_divide:
		vector3d a = (p1+p2)*0.5;
		vector3d b = (p2+p3)*0.5;
		vector3d c = (p3+p4)*0.5;
		vector3d d = (p4+p1)*0.5;
		vector3d e = (p1+p2+p3+p4)*0.25;
		PutCityBit(rand, rot, p1, a, e, d);
		PutCityBit(rand, rot, a, p2, b, e);
		PutCityBit(rand, rot, e, b, p3, c);
		PutCityBit(rand, rot, d, e, c, p4);
	} else {
		cent = cent.Normalized();
		double height = m_planet->GetTerrainHeight(cent);
		/* don't position below sealevel! */
		if (height - m_planet->GetSBody()->GetRadius() <= 0.0) return;
		cent = cent * height;

		assert(cmesh);
		Geom *geom = new Geom(cmesh->geomTree);
		int rotTimes90 = rand.Int32(4);
		matrix4x4d grot = rot * matrix4x4d::RotateYMatrix(M_PI*0.5*double(rotTimes90));
		geom->MoveTo(grot, cent);
		geom->SetUserData(this);
//		f->AddStaticGeom(geom);

		BuildingDef def = { model, cmesh->GetBoundingRadius(), rotTimes90, cent, geom, false };
		m_buildings.push_back(def);
	}
}
void display()
{   glClearColor(0.6, 0.75, 0.85, 1); // sky color is light blue
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	/* Clear The Screen And The Depth Buffer */
	if(view==0)
	{   
		display1();
	}

  else if (view==1)
	{
	display2();
   }
   else if(view==2)
   {
   //  Erase the window and the depth buffer
   
   glClearColor(0.6, 0.75, 0.85, 1); // sky color is light blue
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    //  Enable Z-buffering in OpenGL
   glEnable(GL_DEPTH_TEST);
   glCullFace(GL_BACK);
  
   glLoadIdentity();
    
   // Set the look at position for perspective projection
      		gluLookAt(
			Ex,      Ey,      Ez,
			Ex + lx, Ey, Ez+lz,
			0.0,    1,    0.0);
   
    
  glShadeModel(GL_SMOOTH);
  glDisable(GL_DEPTH_TEST);
  glDepthMask(GL_FALSE);
  glPushMatrix();
  glTranslatef(	Ex + lx, 0, Ez+lz-4);
  Sky(1000); 
  glPopMatrix();
  glDepthMask(GL_TRUE);
  glEnable(GL_DEPTH_TEST);
  
    //  Light switch
   if (light)
   {
        //  Translate intensity to color vectors
        float Ambient[]   = {0.01*ambient ,0.01*ambient ,0.01*ambient ,1.0};
        float Diffuse[]   = {0.01*diffuse ,0.01*diffuse ,0.01*diffuse ,1.0};
        float Specular[]  = {0.01*specular,0.01*specular,0.01*specular,1.0};
        //  Light position
      float Position[]={0,1000,5000};
        //  Draw light position as ball (still no lighting here)
        glColor3f(1,1,1);
         ball(Position[0],Position[1],Position[2] , 0.2);
        //  OpenGL should normalize normal vectors
        glEnable(GL_NORMALIZE);
        //  Enable lighting
        glEnable(GL_LIGHTING);
        //  glColor sets ambient and diffuse color materials
        glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
        glEnable(GL_COLOR_MATERIAL);
        //  Enable light 0
        glEnable(GL_LIGHT0);
        //  Set ambient, diffuse, specular components and position of light 0
        glLightfv(GL_LIGHT0,GL_AMBIENT ,Ambient);
        glLightfv(GL_LIGHT0,GL_DIFFUSE ,Diffuse);
        glLightfv(GL_LIGHT0,GL_SPECULAR,Specular);
        glLightfv(GL_LIGHT0,GL_POSITION,Position);
        
   }
   else
     glDisable(GL_LIGHTING);
   
	if(opt==1)
	{
		 glPushMatrix();
         glTranslatef(	Ex + lx,0, Ez+lz-4);
         glRotatef(90, 1, 0, 0);
         glRotatef(180, 0, 1, 0);
         glColor3f(1,1,1);
		 glCallList(car1);
		 glPopMatrix();
		  int E=Ez+lz-4;
		 if(E<=40)
		 {
		 	Ex=0;
		 	Ez=1000;
		 	lx=0;
		 	lz=-1;
		 }
		
	}
	else if(opt==2)
	{
		 glPushMatrix();
         glTranslatef(	Ex + lx, 0, Ez+lz-4);
         glRotatef(90, 1, 0, 0);
         glRotatef(180, 0, 1, 0);
         glColor3f(1,1,1);
		 glCallList(car2);
		 glPopMatrix();
		   int E=Ez+lz-4;
		 if(E<=40)
		 {
		 	Ex=0;
		 	Ez=1000;
		 	lx=0;
		 	lz=-1;
		 }
	}
	cylinder();
    Road();
    
     
	for(i=0; i<=1000; i+=40)
	{
	glPushMatrix();
    glTranslated(-50,10,i);
    glScaled(50,50,50);
    Tree();
    glPopMatrix();
  }
    for(i=0; i<=1000; i+=40)
	{
	glPushMatrix();
    glTranslated(170,10,i);
    glScaled(50,50,50);
    Tree();
    glPopMatrix();
  }
    for(i=0; i<=1000; i+=60)
	{
	glPushMatrix();
    glTranslated(-70,-10,i);
    glScaled(2,10,10);
    Lamp();
    glPopMatrix();
  }
  for(i=0; i<=1000; i+=60)
	{
	glPushMatrix();
    glTranslated(70,-10,i);
    glRotated(90,0,1,0);
    glScaled(2,10,10);
    Lamp();
    glPopMatrix();
  }
  for(i=-60; i<=1000; i+=60)
	{
	glPushMatrix();
    glTranslated(-38,86.5,i+43);
    glScaled(5,5,5);
    lights();
    glPopMatrix();
  }
   for(i=-60; i<=1000; i+=60)
	{
	glPushMatrix();
    glTranslated(55,87,i+30);
    glScaled(5,5,5);
    lights();
    glPopMatrix();
  }
   for(i=0; i<=1000; i+=400)
	{
	glPushMatrix();
    glTranslated(-50,-10,i);
    glScaled(10,10,10);
    trafficsignal();
    glPopMatrix();
    }
     for(i=0; i<=1000; i+=400)
	{
	glPushMatrix();
    glTranslated(0,0,i+40);
    zebracrossing();
    glPopMatrix();
    }
     for(i=-60; i<=1000; i+=100)
	{
	glPushMatrix();
    glTranslated(200,-15,i+30);
    glScaled(10,15,10);
    buildings();
    glPopMatrix();
  }
   for(i=-60; i<=1000; i+=100)
	{
	glPushMatrix();
    glTranslated(-250,-15,i+30);
    glScaled(10,15,10);
    buildings();
    glPopMatrix();
  }
   for(i=-60; i<=1000; i+=100)
	{
	glPushMatrix();
    glTranslated(350,-15,i+30);
    glScaled(10,30,10);
    buildings();
    glPopMatrix();
  }
   for(i=-60; i<=1000; i+=100)
	{
	glPushMatrix();
    glTranslated(-400,-15,i+30);
    glScaled(10,30,10);
    buildings();
    glPopMatrix();
  }
     
   for(e=0; e<=1000; e+=155)
	{
	  if(e%2==0)
	{
		glPushMatrix();
		glTranslated(-16,-10,e);
		glScaled(6,6,6);
        roadpylon();
        glPopMatrix();
    }
    else
	{
		glPushMatrix();
		glTranslated(16,-10,e);
        glScaled(6,6,6);
        roadpylon();
        glPopMatrix();
    }
    } 
    for(i=0; i<=1000; i+=400)
	{
	glPushMatrix();
    glTranslated(65,0,i);
    glScaled(10,10,10);
    roadsign();
    glPopMatrix();
    }
  
  xpos = xpos + xvel;

if(xpos >= 30)
{
xvel = -xvel;
}
if( xpos <2)
{
xvel = -xvel;
}
  glPushMatrix();
  glTranslated(0,xpos,900);
  glRotated(-90,0,0,1);
  glRotated(-90,0,1,0);
  glScaled(2,2,2);
  glColor3f(1,1,1);
  glCallList(dragon);
  glPopMatrix();
   glWindowPos2i(5,5);
   glColor3f(1,1,1);
  Print("Speed=%f",1,jump);
 
}
   //  Render the scene and make it visible
   ErrCheck("display");
   glFlush();
   glutSwapBuffers();
}