~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to extern/qdune/qdrender.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#ifdef __BCPLUSPLUS__
 
3
#pragma hdrstop
 
4
#include <condefs.h>
 
5
#endif
 
6
 
 
7
#if defined(_MSC_VER)
 
8
#define WIN32_LEAN_AND_MEAN
 
9
#include <windows.h>
 
10
#pragma warning(disable : 4996)
 
11
#endif
 
12
 
 
13
#include <iostream>
 
14
 
 
15
#include "ri.h"
 
16
#include "RIB_parser.h"
 
17
 
 
18
// for points/curves test
 
19
#include "qdVector.h"
 
20
#include "Noise.h"
 
21
 
 
22
#include <float.h>
 
23
#include <malloc.h>
 
24
 
 
25
//---------------------------------------------------------------------------
 
26
// global operators new & delete, to keep track of total memory allocated
 
27
 
 
28
// this code snippet from heaplayers library, not sure if flags are really all necessary (or correct..)
 
29
#if defined (_MSC_VER)
 
30
inline size_t getSize(void* ptr) { return _msize(ptr); }
 
31
#elif defined (__GNUC__) && !defined(__SVR4)
 
32
inline size_t getSize(void* ptr) { return malloc_usable_size(ptr); }
 
33
#elif defined (__APPLE__)
 
34
inline size_t getSize(void* ptr) { return malloc_size(ptr); }
 
35
#endif
 
36
 
 
37
static size_t curmem = 0; // current amount of memory allocated
 
38
static size_t maxmem = 0; // maximum amount of memory allocated
 
39
 
 
40
void* operator new(std::size_t sz) throw (std::bad_alloc)
 
41
{
 
42
#if defined (__GNUC__) && !defined(__SVR4)
 
43
        void* ptr = memalign(8, sz);
 
44
#else
 
45
        void* ptr = malloc((sz & 7) ? ((sz & 0xfffffffffffffff8) + 8) : (sz & 0xfffffffffffffff8));
 
46
#endif
 
47
        if (ptr == NULL) throw std::bad_alloc();
 
48
        if ((curmem += getSize(ptr)) > maxmem) maxmem = curmem;
 
49
        return ptr;
 
50
}
 
51
 
 
52
void* operator new[](std::size_t sz) throw (std::bad_alloc)
 
53
{
 
54
#if defined (__GNUC__) && !defined(__SVR4)
 
55
        void* ptr = memalign(8, sz);
 
56
#else
 
57
        void* ptr = malloc((sz & 7) ? ((sz & 0xfffffffffffffff8) + 8) : (sz & 0xfffffffffffffff8));
 
58
#endif
 
59
        if (ptr == NULL) throw std::bad_alloc();
 
60
        if ((curmem += getSize(ptr)) > maxmem) maxmem = curmem;
 
61
        return ptr;
 
62
}
 
63
 
 
64
void operator delete(void* ptr) throw()
 
65
{
 
66
        if (ptr) {
 
67
                curmem -= getSize(ptr);
 
68
                free(ptr);
 
69
        }
 
70
}
 
71
 
 
72
void operator delete[](void* ptr) throw()
 
73
{
 
74
        if (ptr) {
 
75
                curmem -= getSize(ptr);
 
76
                free(ptr);
 
77
        }
 
78
}
 
79
 
 
80
//-------------------------------------------------------------------------------------------------------------------------------------
 
81
// basic commandline render, no option overrides yet
 
82
 
 
83
#if 1
 
84
void dbout(const std::string &msg)
 
85
{
 
86
        std::cout << msg << std::endl;
 
87
}
 
88
 
 
89
int main(int argc, char* argv[])
 
90
{
 
91
        if (argc == 1) {
 
92
                std::cout << "Usage: " << argv[0] << " ribfile" << std::endl;
 
93
                return 0;
 
94
        }
 
95
 
 
96
        #ifdef WIN32
 
97
        // disable float exceptions
 
98
        _control87(MCW_EM, MCW_EM);
 
99
        #endif
 
100
 
 
101
        QDRender::RIBparse_t rp; //(dbout);
 
102
        rp.parse(argv[1]);
 
103
 
 
104
        std::cout << "Total memory allocated: " << maxmem <<  "( " <<  (maxmem/(float)(1 << 20)) << " Mb )\n" << std::endl;
 
105
}
 
106
#endif
 
107
 
 
108
//-------------------------------------------------------------------------------------------------------------------------------------
 
109
 
 
110
#if 0
 
111
// curves test
 
112
int main()
 
