Categories
iphone programming

GLKit to the max: OpenGL ES 2.0 for iOS – Part 1: Features

Apple uses OpenGL extensively – their 2D desktop libraries (Quartz, CoreAnimation) are OpenGL-based, and iOS has inherited that link (along with some funky tricks you can pull).

December 2013: I’ve converted the sample code from these articles into a standalone library on GitHub, with the article-code as a Demo app. It uses the ‘latest’ version of the code, so the early articles are quite different – but it’s an easy starting point

iOS developers often don’t have an OpenGL background, and most of the ones I work with like the idea of OpenGL, but feel they don’t have the time to learn and master it. However, the combination of “new API (OpenGL ES 2)” with “Apple’s helper classes (GLKit)” makes OpenGL development on mobile suprisingly fast and easy.

A few years ago, Jeff LaMarche wrote a series of simple, easy tutorials on getting started with OpenGL ES 1. In the same spirit, I’m going to write about GL ES 2. In the land of Khronos (the OpenGL standards group), version “2” is fundamentally different to version “1” (this is a good thing: ES 1 was outdated from the start, based on 1990s OpenGL. ES 2 is a modern API, based on 2000’s OpenGL)

I’ll be covering this from an iOS perspective. There are lots of GL ES tutorials out there, but for non-iOS platforms the challenges (and solutions) are different.

Quick links to all posts

iOS 5.0, iOS 6.0, and GLKit

With iOS 5.0, Apple introduced a new Framework: GLKit. GLKit’s apparent aim was to fix ES 2.0 to make it simpler and easier for non-OpenGL experts to use.

iOS 7.0…

It’s not live yet, and I’d hoped Apple might update GLKit to fix some of the holes below. I don’t know what will be in the final iOS 7 release, but I get the impression GLKit hasn’t changed yet. If so, everything that follows applies equally to iOS 7.

Summary of Apple’s GLKit and where it helps/hinders

Porting GL ES 1.x code
Full Apple provided a good, easy-to-use set of classes to make it trivial to port GL ES 1.x code. The documentation is very poor, though.

More importantly: it prevents you from using Shaders, which are one of the easiest and most powerful parts of OpenGL (once you get up and running). So, we’ll be ignoring GL ES 1.x

Classes: anything with the text “Effect” in the class name (GLKBaseEffect, GLKEffectProperty, GLKNamedEffect, etc)

Vector math
Full Every desktop OpenGL implementation assumes you have access to a good Vector Math library.

Until GLKit, Apple’s iOS was the exception: you had to make your own “vector” classes, you had to write all the Dot Product code, etc. (e.g. c.f. Jeff LaMarche’s first tutorial). Not any more :). Apple’s design here is good, and closely follows the conventions of their other frameworks (works the same way as CGRect, CGPoint etc from Quartz/CoreGraphics)

Structs: GLKVector2, GLKVector3, GLKVector4

Quaternions
Full Quaternions have a bad rep. Many people find them incomprehensibly hard to understand/code, and yet … they are essential once you start “rotating 3D objects”.

Apple’s implementation of Quaternions is excellent: you don’t need to understand the mathematics, just use the pre-built methods

Matrix math
Full Like Vector-math, Matrix math is tricky and time consuming to build for yourself and debug.
Apple’s done all of it, with an good set of methods.

Structs: GLKMatrix4, GLKMatrix3

OpenGL Projection
Partial (almost full) OpenGL uses 4-dimensions to deal with 3-dimensional rendering. That could get difficult, fast. Skipping the reasons for it, OpenGL used to be hardcoded to a set of special matries (M, V, and P – model, view, and projection).

GL ES 2 threw away the hard-coded matrices, and says “do it all yourself” (which, as we’ll see later, actually makes things easier in the long run). This is a pain … except Apple’s done it for us. Don’t go writing your own MVP stack code – use Apple’s.

Structs: GLKMatrixStack

Texture loading
Partial (poor) See post: “Part 6”

Before GLKit, you had to write long and complex methods, using CoreAnimation and Quartz, to convert “file.png” and upload it to the graphics chip as an “OpenGL texture”.

That code was hard to debug, and most iOS programmers aren’t familiar with CA/Quartz. Apple wrote a proper Objective-C texturing system that does the work of Quartz and y-flipping for you. For most “simple” code, this is perfect.

