#include<windows.h>
#include<GL/glut.h>
#include<GL/gl.h>
#include <iostream.h>
#include <ctime>
#include <cstdlib>
#include<math.h>
#define pointSize 1
class point4
{
public:
point4(){startx=0;starty=0;endx=0;endy=0;}
int startx;
int starty;
int endx;
int endy;
};
point4 array[20];
int line_number;
int startX,startY,endX,endY;
int mouse_button_state;//0:没有操作 1:第一个点刚按下 2:松开点
int mark;
void lineBresPlane(int xStart,int yStart,int xEnd,int yEnd)
{
int x, y, xx, yy;
if( xStart>xEnd )
{
x = xEnd;
y = yEnd;
xx = xStart;
yy = yStart;
}
else
{
x = xStart;
y = yStart;
xx = xEnd;
yy = yEnd;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
while(x<xx)
{
x ++;
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
}
void lineBresApeak(int xStart,int yStart,int xEnd,int yEnd)
{
int x, y, xx, yy;
if( yStart>yEnd )
{
y = yEnd;
x = xEnd;
yy = yStart;
xx = xStart;
}
else
{
y = yStart;
x = xStart;
yy = yEnd;
xx = xEnd;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
while(y<yy)
{
y ++;
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
}
void lineBresDiagonal(int xStart,int yStart,int xEnd,int yEnd)
{
int x, y, xx, yy;
if( xStart>xEnd )
{
x = xEnd;
y = yEnd;
xx = xStart;
yy = yStart;
}
else
{
x = xStart;
y = yStart;
xx = xEnd;
yy = yEnd;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
bool flag = false;
if(yy>y) flag = true;
while(x<xx)
{
x ++;
if(flag)
y ++;
else
y --;
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
}
void lineBresXbig(int xStart,int yStart,int xEnd,int yEnd)
{
int dx = fabs(xEnd - xStart), dy = fabs(yEnd - yStart);
int p = 2*dy - dx;
int twoDy = 2*dy, twoDyMinusDx = 2*(dy-dx);
int x, y, xx, yy;
if( xStart>xEnd )
{
x = xEnd;
y = yEnd;
xx = xStart;
yy = yStart;
}
else
{
x = xStart;
y = yStart;
xx = xEnd;
yy = yEnd;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
bool flag = false;
if(yy>y) flag = true;
while(x<xx)
{
x ++;
if(p<0)
p += twoDy;
else
{
if(flag)
y ++;
else
y --;
p += twoDyMinusDx;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
}
void lineBresYbig(int xStart,int yStart,int xEnd,int yEnd)
{
int dx = fabs(xEnd - xStart), dy = fabs(yEnd - yStart);
int p = 2*dx - dy;
int twoDx = 2*dx, twoDxMinusDy = 2*(dx-dy);
int x, y, xx, yy;
if( yStart>yEnd )
{
y = yEnd;
x = xEnd;
yy = yStart;
xx = xStart;
}
else
{
y = yStart;
x = xStart;
yy = yEnd;
xx = xEnd;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
bool flag = false;
if(xx>x) flag = true;
while(y<yy)
{
y ++;
if(p<0)
p += twoDx;
else
{
if(flag)
x ++;
else
x --;
p += twoDxMinusDy;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
}
void myinit()
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(0.0, 1.0, 0.0);
glPointSize(pointSize);
glOrtho(0.0,799.0,0.0,699.0,-1.,1.);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_COLOR_LOGIC_OP);
line_number=0;
}
void flood_fill(int x,int y){
int iPixel = 0;
glReadBuffer(GL_FRONT);
glReadPixels(x, y, 1, 1,GL_RED, GL_INT, &iPixel);//读取一点的红色通
if(iPixel!=0)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
flood_fill(x-1,y);
flood_fill(x+1,y);
flood_fill(x,y-1);
flood_fill(x,y+1);
}
}
void drawline(int startx,int starty,int endx,int endy)
{
int dx = fabs(endx - startx), dy = fabs(endy - starty);
if(dx==0) lineBresApeak(startx,starty,endx,endy);
else if(dy==0) lineBresPlane(startx,starty,endx,endy);
else if(dx==dy) lineBresDiagonal(startx,starty,endx,endy);
else if(dx>dy) lineBresXbig(startx,starty,endx,endy);
else lineBresYbig(startx,starty,endx,endy);
glFlush();
}
void motion(int x, int y)
{
if(mouse_button_state==1){
glLogicOp(GL_XOR);
drawline(startX,startY,endX,endY);
endX=x;
endY=700-y;
glLogicOp(GL_XOR);
drawline(startX,startY,endX,endY);
}
}
void mouse(int button,int state,int x,int y)
{
if((mark==0) && (button == GLUT_LEFT_BUTTON))
{
if((mouse_button_state ==0)&&(state == GLUT_DOWN)) //第一次按下
{
mouse_button_state = 1;
startX = x;
startY = 700-y;
}
else if((mouse_button_state ==1)) //长按后松掉
{
mouse_button_state = 0;
glLogicOp(GL_XOR);
drawline(startX,startY,endX,endY);
endX=x;
endY=700-y;
array[line_number].startx=startX;
array[line_number].starty=startY;
array[line_number].endx=endX;
array[line_number].endy=endY;
line_number++;
glLogicOp(GL_COPY);
drawline(startX,startY,endX,endY);
}
}
if((mark==1) && (button == GLUT_LEFT_BUTTON)){
int x1=x;
int y1=700-y;
flood_fill(x1,y1);
}
}
void display(void)
{
glLogicOp(GL_COPY);
glClear(GL_COLOR_BUFFER_BIT);
if(line_number!=0){
for(int i=0;i<line_number;i++)
drawline(array[i].startx,array[i].starty,array[i].endx,array[i].endy);
}
}
void menu(int id)
{
if(id == 1 ) mark=0;
if(id == 2) mark=1;
}
void reshape(int w, int h)
{
glutPostRedisplay();
glViewport(0, 0, w, h);
}
void keyboard(unsigned char key, int x, int y)//Esc
{
switch (key) {
case 27:
exit(0);
break;
}
}
void main(int argc,char **argv)
{
srand(time(0));
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,700);
glutInitWindowPosition(0,0);
glutCreateWindow("bresenham");
myinit();
glutReshapeFunc (reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMouseFunc (mouse);
glutMotionFunc (motion);
glutCreateMenu(menu);
glutAddMenuEntry("画线", 1);
glutAddMenuEntry("种子填充", 2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
- 1
- 2
前往页