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

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

Refactor texture classes, misc. cleanups, cut down on header pollution -- still
need to fix header deps in Makefile.in

  • 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, 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, ++p) {
53        glColor4f(p->color.x, p->color.y, p->color.z, p->color.w);
54        glVertex3f(p->position.x, p->position.y, p->position.z);
55    }
56}
57
58void PointSetRenderer::renderCluster(PCA::ClusterList** bucket, int size, int level)
59{
60    float quadratic[] = { 1.0f, 0.0f, 0.01f };
61
62    glEnable(GL_POINT_SPRITE_ARB);
63    glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, quadratic);
64    glPointParameterfARB(GL_POINT_FADE_THRESHOLD_SIZE_ARB, 60.0f);
65    glPointParameterfARB(GL_POINT_SIZE_MIN_ARB, 1.0f);
66    glPointParameterfARB(GL_POINT_SIZE_MAX_ARB, 100);
67#ifdef USE_TEXTURE
68    glTexEnvf(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
69#endif
70
71    glEnable(GL_POINT_SPRITE_ARB);
72
73    bool setSize = false;
74    glBegin(GL_POINTS);
75
76    PCA::ClusterList* p;
77    for (int i = size - 1; i >= 0; --i) {
78        p = bucket[i];
79        if (p) {
80            if (!setSize) {
81#ifdef USE_SHADER
82                _shader->setScale(p->data->points[0].size);
83#endif
84                setSize = true;
85            }
86        }
87
88        while (p) {
89            renderPoints(p->data->points, p->data->numOfPoints);
90
91            p = p->next;
92        }
93    }
94
95    glEnd();
96
97    glDisable(GL_POINT_SPRITE_ARB);
98    glPointSize(1);
99}
100
101void PointSetRenderer::render(PCA::ClusterAccel *cluster, const Mat4x4& mat,
102                              int sortLevel, const Vector3& scale, const Vector3& origin)
103{
104    _bucketSort->init();
105    _bucketSort->sort(cluster, mat, sortLevel);
106
107    glDisable(GL_TEXTURE_2D);
108
109#ifdef USE_TEXTURE
110    _pointTexture->activate();
111    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
112#endif
113
114    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
115    //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
116    glEnable(GL_BLEND);
117
118    glDisable(GL_LIGHTING);
119    glEnable(GL_COLOR_MATERIAL);
120
121    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
122    glPushMatrix();
123    float s = 1.0f / scale.x;
124    Vector3 shift(origin.x + scale.x * 0.5, origin.x + scale.x * 0.5, origin.x + scale.x * 0.5);
125    glScalef(s, scale.y / scale.x * s, scale.z / scale.x * s);
126
127    //glTranslatef(-shift.x, -shift.y, -shift.z);
128
129#ifdef USE_SHADER
130    _shader->bind();
131#else
132    glPointSize(POINT_SIZE);
133#endif
134    renderCluster(_bucketSort->getBucket(), _bucketSort->getSize(), 4);
135#ifdef USE_SHADER
136    _shader->unbind();
137#else
138    glPointSize(1.0f);
139#endif
140
141    glPopMatrix();
142
143    glDisable(GL_COLOR_MATERIAL);
144    glEnable(GL_LIGHTING);
145    glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
146    glDisable(GL_BLEND);
147
148#ifdef USE_TEXTURE
149    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
150    _pointTexture->deactivate();
151#endif
152}
Note: See TracBrowser for help on using the repository browser.