PDA

View Full Version : Fill with color



Jean Sommeil
03-03-2014, 10:43 AM
Hello, everyone. I have one little ask to you. Can you help me find a problem in my code. I want to fill circle with color. My function Filling() must do it, but she doesnt and i dont know why. I think my algoritm is right and problem is somewhere in OpenGL code, but i cant find it.
Maybe OpenGL isnt good for this task, but i want to do it with him. Thanks for your time and im really sorry for my english, its not my native language.

#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

void Filling( float x, float y, float CurentColor[3], float NewColor[3] )
{
float CurentPixel[3] ;
glReadPixels(x, y , 1, 1, GL_RGB, GL_FLOAT, &CurentPixel);
if ( CurentPixel[0] == CurentColor[0] && CurentPixel[1] == CurentColor[1] && CurentPixel[2] == CurentColor[2] )
{

glBegin(GL_POINTS);
glColor3f( NewColor[0], NewColor[1], NewColor[2] );
glVertex2f(x,y);
glEnd();

Filling( x + 1, y, CurentColor, NewColor ) ;
Filling( x - 1, y, CurentColor, NewColor ) ;
Filling( x, y - 1, CurentColor, NewColor ) ;
Filling( x, y + 1, CurentColor, NewColor ) ;
}
return ;
}


void DrawCircle(float cx, float cy, float r, int num_segments)
{

int i;
float theta = 2 * 3.1415926 / num_segments;
float c = cosf(theta);
float s = sinf(theta);
float t;
float x = r;
float y = 0;
glBegin(GL_LINE_LOOP);
for( i = 0; i < num_segments; i++)
{

glVertex2f(x + cx, y + cy);
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}

void Display(void)
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity(); // Reset The Current Matrix to E
glOrtho( 0, 400, 400, 0, 0, 1 );
glMatrixMode(GL_MODELVIEW);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

DrawCircle( 200, 200, 50, 360 ) ; //Draw circle

float CurentColor[3] = { 0.0, 0.0, 0.0 } ;
float NewColor[3] = { 0.0, 0.0, 1.0 } ;
Filling( 200, 200, CurentColor, NewColor ) ;

DrawCircle( 100, 100, 50, 360 ) ; //check
glFlush();

}


int main(int argc, char **argv)
{
srand( time( NULL ) ) ;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutCreateWindow("Intro");
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}

tonyo_au
03-03-2014, 05:51 PM
Not sure why you doing this but you haven't set a colour for the DrawCircle so I expect you are just getting a blue square.

If you want to fill a circle this way you fill need to do some sort of line raster fill algorithm; you can't just check the colour at a point.

Jean Sommeil
03-03-2014, 10:19 PM
Can you explain what's wrong with my algorithm? I really can't understand why it not working. Or can you show me other ways for filling?

Jean Sommeil
03-04-2014, 10:42 AM
you can't just check the colour at a point.
Why i cant? I already checked it with this - glReadPixels(x, y , 1, 1, GL_RGB, GL_FLOAT, &CurentPixel);
CurentPoint contain a color of curent pixel. What i do wrong?

you haven't set a colour for the DrawCircle so I expect you are just getting a blue square.
On default glVertex2f(x,y); draw with white color, and when i run my program i get a white circle(created with DrawCircle) and blue line from center of circle to right point on it. But after finding the first white pixel, program crash and i dont know why.

Brokenmind
03-04-2014, 11:35 AM
The program might crash due to a stack overflow:
If you find the first white pixel, you recursively iterate through all neighbouring pixels until everything white is filled. But C++ only supports a limited number of stacks, and once this number is reached, I think your program crashes (I only witnessed this in C# so I'm not sure). And after all, you quite a high number of pixels you want to color. Try to achieve this with an iterative solution, with a stack to which you add all the new pixels that should be colored.

Jean Sommeil
03-04-2014, 09:48 PM
Thank you.