示例#1
0
	template<typename T> bool RenderVisitor::visit(QuadNode<T,InstanceTree::MIN_TREE_SIZE>* node, int32_t d) {

		if (d==0)
			visited = 0;

		int32_t x = node->x();
		int32_t y = node->y();
		int32_t size = node->size();

		++visited;
		CellGrid *cg = m_layer->getCellGrid(); ///we have checked for null pointer in  quadtreerenderer::render().. no need to check again


		ExactModelCoordinate emc= cg->toMapCoordinates(ExactModelCoordinate( x,y) );//0.5 for each cell's half-width
		ScreenPoint scrpt1 =m_camera->toScreenCoordinates( emc );
		emc= cg->toMapCoordinates(ExactModelCoordinate( x,y+size) );// this size usage is wrong.. me thinks
		ScreenPoint scrpt2 =m_camera->toScreenCoordinates( emc );
		emc= cg->toMapCoordinates(ExactModelCoordinate( x+size,y) );
		ScreenPoint scrpt3 =m_camera->toScreenCoordinates( emc );
		emc= cg->toMapCoordinates(ExactModelCoordinate( x+size,y+size) );
		ScreenPoint scrpt4 =m_camera->toScreenCoordinates( emc );

		m_renderbackend->drawLine(Point(scrpt1.x,scrpt1.y), Point(scrpt2.x,scrpt2.y), 255, 255, 255);
		m_renderbackend->drawLine(Point(scrpt1.x,scrpt1.y), Point(scrpt3.x,scrpt3.y), 255, 255, 255);
		m_renderbackend->drawLine(Point(scrpt3.x,scrpt3.y), Point(scrpt4.x,scrpt4.y), 255, 255, 255);
		m_renderbackend->drawLine(Point(scrpt2.x,scrpt2.y), Point(scrpt4.x,scrpt4.y), 255, 255, 255);

		return true;
	}
示例#2
0
	void SquareGrid::getVertices(std::vector<ExactModelCoordinate>& vtx, const ModelCoordinate& cell) {
		vtx.clear();
		double x = static_cast<double>(cell.x);
		double y = static_cast<double>(cell.y);
		vtx.push_back(ExactModelCoordinate(x-0.5, y-0.5));
		vtx.push_back(ExactModelCoordinate(x+0.5, y-0.5));
		vtx.push_back(ExactModelCoordinate(x+0.5, y+0.5));
		vtx.push_back(ExactModelCoordinate(x-0.5, y+0.5));
	}
示例#3
0
	ExactModelCoordinate Object::getRotationAnchor() const {
		if (m_multiProperty) {
			return m_multiProperty->m_rotationAnchor;
		}
		if (m_inherited) {
			return m_inherited->getRotationAnchor();
		}
		return ExactModelCoordinate();
	}
示例#4
0
	ModelCoordinate HexGrid::toLayerCoordinatesHelper(const ExactModelCoordinate& coords) {
		// this helper method takes exact layer coordinates with zigzag removed
		// and converts them to layer coordinates
		ExactModelCoordinate elc = coords;

		// approximate conversion using squares instead of hexes
		if( static_cast<int32_t>(round(elc.y)) & 1 )
			elc.x -= 0.5;
		ExactModelCoordinate lc = ExactModelCoordinate(round(elc.x), round(elc.y), round(elc.z));

		int32_t x = static_cast<int32_t>(lc.x);
		int32_t y = static_cast<int32_t>(lc.y);
		int32_t z = static_cast<int32_t>(lc.z);

		// distance of given point from our approximation
		// If y uneven dx=-dx and dy=-dy
		double dx,dy;
		if (y & 1) {
			dx = elc.x - lc.x;
			dy = elc.y - lc.y;
		} else {
			dx = lc.x - elc.x;
			dy = lc.y - elc.y;
		}

		// adjustment for cases where our approximation lies beyond the hex edge
		if (ABS(dy) > ((HEX_TO_CORNER - HEX_EDGE_GRADIENT * ABS(dx)) * VERTICAL_MULTIP_INV)) {
			int8_t ddx, ddy;
			if (dx>0) ddx = -1;
			else ddx = 0;

			if (dy>0) ddy = -1;
			else ddy = 1;

			if (y & 1) {
				ddx = -ddx;
				ddy = -ddy;
			}

			x += ddx;
			y += ddy;
		}

		if (m_axial) {
			if (y >= 0)
				x -= y / 2;
			else
				x -= (y - 1) / 2;
		}

		return ModelCoordinate(x,y,z);
	}
