cpp_photon/src/video/Pen.cpp

199 lines
4.3 KiB
C++
Raw Normal View History

2005-03-02 10:55:29 +00:00
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
2005-04-21 19:30:19 +00:00
// $Id: Pen.cpp,v 1.3 2005/04/21 19:30:19 cozman Exp $
2005-03-02 10:55:29 +00:00
#include "video/Pen.hpp"
2005-04-21 19:30:19 +00:00
#include "GL/gl.h"
2005-03-02 10:55:29 +00:00
#include "math/math.hpp"
namespace photon
{
namespace video
{
Pen::Pen() :
color_(255,255,255,255)
{
}
Pen::Pen(ubyte r, ubyte g, ubyte b, ubyte a) :
color_(r,g,b,a)
{
}
2005-03-03 09:25:19 +00:00
Pen::Pen(const Color& color) :
2005-03-02 10:55:29 +00:00
color_(color)
{
}
void Pen::setColor(ubyte r, ubyte g, ubyte b, ubyte a)
{
color_.setColor(r,g,b,a);
}
2005-03-03 09:25:19 +00:00
void Pen::setColor(const Color& color)
2005-03-02 10:55:29 +00:00
{
color_ = color;
}
2005-03-03 09:25:19 +00:00
void Pen::drawPoint(const math::Point2& point) const
2005-03-02 10:55:29 +00:00
{
glBindTexture(GL_TEXTURE_2D,0);
color_.makeGLColor();
glBegin(GL_POINTS);
glVertex2d(point.x,point.y);
glEnd();
glColor4ub(255,255,255,255);
}
2005-03-03 09:25:19 +00:00
void Pen::drawLine(const math::Point2& p1, const math::Point2& p2) const
2005-03-02 10:55:29 +00:00
{
glBindTexture(GL_TEXTURE_2D,0);
color_.makeGLColor();
glBegin(GL_LINES);
glVertex2d(p1.x,p1.y);
glVertex2d(p2.x,p2.y);
glEnd();
glColor4ub(255,255,255,255);
}
2005-03-03 09:25:19 +00:00
void Pen::drawVector(const math::Point2& point,
const math::Vector2& vector) const
2005-03-02 10:55:29 +00:00
{
double x2,y2,x3,y3,x4,y4;
math::Vector2 v;
x2 = point.x+vector.x;
y2 = point.y+vector.y;
//calculate an arrow (5pi/6)
2005-03-03 09:25:19 +00:00
v.resolveRad(vector.getMagnitude()/5,vector.getAngleRad()+(5./6)*math::PI);
2005-03-02 10:55:29 +00:00
x3 = x2+v.x;
y3 = y2-v.y;
2005-03-03 09:25:19 +00:00
v.resolveRad(vector.getMagnitude()/5,vector.getAngleRad()-(5./6)*math::PI);
2005-03-02 10:55:29 +00:00
x4 = x2+v.x;
y4 = y2-v.y;
glBindTexture(GL_TEXTURE_2D,0);
color_.makeGLColor();
glBegin(GL_LINE_STRIP);
glVertex2d(point.x,point.y);
glVertex2d(x2,y2);
glVertex2d(x3,y3);
glVertex2d(x4,y4);
glVertex2d(x2,y2);
glEnd();
glColor4ub(255,255,255,255);
}
void Pen::drawRectangle(const math::Rect &rect) const
{
glBindTexture(GL_TEXTURE_2D,0);
color_.makeGLColor();
glBegin(GL_LINE_STRIP);
glVertex2d(rect.getLeft(),rect.getTop());
glVertex2d(rect.getRight(),rect.getTop());
glVertex2d(rect.getRight(),rect.getBottom());
glVertex2d(rect.getLeft(),rect.getBottom());
glVertex2d(rect.getLeft(),rect.getTop());
glEnd();
glColor4ub(255,255,255,255);
}
void Pen::fillRectangle(const math::Rect &rect) const
{
glBindTexture(GL_TEXTURE_2D,0);
color_.makeGLColor();
glBegin(GL_QUADS);
glVertex2d(rect.getLeft(),rect.getTop());
glVertex2d(rect.getRight(),rect.getTop());
glVertex2d(rect.getRight(),rect.getBottom());
glVertex2d(rect.getLeft(),rect.getBottom());
glEnd();
glColor4ub(255,255,255,255);
}
void Pen::drawCircle(const math::Circle &circle) const
{
//written from Bresenham's algorithm
double cx(circle.getCenter().x);
double cy(circle.getCenter().y);
double d(3-(2*circle.getRadius()));
double x(0);
double y(circle.getRadius());
glBindTexture(GL_TEXTURE_2D,0);
color_.makeGLColor();
glBegin(GL_POINTS);
while(y > x)
{
glVertex2d(cx+x,cy+y);
glVertex2d(cx+x,cy-y);
glVertex2d(cx-x,cy+y);
glVertex2d(cx-x,cy-y);
glVertex2d(cx+y,cy+x);
glVertex2d(cx+y,cy-x);
glVertex2d(cx-y,cy+x);
glVertex2d(cx-y,cy-x);
if(d < 0)
{
d += x*4 + 6;
}
else
{
d += (x-y)*4 + 10;
--y;
}
++x;
}
glEnd();
glColor4ub(255,255,255,255);
}
void Pen::fillCircle(const math::Circle &circle) const
{
//written from Bresenham's algorithm
double cx(circle.getCenter().x);
double cy(circle.getCenter().y);
double d(3-(2*circle.getRadius()));
double x(0);
double y(circle.getRadius());
glBindTexture(GL_TEXTURE_2D,0);
color_.makeGLColor();
glBegin(GL_LINES);
while(y > x)
{
glVertex2d(cx+x,cy+y);
glVertex2d(cx+x,cy-y);
glVertex2d(cx-x,cy-y);
glVertex2d(cx-x,cy+y);
glVertex2d(cx+y,cy+x);
glVertex2d(cx-y,cy+x);
glVertex2d(cx-y,cy-x);
glVertex2d(cx+y,cy-x);
if(d < 0)
{
d += x*4 + 6;
}
else
{
d += (x-y)*4 + 10;
--y;
}
++x;
}
glEnd();
glColor4ub(255,255,255,255);
}
}
}