[osg-users] Texturing with GLBeginEndAdapter

Aaron Andersen aaron at fosslib.net
Thu Dec 29 05:34:40 PST 2016


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






More information about the osg-users mailing list