示例#5
0
	ModelCoordinate HexGrid::toLayerCoordinates(const ExactModelCoordinate& map_coord) {
		FL_DBG(_log, LMsg("==============\nConverting map coords ") << map_coord << " to int32_t layer coords...");
		ExactModelCoordinate elc = m_inverse_matrix * map_coord;
		elc.y *= VERTICAL_MULTIP_INV;

		// approximate conversion using squares instead of hexes
		if( static_cast<int32_t>(round(elc.y)) & 1 )
			elc.x -= 0.5;
		ExactModelCoordinate lc = ExactModelCoordinate(round(elc.x), round(elc.y), round(elc.z));

		int32_t x = static_cast<int32_t>(lc.x);
		int32_t y = static_cast<int32_t>(lc.y);
		int32_t z = static_cast<int32_t>(lc.z);

		// distance of given point from our approximation
		// If y uneven dx=-dx and dy=-dy
		double dx,dy;
		if (y & 1) {
			dx = elc.x - lc.x;
			dy = elc.y - lc.y;
		} else {
			dx = lc.x - elc.x;
			dy = lc.y - elc.y;
		}

		// adjustment for cases where our approximation lies beyond the hex edge
		if (ABS(dy) > ((HEX_TO_CORNER - HEX_EDGE_GRADIENT * ABS(dx)) * VERTICAL_MULTIP_INV)) {
			int8_t ddx, ddy;
			if (dx>0) ddx = -1;
			else ddx = 0;

			if (dy>0) ddy = -1;
			else ddy = 1;

			if (y & 1) {
				ddx = -ddx;
				ddy = -ddy;
			}

			x += ddx;
			y += ddy;
		}

		return ModelCoordinate(x,y,z);
	}
