[osg-users] Texturing with GLBeginEndAdapter

tianzjyh tianzjyh at 126.com
Thu Dec 29 18:51:11 PST 2016


Hi, 
    I checked again about the function "State::setActiveTextureUnit()", and it seems like it's designed to be called inside the class "State" but not for users's calling. 
   So to enable the GL_TEXTURE_2D mode, and to make a specified unit to be the current, you can use
    >state->applyTextureMode(0, GL_TEXTURE_2D, true);     //this will call State::setActiveTextureUnit(0) internally.


   and to bind a texture object to target GL_TEXTURE_2D, call
    >state->applyTextureAttribute(0, texture[0]);                       //texture[0]->apply(*state) should work too


    Sorry for misleading you. 
    Function "State::setActiveTextureUnit()" only makes a specified unit to be the current, without enabling GL_TEXTURE_2D mode.


    As I understand, in the function drawImplementation(), you can even call native gl*() funtions(like glEnable(GL_TEXTURE_2D), glBindTexture(...)). Just remember to restore the gl state you have changed when leaving the funtion. But do not worry about this too much, as OSG's state manager will minimize the affect——when rendering other drawables, OSG will make states to be as you have pre-settled on them, while those you have not settled somehow will be unsure since you are not using OSG's state system.


--





Cheers, 
---
TianZJ



At 2016-12-29 21:34:40, "Aaron Andersen" <aaron at fosslib.net> wrote:
>Hello TanZJ,
>
>Thank you for the information. I don't think I quite understood your  
>intention though. I apologize if this is obvious but as I said my  
>OpenGL skills are lacking... Here is my code after applying your  
>suggestion about applying the texture to the state:
>
>class MyDrawable {
>osg::ref_ptr<osg::Texture2D> texture[3];
>public:
>     MyDrawable()
>     {
>         for (int i : {0, 1, 2})
>         {
>             const std::string filename = std::to_string(i + 1) + ".png";
>
>             if (osg::Image * image = osgDB::readImageFile(filename))
>             {
>                 texture[i] = new osg::Texture2D;
>                 texture[i]->setImage(image);
>             }
>             else
>             {
>                 std::cerr << "ERROR LOADING IMAGE " << filename << std::endl;
>             }
>         }
>     }
>
>     virtual void drawImplementation(osg::RenderInfo & renderInfo)  
>const override
>     {
>         const float Z = 0.f;
>
>         osg::State * state = renderInfo.getState();
>         osg::GLBeginEndAdapter & gl = state->getGLBeginEndAdapter();
>
>         state->setActiveTextureUnit(0);
>
>         gl.Color4f(1.f, 1.f, 1.f, 1.f);
>
>         float x = 50.f, y = 50.f, w = 64.f, h = 64.f, offset;
>
>         offset = 0.f;
>
>         // apply the first texture so it will draw on the first rect
>         texture[0]->apply(*state);
>         gl.Begin(GL_QUADS);
>         gl.TexCoord2f(0.f, 0.f);
>         gl.Vertex3f(offset + x, y, Z);
>         gl.TexCoord2f(0.f, 1.f);
>         gl.Vertex3f(offset + x + w, y, Z);
>         gl.TexCoord2f(1.f, 1.f);
>         gl.Vertex3f(offset + x + w, y + h, Z);
>         gl.TexCoord2f(1.f, 0.f);
>         gl.Vertex3f(offset + x, y + h, Z);
>         gl.End();
>
>         offset = 256.f;
>
>         // apply the second texture so it will draw on the second rect
>         texture[1]->apply(*state);
>         gl.Begin(GL_QUADS);
>         gl.TexCoord2f(0.f, 0.f);
>         gl.Vertex3f(offset + x, y, Z);
>         gl.TexCoord2f(0.f, 1.f);
>         gl.Vertex3f(offset + x + w, y, Z);
>         gl.TexCoord2f(1.f, 1.f);
>         gl.Vertex3f(offset + x + w, y + h, Z);
>         gl.TexCoord2f(1.f, 0.f);
>         gl.Vertex3f(offset + x, y + h, Z);
>         gl.End();
>
>         offset = 512.f;
>
>         // apply the third texture so it will draw on the third rect
>         texture[2]->apply(*state);
>         gl.Begin(GL_QUADS);
>         gl.TexCoord2f(0.f, 0.f);
>         gl.Vertex3f(offset + x, y, Z);
>         gl.TexCoord2f(0.f, 1.f);
>         gl.Vertex3f(offset + x + w, y, Z);
>         gl.TexCoord2f(1.f, 1.f);
>         gl.Vertex3f(offset + x + w, y + h, Z);
>         gl.TexCoord2f(1.f, 0.f);
>         gl.Vertex3f(offset + x, y + h, Z);
>         gl.End();
>     }
>};
>
>Can you see what I'm doing wrong? No textures show up at all when I  
>try this code.
>
>Thank you,
>Aaron
>
>Quoting tianzjyh <tianzjyh at 126.com>:
>
>> Hi, Andersen,
>> Let's say you have three images, then you can wrap them using  
>> osg::Texture2D, something like this:
>>     >
>>> osg::Image* image0 = osgDB::readImageFile("filename0.xxx");
>>> osg::Texture2D* tex0 = new osg::Texture2D(image0);
>>> tex1->setWrap(..., ...);
>>> tex1->setFilter(..., ...);
>>>
>>
>>
>> And then you can apply them in the drawImplementation() of your  
>> drawable like this :
>>>
>>> osg::State& state = *renderInfo.getState();
>>> state.setActiveTextureUnit(0);        //set texture unit 0 as  
>>> current texture
>>> tex0->apply(state);                        //apply texture paras to  
>>> current texture
>>> ...drawQuad0...
>>>
>>> ...deal with other two quads...
>>>
>>
>>
>> --
>>
>> Cheers,
>> ---
>> TianZJ
>>
>>
>>
>> At 2016-12-29 07:32:01, "Aaron Andersen" <aaron at fosslib.net> wrote:
>>> Hello,
>>>
>>> There is some code in a library which I want to adapt for OSG. The
>>> code is pretty old and uses the old style OpenGL. I don't have the
>>> time it would take to rewrite this code so I would like to use the
>>> GLBeginEndAdapter to make this code work with OSG.
>>>
>>> Looking at the GLBeginEndAdapter there isn't much documentation so I
>>> was hoping someone could help me out. Please keep in mind my OpenGL
>>> skills are lacking (which I why enjoy using a rendering engine like
>>> OSG :-).
>>>
>>> I've created a subclass to the Drawable class called "MyDrawable". In
>>> the drawImplementation function I'm have a couple
>>> gl.Begin(GL_QUADS)/gl.End() calls to draw 3 simple rectangles. I'm
>>> unsure of how to apply 3 separate textures to these 3 rectangles. From
>>> reading the ShapeDrawable class it looks like the state of the
>>> Drawable applies the texture to everything you draw.
>>>
>>> So how can I apply many different textures to my drawing, with 1
>>> texture per shape I draw with GL_QUADS?
>>>
>>> Thank you for any insight you can provide.
>>> Aaron
>>>
>>>
>>> _______________________________________________
>>> osg-users mailing list
>>> osg-users at lists.openscenegraph.org
>>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
>
>_______________________________________________
>osg-users mailing list
>osg-users at lists.openscenegraph.org
>http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20161230/a151a9e8/attachment-0002.htm>


More information about the osg-users mailing list