Archive for July, 2009|Monthly archive page

OpenGL ES Texture2D: Power of two

As most of you have figured out, OpenGL ES doesn’t like textures to be non power of two, and I really didn’t want to have to force all of my textures to be powers of two before loading them into my game. This presented me with a problem that had to be solved in-code.

To really understand texture mapping, especially if you’re doing iPhone development or OpenGL ES development in general, it is a good idea to visit Jeff’s blog on the subject located: HERE, and look up chapter 6 on Texture Mapping. I’ll wait, its a great read and should be glanced over at the very least!

Back? OKAY! So the problem is only powers of two can be used in OpenGL ES, so how do we convert our texture to a power of two? There are several ways to do this, but I chose to look at apples version of a texture file and learn from them.

They came up with the following to convert the width and height into a power of two:

//     Adjust the width and height to be a power of two
if( (_width != 1) && (_width & (_width - 1)) ) 
                i = 1;
                while((sizeToFit ? 2 * i : i) < _width)
                        i *= 2;
                _width = i;
        if( (_height != 1) && (_height & (_height - 1)) ) 
                i = 1;
                while((sizeToFit ? 2 * i : i) < _height)
                        i *= 2;
                _height = i;

The first portion of the code will convert the texture to a power of 2. Since the texture will be stretched, and never shrunk, apple had to figure out a way  to shrink the texture if it became larger than the max texture size of 1024. The following code will do just that:

        //      scale down an image greater than the max texture size
        while((_width > kMaxTextureSize) || (_height > kMaxTextureSize)) 
                _width /= 2;
                _height /= 2;
                transform = CGAffineTransformScale(transform, 0.5, 0.5);
                imageSize.x *= 0.5;
                imageSize.y *= 0.5;

the ‘transform’ is nothing more than a CGAffineTransform; in case you were confused with that. So, now that you’ve glanced over the code, you remember in the article where Jeff was talking about S and T coordinates? We’ll take care of those like this:

        _maxS = imageSize.x / (float)_width;
        _maxT = imageSize.y / (float)_height;

This will give us a number between 0 and 1, depending if the texture was re-sized or not. _maxS and _maxT will also be used in our textureCoordinates array for drawing the texture to screen. This should only render the portion of the texture that has any data, and not the padding we added to the texture.

While I am still learning OpenGL, I figured that a lot of people would be interested in the following solution.. especially if you did what I had, and had thrown out the Apple Texture2D file and re-wrote your own. Full source code to the Texture2D, or any other file within the D’Jinn Engine can be found HERE.

Hope this was informative! If anyone has any further information they would like to add, feel free!

Happy Coding Everyone!


D’Jinn Engine: Updates

Over the last few days I’ve been re-working a few of the OpenGL calls and a few classes in the D’Jinn Engine. The most notable change is removing the Texture2D class that apple provides, and instead loading up a Texture2D class of my own that uses the TextureController for memory management. In revamping the Textures from OpenGL, I was hopeful to figure out the problem I’ve been having with the Particle Emitter, and I have not been able to figure it out… but I’ll keep working!

The Emitters problem is documented in both the class file as well as issues on the SVN site.

Anyway, feel free to poke around and send me an email if you find anything that I could do better, or that could be re-structured better. I will start working on new extensions and making the “engine” an actual engine over the next few weeks.. especially now that the design for my game is starting to complete and I have a better idea of what I’ll need to add to the engine to make it work. After all, the D’Jinn Engine is a project of mine to help learn the in’s and out’s of C++ development, so send in that feedback!

Also of note, I obtained the 3.0 SDK and fixed the minor errors in the code that didn’t let you compile and run it under 3.0 OS. You should be able to now. Full list of changes can be found: HERE

Happy Coding everyone!

D’Jinn Engine: Open Sourced…

Is it ready for full time production? … eh, no…
Is it a fully function engine? … eh, no…
Will it aid in your thurst for knowledge?.. YEAH!