示例#6
0
	ModelCoordinate HexGrid::toLayerCoordinates(const ExactModelCoordinate& map_coord) {
		FL_DBG(_log, LMsg("==============\nConverting map coords ") << map_coord << " to int layer coords...");
		ExactModelCoordinate elc = m_inverse_matrix * map_coord;
		elc.y *= VERTICAL_MULTIP_INV;
		ExactModelCoordinate lc = ExactModelCoordinate(floor(elc.x), floor(elc.y));
		double dx = elc.x - lc.x;
		double dy = elc.y - lc.y;
		int x = static_cast<int>(lc.x);
		int y = static_cast<int>(lc.y);
		FL_DBG(_log, LMsg("elc=") << elc << ", lc=" << lc);
		FL_DBG(_log, LMsg("x=") << x << ", y=" << y << ", dx=" << dx << ", dy=" << dy);
		ModelCoordinate result;

		if ((y % 2) == 0) {
			FL_DBG(_log, "In even row");
			if ((1 - dy) < HEX_EDGE_HALF) {
				FL_DBG(_log, "In lower rect area");
				result = ModelCoordinate(x, y+1);
			}
			else if (dy < HEX_EDGE_HALF) {
				FL_DBG(_log, "In upper rect area");
				if (dx > 0.5) {
					FL_DBG(_log, "...on right");
					result = ModelCoordinate(x+1, y);
				}
				else {
					FL_DBG(_log, "...on left");
					result = ModelCoordinate(x, y);
				}
			}
			// in middle triangle area
			else {
				FL_DBG(_log, "In middle triangle area");
				if (dx < 0.5) {
					FL_DBG(_log, "In left triangles");
					if (ptInTriangle(ExactModelCoordinate(dx, dy),
					                 ExactModelCoordinate(0, VERTICAL_MULTIP * HEX_EDGE_HALF),
					                 ExactModelCoordinate(0, VERTICAL_MULTIP * (1-HEX_EDGE_HALF)),
					                 ExactModelCoordinate(0.5, VERTICAL_MULTIP * HEX_EDGE_HALF)
					                 )) {
						FL_DBG(_log, "..upper part");
						result = ModelCoordinate(x, y);
					} else {
						FL_DBG(_log, "..lower part");
						result = ModelCoordinate(x, y+1);
					}
				} else {
					FL_DBG(_log, "In right triangles");
					if (ptInTriangle(ExactModelCoordinate(dx, dy),
					                 ExactModelCoordinate(1, VERTICAL_MULTIP * HEX_EDGE_HALF),
					                 ExactModelCoordinate(1, VERTICAL_MULTIP * (1-HEX_EDGE_HALF)),
					                 ExactModelCoordinate(0.5, VERTICAL_MULTIP * HEX_EDGE_HALF)
					                 )) {
						FL_DBG(_log, "..upper part");
						result = ModelCoordinate(x+1, y);
					} else {
						FL_DBG(_log, "..lower part");
						result = ModelCoordinate(x, y+1);
					}
				}
			}
		}
		else {
			FL_DBG(_log, "In uneven row");
			if (dy < HEX_EDGE_HALF) {
				FL_DBG(_log, "In upper rect area");
				result = ModelCoordinate(x, y);
			}
			else if ((1 - dy) < HEX_EDGE_HALF) {
				FL_DBG(_log, "In lower rect area");
				if (dx > 0.5) {
					FL_DBG(_log, "...on right");
					result = ModelCoordinate(x+1, y+1);
				}
				else {
					FL_DBG(_log, "...on left");
					result = ModelCoordinate(x, y+1);
				}
			}
			else {
				FL_DBG(_log, "In middle triangle area");
				if (dx < 0.5) {
					FL_DBG(_log, "In left triangles");
					if (ptInTriangle(ExactModelCoordinate(dx, dy),
					                 ExactModelCoordinate(0, VERTICAL_MULTIP * HEX_EDGE_HALF),
					                 ExactModelCoordinate(0, VERTICAL_MULTIP * (1-HEX_EDGE_HALF)),
					                 ExactModelCoordinate(0.5, VERTICAL_MULTIP * (1-HEX_EDGE_HALF))
					                 )) {
						FL_DBG(_log, "..lower part");
						result = ModelCoordinate(x, y+1);
					} else {
						FL_DBG(_log, "..upper part");
						result = ModelCoordinate(x, y);
					}
				} else {
					FL_DBG(_log, "In right triangles");
					if (ptInTriangle(ExactModelCoordinate(dx, dy),
					                 ExactModelCoordinate(1, VERTICAL_MULTIP * HEX_EDGE_HALF),
					                 ExactModelCoordinate(1, VERTICAL_MULTIP * (1-HEX_EDGE_HALF)),
					                 ExactModelCoordinate(0.5, VERTICAL_MULTIP * (1-HEX_EDGE_HALF))
					                 )) {
					        FL_DBG(_log, "..lower part");
						result = ModelCoordinate(x+1, y+1);
					} else {
						FL_DBG(_log, "..upper part");
						result = ModelCoordinate(x, y);
					}
				}
			}
		}
		FL_DBG(_log, LMsg("  result = ") << result);
		return result;
	}
	Location& Instance::getFacingLocationRef() {
		if (!m_facinglocation) {
			m_facinglocation = new Location(m_location);
			m_facinglocation->setExactLayerCoordinates(m_facinglocation->getExactLayerCoordinates() + ExactModelCoordinate(1.0, 0.0));
			//m_facinglocation->setLayerCoordinates(ModelCoordinate(1,0));
		}
		return *m_facinglocation;
	}
