#include #include #include /***********************************************************************//* x, y 좌표에 point 생성 *//***********************************************************************/void setPixel (GLint xCoord, GLint yCoord){glBegin (GL_POINTS);glVertex2i(xCoord, yCoord);glEnd ();}/***********************************************************************//* x, y 좌표값 결정 *//***********************************************************************//* Bresenham line-drawing procedure for |m| < 1.0 */void lineBres (int x0, int y0, int xEnd, int yEnd){int dx = fabs (xEnd - x0), dy = fabs(yEnd - y0);int p = 2 * dy - dx;int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx);int x, y;/* Determine which endpoint to use as start position */if (x0 > xEnd) {x = xEnd;y = yEnd;xEnd = x0;}else {x = x0;y = y0;}setPixel (x, y);while (x < xEnd) {x++;if (p < 0)p += twoDy;else {y++;p += twoDyMinusDx;}setPixel (x, y);}}/***********************************************************************//* 직선을 그리기위한 Display Function *//***********************************************************************/void display (void){glClear (GL_COLOR_BUFFER_BIT); /* clear all pixels */glColor3f (1.0, 0.0, 0.0); /* Set line segment color to red */lineBres(40, 50, 160, 120);glFlush ( );/* don't wait! start processing buffered OpenGL routines */}/***********************************************************************//* Display window를 초기화 *//***********************************************************************/void init (void){glClearColor (1.0, 1.0, 1.0, 0.0); /* set clearing color to black */glMatrixMode (GL_PROJECTION); /* initialize viewing values */gluOrtho2D (0.0, 200.0, 0.0, 150.0);}/***********************************************************************//* Declare initial window size, position, and display mode (single buffer and RGBA)./* Open window with "hello" in its title bar. Call initialization routines./* Register callback function to display graphics. Enter main loop and process events./***********************************************************************/void main (int argc, char** argv){glutInit (&argc, argv); /* initialize glut library */glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* set display mode */glutInitWindowPosition (50, 100); /* set window position in screen */glutInitWindowSize (400, 300); /* set window size in pixels */glutCreateWindow ("Bresenhem Line Algorithm"); /* create window with "hello" on its title bar */init ( );/* Execute initialization procedure. */glutDisplayFunc (display);/* specify the function that's called whenever thecontents of the window need to be redrawn */glutMainLoop ( );/* Enter the GLUT processing loop */}