473,569 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GeneralUseOfTex ture() { MsgBox(tex->Name()); }
};
class cMonsterOGL : public cMonster { public:
virtual void Draw() {
dynamic_cast<cT extureOGL>(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<Timplem entation
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 1665

"Rafal Maj" <no@address.inv alidwrote 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 GeneralUseOfTex ture() { MsgBox(tex->Name()); }
};
class cMonsterOGL : public cMonster { public:
virtual void Draw() {
dynamic_cast<cT extureOGL>(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<Timplem entation
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 MonsterTextureO wner
{
Monster* pMonster;
...whatever...
virtual void DrawMonster() = 0;
virtual ~MonsterTexture Owner();
};

class MonsterTextureO wner_OpenGL
{
Texture_OpenGL textureOGL;
...
virtual void DrawMonster();
...
~MonsterTexture Owner_OpenGL();
};

class MonsterTextureO wner_DirectX
{
Texture_DirectX textureDX;
...
virtual void DrawMonster();
...
~MonsterTexture Owner_DirectX() ;
};

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

-Howard
May 23 '07 #2

"Howard" <al*****@hotmai l.comwrote in message
news:go******** *********@bgtns c05-news.ops.worldn et.att.net...
>
>
Here's an example, where the textures need not be related at all:

class Monster;
class Texture_OpenGL;
class Texture_DirectX ;

class MonsterTextureO wner
{
Monster* pMonster;
...whatever...
virtual void DrawMonster() = 0;
virtual ~MonsterTexture Owner();
};

class MonsterTextureO wner_OpenGL
oops. Should have added ": public MonsterTextureO wner" there
{
Texture_OpenGL textureOGL;
...
virtual void DrawMonster();
...
~MonsterTexture Owner_OpenGL();
};

class MonsterTextureO wner_DirectX

Ditto: should have added ": public MonsterTextureO wner" there
{
Texture_DirectX textureDX;
...
virtual void DrawMonster();
...
~MonsterTexture Owner_DirectX() ;
};

class Monster
{
MonsterTextureO wner* 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
5019
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 256x256x3 of the value 255 to create a completely white texture. When I do this in C it works fine but in PHP I get a array of red, blue, green and...
5
1477
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? Richard Light -- Richard Light SGML/XML and Museum Information Consultancy richard@light.demon.co.uk
19
2158
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)? Or is there a better way? Another need might be church activities of members. I guess that could be activity table and a member -- activity...
1
3201
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. -- Myk Quayce "If we can't be free, at least we can be cheap." - Frank Zappa
5
2239
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 data model. What? A Yahoo-like drill-down menu that is a series of categories and nested categories is a hierarchical relational data model. An...
3
1378
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 loaded, but not how to get the textures from VB into the shader. If i set up two samplers in the shader with no associated texture then when I compile...
122
7253
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 outside of that class. This structured way allows visual tools to host components, and allows programmers to build applications and libraries visually in...
0
1451
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 import *
0
1549
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; BYTE * data; FILE * file;
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.