Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8510515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:43:37+00:00 2026-06-11T03:43:37+00:00

I’m trying to implement the flood fill algorithm. But glReadPixels() is returning float RGB

  • 0

I’m trying to implement the flood fill algorithm. But glReadPixels() is returning float RGB values of a pixel which are slightly different from the actual value set by me, causing the algorithm to fail. Why is this happening?

Outputting returned RGB values to check.

#include<iostream>
#include<GL/glut.h>
using namespace std;

float boundaryColor[3]={0,0,0}, interiorColor[3]={0,0,0.5}, fillColor[3]={1,0,0};
float readPixel[3];

void init(void) {
    glClearColor(0,0,0.5,0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0,500,0,500);
}

void setPixel(int x,int y) {        
        glColor3fv(fillColor);
        glBegin(GL_POINTS); 
             glVertex2f(x,y); 
        glEnd();
}

void getPixel(int x, int y, float *color) {
    glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,color);
}

void floodFill(int x,int y) {
    getPixel(x,y,readPixel);

    //outputting values here to check
    cout<<readPixel[0]<<endl;
    cout<<readPixel[1]<<endl;
    cout<<readPixel[2]<<endl;

    if( readPixel[0]==interiorColor[0] && readPixel[1]==interiorColor[1] && readPixel[2]==interiorColor[2] ) {
        setPixel(x,y);
        floodFill(x+1,y);
        floodFill(x,y+1);
        floodFill(x-1,y);
        floodFill(x,y-1);
    }
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3fv(boundaryColor);
    glLineWidth(3); 

    glBegin(GL_LINE_STRIP);
        glVertex2i(150,150);        
        glVertex2i(150,350);        
        glVertex2i(350,350);        
        glVertex2i(350,150);
        glVertex2i(150,150);
    glEnd();

    floodFill(200,200);

    glFlush();
}

int main(int argc,char** argv) {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(500,500);
    glutCreateWindow("Flood fill");

    init();
    glutDisplayFunc(display);
    glutMainLoop();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:43:38+00:00Added an answer on June 11, 2026 at 3:43 am

    I expected the color to be read as 0.0, 0.0, 0.5.

    Why? You don’t appear to be rendering to a floating-point framebuffer. Which means that you’re likely rendering to a GL_RGBA8 buffer storing normalized integers (meaning that it maps the [0, 255] range to [0, 1] floating-point values). Well, an 8-bit normalized integer cannot store 0.5 exactly. The closest it can get is 127/255, which is 0.498.

    If you wanted “accurate values”, then you should be rendering to a render target that will give you accurate values. IE: not the screen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.