我正在尝试使用seedfill算法填充棋盘中的替代方块,但是当我右键单击这样做时,它会进入无限递归,尽管终止条件。 尽管在路径中遇到白色像素,它仍沿x方向前进填充像素。 谁能指出我哪里出错?
现在我意识到getReadPixels()总是返回我的背景颜色,即0.0,0.0,0.0因为哪个种子填充不起作用。但我无法弄明白为什么?它与FRAME BUFFER或其他什么有关吗?
#include <GL/glut.h>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
#define WIDTH 640
#define HEIGHT 480
#define PI 3.14
float m[25][2], u[25][2];
void initialise(){
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glClear( GL_COLOR_BUFFER_BIT );
glPointSize( 3.0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( -WIDTH/2, WIDTH/2, -HEIGHT/2, HEIGHT/2 );
}
void setpixel( float x, float y ){
glBegin( GL_POINTS );
glVertex2i( x, y );
glEnd();
glFlush();
}
void display( void ){
glBegin( GL_LINES );
glVertex2i( -WIDTH/2, 0 );
glVertex2i( WIDTH/2, 0 );
glVertex2i(0, -HEIGHT/2 );
glVertex2i(0, HEIGHT/2 );
glEnd();
glFlush();
}
//choosing the pixel color selected by the user
void getPixel( int x, int y, GLubyte *color ){
glReadPixels( x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color );
}
//----------------------------------------//
void seedfill( float x, float y ){
GLubyte rgb[3];
getPixel( x, y, rgb );
if( rgb[0] == 0.0 ){
cout << "TRUE" << endl;
}
if( rgb[0] == 0.0 ){
glColor3f( 1.0, 1.0, 1.0 );
glPointSize( 1.0 );
setpixel( x, y );
seedfill( x+1, y );
seedfill( x-1, y );
seedfill( x, y-1 );
seedfill( x, y+1 );
}
}
//using dda algorithm to draw line between two pixels
void dda(float x1, float y1, float x2, float y2){
float dx = 0, dy = 0, i = 1, temp;
float length = 0, slope = 0;
float Dx = 0, Dy = 0;
dx = x2 - x1;
dy = y2 - y1;
//slope = dy/dx;
length = (abs(dx) >= abs(dy))?abs(dx):abs(dy);
Dx = dx/length;
Dy = dy/length;
setpixel(round(x1), round(y1));
for( ; i <= length; i++){
x1 = x1 + Dx;
y1 = y1 + Dy;
setpixel(round(x1), round(y1));
//cout << x1 << "\t" << y1 << endl;
}
}
//----------------------------------------//
void complete( float z[25][2] ){
int index_x, index_y;
for( int i = 0, j = 0 ; i < 25 ; i+=5, j++ ){
index_x = i+4;
index_y = j+20;
dda( z[i][0], z[i][1], z[index_x][0], z[index_x][1] );
dda( z[j][0], z[j][1], z[index_y][0], z[index_y][1] );
}
}
void drawBoard( int x1, int y1, int x2, int y2 ){
int inc;
float x, y = y1;
inc = (x2-x1) / 4;
for( int i = 0 ; i < 25 ; ){
x = x1;
for( int j = 0 ; j < 5 ; j++ ){
m[i][0] = x;
m[i][1] = y;
x = x + inc;
i++;
}
y = y + inc;
}
complete( m );
}
void rotateBoard(){
float r[2][2];
int limit;
initialise();
display();
r[0][0] = cos(45 * PI / 180);
r[0][1] = sin(45 * PI / 180);
r[1][0] = -sin(45 * PI / 180);
r[1][1] = cos(45 * PI / 180);
for( int i = 0 ; i < 25 ; i++ ){
for( int k = 0 ; k < 2 ; k++ ){
u[i][k] = 0;
for( int j = 0 ; j < 2 ; j++ ){
u[i][k] = u[i][k] + (m[i][j] * r[j][k]);
}
}
}
complete( u );
for( int i = 0 ; i < 25 ; ){
seedfill( u[i][0] + 10, u[i][1] + 20 );
i += 2;
seedfill( u[i][0] + 10, u[i][1] + 20 );
if( i+6 == 24 )break;
if( (limit - i) % 2 == 0 ){
i += 4;
}
else{
i += 2;
}
limit += 5;
}
}
void mouse( int button, int state, int x, int y ){
if( state == GLUT_DOWN ){
if( button == GLUT_LEFT_BUTTON ){
drawBoard( 0, 0, 160, 160 );
//cout << getpixel( x, y ) << endl;
}
else if( button == GLUT_RIGHT_BUTTON ){
//do something here
rotateBoard();
}
}
}
int main( int argc, char **argv ){
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( WIDTH, HEIGHT );
glutInitWindowPosition( 100, 100 );
glutCreateWindow("CKR");
initialise();
glutDisplayFunc( display );
glutMouseFunc( mouse );
glutMainLoop();
return 0;
}