source: nanovis/trunk/nanovis.cpp @ 5722

Last change on this file since 5722 was 5722, checked in by ldelgass, 9 years ago

Set flags to update ranges when deleting objects

  • Property svn:eol-style set to native
File size: 25.8 KB
RevLine 
[2798]1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
[226]2/*
3 * ----------------------------------------------------------------------
4 * Nanovis: Visualization of Nanoelectronics Data
5 *
6 * ======================================================================
7 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
[427]8 *           Michael McLennan <mmclennan@purdue.edu>
[226]9 *           Purdue Rendering and Perceptualization Lab (PURPL)
10 *
[3502]11 *  Copyright (c) 2004-2013  HUBzero Foundation, LLC
[226]12 *
13 *  See the file "license.terms" for information on usage and
14 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 * ======================================================================
16 */
[379]17
[391]18#include <sys/time.h>
[3605]19#include <cassert>
[2831]20#include <cstdio>
[4949]21#include <cmath> // for fmod()
[5703]22#include <cfloat>
[2827]23
[3465]24#include <GL/glew.h>
25#include <GL/glut.h>
26
27#include <graphics/RenderContext.h>
[3630]28#include <vrmath/BBox.h>
[3492]29#include <vrmath/Vector3f.h>
[2831]30
[3465]31#include <util/FilePath.h>
32#include <util/Fonts.h>
33
[915]34#include <BMPImageLoaderImpl.h>
35#include <ImageLoaderFactory.h>
[848]36
[2877]37#include "config.h"
[2831]38#include "nanovis.h"
[3605]39#include "nanovisServer.h"
[2831]40#include "define.h"
41
[3597]42#include "Flow.h"
[2831]43#include "Grid.h"
44#include "HeightMap.h"
[3611]45#include "Camera.h"
46#include "LIC.h"
[3612]47#include "Shader.h"
[3605]48#include "OrientationIndicator.h"
[2831]49#include "PlaneRenderer.h"
[3605]50#include "PPMWriter.h"
51#include "Texture2D.h"
[2831]52#include "Trace.h"
[3605]53#include "TransferFunction.h"
[2831]54#include "VelocityArrowsSlice.h"
[3605]55#include "Volume.h"
[2831]56#include "VolumeInterpolator.h"
57#include "VolumeRenderer.h"
[1429]58
[3605]59using namespace nv;
[3463]60using namespace nv::graphics;
61using namespace nv::util;
[3597]62using namespace vrmath;
[3463]63
[848]64// STATIC MEMBER DATA
[2834]65
[3605]66Tcl_Interp *NanoVis::interp = NULL;
[1111]67
[3630]68bool NanoVis::redrawPending = false;
[3567]69bool NanoVis::debugFlag = false;
[860]70
[3567]71int NanoVis::winWidth = NPIX;
72int NanoVis::winHeight = NPIX;
73int NanoVis::renderWindow = 0;
74unsigned char *NanoVis::screenBuffer = NULL;
75Texture2D *NanoVis::legendTexture = NULL;
76Fonts *NanoVis::fonts;
[3630]77Camera *NanoVis::_camera = NULL;
[3567]78RenderContext *NanoVis::renderContext = NULL;
[5717]79GLint NanoVis::max3dTextureSize = 2048;
[934]80
[3567]81NanoVis::TransferFunctionHashmap NanoVis::tfTable;
82NanoVis::VolumeHashmap NanoVis::volumeTable;
83NanoVis::FlowHashmap NanoVis::flowTable;
84NanoVis::HeightMapHashmap NanoVis::heightMapTable;
[1028]85
[3630]86BBox NanoVis::sceneBounds;
[1431]87
[3567]88VolumeRenderer *NanoVis::volRenderer = NULL;
89VelocityArrowsSlice *NanoVis::velocityArrowsSlice = NULL;
[3611]90LIC *NanoVis::licRenderer = NULL;
[3567]91PlaneRenderer *NanoVis::planeRenderer = NULL;
[3605]92OrientationIndicator *NanoVis::orientationIndicator = NULL;
93Grid *NanoVis::grid = NULL;
[431]94
[3567]95//frame buffer for final rendering
96GLuint NanoVis::_finalFbo = 0;
97GLuint NanoVis::_finalColorTex = 0;
98GLuint NanoVis::_finalDepthRb = 0;
99
[2951]100void
101NanoVis::removeAllData()
[1161]102{
[3452]103    TRACE("Enter");
[3630]104
[3362]105    if (grid != NULL) {
106        TRACE("Deleting grid");
107        delete grid;
108    }
[3630]109    if (_camera != NULL) {
110        TRACE("Deleting camera");
111        delete _camera;
[3362]112    }
113    if (volRenderer != NULL) {
114        TRACE("Deleting volRenderer");
115        delete volRenderer;
116    }
117    if (planeRenderer != NULL) {
118        TRACE("Deleting planeRenderer");
119        delete planeRenderer;
120    }
121    if (legendTexture != NULL) {
122        TRACE("Deleting legendTexture");
123        delete legendTexture;
124    }
125    TRACE("Deleting flows");
[3566]126    deleteFlows(interp);
[3362]127    if (licRenderer != NULL) {
128        TRACE("Deleting licRenderer");
129        delete licRenderer;
130    }
131    if (velocityArrowsSlice != NULL) {
132        TRACE("Deleting velocityArrowsSlice");
133        delete velocityArrowsSlice;
134    }
135    if (renderContext != NULL) {
136        TRACE("Deleting renderContext");
137        delete renderContext;
138    }
139    if (screenBuffer != NULL) {
140        TRACE("Deleting screenBuffer");
141        delete [] screenBuffer;
[3559]142        screenBuffer = NULL;
[3362]143    }
[3470]144    if (fonts != NULL) {
145        TRACE("Deleting fonts");
146        delete fonts;
147    }
[3452]148    TRACE("Leave");
[1161]149}
150
[4947]151void
[3630]152NanoVis::eventuallyRedraw()
[1431]153{
[3630]154    if (!redrawPending) {
[2863]155        glutPostRedisplay();
[3630]156        redrawPending = true;
[1431]157    }
158}
159
[1241]160void
[3630]161NanoVis::panCamera(float dx, float dy)
[1236]162{
[3630]163    _camera->pan(dx, dy);
[1312]164
[3630]165    collectBounds();
166    _camera->resetClippingRange(sceneBounds.min, sceneBounds.max);
[1236]167}
168
[2854]169void
[3630]170NanoVis::zoomCamera(float z)
[2854]171{
[3630]172    _camera->zoom(z);
[1236]173
[3630]174    collectBounds();
175    _camera->resetClippingRange(sceneBounds.min, sceneBounds.max);
176}
[3492]177
[3630]178void
179NanoVis::orientCamera(double *quat)
180{
181    _camera->orient(quat);
182
[3492]183    collectBounds();
[3630]184    _camera->resetClippingRange(sceneBounds.min, sceneBounds.max);
185}
[3492]186
[3630]187void
188NanoVis::setCameraPosition(Vector3f pos)
189{
190    _camera->setPosition(pos);
191
192    collectBounds();
193    _camera->resetClippingRange(sceneBounds.min, sceneBounds.max);
[2854]194}
195
[3492]196void
[3630]197NanoVis::setCameraUpdir(Camera::AxisDirection dir)
198{
199    _camera->setUpdir(dir);
200
201    collectBounds();
202    _camera->resetClippingRange(sceneBounds.min, sceneBounds.max);
203}
204
205void
[3492]206NanoVis::resetCamera(bool resetOrientation)
207{
208    TRACE("Resetting all=%d", resetOrientation ? 1 : 0);
209
210    collectBounds();
[3630]211    _camera->reset(sceneBounds.min, sceneBounds.max, resetOrientation);
[3492]212}
213
[2877]214/** \brief Load a 3D volume
215 *
[3597]216 * \param name Volume ID
217 * \param width Number of samples in X direction
218 * \param height Number of samples in Y direction
219 * \param depth Number of samples in Z direction
220 * \param numComponents the number of scalars for each space point. All component
[2877]221 * scalars for a point are placed consequtively in data array
222 * width, height and depth: number of points in each dimension
[3597]223 * \param data Array of floats
224 * \param vmin Min value of field
225 * \param vmax Max value of field
226 * \param nonZeroMin Minimum non-zero value of field
[2877]227 * \param data pointer to an array of floats.
[226]228 */
[927]229Volume *
[2877]230NanoVis::loadVolume(const char *name, int width, int height, int depth,
[3597]231                    int numComponents, float *data, double vmin, double vmax,
232                    double nonZeroMin)
[457]233{
[3567]234    VolumeHashmap::iterator itr = volumeTable.find(name);
235    if (itr != volumeTable.end()) {
[2863]236        WARN("volume \"%s\" already exists", name);
[3567]237        removeVolume(itr->second);
[457]238    }
[3567]239
[3630]240    Volume *volume = new Volume(width, height, depth,
[3597]241                                numComponents,
242                                data, vmin, vmax, nonZeroMin);
[3362]243    Volume::updatePending = true;
[3567]244    volume->name(name);
245    volumeTable[name] = volume;
246
247    return volume;
[226]248}
249
[834]250// Gets a colormap 1D texture by name.
[2930]251TransferFunction *
[3567]252NanoVis::getTransferFunction(const TransferFunctionId& id)
[834]253{
[3567]254    TransferFunctionHashmap::iterator itr = tfTable.find(id);
255    if (itr == tfTable.end()) {
[3568]256        TRACE("No transfer function named \"%s\" found", id.c_str());
[887]257        return NULL;
[3567]258    } else {
259        return itr->second;
[445]260    }
[226]261}
262
[834]263// Creates of updates a colormap 1D texture by name.
[2930]264TransferFunction *
[3567]265NanoVis::defineTransferFunction(const TransferFunctionId& id,
266                                size_t n, float *data)
[587]267{
[3567]268    TransferFunction *tf = getTransferFunction(id);
269    if (tf == NULL) {
270        TRACE("Creating new transfer function \"%s\"", id.c_str());
[3568]271        tf = new TransferFunction(id.c_str(), n, data);
[3567]272        tfTable[id] = tf;
[834]273    } else {
[3567]274        TRACE("Updating existing transfer function \"%s\"", id.c_str());
275        tf->update(n, data);
[834]276    }
[3567]277    return tf;
[834]278}
[380]279
[1089]280int
[2877]281NanoVis::renderLegend(TransferFunction *tf, double min, double max,
[5716]282                      int width, int height, const char *tag)
[829]283{
[3452]284    TRACE("Enter");
[1984]285
[2877]286    int old_width = winWidth;
287    int old_height = winHeight;
[829]288
[2930]289    planeRenderer->setScreenSize(width, height);
[2877]290    resizeOffscreenBuffer(width, height);
[829]291
292    // generate data for the legend
293    float data[512];
294    for (int i=0; i < 256; i++) {
295        data[i] = data[i+256] = (float)(i/255.0);
296    }
[2834]297    legendTexture = new Texture2D(256, 2, GL_FLOAT, GL_LINEAR, 1, data);
[2930]298    int index = planeRenderer->addPlane(legendTexture, tf);
299    planeRenderer->setActivePlane(index);
[829]300
[2930]301    bindOffscreenBuffer();
[829]302    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear screen
[2877]303    planeRenderer->render();
[829]304
[3605]305    glPixelStorei(GL_PACK_ALIGNMENT, 1);
[2877]306    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, screenBuffer);
[1984]307    {
[1282]308        char prefix[200];
[1325]309
[5716]310        TRACE("Sending ppm legend image %s min:%g max:%g", tag, min, max);
311        sprintf(prefix, "nv>legend %s %g %g", tag, min, max);
[3605]312#ifdef USE_THREADS
[3611]313        queuePPM(g_queue, prefix, screenBuffer, width, height);
[3605]314#else
[3611]315        writePPM(g_fdOut, prefix, screenBuffer, width, height);
[3605]316#endif
[1195]317    }
[2930]318    planeRenderer->removePlane(index);
[2877]319    resizeOffscreenBuffer(old_width, old_height);
[829]320
[2951]321    delete legendTexture;
322    legendTexture = NULL;
[3452]323    TRACE("Leave");
[829]324    return TCL_OK;
325}
326
[226]327//initialize frame buffer objects for offscreen rendering
[3605]328bool
[2877]329NanoVis::initOffscreenBuffer()
[580]330{
[3452]331    TRACE("Enter");
[2930]332    assert(_finalFbo == 0);
[1197]333    // Initialize a fbo for final display.
[2877]334    glGenFramebuffersEXT(1, &_finalFbo);
[2831]335
[2877]336    glGenTextures(1, &_finalColorTex);
337    glBindTexture(GL_TEXTURE_2D, _finalColorTex);
[4167]338    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
339    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[2877]340#if defined(HAVE_FLOAT_TEXTURES) && defined(USE_HALF_FLOAT)
341    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, winWidth, winHeight, 0,
[887]342                 GL_RGB, GL_INT, NULL);
[2877]343#elif defined(HAVE_FLOAT_TEXTURES)
344    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, winWidth, winHeight, 0,
345                 GL_RGB, GL_INT, NULL);
[383]346#else
[2877]347    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, winWidth, winHeight, 0,
[1205]348                 GL_RGB, GL_INT, NULL);
[383]349#endif
[2930]350
[2877]351    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _finalFbo);
352    glGenRenderbuffersEXT(1, &_finalDepthRb);
353    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _finalDepthRb);
[1198]354    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24,
[2877]355                             winWidth, winHeight);
[1198]356    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
[2877]357                              GL_TEXTURE_2D, _finalColorTex, 0);
[1198]358    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
[2877]359                                 GL_RENDERBUFFER_EXT, _finalDepthRb);
[1200]360
[1199]361    GLenum status;
[1200]362    if (!CheckFBO(&status)) {
[2877]363        PrintFBOStatus(status, "finalFbo");
[3605]364        return false;
[1199]365    }
[1200]366
[3452]367    TRACE("Leave");
[3605]368    return true;
[226]369}
370
[423]371//resize the offscreen buffer
[3605]372bool
[2877]373NanoVis::resizeOffscreenBuffer(int w, int h)
[834]374{
[3452]375    TRACE("Enter (%d, %d)", w, h);
[2877]376    if ((w == winWidth) && (h == winHeight)) {
[3605]377        return true;
[1205]378    }
[2877]379    winWidth = w;
380    winHeight = h;
[2831]381
[848]382    if (fonts) {
383        fonts->resize(w, h);
[776]384    }
[3452]385    TRACE("screenBuffer size: %d %d", w, h);
[2831]386
[2877]387    if (screenBuffer != NULL) {
388        delete [] screenBuffer;
389        screenBuffer = NULL;
[834]390    }
[2831]391
[3605]392    screenBuffer = new unsigned char[3*winWidth*winHeight];
[2877]393    assert(screenBuffer != NULL);
[3605]394
[834]395    //delete the current render buffer resources
[2877]396    glDeleteTextures(1, &_finalColorTex);
397    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _finalDepthRb);
398    glDeleteRenderbuffersEXT(1, &_finalDepthRb);
[1205]399
[3452]400    TRACE("before deleteframebuffers");
[2877]401    glDeleteFramebuffersEXT(1, &_finalFbo);
[1205]402
[3452]403    TRACE("reinitialize FBO");
[834]404    //Reinitialize final fbo for final display
[2877]405    glGenFramebuffersEXT(1, &_finalFbo);
[1205]406
[2877]407    glGenTextures(1, &_finalColorTex);
408    glBindTexture(GL_TEXTURE_2D, _finalColorTex);
[4947]409    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
410    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[2877]411#if defined(HAVE_FLOAT_TEXTURES) && defined(USE_HALF_FLOAT)
412    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, winWidth, winHeight, 0,
[887]413                 GL_RGB, GL_INT, NULL);
[2877]414#elif defined(HAVE_FLOAT_TEXTURES)
415    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, winWidth, winHeight, 0,
416                 GL_RGB, GL_INT, NULL);
[423]417#else
[2877]418    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, winWidth, winHeight, 0,
[887]419                 GL_RGB, GL_INT, NULL);
[423]420#endif
[2930]421
[2877]422    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _finalFbo);
423    glGenRenderbuffersEXT(1, &_finalDepthRb);
424    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _finalDepthRb);
[1200]425    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24,
[2877]426                             winWidth, winHeight);
[1205]427    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
[2877]428                              GL_TEXTURE_2D, _finalColorTex, 0);
[1200]429    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
[2877]430                                 GL_RENDERBUFFER_EXT, _finalDepthRb);
[2831]431
[1199]432    GLenum status;
[1200]433    if (!CheckFBO(&status)) {
[2877]434        PrintFBOStatus(status, "finalFbo");
[3605]435        return false;
[1199]436    }
[1200]437
[3452]438    TRACE("change camera");
[1205]439    //change the camera setting
[3630]440    _camera->setViewport(0, 0, winWidth, winHeight);
[2930]441    planeRenderer->setScreenSize(winWidth, winHeight);
[1205]442
[3452]443    TRACE("Leave (%d, %d)", w, h);
[3605]444    return true;
[423]445}
446
[3605]447bool NanoVis::init(const char* path)
[848]448{
[2930]449    // print OpenGL driver information
[3452]450    TRACE("-----------------------------------------------------------");
451    TRACE("OpenGL version: %s", glGetString(GL_VERSION));
452    TRACE("OpenGL vendor: %s", glGetString(GL_VENDOR));
453    TRACE("OpenGL renderer: %s", glGetString(GL_RENDERER));
454    TRACE("-----------------------------------------------------------");
[2822]455
[1111]456    if (path == NULL) {
[3452]457        ERROR("No path defined for shaders or resources");
[3605]458        return false;
[1111]459    }
[848]460    GLenum err = glewInit();
461    if (GLEW_OK != err) {
[3452]462        ERROR("Can't init GLEW: %s", glewGetErrorString(err));
[3605]463        return false;
[848]464    }
[3452]465    TRACE("Using GLEW %s", glewGetString(GLEW_VERSION));
[848]466
[2930]467    // OpenGL 2.1 includes VBOs, PBOs, MRT, NPOT textures, point parameters, point sprites,
468    // GLSL 1.2, and occlusion queries.
469    if (!GLEW_VERSION_2_1) {
[3452]470        ERROR("OpenGL version 2.1 or greater is required");
[3605]471        return false;
[2877]472    }
[2930]473
[2962]474    // NVIDIA driver may report OpenGL 2.1, but not support PBOs in
475    // indirect GLX contexts
476    if (!GLEW_ARB_pixel_buffer_object) {
[3452]477        ERROR("Pixel buffer objects are not supported by driver, please check that the user running nanovis has permissions to create a direct rendering context (e.g. user has read/write access to /dev/nvidia* device nodes in Linux).");
[3605]478        return false;
[2962]479    }
480
[2930]481    // Additional extensions not in 2.1 core
482
483    // Framebuffer objects were promoted in 3.0
484    if (!GLEW_EXT_framebuffer_object) {
[3452]485        ERROR("EXT_framebuffer_oject extension is required");
[3605]486        return false;
[2877]487    }
[2930]488    // Rectangle textures were promoted in 3.1
489    // FIXME: can use NPOT in place of rectangle textures
[2884]490    if (!GLEW_ARB_texture_rectangle) {
[3452]491        ERROR("ARB_texture_rectangle extension is required");
[3605]492        return false;
[2884]493    }
[2877]494#ifdef HAVE_FLOAT_TEXTURES
[2930]495    // Float color buffers and textures were promoted in 3.0
[2877]496    if (!GLEW_ARB_texture_float ||
497        !GLEW_ARB_color_buffer_float) {
[3452]498        ERROR("ARB_texture_float and ARB_color_buffer_float extensions are required");
[3605]499        return false;
[2877]500    }
501#endif
[3875]502    // FIXME: should use GLSL for portability
503#ifdef USE_ARB_PROGRAMS
504    if (!GLEW_ARB_vertex_program ||
505        !GLEW_ARB_fragment_program) {
506        ERROR("ARB_vertex_program and ARB_fragment_program extensions are required");
507        return false;
508    }
509#else
[2882]510    if (!GLEW_NV_vertex_program3 ||
511        !GLEW_NV_fragment_program2) {
[3452]512        ERROR("NV_vertex_program3 and NV_fragment_program2 extensions are required");
[3605]513        return false;
[2882]514    }
[3875]515#endif
[2877]516
[3463]517    if (!FilePath::getInstance()->setPath(path)) {
[3452]518        ERROR("can't set file path to %s", path);
[3605]519        return false;
[848]520    }
[1703]521
[2856]522    ImageLoaderFactory::getInstance()->addLoaderImpl("bmp", new BMPImageLoaderImpl());
523
[3605]524    return true;
[848]525}
526
[3605]527bool
[2930]528NanoVis::initGL()
[1089]529{
[3452]530    TRACE("in initGL");
[1351]531    //buffer to store data read from the screen
[2877]532    if (screenBuffer) {
533        delete[] screenBuffer;
534        screenBuffer = NULL;
[1351]535    }
[3605]536    screenBuffer = new unsigned char[3*winWidth*winHeight];
[2877]537    assert(screenBuffer != NULL);
[427]538
[1351]539    //create the camera with default setting
[3630]540    _camera = new Camera(0, 0, winWidth, winHeight);
[226]541
[5717]542    glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &max3dTextureSize);
543    TRACE("Max 3D texture dim: %d", max3dTextureSize);
544
[1351]545    glEnable(GL_TEXTURE_2D);
546    glShadeModel(GL_FLAT);
547    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
[2930]548    glClearColor(0, 0, 0, 1);
549    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[580]550
[1351]551    //initialize lighting
552    GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
553    GLfloat mat_shininess[] = {30.0};
554    GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0};
[392]555
[1351]556    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
557    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
558    glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
559    glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
[3630]560    glLightfv(GL_LIGHT1, GL_DIFFUSE, white_light);
[1351]561    glLightfv(GL_LIGHT1, GL_SPECULAR, white_light);
[392]562
[3605]563    //frame buffer object for offscreen rendering
564    if (!initOffscreenBuffer()) {
565        return false;
566    }
[226]567
[3630]568    fonts = new Fonts();
569    fonts->addFont("verdana", "verdana.fnt");
570    fonts->setFont("verdana");
[418]571
[3463]572    renderContext = new RenderContext();
[2831]573
[3630]574    volRenderer = new VolumeRenderer();
575
[2877]576    planeRenderer = new PlaneRenderer(winWidth, winHeight);
[431]577
[3630]578    velocityArrowsSlice = new VelocityArrowsSlice();
579
580    licRenderer = new LIC();
581
[3605]582    orientationIndicator = new OrientationIndicator();
583
[3630]584    grid = new Grid();
585    grid->setFont(fonts);
586
[1351]587    //assert(glGetError()==0);
[448]588
[3452]589    TRACE("leaving initGL");
[3605]590    return true;
[226]591}
592
[884]593void NanoVis::update()
594{
[2975]595    VolumeInterpolator *volInterp = volRenderer->getVolumeInterpolator();
596    if (volInterp->isStarted()) {
[887]597        struct timeval clock;
598        gettimeofday(&clock, NULL);
[4949]599        double elapsed_time = clock.tv_sec + clock.tv_usec/1000000.0 -
[2975]600            volInterp->getStartTime();
[1089]601
[4949]602        TRACE("%g %g", elapsed_time,
[2975]603              volInterp->getInterval());
[4949]604        double fraction;
605        double f = fmod(elapsed_time, volInterp->getInterval());
[900]606        if (f == 0.0) {
[4949]607            fraction = 0.0;
[887]608        } else {
[2975]609            fraction = f / volInterp->getInterval();
[887]610        }
[4949]611        TRACE("fraction : %g", fraction);
612        volInterp->update((float)fraction);
[887]613    }
[884]614}
615
[3935]616/**
617 * \brief Called when new volumes are added to update ranges
618 */
[932]619void
[2877]620NanoVis::setVolumeRanges()
[932]621{
[3630]622    TRACE("Enter");
[1089]623
[3630]624    double valueMin = DBL_MAX, valueMax = -DBL_MAX;
[3567]625    VolumeHashmap::iterator itr;
626    for (itr = volumeTable.begin();
627         itr != volumeTable.end(); ++itr) {
628        Volume *volume = itr->second;
[3630]629        if (valueMin > volume->wAxis.min()) {
630            valueMin = volume->wAxis.min();
[1089]631        }
[3630]632        if (valueMax < volume->wAxis.max()) {
633            valueMax = volume->wAxis.max();
[1089]634        }
[932]635    }
[3630]636    if ((valueMin < DBL_MAX) && (valueMax > -DBL_MAX)) {
637        Volume::valueMin = valueMin;
638        Volume::valueMax = valueMax;
[932]639    }
[2877]640    Volume::updatePending = false;
[3453]641    TRACE("Leave");
[932]642}
643
[3935]644/**
645 * \brief Called when new heightmaps are added to update ranges
646 */
[932]647void
[2877]648NanoVis::setHeightmapRanges()
[932]649{
[3630]650    TRACE("Enter");
[1089]651
[3630]652    double valueMin = DBL_MAX, valueMax = -DBL_MAX;
[3567]653    HeightMapHashmap::iterator itr;
654    for (itr = heightMapTable.begin();
655         itr != heightMapTable.end(); ++itr) {
656        HeightMap *heightMap = itr->second;
[3630]657        if (valueMin > heightMap->wAxis.min()) {
658            valueMin = heightMap->wAxis.min();
[1089]659        }
[3630]660        if (valueMax < heightMap->wAxis.max()) {
661            valueMax = heightMap->wAxis.max();
[1089]662        }
[932]663    }
[3630]664    if ((valueMin < DBL_MAX) && (valueMax > -DBL_MAX)) {
665        HeightMap::valueMin = valueMin;
666        HeightMap::valueMax = valueMax;
[932]667    }
[3567]668    for (HeightMapHashmap::iterator itr = heightMapTable.begin();
669         itr != heightMapTable.end(); ++itr) {
670        itr->second->mapToGrid(grid);
[1111]671    }
[2877]672    HeightMap::updatePending = false;
[3453]673    TRACE("Leave");
[932]674}
675
[1089]676void
[3492]677NanoVis::collectBounds(bool onlyVisible)
678{
[3630]679    sceneBounds.makeEmpty();
[3492]680
[3567]681    for (VolumeHashmap::iterator itr = volumeTable.begin();
682         itr != volumeTable.end(); ++itr) {
683        Volume *volume = itr->second;
[3492]684
[3567]685        if (onlyVisible && !volume->visible())
[3492]686            continue;
687
[3630]688        BBox bbox;
689        volume->getBounds(bbox.min, bbox.max);
690        sceneBounds.extend(bbox);
[3492]691    }
692
[3567]693    for (HeightMapHashmap::iterator itr = heightMapTable.begin();
694         itr != heightMapTable.end(); ++itr) {
695        HeightMap *heightMap = itr->second;
[3492]696
697        if (onlyVisible && !heightMap->isVisible())
698            continue;
699
[3630]700        BBox bbox;
701        heightMap->getBounds(bbox.min, bbox.max);
702        sceneBounds.extend(bbox);
703    }
[3492]704
[3935]705    for (FlowHashmap::iterator itr = flowTable.begin();
706         itr != flowTable.end(); ++itr) {
707        Flow *flow = itr->second;
708
[3630]709        BBox bbox;
[3935]710        flow->getBounds(bbox.min, bbox.max, onlyVisible);
[3630]711        sceneBounds.extend(bbox);
[3492]712    }
713
[3630]714    if (!sceneBounds.isEmptyX()) {
[5478]715        grid->xAxis.setRange(sceneBounds.min.x, sceneBounds.max.x);
[3492]716    }
[3630]717    if (!sceneBounds.isEmptyY()) {
[5478]718        grid->yAxis.setRange(sceneBounds.min.y, sceneBounds.max.y);
[3630]719    }
720    if (!sceneBounds.isEmptyZ()) {
[5478]721        grid->zAxis.setRange(sceneBounds.min.z, sceneBounds.max.z);
[3630]722    }
[3492]723
[3900]724#if 0
[3630]725    if (!onlyVisible || grid->isVisible()) {
726        BBox bbox;
727        grid->getBounds(bbox.min, bbox.max);
728        sceneBounds.extend(bbox);
729    }
[3900]730#endif
[3492]731
[3630]732    if (sceneBounds.isEmpty()) {
733        sceneBounds.min.set(-0.5, -0.5, -0.5);
734        sceneBounds.max.set( 0.5,  0.5,  0.5);
[3492]735    }
736
737    TRACE("Scene bounds: (%g,%g,%g) - (%g,%g,%g)",
[3630]738          sceneBounds.min.x, sceneBounds.min.y, sceneBounds.min.z,
739          sceneBounds.max.x, sceneBounds.max.y, sceneBounds.max.z);
[3492]740}
741
742void
[3478]743NanoVis::setBgColor(float color[3])
744{
745    TRACE("Setting bgcolor to %g %g %g", color[0], color[1], color[2]);
746    glClearColor(color[0], color[1], color[2], 1);
747}
748
[3605]749void
750NanoVis::removeVolume(Volume *volume)
751{
752    VolumeHashmap::iterator itr = volumeTable.find(volume->name());
753    if (itr != volumeTable.end()) {
754        volumeTable.erase(itr);
755    }
756    delete volume;
[5722]757    Volume::updatePending = true;
[3605]758}
759
[3597]760Flow *
761NanoVis::getFlow(const char *name)
762{
763    FlowHashmap::iterator itr = flowTable.find(name);
764    if (itr == flowTable.end()) {
765        TRACE("Can't find flow '%s'", name);
766        return NULL;
767    }
768    return itr->second;
769}
770
771Flow *
772NanoVis::createFlow(Tcl_Interp *interp, const char *name)
773{
774    FlowHashmap::iterator itr = flowTable.find(name);
775    if (itr != flowTable.end()) {
776        ERROR("Flow '%s' already exists", name);
777        return NULL;
778    }
779    Flow *flow = new Flow(interp, name);
780    flowTable[name] = flow;
781    return flow;
782}
783
784/**
785 * \brief Delete flow object and hash table entry
786 *
787 * This is called by the flow command instance delete callback
788 */
[3478]789void
[3597]790NanoVis::deleteFlow(const char *name)
791{
792    FlowHashmap::iterator itr = flowTable.find(name);
793    if (itr != flowTable.end()) {
794        delete itr->second;
795        flowTable.erase(itr);
[5722]796        Flow::updatePending = true;
[3597]797    }
798}
799
800/**
801 * \brief Delete all flow object commands
802 *
803 * This will also delete the flow objects and hash table entries
804 */
805void
806NanoVis::deleteFlows(Tcl_Interp *interp)
807{
808    FlowHashmap::iterator itr;
809    for (itr = flowTable.begin();
810         itr != flowTable.end(); ++itr) {
811        Tcl_DeleteCommandFromToken(interp, itr->second->getCommandToken());
812    }
813    flowTable.clear();
814}
815
[3935]816/**
817 * \brief Called when new flows are added to update ranges
818 */
[3597]819bool
[3935]820NanoVis::setFlowRanges()
[3597]821{
822    TRACE("Enter");
823
[3630]824    Flow::magMin = DBL_MAX;
825    Flow::magMax = -DBL_MAX;
[3597]826    for (FlowHashmap::iterator itr = flowTable.begin();
827         itr != flowTable.end(); ++itr) {
828        Flow *flow = itr->second;
829        if (!flow->isDataLoaded()) {
830            continue;
831        }
[3935]832        double range[2];
833        flow->getVectorRange(range);
834        if (range[0] < Flow::magMin) {
835            Flow::magMin = range[0];
[3597]836        }
[3935]837        if (range[1] > Flow::magMax) {
838            Flow::magMax = range[1];
[3597]839        }
840    }
841
[3630]842    TRACE("magMin=%g magMax=%g", Flow::magMin, Flow::magMax);
[3597]843
844    for (FlowHashmap::iterator itr = flowTable.begin();
845         itr != flowTable.end(); ++itr) {
846        Flow *flow = itr->second;
847        if (!flow->isDataLoaded()) {
848            continue; // Flow exists, but no data has been loaded yet.
849        }
850        if (flow->visible()) {
851            flow->initializeParticles();
852        }
853    }
[3630]854
855    Flow::updatePending = false;
[3597]856    return true;
857}
858
859void
860NanoVis::renderFlows()
861{
862    for (FlowHashmap::iterator itr = flowTable.begin();
863         itr != flowTable.end(); ++itr) {
864        Flow *flow = itr->second;
865        if (flow->isDataLoaded() && flow->visible()) {
866            flow->render();
867        }
868    }
869}
870
871void
872NanoVis::resetFlows()
873{
[3630]874    NanoVis::licRenderer->reset();
[3597]875    for (FlowHashmap::iterator itr = flowTable.begin();
876         itr != flowTable.end(); ++itr) {
877        Flow *flow = itr->second;
[3630]878        if (flow->isDataLoaded()) {
[3597]879            flow->resetParticles();
880        }
881    }
882}   
883
884void
885NanoVis::advectFlows()
886{
[3630]887    TRACE("Enter");
[3597]888    for (FlowHashmap::iterator itr = flowTable.begin();
889         itr != flowTable.end(); ++itr) {
890        Flow *flow = itr->second;
[3630]891        if (flow->isDataLoaded()) {
[3597]892            flow->advect();
893        }
894    }
895}
896
897void
[3497]898NanoVis::render()
[226]899{
[3452]900    TRACE("Enter");
901
[3630]902    if (Flow::updatePending) {
[3935]903        setFlowRanges();
[1431]904    }
[2877]905    if (HeightMap::updatePending) {
906        setHeightmapRanges();
[932]907    }
[2877]908    if (Volume::updatePending) {
909        setVolumeRanges();
[932]910    }
[2930]911
[3630]912    collectBounds();
[2930]913
[3630]914    // Start final rendering
915
[2930]916    // Need to reset fbo since it may have been changed to default (0)
917    bindOffscreenBuffer();
918
[4901]919    // Clear screen
[3597]920    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[226]921
[3567]922    glEnable(GL_DEPTH_TEST);
923    glEnable(GL_COLOR_MATERIAL);
[1028]924
[3630]925    // Emit modelview and projection matrices
926    _camera->initialize();
[1089]927
[3630]928    // Now render things in the scene
[2863]929
[3630]930    orientationIndicator->setPosition(sceneBounds.getCenter());
931    orientationIndicator->setScale(sceneBounds.getSize());
932    orientationIndicator->render();
[1089]933
[3630]934    grid->render();
[3605]935
[3630]936    licRenderer->render();
937
938    velocityArrowsSlice->render();
939
[3605]940    renderFlows();
[3630]941
[3567]942    volRenderer->renderAll();
[1089]943
[3630]944    for (HeightMapHashmap::iterator itr = heightMapTable.begin();
[3567]945         itr != heightMapTable.end(); ++itr) {
[3630]946        itr->second->render(renderContext);
[4901]947    }
[2831]948
[1984]949    CHECK_FRAMEBUFFER_STATUS();
[3452]950    TRACE("Leave");
[3630]951    redrawPending = false;
[226]952}
Note: See TracBrowser for help on using the repository browser.