Abstracting the graphics API...

Jesse
Unregistered
 
Post: #1
Hi all,

I'm attempting to make my current engine api- and os-independent from the ground up (if possible) for flexibility and ease of porting. I was wondering if anyone here has done this or can offer some thoughts as to whether it's worth doing (since OpenGL is cross-platform), and how it might be done.

My current plan is to create an abstract GraphicsAPI class and instantiate a derived class with the appropriate API-specific code at runtime. Of course this involves abstracting all the functionality of the API that you want to use into the interface. Essentially you end up 'mirroring' the functionality of the API in the interface, with the advantage that when it comes time to add support for another API, you just write a new subclass rather than changing every API call in your code.

So is this the right idea? Is there a better way?

Thanks in advance for your input.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Sounds reasonable. I suggest you learn at least two graphics APIs before you start, though, to give you an idea of what kind of interface will work for a generic rendering system.

It's worth doing this even if all your subclasses use OpenGL, because chances are (depending on your game) you could have 5 rendering paths to support different graphics cards.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #3
Sounds like a lot of work, if not in programming, in getting a good design approach for sure. I suggest you really think about the design before you code away, otherwise you may make things even worse.
Quote this message in a reply
DCoder
Unregistered
 
Post: #4
One of the best examples of how to build a solid, cross-platform abstraction layer that I've ever come across is the Simple DirectMedia Library. It is pretty mature and robust and supports a *huge* number of platforms -- Win32, MacOS (carbonlib & X), BeOS, Amiga, and just about any *nix you can think of (including Linux).

Oh, and it's licensed under the LGPL, which means that its complete source base is available.

I'm actually in the process of attempting to port the LWJGL from Win32/Linux to MacOS X, and I seek guidance from the SDL cvs tree frequently. However, since the licenses are not directly compatible, I don't ever pull source code, I merely examine the methodology. Any existing open-source project that is similar (such as OpenAL, for example) may provide similar clues.

Good luck!

-daniel
Quote this message in a reply
Feanor
Unregistered
 
Post: #5
Sounds cool, but if such a thing exists already (as SDL, or maybe others -- MacDX?) is it worth doing? Yes, if you are using a different language (say, Objective-C) or a high-level API, but if you are just doing the same thing over again -- I guess you'd have to learn a hell of lot to do such a thing. I'd give you respect if you did it! If it was in Objective-C, I might give you money to use it (one day).
Quote this message in a reply
Jesse
Unregistered
 
Post: #6
Thanks for the responses. Yes, I'm beginning to see that it would be no small task to create even a limited abstraction for the purposes of my own engine, and I would probably need to learn quite a bit about other APIs just to extract the common elements for the interface. I'll have to look around and see if someone else has already done this and made the code available - it'd be nice to avoid reinventing the wheel if possible.

Abstracting the OS is a similar problem, and is maybe more important than API-independence (since OpenGL is cross-platform). Any thoughts on a good approach to OS-independence would also be appreciated.

Thanks again for the feedback.
Quote this message in a reply
Member
Posts: 145
Joined: 2002.06
Post: #7
A better way to do the abstraction would be farther into your application code. Instead of creating an abstract graphics API at the level of OpenGL, DirectX, GLIDE, RAVE, etc, instead create the abstraction in the form of your drawing code. create a way of representing a scene, and then make all the drawing function abstract with generic functionality appropriate for the largest number of APIs possible. Use inheritance to specialize for the drawing API.

This could be thought of as a sort of MVC model. The Controller specializes for the object type, the View specializes for the graphics API. The important part is designing a Model thats able to be drawn efficiently on a wide range of APIs. for 3d polygonal graphics this is probably something like what you would pass to glArray* and glDrawElements, as data in that format can efficiently be converted to most others.

"He who breaks a thing to find out what it is, has left the path of wisdom."
- Gandalf the Gray-Hat

Bring Alistair Cooke's America to DVD!
Quote this message in a reply
Feanor
Unregistered
 
Post: #8
Quote:Originally posted by Jesse
Abstracting the OS is a similar problem, and is maybe more important than API-independence (since OpenGL is cross-platform). Any thoughts on a good approach to OS-independence would also be appreciated.


Stage 1: Ph.D. in Computer Science.
Stage 2: Get a DARPA grant.
Stage 3: ...
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #9
...
Stage 4: ???
Stage 5: Profit!

Ninja
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
Abstracting the OS is generally pretty easy. There are relatively few different tasks, and they tend to work relatively similarly on different OSs. Consoles might be a bit different I guess, just depends what you're aiming for.
Quote this message in a reply
Feanor
Unregistered
 
Post: #11
Quote:Originally posted by OneSadCookie
Abstracting the OS is generally pretty easy. There are relatively few different tasks, and they tend to work relatively similarly on different OSs. Consoles might be a bit different I guess, just depends what you're aiming for.

Yeah, well, that would depend on your definition of OS. To provide a consistent interface to memory management, processes (light and heavyweight), file systems (inc. permissions), networking, audio, RPC, low-level events, hardware interfaces ... anyway I was exaggerating for COMIC EFFECT. Yes? Rasp
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
Exaggeration for comic effect is fine unless you're misleading Rasp

In the general case, abstracting the OS is impossible (see: Qt). But for the specific case of a game, particularly a full-screen game, it's generally pretty easy.
Quote this message in a reply
Sta7ic
Unregistered
 
Post: #13
Look at OGRE -- ogre.sourceforge.net I think -- since they have a working example of what you're thinking about. Even if it's only between OGL and D3D.
I'm kinda surprised that nobody else has brought it up yet.

-Sta7ic
Quote this message in a reply
Post Reply