Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

iphone Programming : Mixing OpenGL ES and UIKit, Rendering Confetti, Fireworks, and More: Point Sprites

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/9/2012 4:08:22 PM

1. Mixing OpenGL ES and UIKit

Sprites are often used for rendering interactive widgets in a HUD. Handling mouse interaction can be a chore when you don’t have a UI framework to stand upon. If you’re developing a 3D application and find yourself yearning for UIKit, don’t dismiss it right away. It’s true that mixing UIKit and OpenGL ES is generally ill-advised for performance reasons, but in many situations, it’s the right way to go. This is especially true with 3D applications that aren’t as graphically demanding as huge, professionally produced games. Figure 1 depicts an application that overlays a UISegmentedControl widget with a 3D scene.


Note:

The performance of “mixed” rendering has been improving as Apple rolls out new devices and new revisions to the iPhone OS. By the time you read this, using nonanimated UIKit controls in conjunction with OpenGL might be a perfectly acceptable practice.


Figure 1. Mixing UIKit with OpenGL ES


First we need to add a field to the GLView class declaration for the new control. See the bold line in Example 1.

Example 1. Adding a UIKit control to GLView.h
#import "Interfaces.hpp"
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface GLView : UIView {
@private
    IRenderingEngine* m_renderingEngine;
    IResourceManager* m_resourceManager;
    EAGLContext* m_context;
    float m_timestamp;
    UISegmentedControl* m_filterChooser;
}

- (void) drawView: (CADisplayLink*) displayLink;

@end

Next, we need to instance the control and create a method for event handling; see Example 2.

Example 2. Adding a UIKit control to GLView.mm
...

- (id) initWithFrame: (CGRect) frame
{
    if (self = [super initWithFrame:frame])
    {
        CAEAGLLayer* eaglLayer = (CAEAGLLayer*) self.layer;
        eaglLayer.opaque = YES;

        EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES1;
        m_context = [[EAGLContext alloc] initWithAPI:api];
        
        ...
        
        // Create and configure the UIKit control:
        
        NSArray* labels = [NSArray arrayWithObjects:@"Nearest",
                                                    @"Bilinear",
                                                    @"Trilinear", nil];
        
        m_filterChooser = 
          [[[UISegmentedControl alloc] initWithItems:labels] autorelease];
        m_filterChooser.segmentedControlStyle = UISegmentedControlStyleBar;
        m_filterChooser.selectedSegmentIndex = 0;
        
        [m_filterChooser addTarget:self
                         action:@selector(changeFilter:)
                         forControlEvents:UIControlEventValueChanged];

        // Add the control to GLView's children:
        
        [self addSubview:m_filterChooser];
        
        // Position the UIKit control:
        
        const int ScreenWidth = CGRectGetWidth(frame);
        const int ScreenHeight = CGRectGetHeight(frame);
        const int Margin = 10;

        CGRect controlFrame = m_filterChooser.frame;
        controlFrame.origin.x = ScreenWidth / 2 - controlFrame.size.width / 2;
        controlFrame.origin.y = ScreenHeight - controlFrame.size.height - Margin;
        m_filterChooser.frame = controlFrame;
    }
    return self;
}

- (void) changeFilter: (id) sender
{
    TextureFilter filter = (TextureFilter) [sender selectedSegmentIndex];
    m_renderingEngine->SetFilter(filter);
}

...


					  

Example 2 includes some UIKit and Objective-C mechanisms that we haven’t seen before (such as @selector), but it will be familiar to iPhone developers.

Note that you can also use UIKit to render “look-alike” controls, rather than using the actual UIKit controls. For example, you can render some buttons into a CGImage at launch time and then create an OpenGL texture from that . This would give your buttons the look and feel of the iPhone’s native UI, plus it wouldn’t suffer from the potential performance issues inherent in mixing the actual UIKit control with OpenGL. The downside is that you’d need to implement the interactivity by hand.

2. Rendering Confetti, Fireworks, and More: Point Sprites

You may find yourself wanting to render a system of particles that need a bit more pizzazz than mere single-pixel points of light. The first thing that might come to mind is rendering a small alpha-blended quad for each particle. This is a perfectly reasonable approach, but it requires you to come up with the coordinates for two textured triangles at each point.

It turns out the iPhone supports an extension to make this much easier by enabling point sprites. Point sprites are small screen-aligned quads that get drawn at each vertex in a vertex array or VBO. For simplicity, a point sprite uses an entire texture; there’s no need to provide texture coordinates. This makes it a breeze to render particle systems such as the one depicted in Figure 2.

Figure 2. Point sprites


For OpenGL ES 1.1, the name of the extension is GL_OES_point_sprite, and it allows you to make the following function calls:

glEnable(GL_POINT_SPRITE_OES);
glDisable(GL_POINT_SPRITE_OES);
glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_FALSE);

With OpenGL ES 2.0, point sprites are supported in the core specification rather than an extension. There’s no need to call any of these functions because point sprite functionality is implicit. You’ll see how to use point sprites in both ES 1.1 and ES 2.0 in the upcoming SpringyStars sample.
Other -----------------
- iphone Programming : Animation with Sprite Sheets, Image Composition and a Taste of Multitexturing
- XNA Game Studio 3.0 : Creating Game Components - Adding Game Sounds
- Iphone Application : Using Gesture Recognizers (part 4)
- Iphone Application : Using Gesture Recognizers (part 3)
- Iphone Application : Using Gesture Recognizers (part 2)
- Iphone Application : Using Gesture Recognizers (part 1)
- Handling Input on Windows Phone 7 : Microphone Input
- Handling Input on Windows Phone 7 : Accelerometer
- XNA Game Studio 4.0 : XNA Game Studio Storage (part 2) - Getting a Device
- XNA Game Studio 4.0 : XNA Game Studio Storage (part 1) - Recreating the Project on Xbox, Devices and Containers
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server