source: branches/blt4/packages/vizservers/nanovis/VelocityArrowsSlice.cpp @ 2742

Last change on this file since 2742 was 2409, checked in by gah, 13 years ago

update from branch

File size: 15.3 KB
Line 
1#include <GL/glew.h>
2#ifdef _WIN32
3#include <windows.h>
4#endif
5#include <GL/gl.h>
6#include "VelocityArrowsSlice.h"
7#ifdef WIN32
8#include <GL/glaux.h>
9#else
10//#include <opencv/cv.h>
11//#include <opencv/highgui.h>
12#endif
13
14#ifdef USE_NANOVIS_LIB
15#include "global.h"
16#include "Nv.h"
17#include "R2/R2FilePath.h"
18#endif
19
20#define USE_VERTEX_BUFFER
21
22VelocityArrowsSlice::VelocityArrowsSlice()
23{
24    _enabled = false;
25        _context = cgCreateContext();
26        _vectorFieldGraphicsID = 0;
27        _vfXscale = _vfYscale = _vfZscale = 0;
28        _slicePos = 0.5f;
29        _vertexBufferGraphicsID = 0;
30        axis(2);
31        //_renderMode = GLYPHES;
32        _renderMode = LINES;
33       
34        _tickCountForMinSizeAxis = 10;
35
36        _queryVelocityFP =
37#ifdef USE_NANOVIS_LIB
38            LoadCgSourceProgram(_context, "queryvelocity.cg", CG_PROFILE_FP30, "main");
39#else
40            cgCreateProgramFromFile(_context, CG_SOURCE, "queryvelocity.cg",
41                                                        CG_PROFILE_FP30, "main", NULL);
42            cgGLLoadProgram(_queryVelocityFP);
43#endif
44        _qvVectorFieldParam = cgGetNamedParameter(_queryVelocityFP, "vfield");
45
46        _dirty = true;
47        _dirtySamplingPosition = true;
48        _dirtyRenderTarget = true;
49        _maxVelocityScale.x = _maxVelocityScale.y = _maxVelocityScale.z = 1.0f;
50
51        _renderTargetWidth = 128;
52        _renderTargetHeight = 128;
53        _velocities = new Vector3[_renderTargetWidth * _renderTargetHeight];
54
55        ::glGenBuffers(1, &_vertexBufferGraphicsID);
56        glBindBufferARB(GL_ARRAY_BUFFER_ARB, _vertexBufferGraphicsID);
57        glBufferDataARB(GL_ARRAY_BUFFER, _renderTargetWidth * _renderTargetHeight * 3 * sizeof(float),
58                0, GL_DYNAMIC_DRAW_ARB);
59        glBindBufferARB(GL_ARRAY_BUFFER, 0);
60        createRenderTarget();
61        _pointCount = 0;
62
63/*
64        _particleVP =
65#ifdef USE_NANOVIS_LIB
66                LoadCgSourceProgram(_context, "velocityslicevp.cg", CG_PROFILE_VP40, "vpmain");
67#else
68                cgCreateProgramFromFile(_context, CG_SOURCE, "velocityslicevp.cg",
69                                                        CG_PROFILE_VP40, "vpmain", NULL);
70                cgGLLoadProgram(_particleVP);
71#endif
72        _mvpParticleParam = cgGetNamedParameter(_particleVP, "mvp");
73        _mvParticleParam = cgGetNamedParameter(_particleVP, "modelview");
74        _mvTanHalfFOVParam = cgGetNamedParameter(_particleVP, "tanHalfFOV");
75
76        // TBD..
77        //_particleFP =
78#ifdef USE_NANOVIS_LIB
79//                LoadCgSourceProgram(_context, "velocityslicefp.cg", CG_PROFILE_FP40, "fpmain");
80#else
81                cgCreateProgramFromFile(_context, CG_SOURCE, "velocityslicefp.cg",
82                                                        CG_PROFILE_FP40, "fpmain", NULL);
83                cgGLLoadProgram(_particleFP);
84#endif
85        _vectorParticleParam = cgGetNamedParameter(_particleVP, "vfield");
86*/
87
88#ifdef USE_NANOVIS_LIB
89
90#ifdef WIN32
91        AUX_RGBImageRec *pTextureImage = auxDIBImageLoad("arrows.bmp");
92        _arrowsTex->setPixels(CF_RGB, DT_UBYTE, pTextureImage->sizeX, pTextureImage->sizeY, (void*) pTextureImage->data);
93
94        _arrowsTex = new Texture2D(pTextureImage->sizeX, pTextureImage->sizeY, GL_FLOAT, GL_LINEAR, 3, (void*) pTextureImage->data);
95
96        //delete pTextureImage;
97#else
98    /*
99      TRACE("test1\n");
100        const char *path = R2FilePath::getInstance()->getPath("arrows_flip2.png");
101        if (path) TRACE("test2 %s\n", path);
102        else TRACE("tt\n");
103        IplImage* pTextureImage = cvLoadImage(path);
104        TRACE("test3\n");
105        if (pTextureImage)
106        {
107            TRACE("file(%s) has been loaded\n", path);
108            _arrowsTex = new Texture2D(pTextureImage->width, pTextureImage->height, GL_FLOAT, GL_LINEAR, 3, (float*) pTextureImage->imageData);
109            TRACE("file(%s) has been loaded\n", path);
110            //cvReleaseImage(&pTextureImage);
111        }
112        else
113        {
114            TRACE("not found\n");
115        }
116        if (path) delete [] path;
117*/
118
119#endif
120
121#else
122        _arrowsTex = new Texture2D();
123        _arrowsTex->setWrapS(TW_MIRROR);
124        _arrowsTex->setWrapT(TW_MIRROR);
125#ifdef WIN32
126        AUX_RGBImageRec *pTextureImage = auxDIBImageLoad("arrows.bmp");
127        _arrowsTex->setPixels(CF_RGB, DT_UBYTE, pTextureImage->sizeX, pTextureImage->sizeY, (void*) pTextureImage->data);
128
129        //delete pTextureImage;
130#else
131        IplImage* pTextureImage = cvLoadImage("arrows_flip2.png");
132        _arrowsTex->setPixels(CF_RGB, DT_UBYTE, pTextureImage->width, pTextureImage->height, (void*) pTextureImage->imageData);
133
134        //cvReleaseImage(&pTextureImage);
135#endif
136
137#endif
138    _arrowColor.set(1, 1, 0);
139
140    TRACE("test1\n");
141}
142
143VelocityArrowsSlice::~VelocityArrowsSlice()
144{
145    cgDestroyProgram(_queryVelocityFP);
146    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbo);
147    glDeleteTextures(1, &_tex);
148    glDeleteFramebuffersEXT(1, &_fbo);
149
150#ifdef USE_NANOVIS_LIB
151        delete _arrowsTex;
152#else
153        _arrowsTex->unref();
154#endif
155        cgDestroyProgram(_particleFP);
156        cgDestroyProgram(_particleVP);
157
158        glDeleteBuffers(1, &_vertexBufferGraphicsID);
159        delete [] _velocities;
160}
161
162void VelocityArrowsSlice::createRenderTarget()
163{
164        glGenFramebuffersEXT(1, &_fbo);
165    glGenTextures(1, &_tex);
166
167    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbo);
168   
169        glViewport(0, 0, _renderTargetWidth, _renderTargetHeight);
170        glMatrixMode(GL_PROJECTION);
171        glLoadIdentity();
172        glOrtho(0, _renderTargetWidth, 0, _renderTargetHeight, -10.0f, 10.0f);
173        glMatrixMode(GL_MODELVIEW);
174        glLoadIdentity();
175
176    glBindTexture(GL_TEXTURE_RECTANGLE_NV, _tex);
177    glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
178    glTexParameterf(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
179        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
180        glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
181
182    glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV,
183        _renderTargetWidth, _renderTargetHeight, 0, GL_RGBA, GL_FLOAT, NULL);
184
185 
186        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
187                GL_TEXTURE_RECTANGLE_NV, _tex, 0);
188
189    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
190}
191
192void VelocityArrowsSlice::axis(int axis)
193{
194        _axis = axis;
195        switch (_axis)
196        {
197        case 0 :
198                _projectionVector.x = 0;
199                _projectionVector.y = 1;
200                _projectionVector.z = 1;
201                break;
202        case 1 :
203                _projectionVector.x = 1;
204                _projectionVector.y = 0;
205                _projectionVector.z = 1;
206                break;
207        case 2 :
208                _projectionVector.x = 1;
209                _projectionVector.y = 1;
210                _projectionVector.z = 0;
211                break;
212        }
213        _dirtySamplingPosition = true;
214        _dirtyRenderTarget = true;
215    //_dirty = true;
216}
217
218
219void VelocityArrowsSlice::queryVelocity()
220{
221        if (!_enabled) return;
222
223        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbo);
224        glDisable(GL_DEPTH_TEST);
225        cgGLBindProgram(_queryVelocityFP);
226        cgGLEnableProfile(CG_PROFILE_FP30);
227        cgGLSetTextureParameter(_qvVectorFieldParam, _vectorFieldGraphicsID);
228        cgGLEnableTextureParameter(_qvVectorFieldParam);
229
230        glPushAttrib(GL_VIEWPORT_BIT);
231        glViewport(0, 0, _renderTargetWidth, _renderTargetHeight);
232        glMatrixMode(GL_PROJECTION);
233        glPushMatrix();
234        glLoadIdentity();
235        glOrtho(0, _renderTargetWidth, 0, _renderTargetHeight, -10.0f, 10.0f);
236        glMatrixMode(GL_MODELVIEW);
237        glPushMatrix();
238        glLoadIdentity();
239        glBegin(GL_POINTS);
240
241        for (unsigned int index = 0; index < _samplingPositions.size(); ++index)
242        {
243                glMultiTexCoord3f(0, _samplingPositions[index].x,_samplingPositions[index].y,_samplingPositions[index].z);
244                glVertex2f(index % _renderTargetWidth,
245                        index / _renderTargetHeight);
246        }
247
248        glEnd();
249
250        glDisable(GL_TEXTURE_RECTANGLE_NV);
251        cgGLDisableTextureParameter(_qvVectorFieldParam);
252        cgGLDisableProfile(CG_PROFILE_FP30);
253
254        glReadPixels(0, 0, _renderTargetWidth, _renderTargetHeight, GL_RGB, GL_FLOAT, _velocities);
255
256        glMatrixMode(GL_PROJECTION);
257        glPopMatrix();
258        glMatrixMode(GL_MODELVIEW);
259        glPopMatrix();
260        glPopAttrib();
261
262        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
263}
264
265void VelocityArrowsSlice::render()
266{
267    //if (!_enabled || (_vectorFieldGraphicsID == 0)) return;   
268    if (!_enabled) return;
269   
270    if (_dirty) {
271        computeSamplingTicks();
272        queryVelocity();
273        _dirty = false;
274    }
275   
276   
277    glPushMatrix();
278   
279    glScalef(_vfXscale,_vfYscale, _vfZscale);
280    glTranslatef(-0.5f, -0.5f, -0.5f);
281    if (_renderMode == LINES) {
282       
283        glDisable(GL_TEXTURE_2D);
284        glLineWidth(2.0);
285        glColor3f(_arrowColor.x, _arrowColor.y, _arrowColor.z);
286        glBegin(GL_LINES);
287        Vector3 pos;
288        Vector3 pos2;
289        Vector3 vel(1, 0, 0);
290        Vector3 v, v2;
291        if (_axis == 2) {
292            int index = 0;
293            for (int y = 1; y <= _tickCountY; ++y) {
294                for (int x = 1; x <= _tickCountX; ++x, ++index) {
295                    pos = this->_samplingPositions[index];
296                    pos2 = this->_velocities[index].scale(_projectionVector).scale(_maxVelocityScale) + pos;
297                    glVertex3f(pos.x, pos.y, pos.z);
298                    glVertex3f(pos2.x, pos2.y, pos2.z);
299                    /*v = pos - pos2;
300                     
301                   
302                    v2.x = 1;
303                    v2.y = 1;
304                    v2.z = (-(x1-x2)-(y1-y2))/(z1-z2)
305                    adj = v.length() / (4 * sqrt((x3^2)+(y3^2)+(z3^2)));
306                    x3 *= adj
307                    y3 *= adj
308                    z3 *= adj
309                    */
310                   
311                }
312            }
313        } else if (_axis == 1) {
314            int index = 0;
315            for (int z = 1; z <= _tickCountZ; ++z)
316                for (int x = 1; x <= _tickCountX; ++x, ++index) {
317                    pos = _samplingPositions[index];
318                    pos2 = this->_velocities[index].scale(_projectionVector).scale(_maxVelocityScale) + pos;
319                   
320                    glVertex3f(pos.x, pos.y, pos.z);
321                    glVertex3f(pos2.x, pos2.y, pos2.z);
322                }
323        } else if (_axis == 0) {
324            int index = 0;
325            for (int z = 1; z <= _tickCountZ; ++z)
326                for (int y = 1; y <= _tickCountY; ++y, ++index) {
327                    pos = _samplingPositions[index];
328                    pos2 = this->_velocities[index].scale(_projectionVector).scale(_maxVelocityScale) + pos;
329                   
330                    glVertex3f(pos.x, pos.y, pos.z);
331                    glVertex3f(pos2.x, pos2.y, pos2.z);
332                }
333        }
334       
335        glEnd();
336        glLineWidth(1.0);
337    } else {
338        glColor3f(_arrowColor.x, _arrowColor.y, _arrowColor.z);
339        glEnable(GL_BLEND);
340        glBlendFunc(GL_SRC_ALPHA, GL_ONE);
341        glEnable(GL_POINT_SPRITE_NV);
342        glPointSize(20);                               
343        glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
344       
345#ifdef USE_NANOVIS_LIB
346        _arrowsTex->activate();
347#else
348        _arrowsTex->bind(0);
349        glEnable(GL_TEXTURE_2D);
350#endif
351        glPointParameterfARB( GL_POINT_FADE_THRESHOLD_SIZE_ARB, 0.0f );
352       
353        glPointParameterfARB( GL_POINT_SIZE_MIN_ARB, 1.0f);
354        glPointParameterfARB( GL_POINT_SIZE_MAX_ARB, 100.0f);
355        glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
356       
357        cgGLBindProgram(_particleVP);
358        cgGLBindProgram(_particleFP);
359        cgGLEnableProfile(CG_PROFILE_VP40);
360        cgGLEnableProfile(CG_PROFILE_FP40);
361       
362        cgGLSetTextureParameter(_vectorParticleParam, _vectorFieldGraphicsID);
363        cgGLEnableTextureParameter(_vectorParticleParam);
364       
365       
366        //cgSetParameter1f(_mvTanHalfFOVParam, -tan(_fov * 0.5) * _screenHeight * 0.5);
367       
368        cgGLSetStateMatrixParameter(_mvpParticleParam,
369                                    CG_GL_MODELVIEW_PROJECTION_MATRIX,
370                                    CG_GL_MATRIX_IDENTITY);
371        cgGLSetStateMatrixParameter(_mvParticleParam,
372                                    CG_GL_MODELVIEW_MATRIX,
373                                    CG_GL_MATRIX_IDENTITY);
374       
375        glEnableClientState(GL_VERTEX_ARRAY);
376        glBindBufferARB(GL_ARRAY_BUFFER_ARB, _vertexBufferGraphicsID);
377        glVertexPointer(3, GL_FLOAT, 0, 0);
378        //glEnableClientState(GL_COLOR_ARRAY);
379       
380        // TBD..
381        glDrawArrays(GL_POINTS, 0, _pointCount);
382        glPointSize(1);
383        glDrawArrays(GL_POINTS, 0, _pointCount);
384       
385        glDisableClientState(GL_VERTEX_ARRAY);
386       
387        cgGLDisableProfile(CG_PROFILE_VP40);
388        cgGLDisableProfile(CG_PROFILE_FP40);
389       
390        glDepthMask(GL_TRUE);
391       
392        glDisable(GL_POINT_SPRITE_NV);
393        glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
394       
395        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
396        glDisable(GL_BLEND);
397#ifdef USE_NANOVIS_LIB
398        _arrowsTex->deactivate();
399#else
400        _arrowsTex->unbind();
401#endif
402    }
403    glPopMatrix();
404}
405
406
407void
408VelocityArrowsSlice::vectorField(unsigned int vfGraphicsID,
409                                 float xScale, float yScale, float zScale)
410{
411    _vectorFieldGraphicsID = vfGraphicsID;
412    _vfXscale = xScale;
413    _vfYscale = yScale;
414    _vfZscale = zScale;
415
416    //_dirty = true;
417}
418
419void VelocityArrowsSlice::tickCountForMinSizeAxis(int tickCount)
420{
421        _tickCountForMinSizeAxis = tickCount;
422       
423    //_dirty = true;
424}
425
426
427void VelocityArrowsSlice::computeSamplingTicks()
428{
429        if (_vfXscale < _vfYscale)
430        {
431                if (_vfXscale < _vfZscale)
432                {
433                        // vfXscale
434                        _tickCountX = _tickCountForMinSizeAxis;
435                       
436                        float step = _vfXscale / (_tickCountX + 1);
437                       
438                        _tickCountY = (int) (_vfYscale/step);
439                        _tickCountZ = (int) (_vfZscale/step);
440                        //vscalex = 1;
441                        //vscaley = _vfXscale / _vfXscale;
442                        //vscalez = _vfZscale / _vfXscale;
443                }
444                else
445                {
446                        // vfZscale
447                        _tickCountZ = _tickCountForMinSizeAxis;
448
449                        float step = _vfZscale / (_tickCountZ + 1);
450                        _tickCountX = (int) (_vfXscale/step);
451                        _tickCountY = (int) (_vfYscale/step);
452                        //vscalex = _vfXscale / _vfZscale;
453                        //vscaley = _vfYscale / _vfZscale;
454                        //vscalez = 1;
455                }
456
457        }
458        else
459        {
460                if (_vfYscale < _vfZscale)
461                {
462                        // _vfYscale
463                        _tickCountY = _tickCountForMinSizeAxis;
464
465                        float step = _vfYscale / (_tickCountY + 1);
466                        _tickCountX = (int) (_vfXscale/step);
467                        _tickCountZ = (int) (_vfZscale/step);
468
469                        //vscalex = _vfXscale / _vfYscale;
470                        //vscaley = 1;
471                        //vscalez = _vfZscale / _vfYscale;
472                }
473                else
474                {
475                        // vfZscale
476                        _tickCountZ = _tickCountForMinSizeAxis;
477                       
478                        float step = _vfZscale / (_tickCountZ + 1);
479                        _tickCountX = (int) (_vfXscale/step);
480                        _tickCountY = (int) (_vfYscale/step);
481
482                        //vscalex = _vfXscale / _vfZscale;
483                        //vscaley = _vfYscale / _vfZscale;
484                        //vscalez = 1;
485               
486                       
487                }
488        }
489       
490        _maxVelocityScale.x = 1.0f / _tickCountX * 0.8f;
491        _maxVelocityScale.y = 1.0f / _tickCountY * 0.8f;
492        _maxVelocityScale.z = 1.0f / _tickCountZ * 0.8f;
493       
494        int pointCount = _tickCountX * _tickCountY * _tickCountZ;
495        if (_pointCount != pointCount)
496        {
497                _samplingPositions.clear();
498                _samplingPositions.reserve(pointCount);
499                _pointCount = pointCount;
500        }
501
502        if (_renderMode == LINES)
503        {
504                Vector3 pos;
505                if (_axis == 2)
506                {
507                        for (int y = 1; y <= _tickCountY; ++y)
508                                for (int x = 1; x <= _tickCountX; ++x)
509                                {
510                                        pos.x = (1.0f / (_tickCountX + 1)) * x;
511                                        pos.y = (1.0f / (_tickCountY + 1)) * y;
512                                        pos.z = _slicePos;
513                                        _samplingPositions.push_back(pos);
514       
515                                }
516                }
517                else if (_axis == 1)
518                {
519                        for (int z = 1; z <= _tickCountZ; ++z)
520                                for (int x = 1; x <= _tickCountX; ++x)
521                                {
522                                        pos.x = (1.0f / (_tickCountX + 1)) * x;
523                                        pos.y = _slicePos;
524                                        pos.z = (1.0f / (_tickCountZ + 1)) * z;
525                                        _samplingPositions.push_back(pos);
526                                }
527                }
528                else if (_axis == 0)
529                {
530                        for (int z = 1; z <= _tickCountZ; ++z)
531                                for (int y = 1; y <= _tickCountY; ++y)
532                                {
533                                        pos.x = _slicePos;
534                                        pos.y = (1.0f / (_tickCountY + 1)) * y;
535                                        pos.z = (1.0f / (_tickCountZ + 1)) * z;
536                                        _samplingPositions.push_back(pos);
537                                }
538                }
539        }
540        else
541        {
542                glBindBufferARB(GL_ARRAY_BUFFER_ARB, _vertexBufferGraphicsID);
543                Vector3* pinfo = (Vector3*) glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY_ARB);
544
545                Vector3 pos;
546                if (_axis == 2)
547                {
548                        for (int y = 1; y <= _tickCountY; ++y)
549                                for (int x = 1; x <= _tickCountX; ++x)
550                                {
551                                        pos.x = (1.0f / (_tickCountX + 1)) * x;
552                                        pos.y = (1.0f / (_tickCountY + 1)) * y;
553                                        pos.z = _slicePos;
554
555                                        *pinfo = pos;
556                                        ++pinfo;
557                        }
558                }
559                else if (_axis == 1)
560                {
561                        for (int z = 1; z <= _tickCountZ; ++z)
562                                for (int x = 1; x <= _tickCountX; ++x)
563                                {
564                                        pos.x = (1.0f / (_tickCountX + 1)) * x;
565                                        pos.y = _slicePos;
566                                        pos.z = (1.0f / (_tickCountZ + 1)) * z;
567       
568                                        *pinfo = pos;
569                                        ++pinfo;
570                                }
571                }
572                else if (_axis == 0)
573                {
574                        for (int z = 1; z <= _tickCountZ; ++z)
575                                for (int y = 1; y <= _tickCountY; ++y)
576                                {
577                                        pos.x = _slicePos;
578                                        pos.y = (1.0f / (_tickCountY + 1)) * y;
579                                        pos.z = (1.0f / (_tickCountZ + 1)) * z;
580                                       
581                                        *pinfo = pos;
582                                        ++pinfo;
583                                }
584                }
585
586                glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
587        }
588}
589
590void VelocityArrowsSlice::slicePos(float pos)
591{
592    _slicePos = pos;
593    _dirty = true;
594}
595
Note: See TracBrowser for help on using the repository browser.