As you read yesterday, I’ve decided to put what I have of the D’Jinn Engine up on google code, for anyone to poke, pry, and have fun with. I will continue my trek to modify and improve the engine pretty much every day, but it is also free for anyone who also wishes to take what I have to use in their own project.

What is the catch? Simple… If you make any improvements, let us know about it! The iPhone and iPod Touch community is hungry for knowledge on game development, and could really use a few great resources. Will the D’Jinn Engine ever be a general enough resource for anyone to pick up and use? Probably not… but at least its there for the few of us who enjoy C++ enough to torture ourselves into making a game engine on the iPhone :)

I have spent some time working on the description and trying to comment the code as much as I could. As we speak, I am working on a texture controller class that will eliminate the need for the Texture2D from apple (and in the process, hopefully eliminate a nasty bug I introduced with the particle system…)

Remember, I will be working on this thing pretty much every day, if not every other day. Don’t expect to see a commit every day, but know that there are people making improvements to it. Dive in, have fun, learn a thing or two, teach me a thing or three… and feel free to join the discussion group if you have something interesting to share!

D’Jinn Engine: iPhone C++ 2D engine

Happy Coding everyone!

C++ on the iPhone: Djinn Engine

EDIT: I would like to point out that below, when mentioning “Mike” … I forgot to plug his website.. whoops! In any rate, he has a few great tutorials (in video form) located: … HIGHLY recommend you check them out if you’re using Obj-C on the iPhone.

Hello everyone! I have been reading through your emails and appreciate all the letters. One question I kept asking myself with this project was “Do I want to make the engine open source?” My decision came apparent when I realized that this engine, although it will be used for my iPhone games, was first and foremost a learning experience for me and a tool that I used in order to become a better programmer.

So here I am, with a working copy of the Djinn Engine, and I have decided to make the project ‘open source.’ Before I upload the source to a SVN however, I want to ask the communities opinion on what they want to see?

The engine works, but it is crude. Currently in the engine is the following:

TestGame – I’ve been using this as a static unit testing for the engine

Actor System – Handles any actors in the game. Started by using the Angel Engine as template

Animation System – Animation Manager used for stopping, starting, and updating various animation playlists.

Game Screen Management – A C++ version of the Screen Controller tutorial from below.

Serializable objects – All objects (Characters, particle effects, etc) will be serialized in .dat files for dynamic loading. This way they do not need to be hard coded into the engine.

Input management (single touch only) – A C++ version of the input manager tutorial below.

Particle System – Using Mike’s tutorial as a starting point, dynamic particle system that includes a controller, effect, and various emitters for every particle effect. The one i currently have been using is “fire”

OpenAL Sound Support (have not put in OGG support yet, plan to) – Also using Mike’s tutorials as a staging point, this is an OpenAL sound engine that currently only works with sound effects… I want to add OGG support in the future.

File Management – pass in the asset name of a file, and a pointer to the object you wish to unserialize, and the file manager will take care of the rest.

Random Number Generation – … does just what it says it will do :)

Modified version of Apples Texture2D for C++ (will need to revamp this)

Math Utilities class (IE: Clamp, Min / Max, Abs, Lerp, etc etc)

and a couple of other things. Keep in mind, this is a 2D engine! So… iPhone community… We have a few choices before this thing goes Open Source. I will continue working on it, adding things that will meet my needs (keeping in mind, this will be the engine I am using for my iPhone Game) and I figure if I need a feature, maybe the community could use it as well. Should I just release it “as is” right now and continue working on it while you all can get your hands on it and modify it depending on what you need? Should I clean it up a bit, so its easier to understand when you’re reading through the code? or should I wait til the engine is pretty much done and release it then?

Let me know either via comment or email what you wish, and we’ll make it happen. I’ll have to work on some documentation, especially for the crude object serialization haha.. but that shouldn’t be too hard to do. Let me know what you think, and for the few of us who actually enjoy C++ on the iPhone, lets make it happen!