113
{
 
114
        #ifdef WIN32
 
115
        // disable float exceptions
 
116
        _control87(MCW_EM, MCW_EM);
 
117
        #endif
 
118
 
 
119
        RiBegin(NULL);
 
120
 
 
121
                //RiDisplay("curvehair.tga", RI_FILE, RI_RGB, RI_NULL);
 
122
                RiDisplay("curvehair", RI_FRAMEBUFFER, RI_RGB, RI_NULL);
 
123
                RtFloat fov = 55;
 
124
                RiProjection("perspective", RI_FOV, &fov, RI_NULL);
 
125
 
 
126
                //RiExposure(1.f, 0.666666f);
 
127
                RiFormat(800, 800, 1);
 
128
                RiPixelSamples(4, 4);
 
129
                // cropwindow is useful to only render parts of image when rendering huge number of hairs.
 
130
                //RiCropWindow(0, 0.5, 0, 0.5);
 
131
 
 
132
                RiTranslate(0, 0.1, 5);
 
133
 
 
134
                RiWorldBegin();
 
135
 
 
136
                        //RtFloat int1 = 0;
 
137
                        //RiLightSource("ambientlight", "intensity", &int1, RI_NULL);
 
138
 
 
139
                        RtPoint from = {6, 6, 6};
 
140
                        RtPoint to = {0, 0, 0};
 
141
                        RtFloat int2 = 1.5f;
 
142
                        RiLightSource("distantlight", "intensity", &int2, "from", &from, "to", &to, RI_NULL);
 
143
 
 
144
                        const float srad = 1.25f;
 
145
                        //RiAttributeBegin();
 
146
                        //RiSurface("matte", RI_NULL);
 
147
                        //RtColor sc = {0.25, 0.025, 0.0025};
 
148
                        //RiColor(sc);
 
149
                        //RiSphere(srad, -srad, srad, 360, RI_NULL);
 
150
                        //RiAttributeEnd();
 
151
 
 
152
                        //RtFloat rg = 0.15f, ks = 0.2, kd = 0.8;
 
153
                        RiSurface("hair", RI_NULL); //"roughness", &rg, "Ks", &ks, "Kd", &kd, RI_NULL);
 
154
                        //RtFloat sp1 = 0.1, sp2 = 0.03;
 
155
                        //RiSurface("SIG2k_srf_fur", "color rootcolor", &sc, "float SPEC1", &sp1, "float SPEC2", &sp2, RI_NULL);
 
156
 
 
157
                        // 1500000 hairs at 1000x1000 res with 8x8 samples and 4x4 sinc filter, 16 max gridsize:
 
158
                        // 10080772 prims, 5173370 grids, 39143712 mp's, +23285sec, 319.9Mb... horrible...
 
159
                        // (Cause of the bad rendertime is only having 512Mb, so it was basicaly all HD swapping,
 
160
                        //  taskmanager indicated a mem usage of about 770Mb, cpu was barely active at all...)
 
161
 
 
162
                        // +14.6sec 47.6Mb
 
163
                        // -16.4sec 28.6Mb with sqrt(maxgridsize) splitting
 
164
                        // +15.1sec 28.4Mb
 
165
                        // +15.5sec 26.7Mb, immediate split into individual segments (with forced no split 14.2s 47Mb)
 
166
                        // +15.3sec 24.4Mb separate saving of P, excluded from primvar
 
167
 
 
168
                        //RiBasis(RiBezierBasis, RI_BEZIERSTEP, RiBSplineBasis, RI_BSPLINESTEP);
 
169
                        // make a sphere with hair
 
170
                        const int numhairs = 100000;
 
171
                        const int numcp = 4; // number of control points, 4 minimum
 
172
                        RtPoint* pts = new RtPoint[numhairs*numcp];
 
173
                        RtInt* verts = new RtInt[numhairs];
 
174
                        RtFloat* wd = new RtFloat[numhairs*2]; // 4 cp, 2 varyings
 
175
                        const QDRender::stdPerlin_t ngen;
 
176
                        for (int i=0; i<numhairs; ++i) {
 
177
                                QDRender::Point3 sp = srad*QDRender::RandomVectorSphere();
 
178
                                if (sp.z > 0.f) sp *= -1.f; // all hairs on the font of the sphere
 
179
                                QDRender::Vector op = toVector(sp).normalize();
 
180
                                for (int j=0; j<numcp; ++j) {
 
181
                                        int icp = i*numcp + j;
 
182
                                        pts[icp][0] = sp.x, pts[icp][1] = sp.y, pts[icp][2] = sp.z;
 
183
                                        QDRender::Point3 tp = sp*4.38462f;
 
184
                                        QDRender::Point3 ofs(QDRender::getSignedNoise(&ngen, tp),
 
185
                                                             QDRender::getSignedNoise(&ngen, tp + QDRender::Point3(10.1)),
 
186
                                                             QDRender::getSignedNoise(&ngen, tp - QDRender::Point3(10.1)));
 
187
                                        ofs = 0.5f*(op + ofs);
 
188
                                        sp += (0.3f*QDRender::frand() + 0.2f)*ofs;
 
189
                                }
 
190
                                // add some random 'gravity' to the hair endpoint to make it slope downwards
 
191
                                pts[i*numcp + numcp - 1][1] -= 0.25f*QDRender::frand();
 
192
                                verts[i] = numcp;
 
193
                                wd[i*2] = 0.01f;
 
194
                                wd[i*2+1] = 0.001f;
 
195
                        }
 
196
                        RiCurves("cubic", numhairs, verts, "nonperiodic", "P", pts, "width", wd, RI_NULL);
 
197
                        //RtFloat cwd = 0.001f;
 
198
                        //RiCurves("cubic", numhairs, verts, "nonperiodic", "P", pts, "constantwidth", &cwd, RI_NULL);
 
199
                        // arrays can now be deleted here
 
200
                        //delete[] wd;
 
201
                        delete[] verts;
 
202
                        delete[] pts;
 
203
 
 
204
                RiWorldEnd();
 
205
 
 
206
        RiEnd();
 
207
 
 
208
        std::cout << "Total memory allocated: " << maxmem <<  "( " <<  (maxmem/(float)(1 << 20)) << " Mb )\n" << std::endl;
 
209
        return 0;
 
210
}
 
