gridrenderer.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
00030 #include "video/renderbackend.h"
00031 #include "util/math/fife_math.h"
00032 #include "util/log/logger.h"
00033 #include "model/metamodel/grids/cellgrid.h"
00034 #include "model/structures/instance.h"
00035 #include "model/structures/layer.h"
00036 #include "model/structures/location.h"
00037 
00038 #include "view/camera.h"
00039 #include "gridrenderer.h"
00040 
00041 
00042 namespace FIFE {
00043     static Logger _log(LM_VIEWVIEW);
00044 
00045     GridRenderer::GridRenderer(RenderBackend* renderbackend, int position):
00046         RendererBase(renderbackend, position) {
00047         setEnabled(false);
00048     }
00049 
00050     GridRenderer::GridRenderer(const GridRenderer& old):
00051         RendererBase(old) {
00052         setEnabled(false);
00053     }
00054 
00055     RendererBase* GridRenderer::clone() {
00056         return new GridRenderer(*this);
00057     }
00058 
00059     GridRenderer::~GridRenderer() {
00060     }
00061 
00062     void GridRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) {
00063         CellGrid* cg = layer->getCellGrid();
00064         if (!cg) {
00065             FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
00066             return;
00067         }
00068 //
00069 //
00070 //      //render elev_coord box
00071 //      //draw front quad
00072 //      // 1,1,1
00073 //      //1,-1,1
00074 //      //-1,-1,1
00075 //      //-1,1,1
00076         Point a,b,c,d;
00077 
00078 
00079         ScreenPoint copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
00080         ScreenPoint copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
00081         Point coptt1(copt1.x,copt1.y);
00082         Point coptt2(copt2.x,copt2.y);
00083         m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
00084         a = coptt1;
00085 
00086         copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
00087         copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
00088         coptt1 = Point(copt1.x,copt1.y);
00089         coptt2 = Point(copt2.x,copt2.y);
00090         m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
00091         b = coptt1;
00092 
00093         copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
00094         copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
00095         coptt1 = Point(copt1.x,copt1.y);
00096         coptt2 = Point(copt2.x,copt2.y);
00097         m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
00098         c = coptt1;
00099 
00100         copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
00101         copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
00102         coptt1 = Point(copt1.x,copt1.y);
00103         coptt2 = Point(copt2.x,copt2.y);
00104         m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 20);
00105         d = coptt1;
00106 
00107         m_renderbackend->drawQuad(a,b,c,d,15, 15, 200);
00108 //
00109 //
00110 //      //draw back quad
00111         copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
00112         copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
00113         coptt1 = Point(copt1.x,copt1.y);
00114         coptt2 = Point(copt2.x,copt2.y);
00115         m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
00116 
00117         copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
00118         copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
00119         coptt1 = Point(copt1.x,copt1.y);
00120         coptt2 = Point(copt2.x,copt2.y);
00121         m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
00122 
00123         copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
00124         copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
00125         coptt1 = Point(copt1.x,copt1.y);
00126         coptt2 = Point(copt2.x,copt2.y);
00127         m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
00128 
00129         copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
00130         copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
00131         coptt1 = Point(copt1.x,copt1.y);
00132         coptt2 = Point(copt2.x,copt2.y);
00133         m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
00134 
00135 
00136         Rect cv = cam->getViewPort();
00137         std::vector<Instance*>::const_iterator instance_it = instances.begin();
00138         for (;instance_it != instances.end(); ++instance_it) {
00139             Instance* instance = *instance_it;
00140             std::vector<ExactModelCoordinate> vertices;
00141             cg->getVertices(vertices, instance->getLocationRef().getLayerCoordinates());
00142             std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
00143             ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00144             Point pt1(firstpt.x, firstpt.y);
00145             Point pt2;
00146             ++it;
00147             for (; it != vertices.end(); it++) {
00148                 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00149                 pt2.x = pts.x; pt2.y = pts.y;
00150                 Point cpt1 = pt1;
00151                 Point cpt2 = pt2;
00152                 /* FIXME: limit grid drawing to current camera view port
00153                    code below does not do it, but may act as a starting point
00154 
00155                 int cvx2 = cv.x+cv.w;
00156                 int cvy2 = cv.y+cv.h;
00157 
00158                 if (((pt1.x < cv.x) && (pt2.x < cv.x)) ||
00159                     ((pt1.x > cvx2) && (pt2.x > cvx2)) ||
00160                     ((pt1.y < cv.y) && (pt2.y < cv.y)) ||
00161                     ((pt1.y > cvy2) && (pt2.y > cvy2))) {
00162                     pt1 = pt2;
00163                     continue;
00164                 }
00165 
00166                 if (cpt1.x < cv.x) cpt1.x = cv.x;
00167                 if (cpt2.x < cv.x) cpt2.x = cv.x;
00168                 if (cpt1.y < cv.y) cpt1.y = cv.y;
00169                 if (cpt2.y < cv.y) cpt2.y = cv.y;
00170                 if (cpt1.x > cvx2) cpt1.x = cvx2;
00171                 if (cpt2.x > cvx2) cpt2.x = cvx2;
00172                 if (cpt1.y > cvy2) cpt1.y = cvy2;
00173                 if (cpt2.y > cvy2) cpt2.y = cvy2;
00174                 */
00175                 m_renderbackend->drawLine(cpt1, cpt2, 0, 255, 0);
00176                 pt1 = pt2;
00177             }
00178             m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), 0, 255, 0);
00179         }
00180     }
00181 }