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

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

Const correctness fixes, pass vector/matrix objects by reference, various
formatting and style cleanups, don't spam syslog and uncomment openlog() call.
Still needs to be compiled with -DWANT_TRACE to get tracing, but now trace
output will be output to file in /tmp.

  • 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 <R2/R2FilePath.h>
8
9#include "PointSetRenderer.h"
10#include "PCASplit.h"
11
12#define USE_TEXTURE
13//#define USE_SHADER
14#define POINT_SIZE 5
15
16PointSetRenderer::PointSetRenderer()
17{
18    _shader = new PointShader();
19    const char *path = R2FilePath::getInstance()->getPath("particle2.bmp");
20    if (path == NULL) {
21        ERROR("pointset file not found - %s\n", path);
22        return;
23    }
24   
25    ImageLoader* loader = ImageLoaderFactory::getInstance()->createLoader("bmp");
26    Image* image = loader->load(path, Image::IMG_RGBA);
27    delete [] path;
28    unsigned char* bytes = (unsigned char*) image->getImageBuffer();
29    if (bytes) {
30        for (unsigned int y = 0; y < image->getHeight(); ++y) {
31            for (unsigned int x = 0; x < image->getWidth(); ++x, bytes +=4) {
32                bytes[3] =  (bytes[0] == 0)? 0 : 255;
33            }
34        }
35    }
36
37    if (image) {
38        _pointTexture = new Texture2D(image->getWidth(), image->getHeight(),
39                                      GL_UNSIGNED_BYTE, GL_LINEAR,   
40                                      4, (float*) image->getImageBuffer());
41    } else {
42        ERROR("fail to load image [%s]\n", "particles2.bmp");
43    }
44
45    delete loader;
46    _bucketSort = new PCA::BucketSort(1024);
47}
48
49void PointSetRenderer::renderPoints(PCA::Point* points, int length)
50{
51    PCA::Point* p = points;
52    for (int i = 0; i < length; ++i)
53    {
54        glColor4f(p->color.x, p->color.y, p->color.z, p->color.w);
55        glVertex3f(p->position.x, p->position.y, p->position.z);
56   
57        ++p;
58    }
59}
60
61void PointSetRenderer::renderCluster(PCA::ClusterList** bucket, int size, int level)
62{
63    float quadratic[] = { 1.0f, 0.0f, 0.01f };
64
65    glEnable(GL_POINT_SPRITE_ARB);
66    glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, quadratic);
67    glPointParameterfARB(GL_POINT_FADE_THRESHOLD_SIZE_ARB, 60.0f);
68    glPointParameterfARB(GL_POINT_SIZE_MIN_ARB, 1.0f);
69    glPointParameterfARB(GL_POINT_SIZE_MAX_ARB, 100);
70#ifdef USE_TEXTURE
71    glTexEnvf(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
72#endif
73
74    glEnable(GL_POINT_SPRITE_ARB);
75
76    bool setSize = false;
77    glBegin(GL_POINTS);
78
79    PCA::ClusterList* p;
80    for (int i = size - 1; i >= 0; --i)
81    {
82        p = bucket[i];
83        if (p)
84        {
85            if (!setSize)
86            {
87#ifdef USE_SHADER
88                _shader->setScale(p->data->points[0].size);
89#endif
90                setSize = true;
91            }
92        }
93
94        while (p)
95        {
96            renderPoints(p->data->points, p->data->numOfPoints);
97
98            p = p->next;
99        }
100       
101    }
102
103    glEnd();
104
105    glDisable(GL_POINT_SPRITE_ARB);
106    glPointSize(1);
107
108}
109
110void PointSetRenderer::render(PCA::ClusterAccel* cluster, const Mat4x4& mat, int sortLevel, const Vector3& scale, const Vector3& 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}
161
Note: See TracBrowser for help on using the repository browser.