Tut 06: The iOS Way


December 7th, 2012 by Eric Berna

I had a choice when porting to iOS Jason L. McKesson’s example programs from his book Learning Modern 3D Graphics. Since Xcode supports including C++ code in iOS apps in combination with Objective C, I could have kept to C++ and tried to get McKesson’s programs to run on iOS with minimal changes. Mostly this would mean replacing calls to the GLUT framework with Cocoa Touch equivalents. Or I could rewrite McKesson’s programs in Objective C with Cocoa Touch. I decided that the point of my project is to figure out and share how to use OpenGL ES 2.0 on iOS with Objective C. This guides me to make changes to the code wherever the iOS way is different from what I find in McKesson’s programs.

Chapter 6 of Learning Modern 3D Graphics by Jason L. McKesson covers manipulating 3D models. This chapter covers how to express transformations including moving, rotating, and scaling objects, expanding on matrix manipulations to represent these manipulations. This chapter doesn’t cover any new ground in the OpenGL API, concentrating on model manipulations on the CPU side. In this chapter McKesson introduces GLM, OpenGL Mathematics. Since GLKit includes similar functionality, optimized for iOS devices, I substitute GLKit’s math for GLM. McKesson also introduces the concept of a matrix stack, including a C++ implementation. Since GLKit includes a matrix stack data type with a collection of functions for manipulating that stack, I didn’t bother to port McKesson’s implementation.

Furthermore, my port of Hierarchy.cpp required a user interface that works on iOS. Hitting keyboard keys to manipulate the model just doesn’t work. Thinking about a proper iOS interface for this project distracted me with ideas about direct manipulation of the robot arm, maybe even using the accelerometer and gyroscope to control the model. Yet, that much effort was not appropriate for this example that is merely trying to demonstrate how to use a matrix stack. So I settled on a simple set of UISlider controls to replace the key presses.

One further change was the design of the Hierarchy class. The differences between C++ and Objective C, and differences between Cocoa Touch and GLUT, pushed me to major changes in the structure when implementing my version of the Hierarchy class. The fundamental difference is that McKesson’s Hierachy class draws itself, while mine merely maintains the model state so the view controller can draw the scene. Mine will respond to inputs from the user interface to update the matrixes for each part of the robot arm, and supply those matrixes to the view controller at draw time. This results in an optimization. The matrix calculations only occur when there is user input, not every time through the draw loop.

One change I didn’t make that I would make in real production code is which angle specification units I would use. In non demo projects I would work in radians instead of degrees. Once I stopped trying to convert radians to degrees or pi to decimal in my head, and started trying to think in radians, I found radians easier to use than degrees. In production code I would prefer to store my angles as radians, avoiding running the degree to radian conversion code at runtime.

Looking ahead in the McKesson’s book I see that my porting effort will see many similar situations. I’ll continue to use Cocoa Touch replacements for GLM and GLUT, use the GLKit supplied matrix stack, and redesign program structure to better fit the Cocoa Touch patterns.

Posted in Uncategorized | Comments (0)

Tut 05: The Missing Apps


December 4th, 2012 by Eric Berna

As I’ve already covered, OpenGL ES is a subset of Open GL. Chapter 5 of Learning Modern 3D Graphics by Jason L. McKesson has two tutorial programs specifically highlighting features of desktop OpenGL ES that are not included in OpenGL ES 2.0. These shortcoming of OpenGL ES make porting the examples to iOS rather pointless, so I’ve just skipped those two programs.

The BaseVertexOverlap.cpp tutorial program showcases glDrawElementsBaseVertex, not available in OpenGL ES, as an optimization for drawing multiple meshes in one VAO. The remaining tutorials in this chapter all use this optimization method. In order to show the other aspects highlighted in the remaining tutorials, I’ve ported them to iOS with the two VAOs and glDrawElements function used in the first tutorial.

Depth clamping is also not supported in OpenGL ES 2.0, making an iOS port of DepthClamping.cpp difficult.

McKesson includes a program called DepthFighting.cpp in his example code, but does not describe the program in his book, so I skipped that program also.

Posted in Uncategorized | Comments (0)

The Rest of Tutorial 3


November 30th, 2012 by Eric Berna

I’m running out of comments. If you’ve been following along in my Learning Modern 3D Graphics Programming on iOS series (see the announcement) you’ll already know about all the differences between desktop OpenGL and OpenGL ES 2.0 on iOS combined with GLKit that matter for writing the programs in chapter 3 of McKesson’s book. After completing the ports of the last three programs in chapter 3 I have nothing intelligent to add to my series of comments. I plan to continue to add all of the iOS ports of the tutorial programs to this project’s repository on Bitbucket, but I will only add more blog posts for programs that introduce new aspects of GLKit or OpenGL ES 2.0′s differences from desktop OpenGL.

Posted in C, Graphics, iOS, Open GL, Programming | Comments (0)

Tut 03 CPU Position Offset


November 30th, 2012 by Eric Berna

This tutorial program in my Learning Modern 3D Graphics Programming on iOS series (see the announcement) is very similar to the previous programs in the series. Most of the differences seen between the original program and the iOS version of the program are already covered in the previous blog posts. The only new concept needed for the iOS version is the differences between GLUT and GLKit’s update methodology.

Preliminaries

This is the first port in this series I’ve started after iOS 6 shipped. Instead of starting with the Chapter 2 projects, I’ve created a new project from Xcode’s OpenGL Game for iOS template. This introduces some subtle differences in the overall app. I haven’t noticed any GLKit specific changes in the template, and I’ll avoid discussing general iOS 6 changes.

