~ubuntu-branches/ubuntu/quantal/aqsis/quantal

« back to all changes in this revision

Viewing changes to libs/core/mpdump.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2009-08-06 04:53:26 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090806045326-z6xeaaao62idxcc6
Tags: 1.6.0-0ubuntu1
* New upstream release
* debian/control:
  - changed name of lib package to libaqsis1 instead of aqsis-libsc2a
  - changed name of dev package to libaqsis-dev instead of aqsis-libs-dev
  - Added aqsis-data package
  - Revised summary text according to that specified by the RISpec (Pixar)
* Moved examples installation from aqsis.install to aqsis-data.install
* debian/rules: 
  - added content to binary-indep target
* debian/rules: added explicit name of mime file to force dh_installmime
  to generate postinst and prerm scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright (C) 1997 - 2001, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.org
 
5
//
 
6
// This library is free software; you can redistribute it and/or
 
7
// modify it under the terms of the GNU General Public
 
8
// License as published by the Free Software Foundation; either
 
9
// version 2 of the License, or (at your option) any later version.
 
10
//
 
11
// This library is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
// General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public
 
17
// License along with this library; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
 
 
21
#include "mpdump.h"
 
22
#include "micropolygon.h"
 
23
#include "bucketprocessor.h"
 
24
 
 
25
namespace Aqsis {
 
26
 
 
27
// Constructor
 
28
CqMPDump::CqMPDump()
 
29
        : m_outFile(NULL),
 
30
        m_mpcount(0)
 
31
{}
 
32
 
 
33
 
 
34
 
 
35
// Destructor
 
36
CqMPDump::~CqMPDump()
 
37
{
 
38
        close();
 
39
}
 
40
 
 
41
// Open the dump file
 
42
void CqMPDump::open()
 
43
{
 
44
        char filename[20] = "mpdump.mp";
 
45
        int sf = sizeof(TqFloat);
 
46
 
 
47
        close();
 
48
        m_mpcount = 0;
 
49
        m_outFile = fopen(filename, "wb");
 
50
        if (m_outFile!=NULL)
 
51
        {
 
52
                Aqsis::log() << info << "Creating '" << filename << "'" << std::endl;
 
53
                size_t len_written = fwrite((void*)&sf, sizeof(int), 1, m_outFile);
 
54
                if(len_written != 1)
 
55
                        AQSIS_THROW_XQERROR(XqInvalidFile, EqE_System,
 
56
                                "Error writing mpdump file");
 
57
        }
 
58
        else
 
59
                Aqsis::log() << error << "Could not create '" << filename << "'" << std::endl;
 
60
}
 
61
 
 
62
// Close the dump file
 
63
void CqMPDump::close()
 
64
{
 
65
        if (m_outFile!=NULL)
 
66
        {
 
67
                fclose(m_outFile);
 
68
                m_outFile=NULL;
 
69
                Aqsis::log() << info << m_mpcount << " micro polygons dumped" << std::endl;
 
70
        }
 
71
}
 
72
 
 
73
// Dump global information about the image
 
74
void CqMPDump::dumpImageInfo()
 
75
{
 
76
        short id = 3;
 
77
 
 
78
        if (m_outFile==NULL)
 
79
        {
 
80
                Aqsis::log() << error << "Attempted to write to unopened mpdump file." << std::endl;
 
81
                return;
 
82
        }
 
83
 
 
84
        int width = QGetRenderContext() ->poptCurrent()->GetIntegerOption( "System", "Resolution" ) [ 0 ];
 
85
        int height = QGetRenderContext() ->poptCurrent()->GetIntegerOption( "System", "Resolution" ) [ 1 ];
 
86
        size_t len_written = fwrite((void*)&id, sizeof(short), 1, m_outFile);
 
87
        len_written += fwrite((void*)&width, sizeof(int), 1, m_outFile);
 
88
        len_written += fwrite((void*)&height, sizeof(int), 1, m_outFile);
 
89
        if(len_written != 3)
 
90
                AQSIS_THROW_XQERROR(XqInvalidFile, EqE_System,
 
91
                                "Error writing mpdump file");
 
92
}
 
93
 
 
94
// Dump all pixel samples of the current bucket
 
95
void CqMPDump::dumpPixelSamples(const CqBucketProcessor& bp)
 
96
{
 
97
        const std::vector<CqImagePixelPtr>& pixels = bp.pixels();
 
98
        for(std::vector<CqImagePixelPtr>::const_iterator p = pixels.begin(),
 
99
                        e = pixels.end(); p != e; ++p)
 
100
        {
 
101
                const CqImagePixel& pixel = **p;
 
102
                for(int i = 0, numSamples = pixel.numSamples(); i < numSamples; ++i)
 
103
                {
 
104
                        CqVector2D pos = pixel.SampleData(i).position;
 
105
                        if(!(  pos.x() <= bp.SampleRegion().xMin()
 
106
                                || pos.x() > bp.SampleRegion().xMax()
 
107
                                || pos.y() <= bp.SampleRegion().yMin()
 
108
                                || pos.y() > bp.SampleRegion().yMax() ) )
 
109
                        {
 
110
                                // Only dump samples which are inside bp.SampleRegion()
 
111
                                // this means that only samples which are actually computed for
 
112
                                // the current bucket will be considered.
 
113
                                dump(lfloor(pos.x()), lfloor(pos.y()), i, pos);
 
114
                        }
 
115
                }
 
116
        }
 
117
}
 
118
 
 
119
// Dump a pixel sample
 
120
void CqMPDump::dump(int x, int y, int idx, const CqVector2D& pos)
 
121
{
 
122
        short id = 2;
 
123
        TqFloat f;
 
124
 
 
125
        if (m_outFile==NULL)
 
126
        {
 
127
                Aqsis::log() << error << "Attempted to write to unopened mpdump file." << std::endl;
 
128
                return;
 
129
        }
 
130
 
 
131
        size_t len_written = fwrite((void*)&id, sizeof(short), 1, m_outFile);
 
132
        len_written += fwrite((void*)&x, sizeof(int), 1, m_outFile);
 
133
        len_written += fwrite((void*)&y, sizeof(int), 1, m_outFile);
 
134
        len_written += fwrite((void*)&idx, sizeof(int), 1, m_outFile);
 
135
        f = pos.x();
 
136
        len_written += fwrite((void*)&f, sizeof(TqFloat), 1, m_outFile);
 
137
        f = pos.y();
 
138
        len_written += fwrite((void*)&f, sizeof(TqFloat), 1, m_outFile);
 
139
        if(len_written != 6)
 
140
                AQSIS_THROW_XQERROR(XqInvalidFile, EqE_System,
 
141
                                "Error writing mpdump file");
 
142
}
 
143
 
 
144
// Dump a micro polygon
 
145
void CqMPDump::dump(const CqMicroPolygon& mp)
 
146
{
 
147
        CqColor c;
 
148
        short id = 1;
 
149
 
 
150
        if (m_outFile==NULL)
 
151
        {
 
152
                Aqsis::log() << error << "Attempted to write to unopened mpdump file." << std::endl;
 
153
                return;
 
154
        }
 
155
 
 
156
        m_mpcount++;
 
157
        size_t len_written = fwrite((void*)&id, sizeof(short), 1, m_outFile);
 
158
        if(len_written != 1)
 
159
                AQSIS_THROW_XQERROR(XqInvalidFile, EqE_System,
 
160
                                "Error writing mpdump file");
 
161
 
 
162
        // Dump vertices in a funny circular order for backward-compatibility
 
163
        // rather than in the usual bilinear patch type order.
 
164
        CqVector3D P[4];
 
165
        mp.GetVertices(P);
 
166
        dumpVec3(P[0]);
 
167
        dumpVec3(P[1]);
 
168
        dumpVec3(P[3]);
 
169
        dumpVec3(P[2]);
 
170
        if (mp.pGrid()->pVar(EnvVars_Ci)!=NULL)
 
171
                c = *mp.colColor();
 
172
        else
 
173
                c = CqColor(0.9,0.9,1);
 
174
        dumpCol(c);
 
175
        if (mp.pGrid()->pVar(EnvVars_Oi)!=NULL)
 
176
                c = *mp.colOpacity();
 
177
        else
 
178
                c = CqColor(0.9,0.9,1);
 
179
        dumpCol(c);
 
180
}
 
181
 
 
182
// Dump a 3d vector
 
183
void CqMPDump::dumpVec3(const CqVector3D& v)
 
184
{
 
185
        TqFloat x = v.x();
 
186
        TqFloat y = v.y();
 
187
        TqFloat z = v.z();
 
188
 
 
189
        size_t len_written = fwrite((void*)&x, sizeof(TqFloat), 1, m_outFile);
 
190
        len_written += fwrite((void*)&y, sizeof(TqFloat), 1, m_outFile);
 
191
        len_written += fwrite((void*)&z, sizeof(TqFloat), 1, m_outFile);
 
192
        if(len_written != 3)
 
193
                AQSIS_THROW_XQERROR(XqInvalidFile, EqE_System,
 
194
                                "Error writing mpdump file");
 
195
}
 
196
 
 
197
// Dump a color
 
198
void CqMPDump::dumpCol(const CqColor& c)
 
199
{
 
200
        TqFloat r = c.r();
 
201
        TqFloat g = c.g();
 
202
        TqFloat b = c.b();
 
203
 
 
204
        size_t len_written = fwrite((void*)&r, sizeof(TqFloat), 1, m_outFile);
 
205
        len_written += fwrite((void*)&g, sizeof(TqFloat), 1, m_outFile);
 
206
        len_written += fwrite((void*)&b, sizeof(TqFloat), 1, m_outFile);
 
207
        if(len_written != 3)
 
208
                AQSIS_THROW_XQERROR(XqInvalidFile, EqE_System,
 
209
                                "Error writing mpdump file");
 
210
}
 
211
 
 
212
 
 
213
/// Global dump object
 
214
CqMPDump mpdump;
 
215
 
 
216
} // namespace Aqsis
 
217