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

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

Remove Nv prefix from shader classes now that they are in nv namespace.

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