Пример #1
0
double Rectangle::intersect(Vector rayVec, Camera camera) const {
  Position vecPos = camera.pos;
  this->applyTransformations(vecPos, rayVec);
  Vector normal(-this->width, 0, this->height);
  Vector objPos(pos.x, pos.y, pos.z);
  Vector camPos(vecPos.x, vecPos.y, vecPos.z);
  Vector B(this->width, 0, this->height);
  double r, d, n;

  d = normal.dot(rayVec);
  n = normal.dot(objPos) - normal.dot(camPos);
  if (d == 0)
    return -1;
  r = n / d;
  if (r < 0)
    return -1;
  return r;
  Position inter;
  inter.x = vecPos.x + r * rayVec.x;
  inter.y = vecPos.y + r * rayVec.y;
  inter.z = vecPos.z + r * rayVec.z;
  Vector interVec(pos.x - inter.x, pos.y - inter.y, pos.z - inter.z);
  interVec.makeUnit();
  B.makeUnit();
  double p = interVec.dot(B);
  // std::cout << p << "\n";
  if (p >= -0.000001 && p <= 0.000001)
    return r;
  return -1;
}
// ######################################################################
// Prompt the user to return the object back to the tray place an object
void promptReturnObjectToTray(SceneSetup const & setup, int objIdx)
{
  LINFO("Placing back object %d into its tray. Follow instructions in User Interactive window...", objIdx);
  const SceneSetup::Object &obj = setup.objects[objIdx];

  Image< PixRGB<byte> > trayImg = Raster::ReadRGB(setup.setupPath + "/" + obj.trayFileName);
  trayImg = rescale(trayImg, trayImg.getDims()/2);

  Point2D<int> objPos(obj.trayX*trayImg.getWidth(), obj.trayY*trayImg.getHeight());

  drawCircle(trayImg, objPos, trayImg.getWidth()/12, PixRGB<byte>(255, 0, 0), 2);
  drawCross(trayImg, objPos, PixRGB<byte>(255, 0, 0), trayImg.getWidth()/8, 2);

  std::ostringstream ss;
  ss << "Place back Obj(" <<
    "row " << obj.trayRow << 
    ", col " << obj.trayColumn << 
    ") into tray " << obj.trayName << 
    " and ENTER";
  writeText(trayImg, Point2D<int>(0, 0), ss.str().c_str());
  
  userInteractiveWindow->drawImage(trayImg, 0, 0, true);

  // eat all previous mouse clicks and key presses, just in case:
  while (userInteractiveWindow->getLastMouseClick() != Point2D<int>(-1, -1)) { }
  while (userInteractiveWindow->getLastKeyPress() != -1) { }

  // wait for ENTER:
  while (userInteractiveWindow->getLastKeyPress() != 36) usleep(100000);

  LINFO("Done placing back object %d into its tray.", objIdx);
}
Пример #3
0
void LocalMap::generate(weak_ptr<AStarSearch> pf, weak_ptr<EventService> ev) {
	_events = ev;
	_pFinder = pf;
	_actor = make_shared<Actor>(_res->getObjectID("actor"), Point(54, 54), 24);
	srand((unsigned)time(NULL));
	_rowmax = TD_MAP_ROWS;
	_colmax = TD_MAP_COLS;
	_xmax = _colmax * TILE_MASK;
	for (unsigned row = 0; row < _rowmax; row++){
		for (unsigned col = 0; col < _colmax; col++){
			_tiles.push_back(MapTile(8 + (rand() % 6)));
		}
	}

	unsigned pRow = 0, pCol = 0;
	pRow += rand() % 7;
	while (pRow < _rowmax){
		pCol += rand() % 15;
		while (pCol < _colmax){
			_tiles[pRow * _colmax + pCol].setType(47);
			pCol += rand() % 15;
		}
		pRow += rand() % 7;
		pCol = 0;
	}

	// tree generation (sprite 79)
	int objMaxDensity = 10; // 10% max
	int randOffset = 0;
	//_objects.reserve(_rowmax * _colmax / 100);

	_objects.setObject(make_shared<MapObject>(_res->getObjectID("hut"), Point(150, 150)));
	_objects.setObject(make_shared<MapObject>(_res->getObjectID("hide"), Point(80, 90)));
	_objects.setObject(make_shared<SmartActor>(_res->getObjectID("actor"), Point(110, 110), 24, _events));
	int reetID = _res->getObjectID("reet");
	for (unsigned row = 0; row < _rowmax; row += 10){
		for (unsigned col = 0; col < _colmax; col += 10){
			for (int k = 0; k < objMaxDensity; k++){
				randOffset = rand() % 100;
				Point objPos((col + (randOffset % 10)) * TILE_MASK, (row + (randOffset / 10)) * TILE_MASK);
				
				cout << "OBJ: " << col + (randOffset % 10) << "," << row + (randOffset / 10);
				cout << "  >> " << objPos._x << "," << objPos._y << " Prio: " << objPos.toRenderPriority() << endl;
				
				_objects.setObject(make_shared<MapObject>(reetID, objPos));
			}
		}
	}
	_ready = true;
}
OCRenderTreeNode *
OcclusionCullingTreeBuilder::createNode(RenderActionBase    *pAction,
                                        RenderPartitionBase *pPart,
                                        DrawFunctor         &drawFunc,
                                        State               *pState,
                                        StateOverride       *pStateOverride)
{
    RenderPartition  *pRPart = dynamic_cast<RenderPartition *>(pPart);
    OCRenderTreeNode *pNode  = _pNodePool->create<OCRenderTreeNode>(_uiNodePoolIdx);

    Node            *actNode = _ract->getActNode();
    const BoxVolume &objVol  = actNode->getVolume();

#ifndef OSG_ENABLE_DOUBLE_MATRIX_STACK
    Pnt3f objPos(TypeTraits<Real32>::getMax(),
                 TypeTraits<Real32>::getMax(),
                 TypeTraits<Real32>::getMax() );
    Pnt3f volVert[8];
#else
    Pnt3d objPos(TypeTraits<Real64>::getMax(),
                 TypeTraits<Real64>::getMax(),
                 TypeTraits<Real64>::getMax() );
    Pnt3f volVert[8];
#endif

    Pnt3f volMin;
    Pnt3f volMax;

    objVol.getBounds(volMin, volMax);

    volVert[0].setValues(volMin[0], volMin[1], volMin[2]);
    volVert[1].setValues(volMax[0], volMin[1], volMin[2]);
    volVert[2].setValues(volMin[0], volMax[1], volMin[2]);
    volVert[3].setValues(volMin[0], volMin[1], volMax[2]);
    volVert[4].setValues(volMax[0], volMax[1], volMin[2]);
    volVert[5].setValues(volMax[0], volMin[1], volMax[2]);
    volVert[6].setValues(volMin[0], volMax[1], volMax[2]);
    volVert[7].setValues(volMax[0], volMax[1], volMax[2]);

    for(UInt32 i = 0; i < 8; ++i)
    {
        pRPart->getModelViewMatrix().mult(volVert[i], volVert[i]);

        if(volVert[i][2] < objPos[2])
            objPos = volVert[i];
    }

    pNode->setScalar( (-objPos[2]       - pRPart->getNear()) /
                      (pRPart->getFar() - pRPart->getNear())  );

    pNode->setFunctor      (drawFunc               );
    pNode->setState        (pState                 );
    pNode->setStateOverride(pStateOverride         );
    pNode->setLightState   (pRPart->getLightState());
    pNode->setNode         (_ract->getActNode()    );
    pNode->setVolume       (objVol                 );

#ifndef OSG_ENABLE_DOUBLE_MATRIX_STACK
    pNode->setMatrixStore  (pRPart->getMatrixStackTop());
#else
    Matrix4f tmpMat;
    tmpMat.convertFrom(pRPart->getModelViewMatrix());
    MatrixStore tmpMS (pRPart->getMatrixStackTop().first, tmpMat);

    pNode->setMatrixStore(tmpMS);
#endif

    return pNode;
}
// ######################################################################
// Prompt the user to place an object from a tray onto the table, and 
// then present them with an interface to outline that object
std::vector< Point2D<int> > promptPlaceObjectOnScene(SceneSetup const & setup, int objIdx)
{
  LINFO("Place object %d onto table. Follow instructions in User Interactive window...", objIdx);

  const SceneSetup::Object &obj = setup.objects[objIdx];

  Image< PixRGB<byte> > trayImg = Raster::ReadRGB(setup.setupPath + "/" + obj.trayFileName);
  trayImg = rescale(trayImg, trayImg.getDims()/2);

  Point2D<int> objPos(obj.trayX*trayImg.getWidth(), obj.trayY*trayImg.getHeight());

  drawCircle(trayImg, objPos, trayImg.getWidth()/12, PixRGB<byte>(255, 0, 0), 2);
  drawCross(trayImg, objPos, PixRGB<byte>(255, 0, 0), trayImg.getWidth()/8, 2);

  std::ostringstream ss;
  ss << "Place Tray " << obj.trayName << 
    " (row " << obj.trayRow << 
    ", col " << obj.trayColumn << 
    ") on table and ENTER";
  writeText(trayImg, Point2D<int>(0, 0), ss.str().c_str());

  userInteractiveWindow->drawImage(trayImg, 0, 0, true);
  while(userInteractiveWindow->getLastKeyPress() != 36) usleep(100000);
  
  pthread_mutex_lock(&imgMutex);
  Image<PixRGB<byte> > cameraImg = inputImage;
  pthread_mutex_unlock(&imgMutex);
  userInteractiveWindow->drawImage(cameraImg, 0, 0, true);
  std::vector< Point2D<int> > poly;
  std::string msg = "Outline new object using 4 points. ESCAPE to undo point. SPACE to refresh image. ENTER when finished.";

  // eat all previous mouse clicks and key presses, just in case:
  while (userInteractiveWindow->getLastMouseClick() != Point2D<int>(-1, -1)) { }
  while (userInteractiveWindow->getLastKeyPress() != -1) { }

  bool finished = false;
  while(!finished) {
    Point2D<int> mouseClick = userInteractiveWindow->getLastMouseClick();
    if(mouseClick != Point2D<int>(-1, -1) && poly.size() < 4)
      poly.push_back(mouseClick);

    int lastKeyPress = userInteractiveWindow->getLastKeyPress();
    switch(lastKeyPress) {
    case -1: // No Key
      break;
    case 9:  // ESCAPE
      if(poly.size()) poly.erase(poly.end()-1);
      break;
    case 36: // ENTER
      if(poly.size() == 4) finished = true;
      break;
    case 65: // SPACE
      pthread_mutex_lock(&imgMutex);
      cameraImg = inputImage;
      pthread_mutex_unlock(&imgMutex);
      userInteractiveWindow->drawImage(cameraImg, 0, 0, true);
      break;
    default:
      LINFO("Key Pressed: %d", lastKeyPress);
      break;
    }

    Image< PixRGB<byte> > dispImage = cameraImg;
    for(size_t i=0; i<poly.size(); ++i) drawCircle(dispImage, poly[i], 5, PixRGB<byte>(255, 0, 0), 3);
    drawOutlinedPolygon(dispImage, poly, PixRGB<byte>(0, 0, 255), Point2D<int>(0,0),0,1,0,0,3);
    writeText(dispImage, Point2D<int>(0,0), msg.c_str());
    userInteractiveWindow->drawImage(dispImage, 0, 0, true);

    usleep(100000);
  }

  LINFO("Done placing object %d onto table.", objIdx);

  return poly;
}