…but: they screwed up in a few places, some major bugs. When it works, it’s fine – and it only needs two lines of code! – so we’ll use it in the early articles, but we’ll throw it away and write a “fixed” version for the later articles.

Classes: GLKTextureInfo, GLKTextureLoader

Mixing UIKit with OpenGL
Partial (OK) See post: “Part 2”

There’s a lot of FUD on the web that says this is “impossible” or “slow” or … etc.

Don’t believe it. There are bugs in Apple’s CALayer / Quartz / CoreAnimation classes that make them slow *independent* of whether you’re running OpenGL. It’s just that the things people want to do when using UIKit with OpenGL are usually the ones that show up the bugs in UIKit/Quartz.

We’ll cover the main gotchas, and look at how to avoid or improve them. But for the most part: it works automagically. (there’s a reason for this: UIKit is implemented on top of OpenGL, so it’s already integrated to a high standard. It’s just that Apple hid the integration points)

Shaders (vertex, fragment)
None See post: Part 3
See post: “Part 6”

GLKit pretends that shaders don’t exist. The most important feature of OpenGL ES 2.0 – and Apple ignored it. Sad, but true. We’ll fix that.

Multithreading, context-switching
Full OpenGL supports multi-threading, background loading, all sorts of funky stuff.

Although it’s not strictly part of GLKit, Apple has re-used their old EAGLContext class to provide access to all of this. This is probably because it worked fine in the first place. However, to be clear: if you’re used to EAGLContext, it’s still used EVERYWHERE by GLKit.

Classes: EAGLContext

Multi-pass rendering
None See post: “Part 2”

You can make a cube appear on screen, textured, using single-pass rendering.

Just about everything else you ever want to do … needs more than one pass.

Apple provides no support for this, so you have to write it yourself (and there’s a surprisingly large amount of boilerplate you need to write here).

3D models, animation, data formats
Partial (very little) See post: Part 3
See post: Part 4b

GLKit does one great thing with 3D data formats: it provides a Vector class that all iOS apps/libraries/source can use, and be fully compatible with each other.

But it provides zero support for the rest: meshes, 3D models, VAOs, VBOs, etc.

Error handling and state management
None See post: Part 4

When you have a bug in your code, GL does nothing. Nor does Apple. Sooner or later you’ll find yourself weeping in frustration.

Performance analysis
Partial (some) Apple makes a lot of noise about how good Instruments is with OpenGL.

This is true, it’s good. But Apple also blocks you from accessing the hardware-manufacturers own performance tools, which may be better.

If you already know Instruments inside-out (e.g. you know about the invisible RHS-panel…), you’ll be right at home.

Next steps

If you know nothing at all about OpenGL, I recommend skim-reading the first 8 of Jeff LaMarche’s posts on GL ES 1.

NOTE: a *lot* of the detail in Jeffs’ posts is now unnecessary (or superceded). But it all helps with understanding the newer/cleaner GL ES 2 (and GLKit) versions. If you get stuck or struggle, skim that post and move on to the next. Each of his posts works well standalone.

Then head on to Part 2 – Drawing and Draw calls.

Also posted to #AltDevBlog at www.altdevblogaday.com/2013/08/29/glkit-to-the-max-opengl-es-2-0-for-ios-part-1-glkit-features/

Categories
Uncategorized

Off-peak tickets valid in 2013 on Southern trains

UK train companies have an expensive fare (“Peak”) for early morning commuters, and a normal fare for everyone else. Each company has unique rules on time/validity. Southern Rail’s section on fares on their website recognizes this, but refuses to say when their normal (“Off Peak”) fares are valid. (see below for official times as of August 2013)

Categories
advocacy agile project management

A 1st-hand description of doing Scrum/Agile correctly (in games industry)

Let’s be honest: many games-industry teams/studios abuse Scrum. Then, after months of pain and suffering (and poor project-progress), the team members go to other teams or companies, and take a hatred of Scrum/Agile with them.

So it’s interesting to hear from people who’ve made the transition from “Agile/Scrum done wrong” to “Agile/Scrum done right”.

