Hi,
First of all, I would like to ask a permission to use and modify the SkyDome
used in the oceanExample and make it into a separate library. I know that it is
not really a part of osgOcean library, but I reckon that the devs should be the
same. So I asked the question here.
Like the title above, I wanted to change the SkyDome and make it to use a
repeating Texture2D instead of the TextureCubeMap. I need the SkyDome to
display the starfield repeatedly, while also making the texture stretching be
as natural as possible (ie. not overstretched like the current SkyDome). It
still didn't work because it showed the color array of the SphereSegment
geometry. The SphereSegment class is the same as the one in oceanExample.
However, I've reworked the SkyDome class. This is my SkyDome class.
Thank you!
Cheers,
Bawenang
Code:
SkyDome.h
class OSGATMOSPHERE_EXPORT SkyDome : public SphereSegment
{
public:
/**
* Sky Dome Parameters struct
*
*/
struct SkyDomeParams
{
float radius;
unsigned int longSteps;
unsigned int latSteps;
std::string starfieldTexName;
///Ctor
SkyDomeParams()
// : radius(10000)
: radius(1900)
, longSteps(4)
, latSteps(4)
, starfieldTexName("")
{
}
///Ctor with params
SkyDomeParams(float r, unsigned int longitudeStep, unsigned int
latitudeStep, const std::string& texName)
// : radius(10000)
: radius(r)
, longSteps(longitudeStep)
, latSteps(latitudeStep)
, starfieldTexName(texName)
{
}
///Copy Ctor
SkyDomeParams(const SkyDomeParams& copy)
{
this->radius = copy.radius;
this->longSteps = copy.longSteps;
this->latSteps = copy.latSteps;
this->starfieldTexName = copy.starfieldTexName;
}
///Assignment Op
SkyDomeParams& operator = (const SkyDomeParams& copy)
{
this->radius = copy.radius;
this->longSteps = copy.longSteps;
this->latSteps = copy.latSteps;
this->starfieldTexName = copy.starfieldTexName;
return *this;
}
///Dtor
~SkyDomeParams()
{
}
};
SkyDome( void );
SkyDome( const SkyDome& copy, const osg::CopyOp&
copyop=osg::CopyOp::SHALLOW_COPY );
SkyDome( float radius, unsigned int longSteps, unsigned int
latSteps, const std::string& starfieldTexName );
SkyDome( const SkyDomeParams& skyDomeParams );
protected:
~SkyDome(void);
public:
void setupStateSet( );
void create( float radius, unsigned int latSteps, unsigned int
longSteps, const std::string& starfieldTexName );
void create( const SkyDomeParams& skyDomeParams );
//++ BAWE-20111128: SKYDOME
/**
*
*/
inline void setStarField( const std::string& starfieldTexName )
{
if (!mStarfieldTexture.valid())
{
mStarfieldTexture = new osg::Texture2D;
_setTextureParams( );
}
_createTexture( starfieldTexName );
}
void showPolyLine(bool isShowLine);
//-- BAWE-20111128: SKYDOME
private:
osg::ref_ptr<osg::Program> _createShader(void);
void _setTextureParams( );
void _createTexture( const std::string& starfieldTexName );
void _createFromParams();
SkyDomeParams mSkyDomeParams;
bool mIsShowPolyLine;
osg::ref_ptr<osg::Texture2D> mStarfieldTexture;
};
Code:
SkyDome.cpp
SkyDome::SkyDome( void )
: mIsShowPolyLine (false)
, mSkyDomeParams ()
{
}
SkyDome::SkyDome( const SkyDome& copy, const osg::CopyOp& copyop ):
SphereSegment( copy, copyop )
{
}
SkyDome::SkyDome( float radius, unsigned int longSteps, unsigned int latSteps,
const std::string& starfieldTexName )
: mIsShowPolyLine (false)
, mSkyDomeParams ()
{
create( radius, longSteps, latSteps,starfieldTexName );
setupStateSet();
showPolyLine(false);
}
SkyDome::SkyDome( const SkyDomeParams& skyDomeParams )
: mIsShowPolyLine (false)
, mSkyDomeParams ()
{
create( skyDomeParams );
setupStateSet();
showPolyLine(false);
}
SkyDome::~SkyDome(void)
{
}
void SkyDome::create( float radius, unsigned int longSteps, unsigned int
latSteps, const std::string& starfieldTexName )
{
//compute( radius, longSteps, latSteps, 90.f, 180.f, 0.f, 360.f );
mSkyDomeParams.radius = radius;
mSkyDomeParams.latSteps = latSteps;
mSkyDomeParams.longSteps = longSteps;
mSkyDomeParams.starfieldTexName = starfieldTexName;
_createFromParams();
}
void SkyDome::create( const SkyDomeParams& skyDomeParams )
{
//compute( radius, longSteps, latSteps, 90.f, 180.f, 0.f, 360.f );
mSkyDomeParams = skyDomeParams;
_createFromParams();
}
void SkyDome::_createFromParams( )
{
//compute( radius, longSteps, latSteps, 90.f, 180.f, 0.f, 360.f );
compute( mSkyDomeParams.radius, mSkyDomeParams.longSteps,
mSkyDomeParams.latSteps, 0.f, 180.f, 0.f, 360.f );
setStarField(mSkyDomeParams.starfieldTexName);
}
void SkyDome::setupStateSet( )
{
osg::StateSet* ss = new osg::StateSet;
ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
ss->setAttributeAndModes( _createShader().get(), osg::StateAttribute::ON );
ss->addUniform( new osg::Uniform("uEnvironmentMap", 0) );
ss->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::OFF);
ss->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::ON);
ss->setTextureMode(0,GL_TEXTURE_3D,osg::StateAttribute::OFF);
setStateSet(ss);
}
osg::ref_ptr<osg::Program> SkyDome::_createShader(void)
{
osg::ref_ptr<osg::Program> program = new osg::Program;
// Do not use shaders if they were globally disabled.
if (ShaderManager::instance().areShadersEnabled())
{
char vertexSource[]=
"varying vec2 vTexCoord;\n"
"\n"
"void main(void)\n"
"{\n"
" gl_TexCoord[0] = gl_MultiTexCoord0;\n"
" vTexCoord = gl_Vertex.xy;\n"
"}\n";
char fragmentSource[]=
"uniform sampler2D uEnvironmentMap;\n"
"varying vec2 vTexCoord;\n"
"\n"
"void main(void)\n"
"{\n"
" gl_FragData[0] = texture2D( uEnvironmentMap, gl_TexCoord[0].st
);\n"
//" gl_FragData[0].a = 0.0;\n"
" gl_FragData[0].a = 1.0;\n"
" gl_FragData[1] = vec4(0.0);\n"
"}\n";
program->setName( "sky_dome_shader" );
program->addShader(new osg::Shader(osg::Shader::VERTEX,
vertexSource));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT,
fragmentSource));
}
return program;
}
void SkyDome::_setTextureParams( )
{
mStarfieldTexture->setInternalFormat(GL_RGBA);
mStarfieldTexture->setFilter( osg::Texture::MIN_FILTER,
osg::Texture::NEAREST_MIPMAP_NEAREST);
mStarfieldTexture->setFilter( osg::Texture::MAG_FILTER,
osg::Texture::NEAREST);
mStarfieldTexture->setWrap ( osg::Texture::WRAP_S,
osg::Texture::REPEAT);
mStarfieldTexture->setWrap ( osg::Texture::WRAP_T,
osg::Texture::REPEAT);
// mStarfieldTexture->setWrap ( osg::Texture::WRAP_T,
osg::Texture::CLAMP_TO_EDGE);
// mStarfieldTexture->setWrap ( osg::Texture::WRAP_S,
osg::Texture::CLAMP_TO_EDGE);
}
void SkyDome::_createTexture( const std::string& starfieldTexName )
{
mStarfieldTexture->setImage( osgDB::readImageFile( starfieldTexName ) );
getOrCreateStateSet()->setTextureAttributeAndModes( 0,
mStarfieldTexture.get(), osg::StateAttribute::ON );
}
void SkyDome::showPolyLine(bool isShowLine)
{
//
--------------------------------------------------------------------------------
// Grab the state set of the node, this will a StateSet if one does not
exist
//
--------------------------------------------------------------------------------
osg::StateSet *state = getOrCreateStateSet();
//
----------------------------------------------------------------------------------------------
// We need to retrieve the Polygon mode of the state set, and create one
if does not have one
//
----------------------------------------------------------------------------------------------
osg::PolygonMode *polyModeObj;
polyModeObj = dynamic_cast< osg::PolygonMode* >
( state->getAttribute( osg::StateAttribute::POLYGONMODE ));
if ( !polyModeObj ) {
polyModeObj = new osg::PolygonMode;
state->setAttribute( polyModeObj );
}
// --------------------------------------------
// Now we can set the state to WIREFRAME
// --------------------------------------------
if (isShowLine)
polyModeObj->setMode( osg::PolygonMode::FRONT_AND_BACK,
osg::PolygonMode::LINE );
else
polyModeObj->setMode( osg::PolygonMode::FRONT_AND_BACK,
osg::PolygonMode::FILL );
mIsShowPolyLine = isShowLine;
}
So what went wrong here? Is it because of the texture mapping with the texcoord
array in SphereSegment? Thanks in advance.
------------------------
"There's no place like 127.0.0.1"
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=44155#44155
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org