source: nanovis/branches/1.1/PointSetRenderer.cpp @ 4804

Last change on this file since 4804 was 3502, checked in by ldelgass, 7 years ago

Add basic VTK structured points reader to nanovis, update copyright dates.

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (c) 2004-2013  HUBzero Foundation, LLC
4 *
5 */
6#include <GL/glew.h>
7
8#include <Image.h>
9#include <ImageLoaderFactory.h>
10#include <ImageLoader.h>
11#include <util/FilePath.h>
12
13#include "PointSetRenderer.h"
14#include "PCASplit.h"
15
16using namespace nv::util;
17
18#define USE_TEXTURE
19//#define USE_SHADER
20#define POINT_SIZE 5
21
22PointSetRenderer::PointSetRenderer()
23{
24    _shader = new PointShader();
25    std::string path = FilePath::getInstance()->getPath("particle2.bmp");
26    if (path.empty()) {
27        ERROR("Particle image not found");
28        return;
29    }
30
31    ImageLoader *loader = ImageLoaderFactory::getInstance()->createLoader("bmp");
32    Image *image = loader->load(path.c_str(), Image::IMG_RGBA);
33    unsigned char *bytes = (unsigned char *)image->getImageBuffer();
34    if (bytes) {
35        for (unsigned int y = 0; y < image->getHeight(); ++y) {
36            for (unsigned int x = 0; x < image->getWidth(); ++x, bytes += 4) {
37                bytes[3] = (bytes[0] == 0) ? 0 : 255;
38            }
39        }
40    }
41
42    if (image) {
43        _pointTexture = new Texture2D(image->getWidth(), image->getHeight(), 
44                                      GL_UNSIGNED_BYTE, GL_LINEAR,   
45                                      4, image->getImageBuffer());
46    } else {
47        ERROR("Failed to load particle image");
48    }
49
50    delete loader;
51    delete image;
52    _bucketSort = new PCA::BucketSort(1024);
53}
54
55PointSetRenderer::~PointSetRenderer()
56{
57}
58
59void PointSetRenderer::renderPoints(PCA::Point *points, int length)
60{
61    PCA::Point *p = points;
62    for (int i = 0; i < length; ++i, ++p) {
63        glColor4f(p->color.x, p->color.y, p->color.z, p->color.w);
64        glVertex3f(p->position.x, p->position.y, p->position.z);
65    }
66}
67
68void PointSetRenderer::renderCluster(PCA::ClusterList** bucket, int size, int level)
69{
70    float quadratic[] = { 1.0f, 0.0f, 0.01f };
71
72    glEnable(GL_POINT_SPRITE_ARB);
73    glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, quadratic);
74    glPointParameterfARB(GL_POINT_FADE_THRESHOLD_SIZE_ARB, 60.0f);
75    glPointParameterfARB(GL_POINT_SIZE_MIN_ARB, 1.0f);
76    glPointParameterfARB(GL_POINT_SIZE_MAX_ARB, 100);
77#ifdef USE_TEXTURE
78    glTexEnvf(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
79#endif
80
81    bool setSize = false;
82    glBegin(GL_POINTS);
83
84    PCA::ClusterList *p;
85    for (int i = size - 1; i >= 0; --i) {
86        p = bucket[i];
87        if (p) {
88            if (!setSize) {
89#ifdef USE_SHADER
90                _shader->setScale(p->data->points[0].size);
91#endif
92                setSize = true;
93            }
94        }
95
96        while (p) {
97            renderPoints(p->data->points, p->data->numOfPoints);
98
99            p = p->next;
100        }
101    }
102
103    glEnd();
104
105    glDisable(GL_POINT_SPRITE_ARB);
106    glPointSize(1);
107}
108
109void PointSetRenderer::render(PCA::ClusterAccel *cluster, const Matrix4x4d& mat,
110                              int sortLevel, const Vector3f& scale, const Vector3f& origin)
111{
112    _bucketSort->init();
113    _bucketSort->sort(cluster, mat, sortLevel);
114
115    glDisable(GL_TEXTURE_2D);
116
117#ifdef USE_TEXTURE
118    _pointTexture->activate();
119    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
120#endif
121
122    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
123    //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
124    glEnable(GL_BLEND);
125
126    glDisable(GL_LIGHTING);
127    glEnable(GL_COLOR_MATERIAL);
128
129    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
130    glPushMatrix();
131    float s = 1.0f / scale.x;
132    Vector3 shift(origin.x + scale.x * 0.5, origin.x + scale.x * 0.5, origin.x + scale.x * 0.5);
133    glScalef(s, scale.y / scale.x * s, scale.z / scale.x * s);
134
135    //glTranslatef(-shift.x, -shift.y, -shift.z);
136
137#ifdef USE_SHADER
138    _shader->bind();
139#else
140    glPointSize(POINT_SIZE);
141#endif
142    renderCluster(_bucketSort->getBucket(), _bucketSort->getSize(), 4);
143#ifdef USE_SHADER
144    _shader->unbind();
145#else
146    glPointSize(1.0f);
147#endif
148
149    glPopMatrix();
150
151    glDisable(GL_COLOR_MATERIAL);
152    glEnable(GL_LIGHTING);
153    glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
154    glDisable(GL_BLEND);
155
156#ifdef USE_TEXTURE
157    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
158    _pointTexture->deactivate();
159#endif
160}
Note: See TracBrowser for help on using the repository browser.