istereo

changeset 0:1bb950d0976b

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 06 Sep 2011 08:07:14 +0300
parents
children 4d25539806d2
files src/EAGLView.h src/EAGLView.m src/ES1Renderer.h src/ES1Renderer.m src/ES2Renderer.h src/ES2Renderer.m src/ESRenderer.h src/istereo.c src/istereo.h src/istereoAppDelegate.h src/istereoAppDelegate.m src/main.m src/sdr.c src/sdr.h
diffstat 14 files changed, 978 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/EAGLView.h	Tue Sep 06 08:07:14 2011 +0300
     1.3 @@ -0,0 +1,40 @@
     1.4 +//
     1.5 +//  EAGLView.h
     1.6 +//  istereo
     1.7 +//
     1.8 +//  Created by nuclear on 9/6/11.
     1.9 +//  Copyright __MyCompanyName__ 2011. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import <UIKit/UIKit.h>
    1.13 +#import <QuartzCore/QuartzCore.h>
    1.14 +
    1.15 +#import "ESRenderer.h"
    1.16 +
    1.17 +// This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass.
    1.18 +// The view content is basically an EAGL surface you render your OpenGL scene into.
    1.19 +// Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.
    1.20 +@interface EAGLView : UIView
    1.21 +{    
    1.22 +@private
    1.23 +    id <ESRenderer> renderer;
    1.24 +
    1.25 +    BOOL animating;
    1.26 +    BOOL displayLinkSupported;
    1.27 +    NSInteger animationFrameInterval;
    1.28 +    // Use of the CADisplayLink class is the preferred method for controlling your animation timing.
    1.29 +    // CADisplayLink will link to the main display and fire every vsync when added to a given run-loop.
    1.30 +    // The NSTimer class is used only as fallback when running on a pre 3.1 device where CADisplayLink
    1.31 +    // isn't available.
    1.32 +    id displayLink;
    1.33 +    NSTimer *animationTimer;
    1.34 +}
    1.35 +
    1.36 +@property (readonly, nonatomic, getter=isAnimating) BOOL animating;
    1.37 +@property (nonatomic) NSInteger animationFrameInterval;
    1.38 +
    1.39 +- (void)startAnimation;
    1.40 +- (void)stopAnimation;
    1.41 +- (void)drawView:(id)sender;
    1.42 +
    1.43 +@end
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/EAGLView.m	Tue Sep 06 08:07:14 2011 +0300
     2.3 @@ -0,0 +1,150 @@
     2.4 +//
     2.5 +//  EAGLView.m
     2.6 +//  istereo
     2.7 +//
     2.8 +//  Created by nuclear on 9/6/11.
     2.9 +//  Copyright __MyCompanyName__ 2011. All rights reserved.
    2.10 +//
    2.11 +
    2.12 +#import "EAGLView.h"
    2.13 +
    2.14 +#import "ES1Renderer.h"
    2.15 +#import "ES2Renderer.h"
    2.16 +
    2.17 +@implementation EAGLView
    2.18 +
    2.19 +@synthesize animating;
    2.20 +@dynamic animationFrameInterval;
    2.21 +
    2.22 +// You must implement this method
    2.23 ++ (Class)layerClass
    2.24 +{
    2.25 +    return [CAEAGLLayer class];
    2.26 +}
    2.27 +
    2.28 +//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
    2.29 +- (id)initWithCoder:(NSCoder*)coder
    2.30 +{    
    2.31 +    if ((self = [super initWithCoder:coder]))
    2.32 +    {
    2.33 +        // Get the layer
    2.34 +        CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
    2.35 +
    2.36 +        eaglLayer.opaque = TRUE;
    2.37 +        eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    2.38 +                                        [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
    2.39 +
    2.40 +        renderer = [[ES2Renderer alloc] init];
    2.41 +
    2.42 +        if (!renderer)
    2.43 +        {
    2.44 +            renderer = [[ES1Renderer alloc] init];
    2.45 +
    2.46 +            if (!renderer)
    2.47 +            {
    2.48 +                [self release];
    2.49 +                return nil;
    2.50 +            }
    2.51 +        }
    2.52 +
    2.53 +        animating = FALSE;
    2.54 +        displayLinkSupported = FALSE;
    2.55 +        animationFrameInterval = 1;
    2.56 +        displayLink = nil;
    2.57 +        animationTimer = nil;
    2.58 +
    2.59 +        // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
    2.60 +        // class is used as fallback when it isn't available.
    2.61 +        NSString *reqSysVer = @"3.1";
    2.62 +        NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    2.63 +        if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
    2.64 +            displayLinkSupported = TRUE;
    2.65 +    }
    2.66 +
    2.67 +    return self;
    2.68 +}
    2.69 +
    2.70 +- (void)drawView:(id)sender
    2.71 +{
    2.72 +    [renderer render];
    2.73 +}
    2.74 +
    2.75 +- (void)layoutSubviews
    2.76 +{
    2.77 +    [renderer resizeFromLayer:(CAEAGLLayer*)self.layer];
    2.78 +    [self drawView:nil];
    2.79 +}
    2.80 +
    2.81 +- (NSInteger)animationFrameInterval
    2.82 +{
    2.83 +    return animationFrameInterval;
    2.84 +}
    2.85 +
    2.86 +- (void)setAnimationFrameInterval:(NSInteger)frameInterval
    2.87 +{
    2.88 +    // Frame interval defines how many display frames must pass between each time the
    2.89 +    // display link fires. The display link will only fire 30 times a second when the
    2.90 +    // frame internal is two on a display that refreshes 60 times a second. The default
    2.91 +    // frame interval setting of one will fire 60 times a second when the display refreshes
    2.92 +    // at 60 times a second. A frame interval setting of less than one results in undefined
    2.93 +    // behavior.
    2.94 +    if (frameInterval >= 1)
    2.95 +    {
    2.96 +        animationFrameInterval = frameInterval;
    2.97 +
    2.98 +        if (animating)
    2.99 +        {
   2.100 +            [self stopAnimation];
   2.101 +            [self startAnimation];
   2.102 +        }
   2.103 +    }
   2.104 +}
   2.105 +
   2.106 +- (void)startAnimation
   2.107 +{
   2.108 +    if (!animating)
   2.109 +    {
   2.110 +        if (displayLinkSupported)
   2.111 +        {
   2.112 +            // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
   2.113 +            // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
   2.114 +            // not be called in system versions earlier than 3.1.
   2.115 +
   2.116 +            displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)];
   2.117 +            [displayLink setFrameInterval:animationFrameInterval];
   2.118 +            [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
   2.119 +        }
   2.120 +        else
   2.121 +            animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView:) userInfo:nil repeats:TRUE];
   2.122 +
   2.123 +        animating = TRUE;
   2.124 +    }
   2.125 +}
   2.126 +
   2.127 +- (void)stopAnimation
   2.128 +{
   2.129 +    if (animating)
   2.130 +    {
   2.131 +        if (displayLinkSupported)
   2.132 +        {
   2.133 +            [displayLink invalidate];
   2.134 +            displayLink = nil;
   2.135 +        }
   2.136 +        else
   2.137 +        {
   2.138 +            [animationTimer invalidate];
   2.139 +            animationTimer = nil;
   2.140 +        }
   2.141 +
   2.142 +        animating = FALSE;
   2.143 +    }
   2.144 +}
   2.145 +
   2.146 +- (void)dealloc
   2.147 +{
   2.148 +    [renderer release];
   2.149 +
   2.150 +    [super dealloc];
   2.151 +}
   2.152 +
   2.153 +@end
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/ES1Renderer.h	Tue Sep 06 08:07:14 2011 +0300
     3.3 @@ -0,0 +1,30 @@
     3.4 +//
     3.5 +//  ES1Renderer.h
     3.6 +//  istereo
     3.7 +//
     3.8 +//  Created by nuclear on 9/6/11.
     3.9 +//  Copyright __MyCompanyName__ 2011. All rights reserved.
    3.10 +//
    3.11 +
    3.12 +#import "ESRenderer.h"
    3.13 +
    3.14 +#import <OpenGLES/ES1/gl.h>
    3.15 +#import <OpenGLES/ES1/glext.h>
    3.16 +
    3.17 +@interface ES1Renderer : NSObject <ESRenderer>
    3.18 +{
    3.19 +@private
    3.20 +    EAGLContext *context;
    3.21 +
    3.22 +    // The pixel dimensions of the CAEAGLLayer
    3.23 +    GLint backingWidth;
    3.24 +    GLint backingHeight;
    3.25 +
    3.26 +    // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view
    3.27 +    GLuint defaultFramebuffer, colorRenderbuffer;
    3.28 +}
    3.29 +
    3.30 +- (void)render;
    3.31 +- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer;
    3.32 +
    3.33 +@end
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/ES1Renderer.m	Tue Sep 06 08:07:14 2011 +0300
     4.3 @@ -0,0 +1,131 @@
     4.4 +//
     4.5 +//  ES1Renderer.m
     4.6 +//  istereo
     4.7 +//
     4.8 +//  Created by nuclear on 9/6/11.
     4.9 +//  Copyright __MyCompanyName__ 2011. All rights reserved.
    4.10 +//
    4.11 +
    4.12 +#import "ES1Renderer.h"
    4.13 +
    4.14 +@implementation ES1Renderer
    4.15 +
    4.16 +// Create an OpenGL ES 1.1 context
    4.17 +- (id)init
    4.18 +{
    4.19 +    if ((self = [super init]))
    4.20 +    {
    4.21 +        context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
    4.22 +
    4.23 +        if (!context || ![EAGLContext setCurrentContext:context])
    4.24 +        {
    4.25 +            [self release];
    4.26 +            return nil;
    4.27 +        }
    4.28 +
    4.29 +        // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
    4.30 +        glGenFramebuffersOES(1, &defaultFramebuffer);
    4.31 +        glGenRenderbuffersOES(1, &colorRenderbuffer);
    4.32 +        glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
    4.33 +        glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    4.34 +        glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
    4.35 +    }
    4.36 +
    4.37 +    return self;
    4.38 +}
    4.39 +
    4.40 +- (void)render
    4.41 +{
    4.42 +    // Replace the implementation of this method to do your own custom drawing
    4.43 +
    4.44 +    static const GLfloat squareVertices[] = {
    4.45 +        -0.5f,  -0.33f,
    4.46 +         0.5f,  -0.33f,
    4.47 +        -0.5f,   0.33f,
    4.48 +         0.5f,   0.33f,
    4.49 +    };
    4.50 +
    4.51 +    static const GLubyte squareColors[] = {
    4.52 +        255, 255,   0, 255,
    4.53 +        0,   255, 255, 255,
    4.54 +        0,     0,   0,   0,
    4.55 +        255,   0, 255, 255,
    4.56 +    };
    4.57 +
    4.58 +    static float transY = 0.0f;
    4.59 +
    4.60 +    // This application only creates a single context which is already set current at this point.
    4.61 +    // This call is redundant, but needed if dealing with multiple contexts.
    4.62 +    [EAGLContext setCurrentContext:context];
    4.63 +
    4.64 +    // This application only creates a single default framebuffer which is already bound at this point.
    4.65 +    // This call is redundant, but needed if dealing with multiple framebuffers.
    4.66 +    glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
    4.67 +    glViewport(0, 0, backingWidth, backingHeight);
    4.68 +
    4.69 +    glMatrixMode(GL_PROJECTION);
    4.70 +    glLoadIdentity();
    4.71 +    glMatrixMode(GL_MODELVIEW);
    4.72 +    glLoadIdentity();
    4.73 +    glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
    4.74 +    transY += 0.075f;
    4.75 +
    4.76 +    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    4.77 +    glClear(GL_COLOR_BUFFER_BIT);
    4.78 +
    4.79 +    glVertexPointer(2, GL_FLOAT, 0, squareVertices);
    4.80 +    glEnableClientState(GL_VERTEX_ARRAY);
    4.81 +    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
    4.82 +    glEnableClientState(GL_COLOR_ARRAY);
    4.83 +
    4.84 +    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    4.85 +
    4.86 +    // This application only creates a single color renderbuffer which is already bound at this point.
    4.87 +    // This call is redundant, but needed if dealing with multiple renderbuffers.
    4.88 +    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    4.89 +    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
    4.90 +}
    4.91 +
    4.92 +- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
    4.93 +{	
    4.94 +    // Allocate color buffer backing based on the current layer size
    4.95 +    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    4.96 +    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer];
    4.97 +    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    4.98 +    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
    4.99 +
   4.100 +    if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
   4.101 +    {
   4.102 +        NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
   4.103 +        return NO;
   4.104 +    }
   4.105 +
   4.106 +    return YES;
   4.107 +}
   4.108 +
   4.109 +- (void)dealloc
   4.110 +{
   4.111 +    // Tear down GL
   4.112 +    if (defaultFramebuffer)
   4.113 +    {
   4.114 +        glDeleteFramebuffersOES(1, &defaultFramebuffer);
   4.115 +        defaultFramebuffer = 0;
   4.116 +    }
   4.117 +
   4.118 +    if (colorRenderbuffer)
   4.119 +    {
   4.120 +        glDeleteRenderbuffersOES(1, &colorRenderbuffer);
   4.121 +        colorRenderbuffer = 0;
   4.122 +    }
   4.123 +
   4.124 +    // Tear down context
   4.125 +    if ([EAGLContext currentContext] == context)
   4.126 +        [EAGLContext setCurrentContext:nil];
   4.127 +
   4.128 +    [context release];
   4.129 +    context = nil;
   4.130 +
   4.131 +    [super dealloc];
   4.132 +}
   4.133 +
   4.134 +@end
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/ES2Renderer.h	Tue Sep 06 08:07:14 2011 +0300
     5.3 @@ -0,0 +1,33 @@
     5.4 +//
     5.5 +//  ES2Renderer.h
     5.6 +//  istereo
     5.7 +//
     5.8 +//  Created by nuclear on 9/6/11.
     5.9 +//  Copyright __MyCompanyName__ 2011. All rights reserved.
    5.10 +//
    5.11 +
    5.12 +#import "ESRenderer.h"
    5.13 +
    5.14 +#import <OpenGLES/ES2/gl.h>
    5.15 +#import <OpenGLES/ES2/glext.h>
    5.16 +
    5.17 +@interface ES2Renderer : NSObject <ESRenderer>
    5.18 +{
    5.19 +@private
    5.20 +    EAGLContext *context;
    5.21 +
    5.22 +    // The pixel dimensions of the CAEAGLLayer
    5.23 +    GLint backingWidth;
    5.24 +    GLint backingHeight;
    5.25 +
    5.26 +    // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view
    5.27 +    GLuint defaultFramebuffer, colorRenderbuffer;
    5.28 +
    5.29 +    GLuint program;
    5.30 +}
    5.31 +
    5.32 +- (void)render;
    5.33 +- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer;
    5.34 +
    5.35 +@end
    5.36 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/ES2Renderer.m	Tue Sep 06 08:07:14 2011 +0300
     6.3 @@ -0,0 +1,93 @@
     6.4 +#import "ES2Renderer.h"
     6.5 +#include "istereo.h"
     6.6 +
     6.7 +@implementation ES2Renderer
     6.8 +
     6.9 +// Create an OpenGL ES 2.0 context
    6.10 +- (id)init
    6.11 +{
    6.12 +    if ((self = [super init]))
    6.13 +    {
    6.14 +        context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    6.15 +
    6.16 +        if (!context || ![EAGLContext setCurrentContext:context])
    6.17 +        {
    6.18 +            [self release];
    6.19 +            return nil;
    6.20 +        }
    6.21 +
    6.22 +        // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
    6.23 +        glGenFramebuffers(1, &defaultFramebuffer);
    6.24 +        glGenRenderbuffers(1, &colorRenderbuffer);
    6.25 +        glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    6.26 +        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    6.27 +        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
    6.28 +		
    6.29 +		init();
    6.30 +    }
    6.31 +
    6.32 +    return self;
    6.33 +}
    6.34 +
    6.35 +- (void)render
    6.36 +{
    6.37 +	redraw();
    6.38 +    // This application only creates a single default framebuffer which is already bound at this point.
    6.39 +    // This call is redundant, but needed if dealing with multiple framebuffers.
    6.40 +    //glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    6.41 +    //glViewport(0, 0, backingWidth, backingHeight);
    6.42 +
    6.43 +    // This call is redundant, but needed if dealing with multiple renderbuffers.
    6.44 +    //glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    6.45 +    [context presentRenderbuffer:GL_RENDERBUFFER];
    6.46 +}
    6.47 +
    6.48 +- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
    6.49 +{
    6.50 +    // Allocate color buffer backing based on the current layer size
    6.51 +    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    6.52 +    [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
    6.53 +    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
    6.54 +    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
    6.55 +
    6.56 +    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    6.57 +    {
    6.58 +        NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
    6.59 +        return NO;
    6.60 +    }
    6.61 +
    6.62 +    return YES;
    6.63 +}
    6.64 +
    6.65 +- (void)dealloc
    6.66 +{
    6.67 +    // Tear down GL
    6.68 +    if (defaultFramebuffer)
    6.69 +    {
    6.70 +        glDeleteFramebuffers(1, &defaultFramebuffer);
    6.71 +        defaultFramebuffer = 0;
    6.72 +    }
    6.73 +
    6.74 +    if (colorRenderbuffer)
    6.75 +    {
    6.76 +        glDeleteRenderbuffers(1, &colorRenderbuffer);
    6.77 +        colorRenderbuffer = 0;
    6.78 +    }
    6.79 +
    6.80 +    if (program)
    6.81 +    {
    6.82 +        glDeleteProgram(program);
    6.83 +        program = 0;
    6.84 +    }
    6.85 +
    6.86 +    // Tear down context
    6.87 +    if ([EAGLContext currentContext] == context)
    6.88 +        [EAGLContext setCurrentContext:nil];
    6.89 +
    6.90 +    [context release];
    6.91 +    context = nil;
    6.92 +
    6.93 +    [super dealloc];
    6.94 +}
    6.95 +
    6.96 +@end
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/ESRenderer.h	Tue Sep 06 08:07:14 2011 +0300
     7.3 @@ -0,0 +1,19 @@
     7.4 +//
     7.5 +//  ESRenderer.h
     7.6 +//  istereo
     7.7 +//
     7.8 +//  Created by nuclear on 9/6/11.
     7.9 +//  Copyright __MyCompanyName__ 2011. All rights reserved.
    7.10 +//
    7.11 +
    7.12 +#import <QuartzCore/QuartzCore.h>
    7.13 +
    7.14 +#import <OpenGLES/EAGL.h>
    7.15 +#import <OpenGLES/EAGLDrawable.h>
    7.16 +
    7.17 +@protocol ESRenderer <NSObject>
    7.18 +
    7.19 +- (void)render;
    7.20 +- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer;
    7.21 +
    7.22 +@end
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/istereo.c	Tue Sep 06 08:07:14 2011 +0300
     8.3 @@ -0,0 +1,17 @@
     8.4 +#include <OpenGLES/ES2/gl.h>
     8.5 +#include "istereo.h"
     8.6 +
     8.7 +int init(void)
     8.8 +{
     8.9 +	return 0;
    8.10 +}
    8.11 +
    8.12 +void cleanup(void)
    8.13 +{
    8.14 +}
    8.15 +
    8.16 +void redraw(void)
    8.17 +{
    8.18 +	glClearColor(0, 1, 0, 1);
    8.19 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    8.20 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/istereo.h	Tue Sep 06 08:07:14 2011 +0300
     9.3 @@ -0,0 +1,8 @@
     9.4 +#ifndef ISTEREO_H_
     9.5 +#define ISTEREO_H_
     9.6 +
     9.7 +int init(void);
     9.8 +void cleanup(void);
     9.9 +void redraw(void);
    9.10 +
    9.11 +#endif	/* ISTEREO_H_ */
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/istereoAppDelegate.h	Tue Sep 06 08:07:14 2011 +0300
    10.3 @@ -0,0 +1,22 @@
    10.4 +//
    10.5 +//  istereoAppDelegate.h
    10.6 +//  istereo
    10.7 +//
    10.8 +//  Created by nuclear on 9/6/11.
    10.9 +//  Copyright __MyCompanyName__ 2011. All rights reserved.
   10.10 +//
   10.11 +
   10.12 +#import <UIKit/UIKit.h>
   10.13 +
   10.14 +@class EAGLView;
   10.15 +
   10.16 +@interface istereoAppDelegate : NSObject <UIApplicationDelegate> {
   10.17 +    UIWindow *window;
   10.18 +    EAGLView *glView;
   10.19 +}
   10.20 +
   10.21 +@property (nonatomic, retain) IBOutlet UIWindow *window;
   10.22 +@property (nonatomic, retain) IBOutlet EAGLView *glView;
   10.23 +
   10.24 +@end
   10.25 +
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/istereoAppDelegate.m	Tue Sep 06 08:07:14 2011 +0300
    11.3 @@ -0,0 +1,46 @@
    11.4 +//
    11.5 +//  istereoAppDelegate.m
    11.6 +//  istereo
    11.7 +//
    11.8 +//  Created by nuclear on 9/6/11.
    11.9 +//  Copyright __MyCompanyName__ 2011. All rights reserved.
   11.10 +//
   11.11 +
   11.12 +#import "istereoAppDelegate.h"
   11.13 +#import "EAGLView.h"
   11.14 +
   11.15 +@implementation istereoAppDelegate
   11.16 +
   11.17 +@synthesize window;
   11.18 +@synthesize glView;
   11.19 +
   11.20 +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   11.21 +{
   11.22 +    [glView startAnimation];
   11.23 +    return YES;
   11.24 +}
   11.25 +
   11.26 +- (void)applicationWillResignActive:(UIApplication *)application
   11.27 +{
   11.28 +    [glView stopAnimation];
   11.29 +}
   11.30 +
   11.31 +- (void)applicationDidBecomeActive:(UIApplication *)application
   11.32 +{
   11.33 +    [glView startAnimation];
   11.34 +}
   11.35 +
   11.36 +- (void)applicationWillTerminate:(UIApplication *)application
   11.37 +{
   11.38 +    [glView stopAnimation];
   11.39 +}
   11.40 +
   11.41 +- (void)dealloc
   11.42 +{
   11.43 +    [window release];
   11.44 +    [glView release];
   11.45 +
   11.46 +    [super dealloc];
   11.47 +}
   11.48 +
   11.49 +@end
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/main.m	Tue Sep 06 08:07:14 2011 +0300
    12.3 @@ -0,0 +1,17 @@
    12.4 +//
    12.5 +//  main.m
    12.6 +//  istereo
    12.7 +//
    12.8 +//  Created by nuclear on 9/6/11.
    12.9 +//  Copyright __MyCompanyName__ 2011. All rights reserved.
   12.10 +//
   12.11 +
   12.12 +#import <UIKit/UIKit.h>
   12.13 +
   12.14 +int main(int argc, char *argv[]) {
   12.15 +    
   12.16 +    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   12.17 +    int retVal = UIApplicationMain(argc, argv, nil, nil);
   12.18 +    [pool release];
   12.19 +    return retVal;
   12.20 +}
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/src/sdr.c	Tue Sep 06 08:07:14 2011 +0300
    13.3 @@ -0,0 +1,323 @@
    13.4 +#include <stdio.h>
    13.5 +#include <stdlib.h>
    13.6 +#include <string.h>
    13.7 +#include <errno.h>
    13.8 +#include <assert.h>
    13.9 +#include <OpenGLES/ES2/gl.h>
   13.10 +
   13.11 +#if defined(unix) || defined(__unix__)
   13.12 +#include <unistd.h>
   13.13 +#include <sys/stat.h>
   13.14 +#endif	/* unix */
   13.15 +
   13.16 +#include "sdr.h"
   13.17 +
   13.18 +
   13.19 +unsigned int create_vertex_shader(const char *src)
   13.20 +{
   13.21 +	return create_shader(src, GL_VERTEX_SHADER);
   13.22 +}
   13.23 +
   13.24 +unsigned int create_pixel_shader(const char *src)
   13.25 +{
   13.26 +	return create_shader(src, GL_FRAGMENT_SHADER);
   13.27 +}
   13.28 +
   13.29 +unsigned int create_shader(const char *src, unsigned int sdr_type)
   13.30 +{
   13.31 +	unsigned int sdr;
   13.32 +	int success, info_len;
   13.33 +	char *info_str = 0;
   13.34 +	GLenum err;
   13.35 +
   13.36 +	sdr = glCreateShader(sdr_type);
   13.37 +	assert(glGetError() == GL_NO_ERROR);
   13.38 +	glShaderSource(sdr, 1, &src, 0);
   13.39 +	err = glGetError();
   13.40 +	assert(err == GL_NO_ERROR);
   13.41 +	glCompileShader(sdr);
   13.42 +	assert(glGetError() == GL_NO_ERROR);
   13.43 +
   13.44 +	glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
   13.45 +	assert(glGetError() == GL_NO_ERROR);
   13.46 +	glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
   13.47 +	assert(glGetError() == GL_NO_ERROR);
   13.48 +
   13.49 +	if(info_len) {
   13.50 +		if((info_str = malloc(info_len + 1))) {
   13.51 +			glGetShaderInfoLog(sdr, info_len, 0, info_str);
   13.52 +			assert(glGetError() == GL_NO_ERROR);
   13.53 +		}
   13.54 +	}
   13.55 +
   13.56 +	if(success) {
   13.57 +		fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
   13.58 +	} else {
   13.59 +		fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
   13.60 +		glDeleteShader(sdr);
   13.61 +		sdr = 0;
   13.62 +	}
   13.63 +
   13.64 +	free(info_str);
   13.65 +	return sdr;
   13.66 +}
   13.67 +
   13.68 +void free_shader(unsigned int sdr)
   13.69 +{
   13.70 +	glDeleteShader(sdr);
   13.71 +}
   13.72 +
   13.73 +unsigned int load_vertex_shader(const char *fname)
   13.74 +{
   13.75 +	return load_shader(fname, GL_VERTEX_SHADER);
   13.76 +}
   13.77 +
   13.78 +unsigned int load_pixel_shader(const char *fname)
   13.79 +{
   13.80 +	return load_shader(fname, GL_FRAGMENT_SHADER);
   13.81 +}
   13.82 +
   13.83 +unsigned int load_shader(const char *fname, unsigned int sdr_type)
   13.84 +{
   13.85 +#if defined(unix) || defined(__unix__)
   13.86 +	struct stat st;
   13.87 +#endif
   13.88 +	unsigned int sdr;
   13.89 +	size_t filesize;
   13.90 +	FILE *fp;
   13.91 +	char *src;
   13.92 +
   13.93 +	if(!(fp = fopen(fname, "r"))) {
   13.94 +		fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
   13.95 +		return 0;
   13.96 +	}
   13.97 +
   13.98 +#if defined(unix) || defined(__unix__)
   13.99 +	fstat(fileno(fp), &st);
  13.100 +	filesize = st.st_size;
  13.101 +#else
  13.102 +	fseek(fp, 0, SEEK_END);
  13.103 +	filesize = ftell(fp);
  13.104 +	fseek(fp, 0, SEEK_SET);
  13.105 +#endif	/* unix */
  13.106 +
  13.107 +	if(!(src = malloc(filesize + 1))) {
  13.108 +		fclose(fp);
  13.109 +		return 0;
  13.110 +	}
  13.111 +	fread(src, 1, filesize, fp);
  13.112 +	src[filesize] = 0;
  13.113 +	fclose(fp);
  13.114 +
  13.115 +	fprintf(stderr, "compiling %s shader: %s... ", (sdr_type == GL_VERTEX_SHADER ? "vertex" : "pixel"), fname);
  13.116 +	sdr = create_shader(src, sdr_type);
  13.117 +
  13.118 +	free(src);
  13.119 +	return sdr;
  13.120 +}
  13.121 +
  13.122 +
  13.123 +unsigned int get_vertex_shader(const char *fname)
  13.124 +{
  13.125 +	return get_shader(fname, GL_VERTEX_SHADER);
  13.126 +}
  13.127 +
  13.128 +unsigned int get_pixel_shader(const char *fname)
  13.129 +{
  13.130 +	return get_shader(fname, GL_FRAGMENT_SHADER);
  13.131 +}
  13.132 +
  13.133 +unsigned int get_shader(const char *fname, unsigned int sdr_type)
  13.134 +{
  13.135 +	unsigned int sdr;
  13.136 +
  13.137 +	if(!(sdr = load_shader(fname, sdr_type))) {
  13.138 +		return 0;
  13.139 +	}
  13.140 +	return sdr;
  13.141 +}
  13.142 +
  13.143 +
  13.144 +/* ---- gpu programs ---- */
  13.145 +
  13.146 +unsigned int create_program(void)
  13.147 +{
  13.148 +	unsigned int prog = glCreateProgram();
  13.149 +	assert(glGetError() == GL_NO_ERROR);
  13.150 +	return prog;
  13.151 +}
  13.152 +
  13.153 +unsigned int create_program_link(unsigned int vs, unsigned int ps)
  13.154 +{
  13.155 +	unsigned int prog;
  13.156 +
  13.157 +	if(!(prog = create_program())) {
  13.158 +		return 0;
  13.159 +	}
  13.160 +
  13.161 +	attach_shader(prog, vs);
  13.162 +	assert(glGetError() == GL_NO_ERROR);
  13.163 +	attach_shader(prog, ps);
  13.164 +	assert(glGetError() == GL_NO_ERROR);
  13.165 +
  13.166 +	if(link_program(prog) == -1) {
  13.167 +		free_program(prog);
  13.168 +		return 0;
  13.169 +	}
  13.170 +	return prog;
  13.171 +}
  13.172 +
  13.173 +unsigned int create_program_load(const char *vfile, const char *pfile)
  13.174 +{
  13.175 +	unsigned int vs, ps;
  13.176 +
  13.177 +	if(!(vs = get_vertex_shader(vfile)) || !(ps = get_pixel_shader(pfile))) {
  13.178 +		return 0;
  13.179 +	}
  13.180 +	return create_program_link(vs, ps);
  13.181 +}
  13.182 +
  13.183 +void free_program(unsigned int sdr)
  13.184 +{
  13.185 +	glDeleteProgram(sdr);
  13.186 +}
  13.187 +
  13.188 +void attach_shader(unsigned int prog, unsigned int sdr)
  13.189 +{
  13.190 +	glAttachShader(prog, sdr);
  13.191 +	assert(glGetError() == GL_NO_ERROR);
  13.192 +}
  13.193 +
  13.194 +int link_program(unsigned int prog)
  13.195 +{
  13.196 +	int linked, info_len, retval = 0;
  13.197 +	char *info_str = 0;
  13.198 +
  13.199 +	glLinkProgram(prog);
  13.200 +	assert(glGetError() == GL_NO_ERROR);
  13.201 +	glGetProgramiv(prog, GL_LINK_STATUS, &linked);
  13.202 +	assert(glGetError() == GL_NO_ERROR);
  13.203 +	glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
  13.204 +	assert(glGetError() == GL_NO_ERROR);
  13.205 +
  13.206 +	if(info_len) {
  13.207 +		if((info_str = malloc(info_len + 1))) {
  13.208 +			glGetProgramInfoLog(prog, info_len, 0, info_str);
  13.209 +			assert(glGetError() == GL_NO_ERROR);
  13.210 +		}
  13.211 +	}
  13.212 +
  13.213 +	if(linked) {
  13.214 +		fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
  13.215 +	} else {
  13.216 +		fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
  13.217 +		retval = -1;
  13.218 +	}
  13.219 +
  13.220 +	free(info_str);
  13.221 +	return retval;
  13.222 +}
  13.223 +
  13.224 +int bind_program(unsigned int prog)
  13.225 +{
  13.226 +	GLenum err;
  13.227 +
  13.228 +	glUseProgram(prog);
  13.229 +	if(prog && (err = glGetError()) != GL_NO_ERROR) {
  13.230 +		/* maybe the program is not linked, try linking first */
  13.231 +		if(err == GL_INVALID_OPERATION) {
  13.232 +			if(link_program(prog) == -1) {
  13.233 +				return -1;
  13.234 +			}
  13.235 +			glUseProgram(prog);
  13.236 +			return glGetError() == GL_NO_ERROR ? 0 : -1;
  13.237 +		}
  13.238 +		return -1;
  13.239 +	}
  13.240 +	return 0;
  13.241 +}
  13.242 +
  13.243 +/* ugly but I'm not going to write the same bloody code over and over */
  13.244 +#define BEGIN_UNIFORM_CODE \
  13.245 +	int loc, curr_prog; \
  13.246 +	glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
  13.247 +	if(curr_prog != prog && bind_program(prog) == -1) { \
  13.248 +		return -1; \
  13.249 +	} \
  13.250 +	if((loc = glGetUniformLocation(prog, name)) != -1)
  13.251 +
  13.252 +#define END_UNIFORM_CODE \
  13.253 +	if(curr_prog != prog) { \
  13.254 +		bind_program(curr_prog); \
  13.255 +	} \
  13.256 +	return loc == -1 ? -1 : 0
  13.257 +
  13.258 +int set_uniform_int(unsigned int prog, const char *name, int val)
  13.259 +{
  13.260 +	BEGIN_UNIFORM_CODE {
  13.261 +		glUniform1i(loc, val);
  13.262 +	}
  13.263 +	END_UNIFORM_CODE;
  13.264 +}
  13.265 +
  13.266 +int set_uniform_float(unsigned int prog, const char *name, float val)
  13.267 +{
  13.268 +	BEGIN_UNIFORM_CODE {
  13.269 +		glUniform1f(loc, val);
  13.270 +	}
  13.271 +	END_UNIFORM_CODE;
  13.272 +}
  13.273 +
  13.274 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
  13.275 +{
  13.276 +	BEGIN_UNIFORM_CODE {
  13.277 +		glUniform3f(loc, x, y, z);
  13.278 +	}
  13.279 +	END_UNIFORM_CODE;
  13.280 +}
  13.281 +
  13.282 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
  13.283 +{
  13.284 +	BEGIN_UNIFORM_CODE {
  13.285 +		glUniform4f(loc, x, y, z, w);
  13.286 +	}
  13.287 +	END_UNIFORM_CODE;
  13.288 +}
  13.289 +
  13.290 +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
  13.291 +{
  13.292 +	BEGIN_UNIFORM_CODE {
  13.293 +		glUniformMatrix4fv(loc, 16, GL_FALSE, mat);
  13.294 +	}
  13.295 +	END_UNIFORM_CODE;
  13.296 +}
  13.297 +
  13.298 +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
  13.299 +{
  13.300 +	BEGIN_UNIFORM_CODE {
  13.301 +		glUniformMatrix4fv(loc, 16, GL_TRUE, mat);
  13.302 +	}
  13.303 +	END_UNIFORM_CODE;
  13.304 +}
  13.305 +
  13.306 +int get_attrib_loc(unsigned int prog, const char *name)
  13.307 +{
  13.308 +	int loc, curr_prog;
  13.309 +
  13.310 +	glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
  13.311 +	if(curr_prog != prog && bind_program(prog) == -1) {
  13.312 +		return -1;
  13.313 +	}
  13.314 +
  13.315 +	loc = glGetAttribLocation(prog, (char*)name);
  13.316 +
  13.317 +	if(curr_prog != prog) {
  13.318 +		bind_program(curr_prog);
  13.319 +	}
  13.320 +	return loc;
  13.321 +}
  13.322 +
  13.323 +void set_attrib_float3(int attr_loc, float x, float y, float z)
  13.324 +{
  13.325 +	glVertexAttrib3f(attr_loc, x, y, z);
  13.326 +}
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/src/sdr.h	Tue Sep 06 08:07:14 2011 +0300
    14.3 @@ -0,0 +1,49 @@
    14.4 +#ifndef SDR_H_
    14.5 +#define SDR_H_
    14.6 +
    14.7 +#ifdef __cplusplus
    14.8 +extern "C" {
    14.9 +#endif	/* __cplusplus */
   14.10 +
   14.11 +/* ---- shaders ---- */
   14.12 +unsigned int create_vertex_shader(const char *src);
   14.13 +unsigned int create_pixel_shader(const char *src);
   14.14 +unsigned int create_shader(const char *src, unsigned int sdr_type);
   14.15 +void free_shader(unsigned int sdr);
   14.16 +
   14.17 +unsigned int load_vertex_shader(const char *fname);
   14.18 +unsigned int load_pixel_shader(const char *fname);
   14.19 +unsigned int load_shader(const char *src, unsigned int sdr_type);
   14.20 +
   14.21 +unsigned int get_vertex_shader(const char *fname);
   14.22 +unsigned int get_pixel_shader(const char *fname);
   14.23 +unsigned int get_shader(const char *fname, unsigned int sdr_type);
   14.24 +
   14.25 +int add_shader(const char *fname, unsigned int sdr);
   14.26 +int remove_shader(const char *fname);
   14.27 +
   14.28 +/* ---- gpu programs ---- */
   14.29 +unsigned int create_program(void);
   14.30 +unsigned int create_program_link(unsigned int vs, unsigned int ps);
   14.31 +unsigned int create_program_load(const char *vfile, const char *pfile);
   14.32 +void free_program(unsigned int sdr);
   14.33 +
   14.34 +void attach_shader(unsigned int prog, unsigned int sdr);
   14.35 +int link_program(unsigned int prog);
   14.36 +int bind_program(unsigned int prog);
   14.37 +
   14.38 +int set_uniform_int(unsigned int prog, const char *name, int val);
   14.39 +int set_uniform_float(unsigned int prog, const char *name, float val);
   14.40 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z);
   14.41 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w);
   14.42 +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat);
   14.43 +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat);
   14.44 +
   14.45 +int get_attrib_loc(unsigned int prog, const char *name);
   14.46 +void set_attrib_float3(int attr_loc, float x, float y, float z);
   14.47 +
   14.48 +#ifdef __cplusplus
   14.49 +}
   14.50 +#endif	/* __cplusplus */
   14.51 +
   14.52 +#endif	/* SDR_H_ */