source: trunk/packages/vizservers/nanovis/PointSetRenderer.cpp @ 3492

Last change on this file since 3492 was 3492, checked in by ldelgass, 6 years ago

Fix camera reset for nanovis. Includes refactoring of vector/matrix classes
in nanovis to consolidate into vrmath library. Also add preliminary canonical
view control to clients for testing.

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