root/OpenSceneGraph/trunk/examples/osgwidgetperformance/osgwidgetperformance.cpp @ 13330

Revision 12292, 4.0 kB (checked in by robert, 3 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
Line 
1#include <iostream>
2#include <osg/ArgumentParser>
3#include <osgDB/ReadFile>
4#include <osgWidget/Canvas>
5#include <osgWidget/WindowManager>
6#include <osgWidget/Util>
7
8void setupArguments(osg::ArgumentParser& args) {
9    args.getApplicationUsage()->setDescription(
10        args.getApplicationName() + " is a performance testing application for osgWidget."
11    );
12
13    args.getApplicationUsage()->setCommandLineUsage(
14        args.getApplicationName() + " [options] widgets"
15    );
16
17    args.getApplicationUsage()->addCommandLineOption(
18        "--width <int>",
19        "The WindowManager width."
20    );
21
22    args.getApplicationUsage()->addCommandLineOption(
23        "--height <int>",
24        "The WindowManager height."
25    );
26
27    args.getApplicationUsage()->addCommandLineOption(
28        "--size <int>",
29        "The size of the square Widgets."
30    );
31
32    args.getApplicationUsage()->addCommandLineOption(
33        "--single-window",
34        "All widgets are put inside a single Window."
35    );
36
37    args.getApplicationUsage()->addCommandLineOption(
38        "--multi-window",
39        "All widgets are in their own Windows."
40    );
41}
42
43void readSize(osg::ArgumentParser& args, const char* opt, unsigned int& val) {
44    std::string size;
45
46    while(args.read(opt, size)) {
47        int s = std::atoi(size.c_str());
48
49        if(s > 0) val = s;
50    }
51}
52
53int doError(const char* errorMsg) {
54    osgWidget::warn() << errorMsg << std::endl;
55
56    return 1;
57}
58
59int doApp(osgViewer::Viewer& viewer, osg::Node* node, unsigned int width, unsigned int height) {
60    osgWidget::WindowManager* wm = new osgWidget::WindowManager(&viewer, width, height, 0x12);
61
62    wm->addChild(node);
63
64    return osgWidget::createExample(viewer, wm);
65}
66
67int main(int argc, char** argv) {
68    osg::ArgumentParser args(&argc, argv);
69
70    setupArguments(args);
71
72    osgViewer::Viewer viewer(args);
73
74    while(args.read("--help")) {
75        args.getApplicationUsage()->write(
76            std::cout,
77            osg::ApplicationUsage::COMMAND_LINE_OPTION
78        );
79
80        return 0;
81    }
82
83    std::string size;
84
85    unsigned int width        = 1280;
86    unsigned int height       = 1024;
87    unsigned int wSize        = 10;
88    bool         singleWindow = false;
89    bool         multiWindow  = false;
90
91    readSize(args, "--width", width);
92    readSize(args, "--height", height);
93    readSize(args, "--size", wSize);
94
95    while(args.read("--single-window")) singleWindow = true;
96   
97    while(args.read("--multi-window")) multiWindow = true;
98
99    unsigned int numWidgets = 0;
100
101    if(args.argc() >= 2) numWidgets = std::atoi(args[1]);
102
103    else return doError("Please specify the number of Widgets to use.");
104
105    if(numWidgets <= 0) return doError("Please specify one or more Widgets to use.");
106
107    if(!singleWindow && !multiWindow) return doError(
108        "Please specify one of --single-window or --multi-window."
109    );
110
111    if(singleWindow) {
112        osgWidget::Canvas* canvas = new osgWidget::Canvas("canvas");
113
114        canvas->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f);
115
116        unsigned int rows = height / (wSize + 2);
117        unsigned int cols = (numWidgets / rows) + 1;
118        unsigned int w    = 0;
119
120        /*
121        osg::Image*     image   = osgDB::readImageFile("osgWidget/natascha.png");
122        osg::Texture2D* texture = new osg::Texture2D();
123
124        texture->setImage(0, image);
125        */
126
127        for(unsigned int c = 0; c < cols; c++) {
128            for(unsigned int r = 0; r < rows; r++) {
129                if(w >= numWidgets) break;
130
131                osgWidget::Widget* widget = new osgWidget::Widget(
132                    "",
133                    wSize,
134                    wSize
135                );
136
137                float col = static_cast<float>(w) / static_cast<float>(numWidgets);
138
139                widget->setColor(col, col, col, 0.9f);
140                // widget->setTexture(texture, true);
141
142                canvas->addWidget(widget, c * (wSize + 2), r * (wSize + 2));
143
144                w++;
145            }
146        }
147
148        return doApp(viewer, canvas, width, height);
149    }
150
151    else doError("Not supported yet.");
152
153    return 1;
154}
Note: See TracBrowser for help on using the browser.