473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Model and Texture but with children


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<Timplementation
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.





May 23 '07 #1
2 1650

"Rafal Maj" <no@address.invalidwrote in message
news:f3**********@inews.gazeta.pl...
>
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<Timplementation
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
May 23 '07 #2

"Howard" <al*****@hotmail.comwrote in message
news:go*****************@bgtnsc05-news.ops.worldnet.att.net...
>
>
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


May 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark L | last post by:
I am trying to create a graphics engine using OpenGL and PHP. I am currently trying to create a completly white texture to test out the texturing capabilities. My idea is to create an array...
5
by: Richard Light | last post by:
A literal-minded reading of the XML 1.0 Spec suggests that elements with content model ANY should not have comments or PIs as their immediate children. Is there a particular reason for this? ...
19
by: Peter T. Keillor III | last post by:
I'm not talking "Days of our Lives", only church stuff. What's the most efficient way to show relations among members, have a member table, then a relations table (member 1, member 2, relation)? ...
1
by: Myk Quayce | last post by:
I have a four-sided polygon that rotates and zooms as the user moves the mouse. Is there a way to incorporate a texture-mapped image without DirectX? The GDI+ doesn't seem to support this. -- ...
5
by: clintonG | last post by:
I'm looking for documentation and would not turn my nose up to any code from anybody who thinks they are good at the design of an algorythm that can be used to generated a hierarchical relational...
3
by: x | last post by:
Still fairly new at this, I have been trying to find out how to compile and effect with multiple samplers. Been able to find loads of examples that show the HSL code once the samplers are...
122
by: Edward Diener No Spam | last post by:
The definition of a component model I use below is a class which allows properties, methods, and events in a structured way which can be recognized, usually through some form of introspection...
0
by: ...:::JA:::... | last post by:
Hello, Is there any real easy example for loading an texture in your directpython window??? For example this is my code: # loading directpython modules import d3dx import d3d from d3dc...
0
by: brixton | last post by:
Hello, I've got the following code that creates a texture from a .RAW file: GLuint MyGLCanvas::LoadTextureRAW( const char * filename, int wrap ) { GLuint texture; int width, height; ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.