Quotes I’ve heard repeatedly:

  • “It doesn’t work”
  • “It’s a load of corporate-trainer bullsh*t”
  • “It works for industries that have no uncertainty in their design; but in games, you don’t have a spec to start with”
    • [this is depressing; it’s where Scrum shines – but people have mis-applied it so much that they’re getting the opposite result]
  • “Have you seen how easy it is to become a Certified Scrum Trainer? And how much they get paid? It’s just a big con”
    • [IMHO the ScrumAlliance has a lot to answer for, in how they’ve allowed this problem over the past 5-10 years. It seems to have been a strategy of short-term gain (more practitioners) for a long-term loss (reputation and quality of practice)]
  • “In Scrum, you have to do a 2-3 hour meeting every day, where everyone on the project gives a full status update, so we never got any work done”
    • [I’ve heard this worryingly often. It’s stunning that anyone could allow their Daily Scrum meetings to go so wrong]
  • “Scrum is all about equal authority and shared responsibility, but some (our manager) are more equal than others, and never responsible for the failures”
  • “Agile is where you can do anything you want and when someone on your team is lazy and claims everything will take 10x the actual time, so they can do no work all day … you’re not allowed to disagree. Because Agile/Scrum says you can’t disagree with each other”
    • [Again, I’ve heard this often. Did these teams accidentally read the Black Bible of Scrum? The one where everything is inverted?]

This came up on a forum today, and one of the posters chimed-in with their own experiences. The forum’s private, so I can’t link to it, but I’m reposting their summary (with permission):

“I’ve done scrum/agile 4 times in games.

Work does 3 week sprints and is now split into 2 teams so we have 2 scrums a day. Time of this is strict, on-time and 15 mins long*.

Our Producer/Product Manager only sees the program in the Sprint meeting. The entire team is in the Sprint meeting also, which is now pretty good. The Sprint meeting lasts the whole day. So that’s 6-8 hours every 3 weeks*.

The morning is broken into what we did in the previous Sprint. We demonstrate each story and if we tick all the requirements defined in the story it gets marked as complete. If we don’t complete it, we say why and a new story gets made with what’s left. This is only if it is still required and may be put into the Backlog and done several Sprints later.

The afternoon is spent deciding on the next stories. These have been defined by: the Customer Requirements/Publisher/Whatever, the Producer and the Leads. The 2 teams break off and the whole of that team decides the requirements AND the time it will take. This prevents the wasters on the team getting away with poorly defined requirements and giving a time that is clearly taking the piss*. It also covers people underestimating. The peer-pressure means people work because next Sprint people have to explain why it wasn’t finished when everyone decided on it. If no one can decide on the time then it clearly hasn’t been defined well enough*. It helps because the team works closely together and they all understand those parts.

We can ask the Product Manager questions at any time etc, but like I said: he never sees the work in progress for 3 weeks. He also never comes in and changes things*. Because it’s the whole team plus the Producer/Product Manager agreeing on things, features gets dropped when needed, priorities get sorted, and blame can be removed from the development cycle*.

There are various other small details but that’s the main bit of it.

If you aren’t doing these things in scrum/agile then really you’re just wasting people’s time and giving waterfall development the wrong name.

* All stuff Game Development scrum failed to do in my experience.”

Categories
games design GamesThatTeach

GamesThatTeach: Papers, Please

An interesting review here of “Papers, Please”. I haven’t played this one yet, but I’ve been following progress via the promo screenshots they released earlier, and this (p)review sounds great:

“At a certain point, the transit papers will get so complex that you will, inevitably, start making a lot of mistakes. This can become a serious problem as every incorrectly reviewed passport will deduct more and more cash from your pay at the end of the day. With your family’s lives on the line and the job getting tougher and tougher, the game asks you — like actually asks you, through gameplay — what are you willing to do to get the money you need? Will you take a bribe from a shady character? Will you start throwing more people in jail so you can get a cut of the prison guard’s bonus? Will you do knowingly dishonest or immoral things just so you can get that extra ten bucks that might allow your family to eat tonight? There are no right or wrong answers.”

Categories
7dfps games design Unity3D Unity3D-tips

JumpTest v0.2 (different jumping algorithms in Unity)

New version up on the webplayer link:

Screen Shot 2013-08-14 at 18.46.23
http://t-machine.org/web/WebPlayer-jumptest/WebPlayer.html

