1.0: A Point in the Right Direction (Setup)

Setup For Drawing a Point On Screen

Now that have a view that allows us to draw to the screen, let's start drawing something.  We'll start by drawing one vertex to the screen.  That may sound boring, but drawing requires covering several concepts at once:  shaders, shader programs (what artists are usually referring to when they talk about "shaders"), buffers (for data, attributes, etc.), and draw commands (we'll just cover one in this post).  All of these concepts together are the foundations of a rendering engine:  another concept that is thrown around, but has a rather ambiguous meaning.  I've seen it used in regard to everything from full applications for making games, to bare bones API's.  If you search for the definition of "rendering engine" on Google, you'll see a definition from PC Mag stating that a rendering engine is, essentially, "software that forms the text and images for display and printing".  What we will be writing here fits that definition quite well.

Setting Up the Project

This step is entirely optional, but it allows you to keep the steps of your code all in one project.  Xcode offers a "Snapshot" feature which you are welcome to use as well, but this rewrites the files in your current project to reflect the changes.  Instead, we're going to use Schemes.  Essentially this makes a new app within your project.  This means all of your code is accessible right when you need it, and running a particular target (app) is as easy as selecting the appropriate scheme.

Before we set up a new scheme, let's work on the one we have.  Every time you make a new scheme, you'll make a new target, info-plist, app, test target, test info-plist, app delegate, view controller, image assets folder, and storyboard.  If you duplicate a target, you reuse the app delegate, view controller, image assets, and storyboard.  In order to tell the difference between each target, we'll rename the target, info-plist, and product.  Click on SwiftOpenGL at the top of the Project Navigator. Then in the Editor, click on the Target and press Return on the keyboard to change the name to Beginning.


Also, rename the Group folder to Beginning as well.  If you don't have a group, select AppDelegate.swift, ViewController.swift, SwiftOpenGLView.swift, images.xcassets, main.storyboard, and Supporting Files.  Then right click on the selection and click on New Group from Selection.  Name this group Beginning.  In the Supporting Files folder, change the name of the Info.plist to Beginning-Info.plist.  Once the name is changed, you need to let Xcode know what the plist is called now and where it is located.  Before we do that though, let's clean up the project's folder in Finder.  You can go straight to the folder to selecting any of the files in the Project Navigator (we'll select Beginning-Info.plist), and then click on the arrow next to the path name.


This will open up the selected file in a Finder window.


Within the SwiftOpenGL project folder, you'll see AppDelegate.swift, ViewController.swift, SwiftOpenGLView.swift, image.xcassets, and Base.proj.  Select these items, right click on them, and click on New Folder with Selection (6 items).  Name this folder Beginning.  Close the window.  In Xcode, you'll notice that you're folders are now red!  You have to click on each and set the path manually.  This is why we didn't set the Info.plist path earlier--we would have had to do that twice!  Click on AppDelegate.swift.  In the Utilities panel, on the File Inspector tab, click on the Folder icon instead of the arrow icon.  This brings up a page down menu.  Navigate to the Beginning folder where the AppDelegate.swift file is located.  Select AppDelegate.swift, and click Choose.



Repeat this for ViewController.swift, SwiftOpenGLView.swift, and images.xcassets as well as the group folder Beginning (if you miss this one, look at post 1.2).  For the Info.plist, click on the SwiftOpenGL project at the top of the Project Navigator.  Then click on Build Settings.  Search for 'info.plist".  Under Packaging you'll see see Info.plist File.  To the right of that is the plist file path.  Double click on the path that's there and change it to Beginning/Beginning-info.plist and press Return on the keyboard.


Now we'll change the name of the scheme.  At the top left of the window, to the right of the Stop button is the Scheme dropdown menu.  Click on it and then click on Manage Schemes... at the bottom.


Again, you see some of the future targets that we're going to create in the image above.  The Manage Schemes menu lists all of the available schemes.  You'll only have one target.  Click on it and press Return on the keyboard.  Rename the scheme as Beginning.  Click close.


Test your app to make sure it still runs.  If it does, you're set to move on to create the next target.  Again, click on the SwiftOpenGL project in the Project Navigator.  In the Editor, at the bottom left of the window, click on the "+".


You'll see the same menu you saw when you created the project.  Make sure Application under OS X is selected.  Then select Cocoa Application and click Next.  On the next window, the only change new thing here, is the Project field.  There's only one option here:  SwiftOpenGL.  Set the Product Name as FirstVertex and all the other fields will be the same.  Click Finish.  Delete the FirstVertex Test target and its plist.

In the Project Navigator, make a new group of the FirstVertex's AppDelegate.swift, ViewController.swift, images.xcassets, main.storyboard, and Info.plist.  You can also make a group called Supporting Files out the Info.plist, if you like.


Rename the Info.plist as FirstVertex-Info.plist as you see at the bottom of the image above.  Now open a Finder window to location of these files like we did for the last target.  Create a new folder in the SwiftOpenGL project folder called FirstVertex.  Now you'll have Beginning and FirstVertex in your project folder.  Move all of the FirstVertex files into the FirstVertex folder like you did for the Beginning target files.

Back in Xcode, set the paths for each file like we did before (remember to set the path for the FirstVertex-Info.plist through Build Phases).  Change the name of the scheme for FirstVertex by clicking Manage Schemes... in the Scheme dropdown.  Test the app by running it.

If you're app runs, you're ready to set up the UI just like the last post.  I won't go back through that process, except for one detail.  I want to remind you to select the right target when you create a file for the SwiftOpenGLView.  Make sure you save the file to FirstVertex, select the FirstVertex group, and select the FirstVertex target, and not Beginning.


Now you have two working targets.  Make sure the scheme is set to FirstVertex and SwiftOpenGLView.swift is selected before you jump into writing the first version of the render engine.

Comments

Popular posts from this blog

4.0.0: MVC OpenGL Part 1

Using Swift with OpenGL

0.0: Swift OpenGL Setup