Model and Texture but with children

Discussion in 'C++' started by =?ISO-8859-2?Q?Rafa=B3?= Maj, May 23, 2007.

  1. Say we have a class Model (a 3d model) and class Texture (representing an
    Texture).

    Model have a texture, Model can be Drawed. Texture have method that returns
    data needed for drawing.

    class Texture {
    public:
    // openGl data here
    glInt GetInt();
    };

    class Model {
    public:
    Texture *tex;
    void Draw() {
    glInt x = tex->GetInt();
    // ...
    }
    };


    And all is well.

    But now - say I want to make this design more flexible, to have
    opengl-texture (and way of drawing for the monster)
    and directX-texture (and directX way of drawing of the monster).
    So I will make Texture a base class, and move OpenGL stuff into TextureOGL
    etc.

    class Texture { public: };
    class TextureOGL : public cTexture
    { public: /* opengl data */ glInt GetInt(); };
    // more Texture*

    THE PROBLEM: but what then happens to the monster?
    1) how to add methods for drawing in different ways (I want that to be
    choosed in runtime)
    2) how to access the texture


    My possible solutions

    Solution-1 - only child have texture
    class Texture { public: };
    class TextureOGL : public cTexture { public: /*...*/ glInt GetInt(); };
    class cMonster { virtual void Draw()=0; };
    class cMonsterOGL : public cMonster { public:
    cTextureOGL *tex;
    virtual void Draw() { tex->GetInt(); /*...*/ }
    };

    Almost ideal but that doesnt modell that EVERY monster always have SOME
    texture (and that information migt be needee)


    Solution-2
    class Texture { public: virtual string Name(); };
    class TextureOGL : public cTexture { public: /*...*/ glInt GetInt(); };
    class cMonster { public: cTexture *tex; virtual void Draw()=0;
    void GeneralUseOfTexture() { MsgBox(tex->Name()); }
    };
    class cMonsterOGL : public cMonster { public:
    virtual void Draw() {
    dynamic_cast<cTextureOGL>(tex)->GetInt(); /*...*/
    // else/catch - wrong dynamic cast - print error "Wrong texture!"
    }
    };
    That models all informations and allow general use of texture ->Name();
    The problem is - it uses a dynamic_cast with is consider not so good, also I
    wonder about speed implications.. upcast is fast and O(1) or not really?

    Other solutions?

    Perhaps using templates... but writting entire Monster<T> implementation
    inside header .h is not too good since its complicated class.
    Also it would be not so easy to have a std::vector of monsters etc.
     
    =?ISO-8859-2?Q?Rafa=B3?= Maj, May 23, 2007
    #1
    1. Advertising

  2. =?ISO-8859-2?Q?Rafa=B3?= Maj

    Howard Guest

    "Rafal Maj" <> wrote in message
    news:f327v2$89l$...
    >
    > Say we have a class Model (a 3d model) and class Texture (representing an
    > Texture).
    >
    > Model have a texture, Model can be Drawed. Texture have method that
    > returns
    > data needed for drawing.
    >
    > class Texture {
    > public:
    > // openGl data here
    > glInt GetInt();
    > };
    >
    > class Model {
    > public:
    > Texture *tex;
    > void Draw() {
    > glInt x = tex->GetInt();
    > // ...
    > }
    > };
    >
    >
    > And all is well.
    >
    > But now - say I want to make this design more flexible, to have
    > opengl-texture (and way of drawing for the monster)
    > and directX-texture (and directX way of drawing of the monster).
    > So I will make Texture a base class, and move OpenGL stuff into TextureOGL
    > etc.
    >
    > class Texture { public: };
    > class TextureOGL : public cTexture
    > { public: /* opengl data */ glInt GetInt(); };
    > // more Texture*
    >
    > THE PROBLEM: but what then happens to the monster?
    > 1) how to add methods for drawing in different ways (I want that to be
    > choosed in runtime)
    > 2) how to access the texture
    >
    >
    > My possible solutions
    >
    > Solution-1 - only child have texture
    > class Texture { public: };
    > class TextureOGL : public cTexture { public: /*...*/ glInt GetInt(); };
    > class cMonster { virtual void Draw()=0; };
    > class cMonsterOGL : public cMonster { public:
    > cTextureOGL *tex;
    > virtual void Draw() { tex->GetInt(); /*...*/ }
    > };
    >
    > Almost ideal but that doesnt modell that EVERY monster always have SOME
    > texture (and that information migt be needee)
    >


    I'm not sure what it means when you say every monstre has SOME texture.
    What's the relationship between an OpenGL texture and a DirectX texture?
    Are they actually related, or just somewhat similar in concept? If they're
    not related, then perhaps thinking of them as the same thing is wrong to
    begin with.

    >
    > Solution-2
    > class Texture { public: virtual string Name(); };
    > class TextureOGL : public cTexture { public: /*...*/ glInt GetInt(); };
    > class cMonster { public: cTexture *tex; virtual void Draw()=0;
    > void GeneralUseOfTexture() { MsgBox(tex->Name()); }
    > };
    > class cMonsterOGL : public cMonster { public:
    > virtual void Draw() {
    > dynamic_cast<cTextureOGL>(tex)->GetInt(); /*...*/
    > // else/catch - wrong dynamic cast - print error "Wrong
    > texture!"
    > }
    > };
    > That models all informations and allow general use of texture ->Name();
    > The problem is - it uses a dynamic_cast with is consider not so good, also
    > I
    > wonder about speed implications.. upcast is fast and O(1) or not really?
    >
    > Other solutions?
    >
    > Perhaps using templates... but writting entire Monster<T> implementation
    > inside header .h is not too good since its complicated class.
    > Also it would be not so easy to have a std::vector of monsters etc.
    >


    Perhaps what you need is to separate the monster and the texture from each
    other? After all, changing how the monster is rendered shouldn't have to
    change the monster itself. (Especially if you can change how they're drawn
    at run-time.)

    One solution would be to make another object which contains (or points to) a
    monster and a texture, thus associating the two. That owner monster/texture
    object would know how to Draw(), using the texture type it's designed for
    and monster it's associated with.

    So, you might create a vector of OpenGL monster/texture owner objects, or
    you might create a vector of DirectX monster/texture owner objects,
    depending on the user choice (or whatever).

    And besides Draw(), any other manipulations that involve both the monster
    and the texture could be done via virtual members of the appropriate type of
    monster/texture owner object. And the monster would hold a pointer to the
    base-class (possible pure abstract?) of the monster/texture owner, so calls
    from either a Monster or one of the Texture classes would be polymorphic,
    removing any concern on the Monster's part as to what type of owner object
    (and thus what type of texture) it's working with.

    Here's an example, where the textures need not be related at all:

    class Monster;
    class Texture_OpenGL;
    class Texture_DirectX;

    class MonsterTextureOwner
    {
    Monster* pMonster;
    ...whatever...
    virtual void DrawMonster() = 0;
    virtual ~MonsterTextureOwner();
    };

    class MonsterTextureOwner_OpenGL
    {
    Texture_OpenGL textureOGL;
    ...
    virtual void DrawMonster();
    ...
    ~MonsterTextureOwner_OpenGL();
    };

    class MonsterTextureOwner_DirectX
    {
    Texture_DirectX textureDX;
    ...
    virtual void DrawMonster();
    ...
    ~MonsterTextureOwner_DirectX();
    };

    class Monster
    {
    MonsterTextureOwner* pOwner;
    ...
    void Draw() { if (pOwner) pOwner->DrawMonster() }
    };

    -Howard
     
    Howard, May 23, 2007
    #2
    1. Advertising

  3. =?ISO-8859-2?Q?Rafa=B3?= Maj

    Howard Guest

    "Howard" <> wrote in message
    news:go35i.20091$...
    >


    >
    > Here's an example, where the textures need not be related at all:
    >
    > class Monster;
    > class Texture_OpenGL;
    > class Texture_DirectX;
    >
    > class MonsterTextureOwner
    > {
    > Monster* pMonster;
    > ...whatever...
    > virtual void DrawMonster() = 0;
    > virtual ~MonsterTextureOwner();
    > };
    >
    > class MonsterTextureOwner_OpenGL


    oops. Should have added ": public MonsterTextureOwner" there

    > {
    > Texture_OpenGL textureOGL;
    > ...
    > virtual void DrawMonster();
    > ...
    > ~MonsterTextureOwner_OpenGL();
    > };
    >
    > class MonsterTextureOwner_DirectX



    Ditto: should have added ": public MonsterTextureOwner" there

    > {
    > Texture_DirectX textureDX;
    > ...
    > virtual void DrawMonster();
    > ...
    > ~MonsterTextureOwner_DirectX();
    > };
    >
    > class Monster
    > {
    > MonsterTextureOwner* pOwner;
    > ...
    > void Draw() { if (pOwner) pOwner->DrawMonster() }
    > };
    >
    > -Howard
    >
    >
     
    Howard, May 24, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    5,421
    Andrew Thompson
    Sep 2, 2005
  2. Xpyder
    Replies:
    3
    Views:
    384
    Victor Bazarov
    Mar 5, 2005
  3. stacey

    Problem with texture of UI

    stacey, Jan 21, 2007, in forum: Java
    Replies:
    3
    Views:
    356
    Larry Barowski
    Jan 23, 2007
  4. Ivan Danicic

    web start texture problem

    Ivan Danicic, Jun 13, 2007, in forum: Java
    Replies:
    5
    Views:
    315
    Ivan Danicic
    Jun 18, 2007
  5. ...:::JA:::...

    directpython simple texture

    ...:::JA:::..., Jul 17, 2007, in forum: Python
    Replies:
    0
    Views:
    335
    ...:::JA:::...
    Jul 17, 2007
Loading...

Share This Page