Read Chapter 3 OpenGL’s Moving Triangle from McKesson’s book, clone my version of the program from Bitbucket, so we can begin.

Read the rest of this entry »

Posted in C, Graphics, iOS, Open GL, Programming | Comments (0)

Tut 02 Playing with Colors VertexColors


November 29th, 2012 by Eric Berna

This post describes this tutorial in my Learning Modern 3D Graphics Programming on iOS series (see the announcement). Tutorial 2 in the book is two programs showing alternative methods of giving the triangle from the previous tutorial more color.

Preliminaries

I’m assuming you’re following along in my series, and will omit many details that I’ve covered in previous blog posts. Yet, there are minor annoying differences between desktop OpenGL and OpenGL ES 2.0. I may repeat myself on these details.

Once you’ve read Chapter 2. Playing with Colors from McKesson’s book, and cloned my version of the program from Bitbucket you can get into the iOS version of the program.

Image Differences

Jumping ahead a bit to the output of this program, the code in McKesson’s book assumes a square window. On iOS devices the screen is not square. The default for GLKit is to scale the output to fit the view. This warps the triangles we see on an iPhone or iPad. Where McKesson describes a right isosceles triangle we see a right triangle with three different sized sides, and where McKesson describes an equilateral triangle we see an isosceles triangle. Later sections will cover adjusting to the screen size on iOS devices. For now we’ll keep the code simple to focus on the topics covered in the book.

Read the rest of this entry »

Posted in C, Graphics, iOS, Open GL, Programming | Comments (0)

Tut 02 Playing with Colors FragPosition


November 29th, 2012 by Eric Berna

This post describes this tutorial in my Learning Modern 3D Graphics Programming on iOS series (see the announcement). Tutorial 2 in the book is two programs showing alternative methods of giving the triangle from the previous tutorial more color.

Preliminaries

I’m assuming you’re following along in my series, and will omit many details that I’ve covered in previous blog posts. Yet, there are minor annoying differences between desktop OpenGL and OpenGL ES 2.0. I may repeat myself on these details.

This tutorial was written with Xcode 4.5, and takes advantage of some of the advances in Xcode and Objective C. I like many of the syntax simplifications in the latest versions of Objective C.

Once you’ve read Chapter 2. Playing with Colors from McKesson’s book, and cloned my version of the program from Bitbucket you can get into the iOS version of the program.

Read the rest of this entry »

Posted in C, Graphics, iOS, Open GL, Programming | Comments (0)

Tut O1 Hello Triangle


July 24th, 2012 by Eric Berna

This post describes the first example program in my Learning Modern 3D Graphics Programming on iOS series (see the announcement). The Hello, Triangle! app shows how to get started by building a very basic program that merely draws a triangle to the screen. My implementation is a blending of Xcode’s iOS OpenGL Game template and McKesson’s example program. This post follows McKesson’s coverage of the program while describing how my iOS version differs from McKesson’s version.

As I wrote this I chose one of two tacks to follow McKesson’s tutorials. My choices were following the text in the book, describing the differences section by section in the chapter, or following the code, describing the differences function (or method) by function. Since many sections in the book require little to no comment to implement on iOS, and some iOS methods don’t map well to sections in the book, I decided to follow the code. I tried to keep to the order in the book, presenting my comments on McKesson’s function in the same order as those functions are described in the book.
Read the rest of this entry »

Posted in Uncategorized | Comments (0)

It’s a Race


June 28th, 2012 by Eric Berna

Who’s faster at shipping to me from China? FedEx or UPS?

I ordered two products from Apple on one order. Both were made in China, and shipped straight to me from the manufacturer. UPS was tasked with delivering the product made in Shanghai, and FedEx was tasked with delivering the product made in Chengdu. Apple sent me two shipment notification emails, one for each product. According to the email headers, UPS got the package at 10:35 pm my time, and FedEx got the package at 10:45 pm my time.

I chose the standard free shipping. My guess is that Apple chose to use UPS and FedEx because they were the cheapest supplier at each location. FedEx calls their service International Economy Distribution, and UPS calls their service World Wide Express, making the UPS service sound faster, but why would Apple pay extra for a higher level of service?

Read the rest of this entry »

Posted in Uncategorized | Comments (0)

Learning Modern 3D Graphics Programming on iOS


January 10th, 2012 by Eric Berna

Modern 3D graphics APIs such as recent versions of OpenGL use a programmable rendering pipeline. Unfortunately I know more about the older fixed-function rendering pipeline. I took a course in college on 3D graphics programming that taught lots of theory and an older version of OpenGL. I’ve produced programs that portably run in GLUT on both Macs and Windows. I’ve written an iOS app that uses OpenGL ES 1. Yet I was slow to make the jump to the programmable pipeline versions of OpenGL.

I recently came across Jason L. McKesson’s Learning Modern 3D Graphics Programming. After a quick perusal I concluded the book could be a refresher for 3D graphics programming while moving me up to the programmable pipeline. I started reading the book, downloaded the source code, and tried to get it running on my Mac.

Read the rest of this entry »

Posted in Graphics, iOS, Open GL, Programming | Comments (5)

Goodbye World


October 14th, 2011 by Eric Berna

#include <stdio.h>
 int main ()
{
    printf("Goodby Dennis Ritchie.");
    return -1;
}

Posted in C, In memoriam, Programming | Comments (0)