211
#endif
 
212
 
 
213
#if 0
 
214
// Points test
 
215
// while it works, memory usage is rather inefficient, just one million particles already use up 150-200Mb
 
216
// It is possible to rasterize directly though, no micropolys needed TODO
 
217
int main()
 
218
{
 
219
        #ifdef WIN32
 
220
        // disable float exceptions
 
221
        _control87(MCW_EM, MCW_EM);
 
222
        #endif
 
223
 
 
224
        RiBegin(NULL);
 
225
 
 
226
                RiDisplay("points test", RI_FRAMEBUFFER, RI_RGB, RI_NULL);
 
227
                RtFloat fov = 55;
 
228
                RiProjection("perspective", RI_FOV, &fov, RI_NULL);
 
229
 
 
230
                RiFormat(512, 512, 1);
 
231
                RiPixelSamples(4, 4);
 
232
                RiExposure(1.5, 0.75);
 
233
 
 
234
                RiTranslate(0, 0, 5);
 
235
 
 
236
                RiWorldBegin();
 
237
 
 
238
                        // constant color with additive blending
 
239
                        RiSurface("constadd", RI_NULL);
 
240
 
 
241
                        const float rad = 3;
 
242
                        const int nh3 = 50;
 
243
                        const int nump = (int)pow(nh3*2, 3);
 
244
                        RtPoint* pts = new RtPoint[nump];
 
245
                        RtColor* cols = new RtColor[nump];
 
246
                        const QDRender::blenderNoise_t ngen;
 
247
                        const float sc = 0.025;
 
248
                        const float csc = 0.0025;
 
249
                        int i = 0;
 
250
                        const float nh3d = 1.f/(2*nh3);
 
251
                        for (int z=-nh3; z<nh3; ++z) {
 
252
                                for (int y=-nh3; y<nh3; ++y) {
 
253
                                        for (int x=-nh3; x<nh3; ++x) {
 
254
                                                const QDRender::Point3 tp(x*sc, y*sc, z*sc);
 
255
                                                cols[i][0] = ngen(tp);
 
256
                                                cols[i][1] = ngen(tp + QDRender::Point3(1.984851f));
 
257
                                                cols[i][2] = ngen(tp - QDRender::Point3(5.8456f));
 
258
                                                //cols[i][0] = 0.5f + x*nh3d, cols[i][1] = 0.5f + y*nh3d, cols[i][2] = 0.5f + z*nh3d;
 
259
                                                pts[i][0] = rad * (2.f*cols[i][0] - 1.f);
 
260
                                                pts[i][1] = rad * (2.f*cols[i][1] - 1.f);
 
261
                                                pts[i][2] = rad * (2.f*cols[i][2] - 1.f);
 
262
                                                cols[i][0] *= csc, cols[i][1] *= csc, cols[i][2] *= csc;
 
263
                                                i++;
 
264
                                        }
 
265
                                }
 
266
                        }
 
267
                        std::cout << "TOTAL POINTS: " << nump << std::endl;
 
268
                        const float cw = 0.075;
 
269
                        RiTransformBegin();
 
270
                                RiRotate(45, 1, 0, 0);
 
271
                                RiPoints(nump, "P", pts, "constantwidth", &cw, "Cs", cols, RI_NULL);
 
272
                        RiTransformEnd();
 
273
                        delete[] cols;
 
274
                        delete[] pts;
 
275
 
 
276
                RiWorldEnd();
 
277
 
 
278
        RiEnd();
 
279
 
 
280
        std::cout << "Total memory allocated: " << maxmem <<  "( " <<  (maxmem/(float)(1 << 20)) << " Mb )\n" << std::endl;
 
281
        return 0;
 
282
}
 
283
#endif