Test jumps

Proving quite fun just playing around with it and testing different jumps:

Screen Shot 2013-08-14 at 18.56.00

Screen Shot 2013-08-14 at 18.54.41

User-aimed jumping

Two jumps in a row using the same algorithm, but it aims your jump based on where you’re looking when you hit the spacebar. In this case, second jump I flicked the mouse up at last moment before jumping, giving a higher/longer arc:

Screen Shot 2013-08-14 at 18.53.35

Categories
7dfps games design

The perfect jump: testing techniques for Jumping in Unity3D

In a physics-based engine, there’s many ways to make a character “jump”. For my 7-day FPS entry, I’m trying to work out the perfect jump for my gameplay. Easier said than done. I’m struggling…

Before I go looking for feedback and suggestions, I thought it would help to make a demo showing the things I’ve been trying-out.

Unity webplayer link to try the examples below

Instructions: wait till you land on skyscraper, then hit a number key to select a jump type, and get jumping (spacebar).

Note: if you fall, no problem, it’ll auto-respawn you after a couple of seconds of falling

Categories
7dfps computer games games design

7DFPS 2013: Day 2 demo (first day!)

I skipped the first day (long week, needed a break), but here’s a crappy playable demo after my first day:

Screen Shot 2013-08-12 at 00.28.30

Download links:

Categories
7dfps games design

Useful Links for the 2013 7dfps

Just gathering links here as I find them or remember them … useful things on FPS design, FPS coding, FPS art, etc.

(I’ve never written an FPS before, so … everything/anything in the way of research is useful!)

Tools

Shaders

Level design

Assets / Art

  • http://dafont.com – Free, high-quality fonts that (unlike most free fonts sites) aren’t “mostly stolen from somewhere else”, nor “not free, but advertised as free, so they can charge you later”
  • http://cgtextures.com – Marcel’s excellent resource site for textures so you can DIY your models and texturing…

FPS Controller design

Specific to my game:

Assets for city-building in Unity

Unity bugfixes

Things I had to do to fix Unity long-standing bugs (that Unity has declared they’ll never fix). Most of these copy/pasted from my “Fix Unity Bugs” folders in other Unity projects ;)…

  • Customize GameObject to add: Find a specific Component on “the nearest ancestor that has one”
  • Customize GameObject to add: When creating a game object, always place it at 0,0,0, instead of “in the middle of nowhere, randomly”
  • Fix fonts so that they DO NOT render through walls (Unity makes everything in your scene transparent-to-text)
  • Auto-rescale all textures on scaled objects (Unity’s editor won’t allow you to scale a single object without scaling its fixed-size, tiling texture (and without breaking all your other objects) – but Unity source code strangely does allow it)
Categories
7dfps games design

Make an FPS in 7 Days – Game idea: “Metamorphosis”

Next week is the 7DFPS challenge – design, code, ship a game in 7 Days, on the theme of “first person shooter”. I want to do something unique and interesting. Here’s one of my ideas.

(if you like this idea, please re-tweet this to vote for it – I’ll decide which to do based on which one(s) people like most) Other ideas: “Runners”, “Follow”)

Title: “Metamorphosis”

Premise: what makes an FPS unique/special among games is the visceral, immersive nature.

But our FPS’s today all project the same, boring, 1:1 flat rectangle as our window on the world.

What if we had eyes on the side of our head? (a la most herbivores – you can see to both sides, and halfway behind you – but have a blindspot dead-ahead. This is why some birds flick their heads left and right all the time – they physically can’t look directly ahead!)

What if you were a Beholder – with 360 simultaneous vision. Looking ahead, behind, up and down all at once?

What does the world look like when your camera is 2 inches off the ground? (a rat’s-eye-view) … or upside-down, attached to the ceiling? (a spider’s…)

Core gameplay

In Metamorphosis, you start in a lab with one way out: a small mouse-hole in the skirting-board. A theft gone wrong, a wizard about to catch you “in flagrante”. You have only one chance: a giant bubbling flask labelled “Rat”. You gobble the potion, dive through the hole, and escape.

…but now you’re a rat, loose in a dungeon full of nasties. Absolutely everything out there is bigger, meaner, and (often) uglier than you. And sadly: loot won’t be much good, since you’re too small to wear armour, and have no hands to hold a sword.

