root/OpenSceneGraph/trunk/examples/osgtexturecompression/osgtexturecompression.cpp @ 13266

Revision 12292, 7.6 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/* OpenSceneGraph example, osgtexture3D.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Node>
20#include <osg/Geometry>
21#include <osg/Notify>
22#include <osg/Texture2D>
23#include <osg/TexGen>
24#include <osg/Geode>
25
26#include <osgDB/ReadFile>
27
28#include <osgText/Text>
29
30#include <osgGA/TrackballManipulator>
31#include <osgViewer/CompositeViewer>
32
33#include <iostream>
34
35osg::Camera* createHUD(const std::string& label)
36{
37    // create a camera to set up the projection and model view matrices, and the subgraph to drawn in the HUD
38    osg::Camera* camera = new osg::Camera;
39
40    // set the projection matrix
41    camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
42
43    // set the view matrix   
44    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
45    camera->setViewMatrix(osg::Matrix::identity());
46
47    // only clear the depth buffer
48    camera->setClearMask(GL_DEPTH_BUFFER_BIT);
49
50    // draw subgraph after main camera view.
51    camera->setRenderOrder(osg::Camera::POST_RENDER);
52
53    // we don't want the camera to grab event focus from the viewers main camera(s).
54    camera->setAllowEventFocus(false);
55
56    // add to this camera a subgraph to render
57    {
58
59        osg::Geode* geode = new osg::Geode();
60
61        std::string font("fonts/arial.ttf");
62
63        // turn lighting off for the text and disable depth test to ensure its always ontop.
64        osg::StateSet* stateset = geode->getOrCreateStateSet();
65        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
66
67        osg::Vec3 position(150.0f,150.0f,0.0f);
68
69        osgText::Text* text = new  osgText::Text;
70        geode->addDrawable( text );
71
72        text->setFont(font);
73        text->setPosition(position);
74        text->setCharacterSize(100.0f);
75        text->setText(label);
76
77        camera->addChild(geode);
78    }
79
80    return camera;
81}
82
83osg::Node* creatQuad(const std::string& name,
84                     osg::Image* image,
85                     osg::Texture::InternalFormatMode formatMode,
86                     osg::Texture::FilterMode minFilter)
87{
88
89    osg::Group* group = new osg::Group;
90   
91    {
92        osg::Geode* geode = new osg::Geode;
93
94        geode->addDrawable(createTexturedQuadGeometry(
95                osg::Vec3(0.0f,0.0f,0.0f),
96                osg::Vec3(float(image->s()),0.0f,0.0f),
97                osg::Vec3(0.0f,0.0f,float(image->t()))));
98
99        geode->setName(name);
100
101        osg::StateSet* stateset = geode->getOrCreateStateSet();
102
103        osg::Texture2D* texture = new osg::Texture2D(image);
104        texture->setInternalFormatMode(formatMode);
105        texture->setFilter(osg::Texture::MIN_FILTER, minFilter);
106        stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
107       
108        group->addChild(geode);
109    }
110   
111    {
112        group->addChild(createHUD(name));
113    }
114   
115    return group;
116}
117
118int main(int argc, char** argv)
119{
120    osg::ArgumentParser arguments(&argc, argv);
121
122    // construct the viewer.
123    osgViewer::CompositeViewer viewer(arguments);
124   
125    if (arguments.argc()<=1)
126    {
127        std::cout<<"Please supply an image filename on the commnand line."<<std::endl;
128        return 1;
129    }
130   
131    std::string filename = arguments[1];
132    osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename);
133   
134    if (!image)
135    {
136        std::cout<<"Error: unable able to read image from "<<filename<<std::endl;
137        return 1;
138    }
139
140    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
141    if (!wsi)
142    {
143        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
144        return 1;
145    }
146
147
148    unsigned int width, height;
149    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
150
151    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
152    traits->x = 0;
153    traits->y = 0;
154    traits->width = width;
155    traits->height = height;
156    traits->windowDecoration = false;
157    traits->doubleBuffer = true;
158   
159    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
160    if (!gc)
161    {
162        std::cout<<"Error: GraphicsWindow has not been created successfully."<<std::endl;
163    }
164
165    gc->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
166    gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
167
168    osg::ref_ptr<osgGA::TrackballManipulator> trackball = new osgGA::TrackballManipulator;
169   
170    typedef std::vector< osg::ref_ptr<osg::Node> > Models;
171   
172    Models models;
173    models.push_back(creatQuad("no compression", image.get(), osg::Texture::USE_IMAGE_DATA_FORMAT, osg::Texture::LINEAR));
174    models.push_back(creatQuad("ARB compression", image.get(), osg::Texture::USE_ARB_COMPRESSION, osg::Texture::LINEAR));
175    models.push_back(creatQuad("DXT1 compression", image.get(), osg::Texture::USE_S3TC_DXT1_COMPRESSION, osg::Texture::LINEAR));
176    models.push_back(creatQuad("DXT3 compression", image.get(), osg::Texture::USE_S3TC_DXT3_COMPRESSION, osg::Texture::LINEAR));
177    models.push_back(creatQuad("DXT5 compression", image.get(), osg::Texture::USE_S3TC_DXT5_COMPRESSION, osg::Texture::LINEAR));
178   
179    int numX = 1;
180    int numY = 1;
181
182    // compute the number of views up and across that are need
183    {
184        float aspectRatio = float(width)/float(height);
185        float multiplier = sqrtf(float(models.size())/aspectRatio);;
186        float multiplier_x = multiplier*aspectRatio;
187        float multiplier_y = multiplier;
188
189
190        if ((multiplier_x/ceilf(multiplier_x)) > (multiplier_y/ceilf(multiplier_y)))
191        {
192            numX = int(ceilf(multiplier_x));
193            numY = int(ceilf(float(models.size())/float(numX)));
194        }
195        else
196        {
197            numY = int(ceilf(multiplier_y));
198            numX = int(ceilf(float(models.size())/float(numY)));
199        }
200    }
201
202    // populate the view with the required view to view each model.
203    for(unsigned int i=0; i<models.size(); ++i)
204    {
205        osgViewer::View* view = new osgViewer::View;
206       
207        int xCell = i % numX;
208        int yCell = i / numX;
209   
210        int vx = int((float(xCell)/float(numX)) * float(width));
211        int vy = int((float(yCell)/float(numY)) * float(height));
212        int vw =  int(float(width) / float(numX));
213        int vh =  int(float(height) / float(numY));
214
215        view->setSceneData(models[i].get());
216        view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(vw) / double(vh), 1.0, 1000.0);
217        view->getCamera()->setViewport(new osg::Viewport(vx, vy, vw, vh));   
218        view->getCamera()->setGraphicsContext(gc.get());
219        view->getCamera()->setClearMask(0);
220        view->setCameraManipulator(trackball.get());
221
222        viewer.addView(view);
223    }
224
225    return viewer.run();
226}
Note: See TracBrowser for help on using the browser.