00001
00008 #include <assert.h>
00009 #include "TransferTexture.h"
00010
00011
00012 TransferTexture::TransferTexture() : tex(0), boundUnit(-1) {}
00013
00014
00015 TransferTexture::~TransferTexture()
00016 {
00017 if(tex)
00018 glDeleteTextures(1, &tex);
00019 }
00020
00021
00022 bool TransferTexture::Load(const char *name)
00023 {
00024 return true;
00025 }
00026
00027
00028 bool TransferTexture::CreateFunction(const std::vector<PlotPoint> &red,
00029 const std::vector<PlotPoint> &green,
00030 const std::vector<PlotPoint> &blue,
00031 const std::vector<PlotPoint> &alpha)
00032 {
00033 float color[256 * 4];
00034
00035 if( !(Interpolate(red, color , 4) &&
00036 Interpolate(green, color + 1, 4) &&
00037 Interpolate(blue, color + 2, 4) &&
00038 Interpolate(alpha, color + 3, 4)) )
00039 {
00040 return false;
00041 }
00042
00043 if(tex)
00044 glDeleteTextures(1, &tex);
00045
00046 glGenTextures(1, &tex);
00047 glBindTexture(GL_TEXTURE_1D, tex);
00048 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00049 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00050 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00051 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_FLOAT, (GLvoid*)color);
00052 glBindTexture(GL_TEXTURE_1D, 0);
00053
00054 return true;
00055 }
00056
00057
00058 bool TransferTexture::Interpolate(const std::vector<PlotPoint> &plot, float *color, int stride)
00059 {
00060 int count = (int)plot.size();
00061
00062 int min = plot[0].dataValue;
00063 int max = plot[count - 1].dataValue;
00064 int len = max - min;
00065
00066 if(max < 255 || min > 0)
00067 return false;
00068
00069 int curr = 0;
00070 int n = (int)(count - 1);
00071 for(int i = 0; i < n; i++)
00072 {
00073 int amt = plot[i + 1].dataValue - plot[i].dataValue;
00074
00075 float c0 = plot[i].value;
00076 float dc = plot[i + 1].value - c0;
00077
00078 for(int j = 0; j < amt; j++)
00079 {
00080 float t = (float)j / amt;
00081 color[curr] = c0 + dc * t;
00082
00083 curr = curr + stride;
00084 }
00085 }
00086
00087 color[curr] = plot[count - 1].value;
00088
00089 return true;
00090 }
00091
00092
00093 void TransferTexture::Bind(int unit)
00094 {
00095 if(unit < 0 || unit > GL_MAX_TEXTURE_UNITS)
00096 return;
00097
00098 if(boundUnit != -1)
00099 return;
00100
00101 glActiveTexture(GL_TEXTURE0 + unit);
00102 glBindTexture(GL_TEXTURE_1D, tex);
00103
00104 boundUnit = unit;
00105 }
00106
00107
00108 void TransferTexture::UnBind()
00109 {
00110 if(boundUnit != -1)
00111 {
00112 glActiveTexture(GL_TEXTURE0 + boundUnit);
00113 glBindTexture(GL_TEXTURE_1D, 0);
00114
00115 boundUnit = -1;
00116 }
00117 }