Special effects

It’s a dungeon-hack with a difference: you’re looking for magic potions that metamorphose you into new and useful creatures. When you metamorphose, everything changes:

  • the lighting (some creatures see by light, bats echo-locate and see only monochrome, other creates have infra-vision/heat-sensitive vision)
  • the viewpoint (high, low, upside-down)
  • the number of simultaneous “cameras” (one per eyeball…)
  • the “lens shape” of each eyeball (normal, fisheye, warped, 360-degrees…)
  • your strength/speed/vulnerability (heavy/light, low hitpoints / high hitpoints, regeneration…)
  • movement abilities (walk, jump (rats jump so high they almost fly…), fly, wall-walk, etc)

Practicality…

An old friend of mine wrote a real-time 360-degree FPS projection 15 years ago, and kindly dug out his original source code + demo app for me. With his permission, I’ll post it here ASAP, but suffice it to say: this stuff is doable, it may sound a bit crazy, but I’ve experimented with it (a little) before.

Upgrades and weapons

…entirely dictated by the creature you’re currently polymorphed into.

Summary

I like this idea because:

  • …polymorphing is fun.
  • …there’s historically been very little experimentation with FPS cameras – the best you can hope for is some post-processing screen effets (depth of field, over-saturation, blur, desaturation)
Categories
7dfps games design

Make an FPS in 7 Days – Game idea: “Runners”

Next week is the 7DFPS challenge – design, code, ship a game in 7 Days, on the theme of “first person shooter”. I want to do something unique and interesting. Here’s one of my ideas.

(if you like this idea, please re-tweet this to vote for it – I’ll decide which to do based on which one(s) people like most) Other ideas: “Follow”, “Metamorphosis”)

Title: “Runners”

Premise: what makes an FPS unique/special among games is the visceral, immersive nature. How many Oculus Rift games are not first-person?

One thing FPS’s should be particularly good at is wish-fulfillment. Well, something I’ve “wished” I could do ever since seeing the opening sequence of The Matrix … is superhuman jumps from rooftop to rooftop. (YouTube: the ‘jump’ program and YouTube: the opening sequence)

(I especially like the second clip – this is the point at which we first get proof that the film can’t quite be real; it’s the first leap down the rabbit-hole…)

Core gameplay

Runners is simply an excuse to do desperate, death-defying, running jumps from rooftop to rooftop, while being shot at (and shooting back).

That’s it!

Challenges to the player

Moment to moment during the game, the player has the following decisions:

  1. Big jumps require big runups; you get faster the longer you run (Super Mario Bros stylee). Better plan your run-up, and don’t chicken-out and slowdown at the last minute!
  2. It’s easy to stand-still and snipe: so we make you invulnerable while in mid-air, and create lots of obstacles on the rooftops, that prevent “easy” shots from a distance
  3. You can’t jump forever (remember the bad old days of deathmatch quake bunnyhopping? Ugh). You have a fatigue meter that forces you to periodically stop and hide while you get your breath back
  4. “Guns. Lots of guns” – long distance, wide-open spaces, enclosed target areas? This is a perfect excuse for overblow weapons of insane destruction. Remember Syndicate’s supremely over-the-top Gauss Gun? Oh yeah!

Decisions, decisions

Three conflicting tensions (no upgrades in this one! Just choice of weapons):

  1. Movement: speed, jump-distance, acceleration (run-ups) … heavy weapons = fail
  2. Accuracy: sniping and head-shots requires keeping still, keeping fatigue at a minimum (no jumping for a while!)
  3. Positioning: is it better to circle round behind an enemy and catch them unawares (lots of jumping, weak weapons) … or go straight at them, guns blazing, and try to scare them into running while tired (they fall to their death)?

Summary

I like this idea because:

  • …who doesn’t want to play in The Matrix?
  • …”Canabalt … with guns”
  • …most FPS’s give you infinite mass and infinite acceleration … this one makes things a lot more realistic and interesting
  • …level-design will be easy! (procedurally generating skyscraper-roofs = WIN)
Categories
7dfps games design GamesThatTeach

Make an FPS in 7 Days – Game idea: “Follow”