示例#8
0
文件: gridrenderer.cpp 项目: m64/PEG
	void GridRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) {
		CellGrid* cg = layer->getCellGrid();
		if (!cg) {
			FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
			return;
		}
//
//
//		//render elev_coord box
//		//draw front quad
//		// 1,1,1
//		//1,-1,1
//		//-1,-1,1
//		//-1,1,1
		Point a,b,c,d;


		ScreenPoint copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
		ScreenPoint copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
		Point coptt1(copt1.x,copt1.y);
		Point coptt2(copt2.x,copt2.y);
		m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
		a = coptt1;

		copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
		copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
		coptt1 = Point(copt1.x,copt1.y);
		coptt2 = Point(copt2.x,copt2.y);
		m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
		b = coptt1;

		copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
		copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
		coptt1 = Point(copt1.x,copt1.y);
		coptt2 = Point(copt2.x,copt2.y);
		m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
		c = coptt1;

		copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
		copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
		coptt1 = Point(copt1.x,copt1.y);
		coptt2 = Point(copt2.x,copt2.y);
		m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 20);
		d = coptt1;

		m_renderbackend->drawQuad(a,b,c,d,15, 15, 200);
//
//
//		//draw back quad
		copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
		copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
		coptt1 = Point(copt1.x,copt1.y);
		coptt2 = Point(copt2.x,copt2.y);
		m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);

		copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
		copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
		coptt1 = Point(copt1.x,copt1.y);
		coptt2 = Point(copt2.x,copt2.y);
		m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);

		copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
		copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
		coptt1 = Point(copt1.x,copt1.y);
		coptt2 = Point(copt2.x,copt2.y);
		m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);

		copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
		copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
		coptt1 = Point(copt1.x,copt1.y);
		coptt2 = Point(copt2.x,copt2.y);
		m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);


		Rect cv = cam->getViewPort();
		std::vector<Instance*>::const_iterator instance_it = instances.begin();
		for (;instance_it != instances.end(); ++instance_it) {
			Instance* instance = *instance_it;
			std::vector<ExactModelCoordinate> vertices;
			cg->getVertices(vertices, instance->getLocationRef().getLayerCoordinates());
			std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
			ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
			Point pt1(firstpt.x, firstpt.y);
			Point pt2;
			++it;
			for (; it != vertices.end(); it++) {
				ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
				pt2.x = pts.x; pt2.y = pts.y;
				Point cpt1 = pt1;
				Point cpt2 = pt2;
				/* FIXME: limit grid drawing to current camera view port
				   code below does not do it, but may act as a starting point

				int cvx2 = cv.x+cv.w;
				int cvy2 = cv.y+cv.h;

				if (((pt1.x < cv.x) && (pt2.x < cv.x)) ||
				    ((pt1.x > cvx2) && (pt2.x > cvx2)) ||
				    ((pt1.y < cv.y) && (pt2.y < cv.y)) ||
				    ((pt1.y > cvy2) && (pt2.y > cvy2))) {
				    pt1 = pt2;
				    continue;
				}

				if (cpt1.x < cv.x) cpt1.x = cv.x;
				if (cpt2.x < cv.x) cpt2.x = cv.x;
				if (cpt1.y < cv.y) cpt1.y = cv.y;
				if (cpt2.y < cv.y) cpt2.y = cv.y;
				if (cpt1.x > cvx2) cpt1.x = cvx2;
				if (cpt2.x > cvx2) cpt2.x = cvx2;
				if (cpt1.y > cvy2) cpt1.y = cvy2;
				if (cpt2.y > cvy2) cpt2.y = cvy2;
				*/
				m_renderbackend->drawLine(cpt1, cpt2, 0, 255, 0);
				pt1 = pt2;
			}
			m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), 0, 255, 0);
		}
	}