Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Fill with color

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    4

    Fill with color

    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.
    Code :
    #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;
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    4
    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?
    Last edited by Jean Sommeil; 03-03-2014 at 10:29 PM.

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    4
    Quote Originally Posted by tonyo_au View Post
    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?
    Quote Originally Posted by tonyo_au View Post
    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.
    Last edited by Jean Sommeil; 03-04-2014 at 10:53 AM.

  5. #5
    Intern Contributor Brokenmind's Avatar
    Join Date
    Feb 2014
    Location
    Aachen / Germany
    Posts
    71
    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.

  6. #6
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    4
    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •