PDA

View Full Version : linear interpolation



sonnendeck
11-16-2007, 11:39 AM
Hallo.

I have some problems with OpenGL. I have to write a program which implements linear interpolation for polygons.
For example: My polygon has 4 vertices and each vertice has another color. I want to interpolate each pixel in the polygon area according to this colors.

My idea is to draw at first the shape of the polygon in red (with white background) and after that i want to read a line with the glReadPixels(), but i don't understand this function exactly. Then i want to find the start and the end point of a line and after that to interpolate each pixel on this line.

Is there a function which tell me, if this pixel is red or not?

Thanks for help.






const GLfloat M = 200;

GLfloat vertices[][2] = { {0.0,M} , {-M,0.0},
{0.0,-2*M} , {2*M,0.0}};

void interpolate()
{
GLfloat xMin = vertices[0][0];
GLfloat yMin = vertices[0][1];
GLfloat xMax = xMin;
GLfloat yMax = yMin;

for(int i=1; i<4; i++)
{
if(xMin >= vertices[i][0])
xMin = vertices[i][0];
else if(xMax >= vertices[i][0])
xMax = vertices[i][0];

if(yMin >= vertices[i][1])
yMin = vertices[i][1];
else if(yMax >= vertices[i][1])
yMax = vertices[i][1];
}

GLint width = (int)(xMax-xMin);
GLint height = (int)(yMax-yMin);
GLint array[(int)(xMax-xMin)][(int)(yMax-yMin)];
glReadBuffer(GL_FRONT_LEFT);
glReadPixels((int)xMin,(int)yMin, width, height, GL_RED, GL_INT, array);

GLint numberOfIntersections = 0;

for(int i=0; i<width; i++)
for(int j=0; j<height; j++)
{
printf("%i\n", array[i][j]);
}
}

ZbuffeR
11-16-2007, 12:48 PM
Read this :
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/readpixels.html
The last parameter, called *pixels in the doc, is a pointer to an 1-D array of colors values from pixels that have been read from framebuffer.

Pixels are returned in row order from the lowest to the highest row, left to right in each row.

Use :
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1);
to not have trouble with alignment.

then, declare and index your array like this :
GLubyte array[width * height * 3]; // RGB 8bits

printf("%u\n", array[3*(i+j*width])+1); // +1 to be on the Green component, 0 for Red and +2 for Blue