/* * TopView.cpp * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "TopView.h" #include #include #include #include #include #include #include namespace ZoomEdit { namespace View { bool TopView::Edge::operator<(const Edge &e) const { if(v1->getX() < e.v1->getX()) return true; if(v1->getX() > e.v1->getX()) return false; if(v1->getY() < e.v1->getY()) return true; if(v1->getY() > e.v1->getY()) return false; if(v1->getZ() < e.v1->getZ()) return true; if(v1->getZ() > e.v1->getZ()) return false; if(v2->getX() < e.v2->getX()) return true; if(v2->getX() > e.v2->getX()) return false; if(v2->getY() < e.v2->getY()) return true; if(v2->getY() > e.v2->getY()) return false; if(v2->getZ() < e.v2->getZ()) return true; if(v2->getZ() > e.v2->getZ()) return false; return false; } void TopView::drawGrid() { float depth = 1.25f + 0.04f*zoomLevel; float depth2 = std::floor(depth); float step = std::pow(0.1f, depth2); float width = viewWidth/scale; float height = viewHeight/scale; float x1 = xCenter - width/2; float x2 = xCenter + width/2; float y1 = yCenter - height/2; float y2 = yCenter + height/2; glLineWidth(1.0f); glBegin(GL_LINES); for(int i = 0; 0.4f*(depth-depth2+i-1) < 0.5f; ++i) { float f = std::min(0.4f*(depth-depth2+i), 0.5f); glColor3f(f, f, f); for(f = x1 - std::fmod(x1, step); f <= x2; f+=step) { glVertex2f(f, y1); glVertex2f(f, y2); } for(f = y1 - std::fmod(y1, step); f <= y2; f+=step) { glVertex2f(x1, f); glVertex2f(x2, f); } step *= 10; } glEnd(); } void TopView::renderRoom(Data::Room *room) { const std::list &floor = room->getFloorTriangles(); std::multiset edges; glColor4f(0.0f, 0.7f, 1.0f, 0.3f); glBegin(GL_TRIANGLES); for(std::list::const_iterator t = floor.begin(); t != floor.end(); ++t) { for(int i = 0; i < 3; ++i) { const Data::Vertex &v = (*t)->getVertex(i); const Data::Vertex &v2 = (*t)->getVertex((i+1)%3); glVertex2f(v.getX(), v.getZ()); edges.insert(Edge(&v, &v2)); } } glEnd(); for(std::multiset::iterator next = edges.begin(); next != edges.end();) { std::multiset::iterator edge = next++; std::multiset::iterator edge2 = edges.find(Edge(edge->v2, edge->v1)); if(edge2 != edges.end()) { edges.erase(edge2); next = edge; ++next; edges.erase(edge); } } glColor4f(0.0f, 0.7f, 1.0f, 0.7f); glLineWidth(1.0f); glBegin(GL_LINES); for(std::multiset::iterator edge = edges.begin(); edge != edges.end(); ++edge) { glVertex2f(edge->v1->getX(), edge->v1->getZ()); glVertex2f(edge->v2->getX(), edge->v2->getZ()); } glEnd(); } void TopView::init() { glClearColor(0, 0, 0, 0); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glEnable(GL_LINE_SMOOTH); glEnable(GL_POINT_SMOOTH); } void TopView::resize(float width, float height) { if(width == viewWidth && height == viewHeight) return; viewWidth = width; viewHeight = height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(width != 0 && height != 0) glScalef(2.0f/width, -2.0f/height, 1); signalUpdate().emit(); } void TopView::zoom(int zoom, float x, float y) { const float oldScale = scale; zoomLevel = std::max(std::min(zoomLevel + zoom, 50), -100); scale = 100*std::pow(1.1f, zoomLevel); xCenter += x*(1/oldScale - 1/scale); yCenter += y*(1/oldScale - 1/scale); signalUpdate().emit(); } void TopView::move(float x, float y, unsigned int state) { if(!(state & GDK_BUTTON3_MASK)) return; xCenter += x/scale; yCenter += y/scale; signalUpdate().emit(); } void TopView::render() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glScalef(scale, scale, 1); glTranslatef(-xCenter, -yCenter, 0); drawGrid(); if(!level) return; const std::list &rooms = level->getRooms(); for(std::list::const_iterator room = rooms.begin(); room != rooms.end(); ++room) renderRoom(*room); } } }