Click to See Complete Forum and Search --> : polygon algorithm in c++


Maverika
May 11th, 2004, 12:40 PM
Hi,

Does anyone know which is the polygon filling algorithm in c++?

It can be done with this code, but what i want is which is the algorithm that is behind this ready made function glBegin( GL_POLYGON ) of open gl.





#include <iostream>
#include <cmath>
#include <windows.h>
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <gl/glut.h>
#include <cmath>

using std::cout;
using std::cin;



int x[4], y[4];



void drawPolygon( int n, int *x, int *y ) {

int i;
glBegin( GL_POLYGON );
for (i = 0; i < n; i++ ) {
glVertex2i( x[i], y[i] );
}
glEnd( );

}
//---------------------------------------------------------------------
void myKeyboard(unsigned char key, int mouse_x, int mouse_y) {

switch (key) {
case 'a' :

glBegin( GL_POLYGON );
drawPolygon( 4, x, y );
x[0] = 300; y[0] = 300;
x[1] = 150; y[1] = 100;
x[2] = 50; y[2] = 200;
x[3] = 200; y[3] = 400;
glFlush();
glEnd( );

break;

}

}

//---------------------------------------------------------------------
void myInit(void) {

//Set background color
glClearColor(1.0, 1.0, 1.0, 0.0);

//Set background color
glColor3f(0.0f, 0.0f, 0.0f);

glPointSize(10.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 480.0, 0.0);

}
//-----------------------------------------------------------------------
void myDisplay(void) {
//Clear the screen
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);


glEnd();
glFlush();
}
//-----------------------------------------------------------------------
void main(int argc, char **argv) {

//Initialize the toolkit
glutInit(&argc, argv);
//Set the display mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//Set window size
glutInitWindowSize(640,480);
//Set the window position on screen
glutInitWindowPosition(100,150);
//Open the screen window
glutCreateWindow("Polygon");

//Register callback functions
glutDisplayFunc(myDisplay);
// glutReshapeFunc(myReshape);
glutKeyboardFunc(myKeyboard);

myInit();
glutMainLoop();
}





If anyone has a code or knows a link with the polygon filling algorithm please reply

Thanks

yiannakop
May 12th, 2004, 09:33 AM
You can use one solution for the point-in-polygon problem. I've done something like that before to compute the area of a polygon (actually to compute how many pixels belong to the inner area of a polygon). So what you have to do is find if a pixel belongs in a polygon and the color it.

I found in the net this (http://www.alienryderflex.com/polygon/) link that explains in a simple way how to accomplise this goal.

Regards,
Theodore.

Marc G
May 12th, 2004, 10:42 AM
The 3D graphics card can choose which algorithm they use.
But you can always download mesa-opengl (software renderer) and see how they fill their polygons in software.