2011-06-29, 12:08 PM
(This post was last modified: 2011-06-29, 12:18 PM by Unauthorized Intruder.)
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;
}
