Let's say I have data matrix 640x256 where each element is a float number within a range from -10 to 600. I want to display this matrix on the screen where each element gets assigned a color from RGB palette. OpenGL, C++ on RaspberryPi
What I currently do:
for (unsigned int i = 0; i < frameSize; ++i) { // frameSize = 640x256
float value = data[i];
if (value > maxVal) {
color = palette.back();
}
else if (value < minVal) {
color = palette.front();
}
else {
color = palette[((value - minVal) / (maxVal - minVal)) * (palette.size() - 1)];
}
imgData[3 * i] = color.R;
imgData[3 * i + 1] = color.G;
imgData[3 * i + 2] = color.B;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imgData);
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(width, 0);
glTexCoord2f(1, 1); glVertex2f(width, height);
glTexCoord2f(0, 1); glVertex2f(0, height);
}
glEnd();
palette is user-defined vector of RGB colors; minVal and maxVal are minimum and maximum value in data, but can be changed by user.
This code works correctly but image is colorized on CPU which can be slow for large data. Is it possible to pass float data as texture directly and colorize it with the palette using OpenGL on the output?
I am not asking how to optimize CPU-bounded loop. My question is if it is possible to completely remove it and do its task during OpenGL rendering.