FIFE  2008.0
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         m_color.r = 0;
00049         m_color.g = 255;
00050         m_color.b = 0;
00051     }
00052 
00053     GridRenderer::GridRenderer(const GridRenderer& old):
00054         RendererBase(old),
00055         m_color(old.m_color) {
00056         setEnabled(false);
00057     }
00058 
00059     RendererBase* GridRenderer::clone() {
00060         return new GridRenderer(*this);
00061     }
00062 
00063     GridRenderer::~GridRenderer() {
00064     }
00065 
00066     GridRenderer* GridRenderer::getInstance(IRendererContainer* cnt) {
00067         return dynamic_cast<GridRenderer*>(cnt->getRenderer("GridRenderer"));
00068     }
00069 
00070     void GridRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
00071         CellGrid* cg = layer->getCellGrid();
00072         if (!cg) {
00073             FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
00074             return;
00075         }
00076         m_renderbackend->disableLighting();
00077 //
00078 //
00079 //      //render elev_coord box
00080 //      //draw front quad
00081 //      // 1,1,1
00082 //      //1,-1,1
00083 //      //-1,-1,1
00084 //      //-1,1,1
00085         //We don't need the elevation box atm
00086 //      Point a,b,c,d;
00087 //
00088 //
00089 //      ScreenPoint copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
00090 //      ScreenPoint copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
00091 //      Point coptt1(copt1.x,copt1.y);
00092 //      Point coptt2(copt2.x,copt2.y);
00093 //      m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
00094 //      a = coptt1;
00095 //
00096 //      copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
00097 //      copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
00098 //      coptt1 = Point(copt1.x,copt1.y);
00099 //      coptt2 = Point(copt2.x,copt2.y);
00100 //      m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
00101 //      b = coptt1;
00102 //
00103 //      copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
00104 //      copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
00105 //      coptt1 = Point(copt1.x,copt1.y);
00106 //      coptt2 = Point(copt2.x,copt2.y);
00107 //      m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
00108 //      c = coptt1;
00109 //
00110 //      copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
00111 //      copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
00112 //      coptt1 = Point(copt1.x,copt1.y);
00113 //      coptt2 = Point(copt2.x,copt2.y);
00114 //      m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 20);
00115 //      d = coptt1;
00116 //
00117 //      m_renderbackend->drawQuad(a,b,c,d,15, 15, 200);
00118 //
00119 //
00120 //      //draw back quad
00121 //      copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
00122 //      copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
00123 //      coptt1 = Point(copt1.x,copt1.y);
00124 //      coptt2 = Point(copt2.x,copt2.y);
00125 //      m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
00126 //
00127 //      copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
00128 //      copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
00129 //      coptt1 = Point(copt1.x,copt1.y);
00130 //      coptt2 = Point(copt2.x,copt2.y);
00131 //      m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
00132 //
00133 //      copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
00134 //      copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
00135 //      coptt1 = Point(copt1.x,copt1.y);
00136 //      coptt2 = Point(copt2.x,copt2.y);
00137 //      m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
00138 //
00139 //      copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
00140 //      copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
00141 //      coptt1 = Point(copt1.x,copt1.y);
00142 //      coptt2 = Point(copt2.x,copt2.y);
00143 //      m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
00144 
00145 
00146         Rect cv = cam->getViewPort();
00147         int cvx2 = cv.x+cv.w;
00148         int cvy2 = cv.y+cv.h;
00149         RenderList::const_iterator instance_it = instances.begin();
00150         for (;instance_it != instances.end(); ++instance_it) {
00151             Instance* instance = (*instance_it)->instance;
00152             std::vector<ExactModelCoordinate> vertices;
00153             cg->getVertices(vertices, instance->getLocationRef().getLayerCoordinates());
00154             std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
00155             ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00156             Point pt1(firstpt.x, firstpt.y);
00157             Point pt2;
00158             ++it;
00159             for (; it != vertices.end(); it++) {
00160                 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00161                 pt2.x = pts.x;
00162                 pt2.y = pts.y;
00163                 Point cpt1 = pt1;
00164                 Point cpt2 = pt2;
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, m_color.r, m_color.g, m_color.b);
00176                 pt1 = pt2;
00177             }
00178             if ((pt2.x >= cv.x) && (pt2.x <= cvx2) && (pt2.y >= cv.y) && (pt2.y <= cvy2)) {
00179                 if ((firstpt.x >= cv.x) && (firstpt.x <= cvx2) && (firstpt.y >= cv.y) && (firstpt.y <= cvy2)) {
00180                     m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
00181                 }
00182             }
00183         }
00184         m_renderbackend->enableLighting();
00185     }
00186 
00187     void GridRenderer::setColor(Uint8 r, Uint8 g, Uint8 b) {
00188         m_color.r = r;
00189         m_color.g = g;
00190         m_color.b = b;
00191     }
00192 }