Southperry.net
OpenGL's glReadPixels problem (boundary fill) - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: OpenGL's glReadPixels problem (boundary fill) (/showthread.php?tid=43637)



OpenGL's glReadPixels problem (boundary fill) - Unauthorized Intruder - 2011-06-29

The problem is with glReadPixels function. It should give color that's on that pixel at that moment, but it instead gives me background color. What did I do wrong?

Code:
#include "stdafx.h"
#include <GL/glut.h>

using namespace System;
struct Color
{
    float red, green, blue;
    bool equals(Color color)
    {
        if (color.red == red && color.green == green && color.blue == blue)
            return true;
        else return false;
    }
    void getColor(int x, int y)
    {
        float temp[3];
        glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, temp);<--------[B] this[/B]
        red = temp[0];
        green = temp[1];
        blue = temp[2];
    }
    void paint(int x, int y)
    {
        glBegin(GL_POINTS);
        glColor3f(red, green, blue);
        glVertex2i(x, y);
        glEnd();
    }
};
void boundaryFill4(int x, int y, Color colorToFill, Color borderColor)
{
    Color temp;
    temp.getColor(x, y);
    if (!(temp.equals(colorToFill) || temp.equals(borderColor)))
    {
        colorToFill.paint(x, y);
        if (x < 200) boundaryFill4(x+1, y, colorToFill, borderColor);
        if (x > 0) boundaryFill4(x-1, y, colorToFill, borderColor);
        if (y < 200)boundaryFill4(x, y+1, colorToFill, borderColor);
        if (y > 0) boundaryFill4(x, y-1, colorToFill, borderColor);
    }
}
void myDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);    
    Color boundColor;
    boundColor.red = 1;
    boundColor.green = 1;
    boundColor.blue = 1;
    glColor3f(boundColor.red, boundColor.green, boundColor.blue);
    glBegin(GL_LINE_LOOP);
    glVertex2i(100, 100);
    glVertex2i(100, 150);
    glVertex2i(150, 150);
    glVertex2i(150, 100);
    glEnd();
    Color colorToFill;
    colorToFill.red = 1;
    colorToFill.green= 0;
    colorToFill.blue = 0;
    boundaryFill4(120, 120, colorToFill, boundColor);
    glFlush();
}
void initializeGL()
{
    glClearColor(0.0, 0, 0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 200.0, 200.0, 0.0);
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0,0);
    glutCreateWindow("My Rectangle");
    glutDisplayFunc(myDisplay);
    initializeGL();
    glutMainLoop();
    return 0;
}



OpenGL's glReadPixels problem (boundary fill) - SaptaZapta - 2011-06-29

Not too familiar with OpenGL, but try doing a glFlush after drawing the shape to be filled.