source: trunk/packages/vizservers/nanovis/VelocityArrowsSlice.cpp @ 1490

Last change on this file since 1490 was 1490, checked in by vrinside, 15 years ago

disabled VelocityArrowSlice? as the initial state

File size: 15.9 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        printf("test1\n");
100        const char *path = R2FilePath::getInstance()->getPath("arrows_flip2.png");
101        if (path) printf("test2 %s\n", path);
102        else printf("tt\n");
103        IplImage* pTextureImage = cvLoadImage(path);
104        printf("test3\n");
105        if (pTextureImage)
106        {
107            printf("file(%s) has been loaded\n", path);
108            _arrowsTex = new Texture2D(pTextureImage->width, pTextureImage->height, GL_FLOAT, GL_LINEAR, 3, (float*) pTextureImage->imageData);
109            printf("file(%s) has been loaded\n", path);
110            //cvReleaseImage(&pTextureImage);
111        }
112        else
113        {
114            printf("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    printf("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        {
272                computeSamplingTicks();
273                queryVelocity();
274                _dirty = false;
275        }
276
277
278        glPushMatrix();
279       
280        glScalef(_vfXscale,_vfYscale, _vfZscale);
281        glTranslatef(-0.5f, -0.5f, -0.5f);
282        if (_renderMode == LINES)
283        {
284
285                glDisable(GL_TEXTURE_2D);
286                glLineWidth(2.0);
287                glColor3f(_arrowColor.x, _arrowColor.y, _arrowColor.z);
288                glBegin(GL_LINES);
289                Vector3 pos;
290                Vector3 pos2;
291                Vector3 vel(1, 0, 0);
292                Vector3 v, v2;
293                if (_axis == 2)
294                {
295                        int index = 0;
296                        for (int y = 1; y <= _tickCountY; ++y)
297                                for (int x = 1; x <= _tickCountX; ++x, ++index)
298                                {
299                                        pos = this->_samplingPositions[index];
300                                        pos2 = this->_velocities[index].scale(_projectionVector).scale(_maxVelocityScale) + pos;
301                                        glVertex3f(pos.x, pos.y, pos.z);
302                                        glVertex3f(pos2.x, pos2.y, pos2.z);
303                                        /*v = pos - pos2;
304                                       
305                                       
306                                        v2.x = 1;
307                                        v2.y = 1;
308                                        v2.z = (-(x1-x2)-(y1-y2))/(z1-z2)
309                                        adj = v.length() / (4 * sqrt((x3^2)+(y3^2)+(z3^2)));
310                                        x3 *= adj
311                                        y3 *= adj
312                                        z3 *= adj
313                                        */
314
315                                }
316                }
317                else if (_axis == 1)
318                {
319                        int index = 0;
320                        for (int z = 1; z <= _tickCountZ; ++z)
321                                for (int x = 1; x <= _tickCountX; ++x, ++index)
322                                {
323                                        pos = _samplingPositions[index];
324                                        pos2 = this->_velocities[index].scale(_projectionVector).scale(_maxVelocityScale) + pos;
325
326                                        glVertex3f(pos.x, pos.y, pos.z);
327                                        glVertex3f(pos2.x, pos2.y, pos2.z);
328                                }
329                }
330                else if (_axis == 0)
331                {
332                        int index = 0;
333                        for (int z = 1; z <= _tickCountZ; ++z)
334                                for (int y = 1; y <= _tickCountY; ++y, ++index)
335                                {
336                                        pos = _samplingPositions[index];
337                                        pos2 = this->_velocities[index].scale(_projectionVector).scale(_maxVelocityScale) + pos;
338
339                                        glVertex3f(pos.x, pos.y, pos.z);
340                                        glVertex3f(pos2.x, pos2.y, pos2.z);
341                                }
342                }
343               
344                glEnd();
345                glLineWidth(1.0);
346        }
347        else
348        {
349                glColor3f(_arrowColor.x, _arrowColor.y, _arrowColor.z);
350                glEnable(GL_BLEND);
351                glBlendFunc(GL_SRC_ALPHA, GL_ONE);
352                glEnable(GL_POINT_SPRITE_NV);
353                glPointSize(20);                               
354                glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
355
356#ifdef USE_NANOVIS_LIB
357                _arrowsTex->activate();
358#else
359                _arrowsTex->bind(0);
360                glEnable(GL_TEXTURE_2D);
361#endif
362                glPointParameterfARB( GL_POINT_FADE_THRESHOLD_SIZE_ARB, 0.0f );
363
364                glPointParameterfARB( GL_POINT_SIZE_MIN_ARB, 1.0f);
365                glPointParameterfARB( GL_POINT_SIZE_MAX_ARB, 100.0f);
366                glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
367
368                cgGLBindProgram(_particleVP);
369                cgGLBindProgram(_particleFP);
370                cgGLEnableProfile(CG_PROFILE_VP40);
371                cgGLEnableProfile(CG_PROFILE_FP40);
372
373                cgGLSetTextureParameter(_vectorParticleParam, _vectorFieldGraphicsID);
374                cgGLEnableTextureParameter(_vectorParticleParam);
375
376
377                //cgSetParameter1f(_mvTanHalfFOVParam, -tan(_fov * 0.5) * _screenHeight * 0.5);
378               
379                cgGLSetStateMatrixParameter(_mvpParticleParam,
380                                                CG_GL_MODELVIEW_PROJECTION_MATRIX,
381                                                CG_GL_MATRIX_IDENTITY);
382                cgGLSetStateMatrixParameter(_mvParticleParam,
383                                                CG_GL_MODELVIEW_MATRIX,
384                                                CG_GL_MATRIX_IDENTITY);
385
386                glEnableClientState(GL_VERTEX_ARRAY);
387                glBindBufferARB(GL_ARRAY_BUFFER_ARB, _vertexBufferGraphicsID);
388                glVertexPointer(3, GL_FLOAT, 0, 0);
389                //glEnableClientState(GL_COLOR_ARRAY);
390
391                // TBD..
392                glDrawArrays(GL_POINTS, 0, _pointCount);
393                glPointSize(1);
394                glDrawArrays(GL_POINTS, 0, _pointCount);
395
396                glDisableClientState(GL_VERTEX_ARRAY);
397               
398                cgGLDisableProfile(CG_PROFILE_VP40);
399                cgGLDisableProfile(CG_PROFILE_FP40);
400
401                glDepthMask(GL_TRUE);
402               
403                glDisable(GL_POINT_SPRITE_NV);
404                glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
405
406                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
407                glDisable(GL_BLEND);
408#ifdef USE_NANOVIS_LIB
409                _arrowsTex->deactivate();
410#else
411                _arrowsTex->unbind();
412#endif
413        }
414        glPopMatrix();
415}
416
417void VelocityArrowsSlice::enabled(bool e)
418{
419        _enabled = e;
420}
421
422void VelocityArrowsSlice::vectorField(unsigned int vfGraphicsID,
423                                float xScale, float yScale, float zScale)
424{
425    _vectorFieldGraphicsID = vfGraphicsID;
426    _vfXscale = xScale;
427    _vfYscale = yScale;
428    _vfZscale = zScale;
429
430    //_dirty = true;
431}
432
433void VelocityArrowsSlice::tickCountForMinSizeAxis(int tickCount)
434{
435        _tickCountForMinSizeAxis = tickCount;
436       
437    //_dirty = true;
438}
439
440
441void VelocityArrowsSlice::computeSamplingTicks()
442{
443        if (_vfXscale < _vfYscale)
444        {
445                if (_vfXscale < _vfZscale)
446                {
447                        // vfXscale
448                        _tickCountX = _tickCountForMinSizeAxis;
449                       
450                        float step = _vfXscale / (_tickCountX + 1);
451                       
452                        _tickCountY = (int) (_vfYscale/step);
453                        _tickCountZ = (int) (_vfZscale/step);
454                        //vscalex = 1;
455                        //vscaley = _vfXscale / _vfXscale;
456                        //vscalez = _vfZscale / _vfXscale;
457                }
458                else
459                {
460                        // vfZscale
461                        _tickCountZ = _tickCountForMinSizeAxis;
462
463                        float step = _vfZscale / (_tickCountZ + 1);
464                        _tickCountX = (int) (_vfXscale/step);
465                        _tickCountY = (int) (_vfYscale/step);
466                        //vscalex = _vfXscale / _vfZscale;
467                        //vscaley = _vfYscale / _vfZscale;
468                        //vscalez = 1;
469                }
470
471        }
472        else
473        {
474                if (_vfYscale < _vfZscale)
475                {
476                        // _vfYscale
477                        _tickCountY = _tickCountForMinSizeAxis;
478
479                        float step = _vfYscale / (_tickCountY + 1);
480                        _tickCountX = (int) (_vfXscale/step);
481                        _tickCountZ = (int) (_vfZscale/step);
482
483                        //vscalex = _vfXscale / _vfYscale;
484                        //vscaley = 1;
485                        //vscalez = _vfZscale / _vfYscale;
486                }
487                else
488                {
489                        // vfZscale
490                        _tickCountZ = _tickCountForMinSizeAxis;
491                       
492                        float step = _vfZscale / (_tickCountZ + 1);
493                        _tickCountX = (int) (_vfXscale/step);
494                        _tickCountY = (int) (_vfYscale/step);
495
496                        //vscalex = _vfXscale / _vfZscale;
497                        //vscaley = _vfYscale / _vfZscale;
498                        //vscalez = 1;
499               
500                       
501                }
502        }
503       
504        _maxVelocityScale.x = 1.0f / _tickCountX * 0.8f;
505        _maxVelocityScale.y = 1.0f / _tickCountY * 0.8f;
506        _maxVelocityScale.z = 1.0f / _tickCountZ * 0.8f;
507       
508        int pointCount = _tickCountX * _tickCountY * _tickCountZ;
509        if (_pointCount != pointCount)
510        {
511                _samplingPositions.clear();
512                _samplingPositions.reserve(pointCount);
513                _pointCount = pointCount;
514        }
515
516        if (_renderMode == LINES)
517        {
518                Vector3 pos;
519                if (_axis == 2)
520                {
521                        for (int y = 1; y <= _tickCountY; ++y)
522                                for (int x = 1; x <= _tickCountX; ++x)
523                                {
524                                        pos.x = (1.0f / (_tickCountX + 1)) * x;
525                                        pos.y = (1.0f / (_tickCountY + 1)) * y;
526                                        pos.z = _slicePos;
527                                        _samplingPositions.push_back(pos);
528       
529                                }
530                }
531                else if (_axis == 1)
532                {
533                        for (int z = 1; z <= _tickCountZ; ++z)
534                                for (int x = 1; x <= _tickCountX; ++x)
535                                {
536                                        pos.x = (1.0f / (_tickCountX + 1)) * x;
537                                        pos.y = _slicePos;
538                                        pos.z = (1.0f / (_tickCountZ + 1)) * z;
539                                        _samplingPositions.push_back(pos);
540                                }
541                }
542                else if (_axis == 0)
543                {
544                        for (int z = 1; z <= _tickCountZ; ++z)
545                                for (int y = 1; y <= _tickCountY; ++y)
546                                {
547                                        pos.x = _slicePos;
548                                        pos.y = (1.0f / (_tickCountY + 1)) * y;
549                                        pos.z = (1.0f / (_tickCountZ + 1)) * z;
550                                        _samplingPositions.push_back(pos);
551                                }
552                }
553        }
554        else
555        {
556                glBindBufferARB(GL_ARRAY_BUFFER_ARB, _vertexBufferGraphicsID);
557                Vector3* pinfo = (Vector3*) glMapBufferARB(GL_ARRAY_BUFFER, GL_WRITE_ONLY_ARB);
558
559                Vector3 pos;
560                if (_axis == 2)
561                {
562                        for (int y = 1; y <= _tickCountY; ++y)
563                                for (int x = 1; x <= _tickCountX; ++x)
564                                {
565                                        pos.x = (1.0f / (_tickCountX + 1)) * x;
566                                        pos.y = (1.0f / (_tickCountY + 1)) * y;
567                                        pos.z = _slicePos;
568
569                                        *pinfo = pos;
570                                        ++pinfo;
571                        }
572                }
573                else if (_axis == 1)
574                {
575                        for (int z = 1; z <= _tickCountZ; ++z)
576                                for (int x = 1; x <= _tickCountX; ++x)
577                                {
578                                        pos.x = (1.0f / (_tickCountX + 1)) * x;
579                                        pos.y = _slicePos;
580                                        pos.z = (1.0f / (_tickCountZ + 1)) * z;
581       
582                                        *pinfo = pos;
583                                        ++pinfo;
584                                }
585                }
586                else if (_axis == 0)
587                {
588                        for (int z = 1; z <= _tickCountZ; ++z)
589                                for (int y = 1; y <= _tickCountY; ++y)
590                                {
591                                        pos.x = _slicePos;
592                                        pos.y = (1.0f / (_tickCountY + 1)) * y;
593                                        pos.z = (1.0f / (_tickCountZ + 1)) * z;
594                                       
595                                        *pinfo = pos;
596                                        ++pinfo;
597                                }
598                }
599
600                glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
601        }
602}
603
604void VelocityArrowsSlice::slicePos(float pos)
605{
606    _slicePos = pos;
607    _dirty = true;
608}
609
Note: See TracBrowser for help on using the repository browser.