Next week is the 7DFPS challenge – design, code, ship a game in 7 Days, on the theme of “first person shooter”. I want to do something unique and interesting. Here’s one of my ideas.

(if you like this idea, please re-tweet this to vote for it – I’ll decide which to do based on which one(s) people like most) Other ideas: “Runners”, “Metamorphosis”)

Title: “Follow”

Premise: what makes an FPS unique/special among games is the visceral, immersive nature. Almost every *other* genre of game gives you 360-degree vision. This was originally to compensate for the way “a computer” fails to use most of your human “peripheral” senses (depth, perpheral vision, positional sound, etc). The most obvious difference with FPS’s: you can’t see what’s behind you.

In most FPS’s, you only have to worry about your own skin. You spin around frequently to check no-one is sneaking up on you. You strafe round corners so that a head-on enemy doesn’t get a chance to blindside you as you slowly rotate around the corner. Stealth games let you “lean” to make up for the danger of corners.

Core gameplay

Follow is different: you are invulnerable, and you’re being followed by creatures you’re trying to rescue / lead to safety. Because they’re always behind you, you cannot see when they’re being attacked – you can gun down all opposition, then turn round to find out everyone’s been eaten behind your back.

But if you walk backwards everywhere, you’re bound to fall off a cliff, or run into an enemy and only notice it when your followers go down in a hail of fire.

Challenges to the player

Moment to moment during the game, the player has to compromise between the following tensions:

  1. Avoid static obstacles/dangers (look where you run) … vs … prevent followers from wandering off (look behind you, at followers)
  2. Takedown enemies (charge forwards, run around obstacles) … vs … lead carefully (avoid leading your followers into traps / fields of fire)
  3. Hands-off (followers wander and get into danger) … vs … Hands-on (use non-lethal weapons to herd and cluster your followers, so you can keep them alive more easily)
  4. Get to level-end / safety quickly (lose lots of followers) … vs … slow, stealthy “no man left behind” (slower and painstaking but higher success rate)

Upgrades

Three interwoven upgrade paths:

  1. Player: might/damage/speed
  2. Relationship with followers: trust/obedience
  3. Followers: speed/intelligence/resilience

At end of each level, you get cash in a virtual currency based on how many followers you rescued, how quickly – and how much they like you.

(if you got them to the end by bullying … they won’t like you so much as if you gently nudged throughout)

Weapon availability

Potentially: various unique weapons only “unlocked” when you achieve difficult non-shooting things, e.g.:

  1. Complete a level with 100% survival of followers
  2. Keep followers at 90% or above happinness for 5 levels in a row
  3. Complete a level in under 50% of the “par” time

Ideally, those weapons would be complementary to the achievement. e.g. for the “complete a level fast” award, you’d get something that made it easier to complete levels stealthily (you’ve already mastered “moving fast”, so this gives you something valuable).

…also: if you don’t like playing fast, this gives you a high cost/reward incentive to do so: you want to play stealthily, you want the uber-weapon for stealth … so you need to take a break from stealth-play to go earn it.

Summary

I like this idea because:

  • …it’s an FPS about protecting people, rather than slaughtering them
  • …it emphasizes the vulnerability of “people other than myself”
  • …it demonstrates that making yourself into a super-soldier may be ultimately useless and unrewarding
  • …it makes the purely-tactical parts of an FPS a lot more strategic, as core gameplay – rather than relying upon “map design” and/or “multiplayer team tactics” to provide all the strategy
Categories
games design iphone programming

Preview of a new game: “Peace by other means” – Screenshots1

I’ve just posted some screenshots + notes for Reddit’s Screenshot Saturday, for the iPad game I’ve been working on for almost 2 years now.

small-IMG_0205

Doesn’t look like much given how long it’s been in development :(, but I’m hoping it’ll speed up from here!

[homepage for the game]

Along the way, I’ve:

  • built the game
  • taught myself advanced Quartz / CoreAnimation
  • wrote a detailed playable game with AI
  • discovered the hard way that Apple doesn’t use hardware-acceleration properly on iPhone/iPad
  • threw away the 2D renderer, re-designed the game around OpenGL + 3D
  • re-wrote everything in 3D with OpenGL 1.x
  • taught myself OpenGLES 2.0
  • re-wrote everything with shaders