Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save suzumura-ss/5907462 to your computer and use it in GitHub Desktop.
Save suzumura-ss/5907462 to your computer and use it in GitHub Desktop.
`LittlePlanet` projection with GPUImage.framework
//
// GPUImageLittlePlanetProjectionFilter.h
//
// Created by Toshiyuki Suzumura on 2013/07/02.
// Copyright (c) 2013 Toshiyuki Suzumura. All rights reserved.
//
#import "GPUImageFilter.h"
@interface GPUImageLittlePlanetProjectionFilter : GPUImageFilter
@property (nonatomic) CGSize renderSize;
@property (nonatomic, setter = setSkyRatio:) float skyRatio;
@end
//
// GPUImageLittlePlanetProjectionFilter.m
//
// Created by Toshiyuki Suzumura on 2013/07/02.
// Copyright (c) 2013 Toshiyuki Suzumura. All rights reserved.
//
#import "GPUImageLittlePlanetProjectionFilter.h"
NSString* const kGPUImageLittlePlanetProjectionVertexShaderString = SHADER_STRING
(
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
uniform float skyRatio;
void main()
{
gl_Position = position;
textureCoordinate = (inputTextureCoordinate.xy - vec2(0.5, 0.5))*skyRatio;
}
);
NSString* const kGPUImageLittlePlanetProjectionFragmentShaderString = SHADER_STRING
(
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
const highp float M_2PI = M_PI*2.0;
void main()
{
highp float x = textureCoordinate.x;
highp float y = textureCoordinate.y;
highp float R = sqrt(x*x + y*y);
highp float S = atan(x, y);
highp float r = 2.0*atan(1.0, R); // R=0 => r=M_PI
S = S/M_2PI;
S = S - floor(S);
gl_FragColor = texture2D(inputImageTexture, vec2(S, r/M_PI));
}
);
@implementation GPUImageLittlePlanetProjectionFilter
- (id)init
{
self = [super initWithVertexShaderFromString:kGPUImageLittlePlanetProjectionVertexShaderString
fragmentShaderFromString:kGPUImageLittlePlanetProjectionFragmentShaderString];
if (self) {
_renderSize = CGSizeMake(512, 512);
[self setSkyRatio:10];
}
return self;
}
- (CGSize)sizeOfFBO
{
return _renderSize;
}
- (void)setSkyRatio:(float)skyRatio
{
_skyRatio = skyRatio;
[self setFloat:_skyRatio forUniformName:@"skyRatio"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment