2012 Archives

Modern OpenGL 03 - Matrices, Depth Buffering, Animation

— Category: Modern OpenGL Series

In this article, we will replace our immobile 2D triangle with a spinning 3D cube. The end result will look like this:

Now that we’re finally getting something interesting on the screen, I can include more pictures! An album of animated gifs is available here: http://imgur.com/a/x8q7R

In order to make this spinning cube, we will learn a bit about matrix math, and how it is used to create perspective projections, rotation, translation, and the concept of a “camera.” We will also learn about depth buffering, and why it is necessary. We will also see how a typical 3D application implements changes over time, such as animation.

Modern OpenGL 02 - Textures

— Category: Modern OpenGL Series

In this article, we will be adding a texture to the triangle. This will involve adding new variables to the vertex and fragment shaders, creating and using texture objects, and learning a bit about texture units and texture coordinates.

This article introduces two new classes to the tdogl namespace: tdogl::Bitmap and tdogl::Texture. These will allow us to load an image from a jpg, png, or bmp file into video memory, for use in the shaders. Also, the tdogl::Program class has some new methods for setting shader variables.

Modern OpenGL 01 - Getting Started in Xcode, Visual C++, and Linux

— Category: Modern OpenGL Series

Welcome to the first of a series of articles about modern OpenGL. All the code will be open source, and downloadable from github: https://github.com/tomdalling/opengl-series.

By the end of this article, you will have a working OpenGL 3.2 project in Visual Studio 2013 on Windows, and Xcode on OSX. The app will have a vertex shader, a fragment shader, and will display a single triangle using a VAO and a VBO. The project will use GLEW to access the OpenGL API, GLFW to handle window creation and input, and GLM for matrix/vector math.

Yes, it is a little bit boring, but just getting a project set up correctly can be difficult, especially for beginners. Once this is out of the way we will get into the more interesting stuff.

Thoughts On Bootstrap

— Category: Web

You may have noticed that I redesigned the site recently. The new design uses Boostrap, mainly to make the site work better on tablets and phones. This post will be about the good and the bad parts of my recent encounter with Bootstrap.

Bootstrap is basically a CSS and JavaScript framework that gives you nice-looking components such as buttons, menus, grid-based layout, etc. One of more interesting parts of Bootstrap is the responsive design functionality, which automatically rearranges and resizes the page to fit nicely on smaller screens, such as phones and tablets.

Why camelCaps Are Superior To_Underscores

— Category: Coding Style/Conventions

I’ve always preferred camel caps to underscores when it comes to naming conventions, but I’ve never really known why. Recently I’ve been writing a lot of Python, where the standard is to use underscores for most names, and now I realise why I don’t like underscores. I hope to make my argument for camel caps objective — or, failing that, at least better than the mouth-frothing, religious reactions you normally get in these “X is better than Y” type of discussions.

An In-depth Look At Manual Memory Management In Objective-C

— Category: Cocoa

It feels like fleets of new developers dash themselves upon the rocky shores of Objective-C memory management every day on StackOverflow. I can’t bear to write the same answers over and over again, so this article will be my final, unabridged explanation of: retain, release, autorelease, alloc, dealloc, copy, and NSAutoreleasePool. There will be some rules to memorize, but we will also take a journey under the hood for a deeper understanding.

This article is intended for people who are new to Objective-C, or who never fully learnt how manual reference counting works. It does assume that you have some programming experience, possibly in another language. If you are a beginner programmer and Objective-C is your first language, I will try to keep the explanations clear, but it may become confusing the more in-depth we get.

We will be covering:

  • A Quick Mention Of ARC And Garbage Collection
  • Coming From C/C++
  • Coming From a Garbage Collected Language (Java, Python, C#, etc.)
  • What Is Reference Counting?
  • How Does Reference Counting Work In Objective-C?
  • NSAutoreleasePool And autorelease
  • Common Mistakes

If something is not explained, or not explained very well, post a question in the comments and I’ll expand the article to cover it.

Resource Acquisition is Initialisation (RAII) Explained

— Category: Software Design

In the competition to make the worst acronym, RAII probably comes second after HATEOS. Nevertheless, it is an important concept because it allows you to write safer code in C++ — a harsh, unforgiving language that is all too happy to help you shoot yourself in the foot.

This article will explain exception-safety and common pitfalls in C++. As we work out how to avoid these problems, we will accidentally discover RAII. Then, we will finish by defining exactly was RAII is, and where it is already being used.

When A Café Is Not A Café – A Short Lesson In Unicode Featuring NSString

— Category: Coding Tips

Let’s start with two exotic strings (console output is in the code comments):

NSString* apples = NSGetFrenchWord();
NSString* oranges = NSGetFrenchWord();

NSLog(@"apples == '%@'", apples); 
//apples == 'café'
NSLog(@"oranges == '%@'", oranges); 
//oranges == 'café'

They look identical, but looks can be deceiving.

NSLog(@"isEqual? %@", [apples isEqual:oranges] ? @"YES" : @"NO");
//isEqual? NO
NSLog(@"[apples length] == %lu", [apples length]);
//[apples length] == 4
NSLog(@"[oranges length] == %lu", [oranges length]);
//[oranges length] == 5

Gotchas With Grand Central Dispatch (libdispatch) And Blocks

— Category: Cocoa

GCD is a nice replacement for the old performSelectorInBackground:withObject: and performSelectorOnMainThread:withObject:waitUntilDone: methods and NSOperation. It’s also a nice supplement to NSThread.

However, I think it was over-hyped a little bit by Apple when it was first released. You probably have all these random deadlocks and race conditions and stuff whenever you use multiple threads, but GCD is soooooo good that you don’t have to worry about that anymore. Just use these magic blocks.

The problem is that concurrent programming is notoriously hard to get right. Maybe GCD helps to make it easier, but It doesn’t solve all of your problems. In fact, GCD and blocks introduce some problems of their own. This article will focus on some of those problems.