#include "Fl_OpenGL_Graphics_Driver.H"
#include <FL/fl_draw.H>
#include <FL/gl.h>
#include <FL/math.h>
#include <stdlib.h>
#define SLOW_COMPLEX_POLY
#ifdef SLOW_COMPLEX_POLY
# define GAP (1e9f)
#endif
void Fl_OpenGL_Graphics_Driver::begin_points() {
n = 0; gap_ = 0;
what = POINTS;
glBegin(GL_POINTS);
}
void Fl_OpenGL_Graphics_Driver::end_points() {
glEnd();
}
void Fl_OpenGL_Graphics_Driver::begin_line() {
n = 0; gap_ = 0;
what = LINE;
glBegin(GL_LINE_STRIP);
}
void Fl_OpenGL_Graphics_Driver::end_line() {
glEnd();
}
void Fl_OpenGL_Graphics_Driver::begin_loop() {
n = 0; gap_ = 0;
what = LOOP;
glBegin(GL_LINE_LOOP);
}
void Fl_OpenGL_Graphics_Driver::end_loop() {
glEnd();
}
void Fl_OpenGL_Graphics_Driver::begin_polygon() {
n = 0; gap_ = 0;
what = POLYGON;
glBegin(GL_POLYGON);
}
void Fl_OpenGL_Graphics_Driver::end_polygon() {
glEnd();
}
void Fl_OpenGL_Graphics_Driver::begin_complex_polygon() {
n = 0;
what = COMPLEX_POLYGON;
#ifndef SLOW_COMPLEX_POLY
glBegin(GL_POLYGON);
#endif
}
void Fl_OpenGL_Graphics_Driver::gap() {
#ifdef SLOW_COMPLEX_POLY
if (n==0 || n==gap_) return;
XPOINT& p = xpoint[gap_];
transformed_vertex(p.x, p.y);
transformed_vertex(GAP, 0.0);
gap_ = n;
#else
glEnd();
glBegin(GL_POLYGON);
#endif
}
#ifdef SLOW_COMPLEX_POLY
void Fl_OpenGL_Graphics_Driver::end_complex_polygon()
{
int i, y;
XPOINT *v0, *v1;
if (n < 2) return;
gap();
v0 = xpoint;
v0->y -= 0.1f;
float xMin = v0->x, xMax = xMin;
int yMin = (int)v0->y, yMax = yMin;
for (i = 1; i < n; i++) {
v0++;
v0->y -= 0.1f;
float v0x = v0->x;
int v0y = (int)v0->y;
if (v0x == GAP) continue;
if (v0x <= xMin) xMin = v0x;
if (v0x >= xMax) xMax = v0x;
if (v0y <= yMin) yMin = v0y;
if (v0y >= yMax) yMax = v0y;
}
int nNodes;
float *nodeX = (float*)malloc((n-1)*sizeof(float)), swap;
if (!nodeX)
return;
for (y = yMin; y <= yMax; y++) {
v0 = xpoint + 0;
v1 = xpoint + 1;
nNodes = 0;
for (i = 1; i < n; i++) {
if (v1->x==GAP) { i++; v0++; v1++; v0++; v1++;
continue;
}
if ( (v1->y < y && v0->y >= y)
|| (v0->y < y && v1->y >= y) )
{
float dy = v0->y - v1->y;
if (fabsf(dy)>.0001f) {
nodeX[nNodes++] = v1->x + ((y - v1->y) / dy) * (v0->x - v1->x);
} else {
nodeX[nNodes++] = v1->x;
}
}
v0++; v1++;
}
i = 0;
while (i < nNodes-1) {
if (nodeX[i] > nodeX[i+1]) {
swap = nodeX[i];
nodeX[i] = nodeX[i+1];
nodeX[i+1] = swap;
if (i) i--;
} else {
i++;
}
}
for (i = 0; i < nNodes; i += 2) {
float x0 = nodeX[i];
if (x0 >= xMax)
break;
float x1 = nodeX[i+1];
if (x1 > xMin) {
if (x0 < xMin)
x0 = xMin;
if (x1 > xMax)
x1 = xMax;
glRectf((GLfloat)(x0-0.25f), (GLfloat)(y), (GLfloat)(x1+0.25f), (GLfloat)(y+1.0f));
}
}
}
::free(nodeX);
}
#else
void Fl_OpenGL_Graphics_Driver::end_complex_polygon() {
glEnd();
}
#endif
void Fl_OpenGL_Graphics_Driver::fixloop() { }
void Fl_OpenGL_Graphics_Driver::transformed_vertex(double xf, double yf) {
#ifdef SLOW_COMPLEX_POLY
if (what==COMPLEX_POLYGON) {
Fl_Graphics_Driver::transformed_vertex(xf, yf);
} else {
glVertex2d(xf, yf);
}
#else
glVertex2d(xf, yf);
#endif
}
void Fl_OpenGL_Graphics_Driver::circle(double cx, double cy, double r) {
double rx = r * (m.c ? sqrt(m.a*m.a+m.c*m.c) : fabs(m.a));
double ry = r * (m.b ? sqrt(m.b*m.b+m.d*m.d) : fabs(m.d));
double rMax;
if (ry>rx) rMax = ry; else rMax = rx;
int num_segments = (int)(10 * sqrt(rMax))+1;
double theta = 2 * M_PI / float(num_segments);
double tangetial_factor = tan(theta);
double radial_factor = cos(theta); double x = r; double y = 0;
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++) {
vertex(x + cx, y + cy); double tx = -y;
double ty = x;
x += tx * tangetial_factor;
y += ty * tangetial_factor;
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}