SkyDome.cpp
Go to the documentation of this file.
00001 /*
00002  * This source file is part of the osgOcean library
00003  *
00004  * Copyright (C) 2009 Kim Bale
00005  * Copyright (C) 2009 The University of Hull, UK
00006  *
00007  * This program is free software; you can redistribute it and/or modify it under
00008  * the terms of the GNU Lesser General Public License as published by the Free Software
00009  * Foundation; either version 3 of the License, or (at your option) any later
00010  * version.
00011 
00012  * This program is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00015  * http://www.gnu.org/copyleft/lesser.txt.
00016  */
00017 
00018 #include <uwsim/SkyDome.h>
00019 #include <osgOcean/ShaderManager>
00020 
00021 SkyDome::SkyDome(void)
00022 {
00023 
00024 }
00025 
00026 SkyDome::SkyDome(const SkyDome& copy, const osg::CopyOp& copyop) :
00027     SphereSegment(copy, copyop)
00028 {
00029 
00030 }
00031 
00032 SkyDome::SkyDome(float radius, unsigned int longSteps, unsigned int latSteps, osg::TextureCubeMap* cubemap)
00033 {
00034   compute(radius, longSteps, latSteps, 90.f, 180.f, 0.f, 360.f);
00035   setupStateSet(cubemap);
00036 }
00037 
00038 SkyDome::~SkyDome(void)
00039 {
00040 }
00041 
00042 void SkyDome::create(float radius, unsigned int latSteps, unsigned int longSteps, osg::TextureCubeMap* cubemap)
00043 {
00044   compute(radius, longSteps, latSteps, 90.f, 180.f, 0.f, 360.f);
00045   setupStateSet(cubemap);
00046 }
00047 
00048 void SkyDome::setupStateSet(osg::TextureCubeMap* cubemap)
00049 {
00050   osg::StateSet* ss = new osg::StateSet;
00051 
00052   ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
00053   ss->setTextureAttributeAndModes(0, cubemap, osg::StateAttribute::ON);
00054   ss->setAttributeAndModes(createShader().get(), osg::StateAttribute::ON);
00055   ss->addUniform(new osg::Uniform("uEnvironmentMap", 0));
00056 
00057   setStateSet(ss);
00058 }
00059 
00060 osg::ref_ptr<osg::Program> SkyDome::createShader(void)
00061 {
00062   osg::ref_ptr < osg::Program > program = new osg::Program;
00063 
00064   // Do not use shaders if they were globally disabled.
00065   if (osgOcean::ShaderManager::instance().areShadersEnabled())
00066   {
00067     char vertexSource[] = "varying vec3 vTexCoord;\n"
00068         "\n"
00069         "void main(void)\n"
00070         "{\n"
00071         "    gl_Position = ftransform();\n"
00072         "    vTexCoord = gl_Vertex.xyz;\n"
00073         "}\n";
00074 
00075     char fragmentSource[] = "uniform samplerCube uEnvironmentMap;\n"
00076         "varying vec3 vTexCoord;\n"
00077         "\n"
00078         "void main(void)\n"
00079         "{\n"
00080         "   vec3 texcoord = vec3(vTexCoord.x, vTexCoord.y, -vTexCoord.z);\n"
00081         "   gl_FragData[0] = textureCube( uEnvironmentMap, texcoord.xzy );\n"
00082         "   gl_FragData[0].a = 0.0;\n"
00083         "   gl_FragData[1] = vec4(0.0);\n"
00084         "}\n";
00085 
00086     program->setName("sky_dome_shader");
00087     program->addShader(new osg::Shader(osg::Shader::VERTEX, vertexSource));
00088     program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentSource));
00089   }
00090 
00091   return program;
00092 }


uwsim
Author(s): Mario Prats
autogenerated on Mon Oct 6 2014 08:24:07