Created
October 26, 2013 02:18
-
-
Save pony012/7164576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <math.h> | |
#include <GL/glut.h> | |
#define WIDTH 400 | |
#define HEIGHT 300 | |
#define STEP 13 | |
int buff[WIDTH][HEIGHT]; | |
typedef struct color{ | |
float r,g,b; | |
}Color3f; | |
typedef struct line{ | |
int x1,y1,x2,y2; | |
}Line; | |
int X=0,Y=0, DX=0, DY=0; | |
int banderaP = 0, leftPressed = 0; | |
void mouse(int btn, int state, int x, int y){ | |
if(state == GLUT_DOWN && btn == GLUT_LEFT_BUTTON){ | |
leftPressed=1; | |
banderaP=1; | |
X=x; | |
Y=HEIGHT-y; | |
DX=X; | |
DY=Y; | |
}else if(state == GLUT_UP && btn == GLUT_LEFT_BUTTON){ | |
banderaP=0; | |
DX=x; | |
DY=HEIGHT-y; | |
glutPostRedisplay(); | |
}else{ | |
leftPressed=0; | |
} | |
} | |
void mouseMotion(int x, int y){ | |
DX=x; | |
DY=HEIGHT-y; | |
glutPostRedisplay(); | |
} | |
void init(void) | |
{ | |
glClearColor(1.0, 1.0, 1.0, 0.0); // color de la ventana, blanco | |
glMatrixMode(GL_PROJECTION); // proyección de la imagen en la ventana de visualización | |
gluOrtho2D(0.0, (double)WIDTH, 0.0, (double)HEIGHT); // proyección en 2D | |
glClear(GL_COLOR_BUFFER_BIT); | |
glReadPixels(0,0,WIDTH,HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,buff); | |
} | |
void linea(int x1, int y1, int x2, int y2, Color3f color, int modo){ | |
glColor3f(color.r, color.g, color.b); | |
int step; | |
if(modo==1){ | |
step = STEP; | |
}else if(modo==0){ | |
step = 1; | |
} | |
int i, j, xaux, yaux, dx, dy, p, incX, incY; | |
dx = abs(x2-x1); | |
dy = abs(y2-y1); | |
if(dx>dy){ | |
if(x1>x2){ | |
xaux=x1; | |
yaux=y1; | |
x1=x2; | |
y1=y2; | |
x2=xaux; | |
y2=yaux; | |
} | |
xaux=x1; | |
yaux=y1; | |
glBegin(GL_POINTS); | |
glVertex2i(xaux, yaux); | |
glEnd(); | |
p = 2.0*dy-dx; | |
incY=y2>y1?1:-1; | |
i=x1; | |
while(i<x2){ | |
for(j=0;j<step;j++){ | |
xaux++; | |
i++; | |
if(p<0){ | |
p=p+2*dy; | |
}else{ | |
yaux+=incY; | |
p = p+2*(dy-dx); | |
} | |
if(i<x2){ | |
glBegin(GL_POINTS); | |
glVertex2i(xaux, yaux); | |
glEnd(); | |
} | |
} | |
for(j=0;j<step;j++){ | |
xaux++; | |
i++; | |
if(p<0){ | |
p=p+2*dy; | |
}else{ | |
yaux+=incY; | |
p = p+2*(dy-dx); | |
} | |
if(modo==0&&i<x2){ | |
glBegin(GL_POINTS); | |
glVertex2i(xaux, yaux); | |
glEnd(); | |
} | |
} | |
} | |
}else{ | |
if(y1>y2){ | |
xaux=x1; | |
yaux=y1; | |
x1=x2; | |
y1=y2; | |
x2=xaux; | |
y2=yaux; | |
} | |
xaux=x1; | |
yaux=y1; | |
glBegin(GL_POINTS); | |
glVertex2i(xaux, yaux); | |
glEnd(); | |
p = 2.0*dx-dy; | |
incX=x2>x1?1:-1; | |
i=y1; | |
while(i<y2){ | |
for(j=0;j<step;j++){ | |
yaux++; | |
i++; | |
if(p<0){ | |
p=p+2*dx; | |
}else{ | |
xaux+=incX; | |
p = p+2*(dx-dy); | |
} | |
if(i<y2){ | |
glBegin(GL_POINTS); | |
glVertex2i(xaux, yaux); | |
glEnd(); | |
} | |
} | |
for(j=0;j<step;j++){ | |
yaux++; | |
i++; | |
if(p<0){ | |
p=p+2*dx; | |
}else{ | |
xaux+=incX; | |
p = p+2*(dx-dy); | |
} | |
if(modo==0&&i<y2){ | |
glBegin(GL_POINTS); | |
glVertex2i(xaux, yaux); | |
glEnd(); | |
} | |
} | |
} | |
} | |
} | |
void dibujaLinea(void){ | |
glClear(GL_COLOR_BUFFER_BIT); | |
Color3f c; | |
c.r=0.0; | |
c.g=0.0; | |
c.b=1.0; | |
glDrawPixels(WIDTH,HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,buff); | |
if(leftPressed==1 && (X!=DX || Y!=DY)){ | |
if(banderaP==1){ | |
glDrawPixels(WIDTH,HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,buff); | |
linea(X,Y,DX,DY,c,1); | |
}else{ | |
glDrawPixels(WIDTH,HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,buff); | |
linea(X,Y,DX,DY,c,0); | |
glReadPixels(0,0,WIDTH,HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,buff); | |
leftPressed=0; | |
} | |
} | |
glFlush(); | |
} | |
int main(int argc, char** argv) | |
{ | |
glutInit(&argc, argv); //inicialización de GLUT | |
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //único búfer de refresco en la ventana de visualización y el modo de color RGB | |
glutInitWindowPosition(50, 100); // posición inicial, esq sup izq | |
glutInitWindowSize(WIDTH, HEIGHT); // alto y ancho de la ventana en pixeles | |
glutCreateWindow("Interactive Bresenham"); // creación de ventana de visualización y título de la misma | |
init(); | |
glutDisplayFunc(dibujaLinea); | |
glutMouseFunc(mouse); | |
glutMotionFunc(mouseMotion); | |
glutMainLoop(); // bucle infinito que comprueba entrada de disp | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment