OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

SkyDome with a repeating Texture2D


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> osgOcean [osgOcean]
View previous topic :: View next topic  
Author Message
benang
User


Joined: 31 Jan 2011
Posts: 21

PostPosted: Thu Dec 01, 2011 11:46 am    Post subject:
SkyDome with a repeating Texture2D
Reply with quote

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 MYLIBRARY_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)
{
    osg::StateSet *state = getOrCreateStateSet();

    osg::PolygonMode *polyModeObj;

    polyModeObj = dynamic_cast< osg::PolygonMode* >
        ( state->getAttribute( osg::StateAttribute::POLYGONMODE ));

    if ( !polyModeObj ) {

        polyModeObj = new osg::PolygonMode;

        state->setAttribute( polyModeObj );   
    }


    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? And how do I fix this? Thanks in advance.

_________________
"There's no place like 127.0.0.1"
Back to top
View user's profile Send private message
kcbale
User


Joined: 04 Mar 2011
Posts: 68

PostPosted: Mon Dec 05, 2011 9:56 am    Post subject:
SkyDome with a repeating Texture2D
Reply with quote

Hi Bawenang,

I don't see a problem with that, the skydome class was only really created to dress up the example code.


Regards,


Kim.




On 1 December 2011 11:46, Bawenang Rukmoko < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
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:ShockedN );
   ss->addUniform( new osg::Uniform("uEnvironmentMap", 0) );
   ss->setTextureMode(0,GL_TEXTURE_1D,osg::StateAttribute::OFF);
   ss->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute:ShockedN);
   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:ShockedN );

}

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.

------------------------
&quot;There's no place like 127.0.0.1&quot;

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=44155#44155





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
benang
User


Joined: 31 Jan 2011
Posts: 21

PostPosted: Fri Dec 23, 2011 3:38 am    Post subject:
Reply with quote

Hi Kim,

Thanks for the reply. Well, I know it is only to showcase the osgOcean but still we need something to show the atmosphere / (at least) the sky. Currently I am using the skydome in my project because I don't know anything that's better. Now the problem is the skydome is always stretched in my project. And that makes the stars in the night scene bigger (like 5 pixels bigger) which is weird and unnatural. So I am trying to solve this by not clamping the texture from edge to edge, but repeat the same texture instead.

I hope this makes sense.


Thank you!

Cheers,
Bawenang

_________________
"There's no place like 127.0.0.1"
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> osgOcean [osgOcean] All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts Pass an osg::Texture2D to CUDA driver... mille25 General 6 Wed Jun 15, 2016 12:37 pm View latest post
No new posts OS X (10.9) Texture2D with hardware c... LEgregius General 8 Thu May 28, 2015 1:52 pm View latest post
No new posts Cylinder/Skydome flicker when using C... bstru osgOcean [osgOcean] 2 Tue Mar 17, 2015 10:04 pm View latest post
No new posts Texture2D subload woes Eric Sokolowsky General 3 Fri Mar 06, 2015 7:58 pm View latest post
No new posts Texture2D Image updating Abluduck General 9 Fri Aug 29, 2014 9:28 am View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP