2007-10-23 19:08:04 +00:00
|
|
|
#include "Renderer.h"
|
2007-12-14 02:47:03 +00:00
|
|
|
#include "PlayerStart.h"
|
2007-10-23 19:08:04 +00:00
|
|
|
#include <GL/gl.h>
|
|
|
|
|
|
|
|
|
|
|
|
void Renderer::drawGrid(const Rectangle &rect, float scale) {
|
|
|
|
float depth = log10f(scale)-0.75f;
|
|
|
|
float depth2 = floorf(depth);
|
|
|
|
float step = powf(0.1f, depth2);
|
|
|
|
float f;
|
|
|
|
int i;
|
|
|
|
float x1 = rect.getVertex1().getX(), y1 = rect.getVertex1().getY();
|
|
|
|
float x2 = rect.getVertex2().getX(), y2 = rect.getVertex2().getY();
|
|
|
|
|
|
|
|
|
|
|
|
glBegin(GL_LINES);
|
|
|
|
|
|
|
|
for(i = 0; 0.4f*(depth-depth2+i-1) < 0.5f; i++) {
|
|
|
|
f = fminf(0.4f*(depth-depth2+i), 0.5f);
|
|
|
|
glColor3f(f, f, f);
|
|
|
|
|
|
|
|
for(f = x1 - fmodf(x1, step) - step; f <= x2; f+=step) {
|
|
|
|
glVertex2f(f, y1);
|
|
|
|
glVertex2f(f, y2);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(f = y1 - fmodf(y1, step) - step; f <= y2; f+=step) {
|
|
|
|
glVertex2f(x1, f);
|
|
|
|
glVertex2f(x2, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
step *= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::fillPolygon(const Polygon &polygon) {
|
|
|
|
std::vector<Triangle> triangles;
|
|
|
|
|
|
|
|
polygon.triangulate(triangles);
|
|
|
|
|
|
|
|
glBegin(GL_TRIANGLES);
|
|
|
|
|
|
|
|
for(std::vector<Triangle>::iterator t = triangles.begin(); t != triangles.end(); t++) {
|
|
|
|
glVertex2f(t->getVertexA().getX(), t->getVertexA().getY());
|
|
|
|
glVertex2f(t->getVertexB().getX(), t->getVertexB().getY());
|
|
|
|
glVertex2f(t->getVertexC().getX(), t->getVertexC().getY());
|
|
|
|
}
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::drawPolygon(const Polygon &polygon, bool close) {
|
2007-12-14 02:47:03 +00:00
|
|
|
glBegin(close ? GL_LINE_LOOP : GL_LINE_STRIP);
|
2007-10-23 19:08:04 +00:00
|
|
|
|
|
|
|
for(Polygon::const_iterator vertex = polygon.begin(); vertex != polygon.end(); vertex++)
|
2007-12-14 02:47:03 +00:00
|
|
|
glVertex2f(vertex->getX(), vertex->getY());
|
2007-10-23 19:08:04 +00:00
|
|
|
|
2007-12-14 02:47:03 +00:00
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::drawCircle(const Vertex &m, float r, int n) {
|
|
|
|
glBegin(GL_LINE_LOOP);
|
|
|
|
|
|
|
|
for(int i = 0; i < n; i++)
|
|
|
|
glVertex2f(m.getX()+r*cosf(2*M_PI*i/n), m.getY()+r*sinf(2*M_PI*i/n));
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
2008-02-09 12:21:00 +00:00
|
|
|
void Renderer::fillCircle(const Vertex &m, float r, int n) {
|
|
|
|
glBegin(GL_POLYGON);
|
|
|
|
|
|
|
|
for(int i = 0; i < n; i++)
|
|
|
|
glVertex2f(m.getX()+r*cosf(2*M_PI*i/n), m.getY()+r*sinf(2*M_PI*i/n));
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
2007-12-14 02:47:03 +00:00
|
|
|
void Renderer::drawCross(const Vertex &m, float r) {
|
|
|
|
glBegin(GL_LINES);
|
|
|
|
|
|
|
|
glVertex2f(m.getX()-r*M_SQRT1_2, m.getY()-r*M_SQRT1_2);
|
|
|
|
glVertex2f(m.getX()+r*M_SQRT1_2, m.getY()+r*M_SQRT1_2);
|
|
|
|
|
|
|
|
glVertex2f(m.getX()+r*M_SQRT1_2, m.getY()-r*M_SQRT1_2);
|
|
|
|
glVertex2f(m.getX()-r*M_SQRT1_2, m.getY()+r*M_SQRT1_2);
|
2007-10-23 19:08:04 +00:00
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
2008-01-16 16:11:00 +00:00
|
|
|
void Renderer::renderObject(const LevelObject &object, bool selected, bool highlighted, float scale) {
|
2007-12-14 02:47:03 +00:00
|
|
|
if(object.isOfType("Room"))
|
2008-01-16 16:11:00 +00:00
|
|
|
renderRoom(*(Room*)&object, selected, highlighted, scale);
|
2007-12-14 02:47:03 +00:00
|
|
|
else if(object.isOfType("PlayerStart"))
|
2008-01-16 16:11:00 +00:00
|
|
|
renderPlayerStart(*(PlayerStart*)&object, selected, highlighted, scale);
|
2008-02-08 21:21:01 +00:00
|
|
|
else if(object.isOfType("Portal"))
|
|
|
|
renderPortal(*(Portal*)&object, selected, highlighted, scale);
|
2007-12-14 02:47:03 +00:00
|
|
|
}
|
|
|
|
|
2008-02-09 12:21:00 +00:00
|
|
|
void Renderer::renderVertex(const Vertex &vertex, bool selected, bool highlighted, float scale) {
|
|
|
|
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
fillCircle(vertex, 3/scale, 16);
|
|
|
|
|
|
|
|
glLineWidth(1.0f);
|
|
|
|
glColor4f(9.0f, 0.7f, 0.0f, 0.9f);
|
|
|
|
drawCircle(vertex, 3/scale, 16);
|
|
|
|
}
|
|
|
|
|
2008-01-16 16:11:00 +00:00
|
|
|
void Renderer::renderRoom(const Room &room, bool selected, bool highlighted, float scale) {
|
2007-12-14 02:47:03 +00:00
|
|
|
if(selected)
|
|
|
|
glColor4f(0.0f, 0.7f, 1.0f, 0.2f);
|
|
|
|
else
|
|
|
|
glColor4f(0.0f, 0.7f, 1.0f, 0.3f);
|
|
|
|
|
|
|
|
fillPolygon(room);
|
|
|
|
|
|
|
|
if(selected) {
|
|
|
|
glColor4f(1.0f, 1.0f, 1.0f, 0.9f);
|
|
|
|
glLineWidth(2.0f);
|
|
|
|
}
|
2008-01-16 16:11:00 +00:00
|
|
|
else if(highlighted) {
|
2007-12-14 02:47:03 +00:00
|
|
|
glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
|
|
|
|
glLineWidth(2.0f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
|
|
|
|
glLineWidth(1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
drawPolygon(room);
|
2008-02-09 12:21:00 +00:00
|
|
|
|
|
|
|
if(selected || highlighted) {
|
|
|
|
for(Room::const_iterator v = room.begin(); v != room.end(); v++)
|
|
|
|
renderVertex(*v, false, false, scale);
|
|
|
|
}
|
2007-12-14 02:47:03 +00:00
|
|
|
}
|
|
|
|
|
2008-01-16 16:11:00 +00:00
|
|
|
void Renderer::renderPlayerStart(const PlayerStart &start, bool selected, bool highlighted, float scale) {
|
2007-12-14 22:03:00 +00:00
|
|
|
if(selected) {
|
|
|
|
glColor4f(1.0f, 1.0f, 1.0f, 0.9f);
|
|
|
|
glLineWidth(2.0f);
|
|
|
|
}
|
2008-01-16 16:11:00 +00:00
|
|
|
else if(highlighted) {
|
2007-12-14 18:22:04 +00:00
|
|
|
glColor4f(0.0f, 0.7f, 0.7f, 0.7f);
|
2007-12-14 22:03:00 +00:00
|
|
|
glLineWidth(2.0f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
glColor4f(0.0f, 0.7f, 0.7f, 0.7f);
|
|
|
|
glLineWidth(1.0f);
|
|
|
|
}
|
2008-02-08 21:21:01 +00:00
|
|
|
drawCircle(Vertex(start.getX(), start.getZ()), 0.3f);
|
2007-12-14 02:47:03 +00:00
|
|
|
|
|
|
|
glLineWidth(2.0f);
|
|
|
|
glColor4f(1.0f, 1.0f, 1.0f, 0.7f);
|
|
|
|
drawCross(Vertex(start.getX(), start.getZ()), 0.5f/sqrtf(scale));
|
|
|
|
}
|
|
|
|
|
2008-02-08 21:21:01 +00:00
|
|
|
void Renderer::renderPortal(const Portal &portal, bool selected, bool highlighted, float scale) {
|
|
|
|
if(selected) {
|
|
|
|
glColor4f(1.0f, 1.0f, 1.0f, 0.9f);
|
|
|
|
glLineWidth(2.0f);
|
|
|
|
}
|
|
|
|
else if(highlighted) {
|
|
|
|
glColor4f(0.0f, 0.7f, 0.7f, 0.7f);
|
|
|
|
glLineWidth(2.0f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
glColor4f(0.0f, 0.7f, 0.7f, 0.7f);
|
|
|
|
glLineWidth(1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
float s = sinf(portal.getOrientation());
|
|
|
|
float c = cosf(portal.getOrientation());
|
|
|
|
float x = portal.getWidth()/2*c;
|
|
|
|
float y = portal.getWidth()/2*s;
|
|
|
|
float ts = portal.getThickness()/2*s;
|
|
|
|
float tc = portal.getThickness()/2*c;
|
|
|
|
|
|
|
|
glBegin(GL_LINES);
|
|
|
|
|
|
|
|
glVertex2f(portal.getPosition().getX()-x+ts, portal.getPosition().getY()-y-tc);
|
|
|
|
glVertex2f(portal.getPosition().getX()-x-ts, portal.getPosition().getY()-y+tc);
|
|
|
|
|
|
|
|
glVertex2f(portal.getPosition().getX()+x+ts, portal.getPosition().getY()+y-tc);
|
|
|
|
glVertex2f(portal.getPosition().getX()+x-ts, portal.getPosition().getY()+y+tc);
|
|
|
|
|
|
|
|
glColor4f(1.0f, 1.0f, 1.0f, 0.9f);
|
|
|
|
|
|
|
|
glVertex2f(portal.getPosition().getX()-x, portal.getPosition().getY()-y);
|
|
|
|
glVertex2f(portal.getPosition().getX()+x, portal.getPosition().getY()+y);
|
|
|
|
|
|
|
|
glEnd();
|
2008-02-09 12:21:00 +00:00
|
|
|
|
|
|
|
if(highlighted || selected) {
|
|
|
|
renderVertex(Vertex(portal.getPosition().getX()-x+ts, portal.getPosition().getY()-y-tc),
|
|
|
|
false, false, scale);
|
|
|
|
renderVertex(Vertex(portal.getPosition().getX()-x-ts, portal.getPosition().getY()-y+tc),
|
|
|
|
false, false, scale);
|
|
|
|
renderVertex(Vertex(portal.getPosition().getX()+x+ts, portal.getPosition().getY()+y-tc),
|
|
|
|
false, false, scale);
|
|
|
|
renderVertex(Vertex(portal.getPosition().getX()+x-ts, portal.getPosition().getY()+y+tc),
|
|
|
|
false, false, scale);
|
|
|
|
}
|
2008-02-08 21:21:01 +00:00
|
|
|
}
|
|
|
|
|
2007-10-23 19:08:04 +00:00
|
|
|
void Renderer::render(const Level &level, const Rectangle &rect, float scale) {
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
glLineWidth(1.0f);
|
2007-12-14 02:47:03 +00:00
|
|
|
glPointSize(10.0f);
|
2007-10-23 19:08:04 +00:00
|
|
|
|
|
|
|
drawGrid(rect, scale);
|
|
|
|
|
2007-12-14 02:47:03 +00:00
|
|
|
for(Level::const_iterator object = level.begin(); object != level.end(); object++) {
|
|
|
|
renderObject(**object, (&**object == editManager->getSelectedObject()),
|
2008-01-16 16:11:00 +00:00
|
|
|
(&**object == editManager->getHighlightedObject()), scale);
|
2007-10-23 19:08:04 +00:00
|
|
|
}
|
|
|
|
}
|