source: trunk/vizservers/nanovis/PointSetRenderer.cpp @ 827

Last change on this file since 827 was 827, checked in by vrinside, 14 years ago

Added color blending with point sprite texture

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