root/OpenSceneGraph/trunk/include/osgUtil/Statistics @ 3526

Revision 2300, 6.6 kB (checked in by robert, 14 years ago)

Warning fixes for Win32.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSGUTIL_STATISTICS
15#define OSGUTIL_STATISTICS 1
16
17#include <osg/Referenced>
18#include <osg/Drawable>
19
20#include <map>
21
22namespace osgUtil {
23
24/**
25 * Statistics base class. Used to extract primitive information from
26 * the renderBin(s).  Add a case of getStats(osgUtil::Statistics *stat)
27 * for any new drawable (or drawable derived class) that you generate
28 * (eg see Geometry.cpp).  There are 20 types of drawable counted - actually only
29 * 14 cases can occur in reality.  these represent sets of GL_POINTS, GL_LINES
30 * GL_LINESTRIPS, LOOPS, TRIANGLES, TRI-fans, tristrips, quads, quadstrips etc
31 * The number of triangles rendered is inferred:
32 * each triangle = 1 triangle (number of vertices/3)
33 * each quad = 2 triangles (nverts/2)
34 * each trifan or tristrip = (length-2) triangles and so on.
35 */
36
37class Statistics : public osg::Drawable::PrimitiveFunctor
38{
39    public:
40
41        typedef std::pair<unsigned int,unsigned int>    PrimitivePair;
42        typedef std::map<GLenum,PrimitivePair>          PrimtiveValueMap;
43        typedef std::map<GLenum, unsigned int>          PrimtiveCountMap;
44
45
46        Statistics()
47        {
48            reset();
49        };
50
51        enum statsType
52        {
53            STAT_NONE, // default
54            STAT_FRAMERATE,
55            STAT_GRAPHS,
56            STAT_PRIMS,
57            STAT_PRIMSPERVIEW,
58            STAT_PRIMSPERBIN,
59            STAT_DC,
60            STAT_RESTART // hint to restart the stats
61        };
62       
63        void reset()
64        {
65            numDrawables=0, nummat=0; depth=0; stattype=STAT_NONE;
66            nlights=0; nbins=0; nimpostor=0;
67           
68            _vertexCount=0;
69            _primitiveCount.clear();           
70           
71            _currentPrimtiveFunctorMode=0;
72        }
73
74        void setType(statsType t) {stattype=t;}
75       
76        virtual void setVertexArray(unsigned int count,const osg::Vec3*) { _vertexCount += count; }
77        virtual void setVertexArray(unsigned int count,const osg::Vec2*) { _vertexCount += count; }
78        virtual void setVertexArray(unsigned int count,const osg::Vec4*) { _vertexCount += count; }
79
80        virtual void drawArrays(GLenum mode,GLint,GLsizei count)
81        {
82            PrimitivePair& prim = _primitiveCount[mode];
83            ++prim.first;
84            prim.second+=count;
85            _primitives_count[mode] += _calculate_primitives_number_by_mode(mode, count);
86        }
87        virtual void drawElements(GLenum mode,GLsizei count,const GLubyte*)
88        {
89            PrimitivePair& prim = _primitiveCount[mode];
90            ++prim.first;
91            prim.second+=count;
92            _primitives_count[mode] += _calculate_primitives_number_by_mode(mode, count);
93        }
94        virtual void drawElements(GLenum mode,GLsizei count,const GLushort*)
95        {
96            PrimitivePair& prim = _primitiveCount[mode];
97            ++prim.first;
98            prim.second+=count;
99            _primitives_count[mode] += _calculate_primitives_number_by_mode(mode, count);
100        }
101        virtual void drawElements(GLenum mode,GLsizei count,const GLuint*)
102        {
103            PrimitivePair& prim = _primitiveCount[mode];
104            ++prim.first;
105            prim.second+=count;
106            _primitives_count[mode] += _calculate_primitives_number_by_mode(mode, count);
107        }
108
109        virtual void begin(GLenum mode)
110        {
111            _currentPrimtiveFunctorMode=mode;
112            PrimitivePair& prim = _primitiveCount[mode];
113            ++prim.first;
114            _number_of_vertexes = 0;
115        }
116
117        inline void vertex()
118        {
119            PrimitivePair& prim = _primitiveCount[_currentPrimtiveFunctorMode];
120            ++prim.second;
121           _number_of_vertexes++;
122        }
123        virtual void vertex(float,float,float) { vertex(); }
124        virtual void vertex(const osg::Vec3&)  { vertex(); }
125        virtual void vertex(const osg::Vec2&)  { vertex(); }
126        virtual void vertex(const osg::Vec4&)  { vertex(); }
127        virtual void vertex(float,float)   { vertex(); }
128        virtual void vertex(float,float,float,float)  { vertex(); }
129
130        virtual void end()
131        {
132          _primitives_count[_currentPrimtiveFunctorMode] +=
133            _calculate_primitives_number_by_mode(_currentPrimtiveFunctorMode, _number_of_vertexes);
134        }
135       
136        void addDrawable() { numDrawables++;}
137        void addMatrix() { nummat++;}
138        void addLight(int np) { nlights+=np;}
139        void addImpostor(int np) { nimpostor+= np; }
140        inline int getBins() { return nbins;}
141        void setDepth(int d) { depth=d; }
142        void addBins(int np) { nbins+= np; }
143
144        void setBinNo(int n) { _binNo=n;}       
145               
146    public:
147               
148        PrimtiveCountMap::iterator GetPrimitivesBegin() { return _primitives_count.begin(); }
149        PrimtiveCountMap::iterator GetPrimitivesEnd() { return _primitives_count.end(); }
150
151        int numDrawables, nummat, nbins;
152        int nlights;
153        int depth; // depth into bins - eg 1.1,1.2,1.3 etc
154        int _binNo;
155        statsType stattype;
156        int nimpostor; // number of impostors rendered
157       
158        unsigned int        _vertexCount;
159        PrimtiveValueMap    _primitiveCount;
160        GLenum              _currentPrimtiveFunctorMode;
161
162    private:
163        PrimtiveCountMap                     _primitives_count;
164
165        unsigned int                         _total_primitives_count;
166        unsigned int                         _number_of_vertexes;
167
168        inline unsigned int _calculate_primitives_number_by_mode(GLenum, GLsizei);
169};
170
171inline unsigned int Statistics::_calculate_primitives_number_by_mode(GLenum mode, GLsizei count)
172{
173  switch (mode)
174    {
175    case GL_POINTS:
176    case GL_LINE_LOOP:
177    case GL_POLYGON:  return count;
178    case GL_LINES: return count / 2;
179    case GL_LINE_STRIP: return count - 1;
180    case GL_TRIANGLES: return count / 3;
181    case GL_TRIANGLE_STRIP:
182    case GL_TRIANGLE_FAN: return count - 2;
183    case GL_QUADS: return count / 4;
184    case GL_QUAD_STRIP: return count - 3;
185    default: return 0;
186    }
187}
188
189}
190
191#endif
Note: See TracBrowser for help on using the browser.