/usr/share/openscenegraph/examples/osggameoflife/GameOfLifePass.cpp is in openscenegraph-examples 3.2.0~rc1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /* OpenSceneGraph example, osggameoflife.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "GameOfLifePass.h"
#include <osgDB/FileUtils>
#include <iostream>
ProcessPass::ProcessPass(osg::TextureRectangle *in_tex,
osg::TextureRectangle *out_tex,
int width, int height):
_TextureWidth(width),
_TextureHeight(height)
{
_RootGroup = new osg::Group;
_InTexture = in_tex;
_OutTexture = out_tex;
_Camera = new osg::Camera;
setupCamera();
_Camera->addChild(createTexturedQuad().get());
_RootGroup->addChild(_Camera.get());
setShader("shaders/gameoflife.frag");
}
ProcessPass::~ProcessPass()
{
}
osg::ref_ptr<osg::Group> ProcessPass::createTexturedQuad()
{
osg::ref_ptr<osg::Group> top_group = new osg::Group;
osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
// counter-clockwise
quad_coords->push_back(osg::Vec3d(0, 0, -1));
quad_coords->push_back(osg::Vec3d(1, 0, -1));
quad_coords->push_back(osg::Vec3d(1, 1, -1));
quad_coords->push_back(osg::Vec3d(0, 1, -1));
osg::ref_ptr<osg::Vec2Array> quad_tcoords = new osg::Vec2Array; // texture coords
quad_tcoords->push_back(osg::Vec2(0, 0));
quad_tcoords->push_back(osg::Vec2(_TextureWidth, 0));
quad_tcoords->push_back(osg::Vec2(_TextureWidth, _TextureHeight));
quad_tcoords->push_back(osg::Vec2(0, _TextureHeight));
osg::ref_ptr<osg::Geometry> quad_geom = new osg::Geometry;
osg::ref_ptr<osg::DrawArrays> quad_da = new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4);
osg::ref_ptr<osg::Vec4Array> quad_colors = new osg::Vec4Array;
quad_colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
quad_geom->setVertexArray(quad_coords.get());
quad_geom->setTexCoordArray(0, quad_tcoords.get());
quad_geom->addPrimitiveSet(quad_da.get());
quad_geom->setColorArray(quad_colors.get(), osg::Array::BIND_OVERALL);
_StateSet = quad_geom->getOrCreateStateSet();
_StateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
_StateSet->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
_StateSet->setTextureAttributeAndModes(0, _InTexture.get(), osg::StateAttribute::ON);
_StateSet->addUniform(new osg::Uniform("textureIn", 0));
quad_geode->addDrawable(quad_geom.get());
top_group->addChild(quad_geode.get());
return top_group;
}
void ProcessPass::setupCamera()
{
// clearing
_Camera->setClearMask(GL_DEPTH_BUFFER_BIT);
// projection and view
_Camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1));
_Camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
_Camera->setViewMatrix(osg::Matrix::identity());
// viewport
_Camera->setViewport(0, 0, _TextureWidth, _TextureHeight);
_Camera->setRenderOrder(osg::Camera::PRE_RENDER);
_Camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
_Camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER), _OutTexture.get());
}
void ProcessPass::setShader(std::string filename)
{
std::string foundFile = osgDB::findDataFile(filename);
if (foundFile.empty())
{
osg::notify(osg::NOTICE)<<"Could not file shader file: "<<filename<<std::endl;
return;
}
osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT );
fshader->loadShaderSourceFromFile(foundFile);
_FragmentProgram = 0;
_FragmentProgram = new osg::Program;
_FragmentProgram->addShader(fshader.get());
_StateSet->setAttributeAndModes(_FragmentProgram.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
}
////////////////////////////////////////////////////////////////////////
GameOfLifePass::GameOfLifePass(osg::Image *in_image)
{
_TextureWidth = in_image->s();
_TextureHeight = in_image->t();
_RootGroup = new osg::Group;
_BranchSwith[0] = new osg::Switch;
_BranchSwith[1] = new osg::Switch;
_RootGroup->addChild(_BranchSwith[0].get());
_RootGroup->addChild(_BranchSwith[1].get());
_ActiveBranch = 0;
activateBranch();
createOutputTextures();
_InOutTextureLife[0]->setImage(in_image);
_ProcessPass[0] = new ProcessPass(_InOutTextureLife[0].get(),
_InOutTextureLife[1].get(),
_TextureWidth, _TextureHeight);
// For the other pass, the input/output textures are flipped
_ProcessPass[1] = new ProcessPass(_InOutTextureLife[1].get(),
_InOutTextureLife[0].get(),
_TextureWidth, _TextureHeight);
_BranchSwith[0]->addChild(_ProcessPass[0]->getRoot().get());
_BranchSwith[1]->addChild(_ProcessPass[1]->getRoot().get());
}
GameOfLifePass::~GameOfLifePass()
{
delete _ProcessPass[0];
delete _ProcessPass[1];
}
osg::ref_ptr<osg::TextureRectangle> GameOfLifePass::getOutputTexture()
{
int out_tex = (_ActiveBranch == 0) ? 1 : 0;
return _ProcessPass[out_tex]->getOutputTexture();
}
void GameOfLifePass::activateBranch()
{
int onb = _ActiveBranch;
int offb = (onb == 1) ? 0 : 1;
_BranchSwith[onb]->setAllChildrenOn();
_BranchSwith[offb]->setAllChildrenOff();
}
void GameOfLifePass::flip()
{
_ActiveBranch = (_ActiveBranch == 1) ? 0 : 1;
activateBranch();
}
void GameOfLifePass::createOutputTextures()
{
for (int i=0; i<2; i++) {
_InOutTextureLife[i] = new osg::TextureRectangle;
_InOutTextureLife[i]->setTextureSize(_TextureWidth, _TextureHeight);
_InOutTextureLife[i]->setInternalFormat(GL_RGBA);
_InOutTextureLife[i]->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::NEAREST);
_InOutTextureLife[i]->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::NEAREST);
}
}
|