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;

}

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;

}