00001 #include "Arrows.h"
00002 #include "Texture.h"
00003
00004 #include <QColorDialog>
00005
00006 Arrows::Arrows(QWidget* parent ) : QObject(parent)
00007 {
00008 m_flowData = NULL;
00009 m_pointSize = 2.0f;;
00010
00011 m_resolution = 1;
00012
00013 m_parent = parent;
00014
00015
00016 m_backgroundData = NULL;
00017
00018
00019 m_scalePoints = false;
00020
00021
00022 m_arrowColor = QColor(Qt::white);
00023
00025 connect(m_parent, SIGNAL(setPointSize(int)), this, SLOT(setPointSize(int)));
00026 connect(m_parent, SIGNAL(setResolution(int)), this, SLOT(setResolution(int)));
00027 connect(m_parent, SIGNAL(setActivePointScale(bool)), this, SLOT(setActivePointScale(bool)));
00028
00030 connect(m_parent, SIGNAL(setArrowColor()), this, SLOT(setColor()));
00031
00032
00033 m_psShader = new Shader();
00034 m_psShader->Load("sprite.vert", "sprite.frag");
00035 m_spriteTexLoc = glGetUniformLocation(m_psShader->GetProgram(), "SpriteTexture");
00036 m_angleLoc = glGetUniformLocation(m_psShader->GetProgram(), "angle");
00037
00038 m_psShader->Bind();
00039 glUniform1i( m_spriteTexLoc, 0 );
00040 m_psShader->Unbind();
00041 }
00042
00043
00044 Arrows::~Arrows()
00045 {
00046 delete m_psShader;
00047 }
00048
00049 void Arrows::setActiveBackground(int bg)
00050 {
00051
00052 if(bg == -1)
00053 {
00054 m_backgroundData = NULL;
00055 return;
00056 }
00057
00058
00059 int backgroundId = -1;
00060
00061 for(int i = 0; i < m_flowData->getChannelCount(); i++)
00062 {
00063 if(m_flowData->getIsScalar(i) == NORMALIZED)
00064 {
00065 backgroundId = i;
00066 break;
00067 }
00068 }
00069
00070 if(bg < m_flowData->getScalarChannelCount())
00071 {
00072 backgroundId += bg;
00073 m_backgroundData = m_flowData->getChannel(backgroundId);
00074 }
00075 else
00076 {
00077 m_backgroundData = NULL;
00078 }
00079 }
00080
00081 void Arrows::setResolution(int res)
00082 {
00083
00084 m_resolution = res;
00085 m_parent->update();
00086 }
00087
00088 void Arrows::setPointSize(int size)
00089 {
00090
00091 m_pointSize = (float) size;
00092 m_parent->update();
00093 }
00094
00095 void Arrows::setActivePointScale(bool ps)
00096 {
00097 m_scalePoints = ps;
00098 m_parent->update();
00099 }
00100
00101 void Arrows::setColor()
00102 {
00103 printf("Arrows: Set Color\n");
00104 QColor white = QColor(Qt::white);
00105
00106
00107 m_arrowColor = QColorDialog::getColor(white);
00108
00109
00110 if(!m_arrowColor.isValid())
00111 {
00112 printf("New Arrow Color not valid - set to white\n");
00113 m_arrowColor = white;
00114 }
00115 }
00116
00117
00118 void Arrows::setFlowData(FlowData* data)
00119 {
00120 if(data == NULL)
00121 {
00122 printf("No flow data set in class Arrows\n");
00123 return;
00124 }
00125
00127 m_flowData = data;
00129 m_geom = m_flowData->getGeometry();
00130
00132 m_dataDimX = m_geom->getDimX();
00133 m_dataDimY = m_geom->getDimY();
00134
00136 Texture texture;
00137
00138
00139 glGenTextures(1, &m_glyphTexID);
00140
00141
00142 m_glyphTexID = texture.CreateGlyph();
00143 }
00144
00145
00146 void Arrows::draw()
00147 {
00149 float xTick = 1.0f / (float) m_resolution;
00150 float yTick = 1.0f / (float) m_resolution;
00151
00153 float xRes = 0.0f;
00154 float yRes = 0.0f;
00155
00156 glPushMatrix();
00157
00158 float oldColor[4];
00159 glGetFloatv( GL_CURRENT_COLOR, oldColor );
00160
00162 glEnable(GL_BLEND);
00163 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
00164
00166 glColor3f(m_arrowColor.redF(), m_arrowColor.greenF(), m_arrowColor.blueF());
00167
00169 glActiveTexture(GL_TEXTURE0);
00170 glEnable(GL_TEXTURE_2D);
00171 glEnable(GL_POINT_SPRITE_ARB);
00172 glBindTexture(GL_TEXTURE_2D, m_glyphTexID);
00173 glTexEnvf(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
00174 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_NONE);
00175
00177 int index = 0;
00178 vec3 pos, vel;
00179 vec3 nullVel;
00180 FlowChannel* chX = m_flowData->getChannel(X);
00181 FlowChannel* chY = m_flowData->getChannel(Y);
00182
00184
00186 for(int y = 0; y < m_dataDimY; y++)
00187 {
00188
00189 index = y * m_dataDimX;
00190
00191
00192 pos = m_geom->getNormPos(index);
00193
00194
00195 if(pos[Y] < yRes)
00196 continue;
00197
00198
00199 yRes += yTick;
00200
00201
00202 xRes = 0.0f;
00203
00204 for(int x = 0; x < m_dataDimX; x++)
00205 {
00206
00207 index = y * m_dataDimX + x;
00208
00209
00210 pos = m_geom->getNormPos(index);
00211
00212
00213 if(pos[X] < xRes)
00214 {
00215 continue;
00216 }
00217
00218
00219 xRes += xTick;
00220
00222 vel = vec3(chX->getValue(index), chY->getValue(index), 0.0);
00223
00224
00225 if(vel == nullVel)
00226 continue;
00227
00229 GLfloat angle = atan2(vel[Y], vel[X]);
00230
00231 if((m_backgroundData != NULL) && (m_scalePoints == true))
00232 {
00233
00234 glPointSize(m_pointSize * m_backgroundData->getValue(index));
00235 }
00236 else
00237 {
00239 glPointSize(m_pointSize);
00240 }
00241 printOglError("m_pointSize");
00242
00244 m_psShader->Bind();
00245
00247 glUniform1f( m_angleLoc, angle);
00248
00249
00250 glBegin(GL_POINTS);
00251 glVertex2f(pos.v[X], pos.v[Y]);
00252 glEnd();
00253
00254 m_psShader->Unbind();
00255 }
00256 }
00257
00259 glColor4fv(oldColor);
00260
00261 glDisable(GL_POINT_SPRITE_ARB);
00262 glDisable(GL_TEXTURE_2D);
00263 glDisable(GL_BLEND);
00264
00265 glPopMatrix();
00266 }