2011년 9월 26일 월요일

understanding glFrustrum using easy code

This code used to understand how the glFrustrum() would work using a regular hexahedron. Each face of regular hexahedron has different color, white, purple, blue, red, green, yellow to distinguish easily. 

Change the z parameter value of glTranslate function or Change the near distance to understand exactly how the glFrustrum function work. 

This code origin from GLGravity source that maden by Apple xcode help. 


#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>

#import "GLGravityView.h"
#import "teapot.h"

// MACROS
#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)

// A class extension to declare private methods
@interface GLGravityView (private)

- (BOOL)createFramebuffer;
- (void)destroyFramebuffer;
- (void)setupView;

@end

@implementation GLGravityView

@synthesize animating;
@dynamic animationFrameInterval;
@synthesize accel;

// Implement this to override the default layer class (which is [CALayer class]).
// We do this so that our view will be backed by a layer that is capable of OpenGL ES rendering.
+ (Class) layerClass
{
return [CAEAGLLayer class];
}

// The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder*)coder {
    
    if ((self = [super initWithCoder:coder])) {
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
return nil;
}
        
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
        [self addGestureRecognizer:panGesture];
        [panGesture release];
animating = FALSE;
displayLinkSupported = FALSE;
animationFrameInterval = 1;
displayLink = nil;
animationTimer = nil;
// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
displayLinkSupported = TRUE;
accel = calloc(3, sizeof(UIAccelerationValue));
        
        translation = CGPointMake(0.f, 0.f);
[self setupView];
}
return self;
}
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    //NSLog(@"panPiece");
    UIView *piece = [gestureRecognizer view];
    if ([gestureRecognizer state] == UIGestureRecognizerStateChanged) 
    {
        translation = [gestureRecognizer translationInView:[piece superview]];
        //NSLog(@"translation = %.2f, %.2f", translation.x, translation.y);
        
    }
}
-(void)setupView
{
    glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_VERTEX_ARRAY);

    
    const GLfloat zNear = 1,
zFar = 100.0,
fieldOfView = 90.0;
GLfloat size;
//Set the OpenGL projection matrix
glMatrixMode(GL_PROJECTION);
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
CGRect rect = self.bounds;
    
glFrustumf(-size, 
               size, 
               -size / (rect.size.width / rect.size.height), 
               size / (rect.size.width / rect.size.height), 
               zNear, 
               zFar);
    
//    mRatio = rect.size.height / rect.size.width;
//    glFrustumf(-1, 
//               1, 
//               - ( rect.size.height / rect.size.width), 
//               rect.size.height / rect.size.width, 
//               1, 
//               1000);
    
glViewport(0, 0, rect.size.width, rect.size.height);
//Make the OpenGL modelview matrix the default
glMatrixMode(GL_MODELVIEW);
}

// Updates the OpenGL view
- (void)drawView
{
// Make sure that you are drawing to the current context
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
//glScalef(1, 1, 1);

    //glRotatef(30, translation.x, translation.y, 0);
    glRotatef(translation.x + translation.y, -translation.y, -translation.x, 0);
    

    float fVal = mRatio;

     GLfloat squareVertices[] = {
        -fVal, -fVal, fVal,
        fVal, -fVal, fVal, 
        -fVal,  fVal, fVal, 
        fVal, -fVal, fVal,
        fVal,  fVal, fVal,
        -fVal,  fVal, fVal,
        
        -fVal,  fVal, fVal,
        fVal,   fVal, fVal,
        -fVal,  fVal, -fVal, 
        fVal,   fVal,fVal,
        fVal,   fVal, -fVal,
        -fVal,  fVal, -fVal,
                
        -fVal, fVal, -fVal,
        fVal, fVal, -fVal, 
        -fVal,  -fVal, -fVal, 
        fVal, fVal, -fVal,
        fVal,  -fVal, -fVal,
        -fVal,  -fVal, -fVal,
        
        -fVal, -fVal, -fVal,
        fVal,  -fVal, -fVal,
        -fVal, -fVal, fVal,
        fVal,  -fVal, -fVal,
        fVal,  -fVal, fVal,
        -fVal, -fVal, fVal,
        
        fVal,  -fVal, fVal,
        fVal,  -fVal, -fVal,
        fVal,  fVal, fVal,
        fVal,  -fVal, -fVal,
        fVal,  fVal, -fVal,
        fVal,  fVal, fVal,
        
        -fVal,  -fVal, -fVal,
        -fVal,  -fVal, fVal,
        -fVal,  fVal, -fVal,
        -fVal,  -fVal, fVal,
        -fVal,  fVal, fVal,
        -fVal,  fVal, -fVal,
    };
    static const GLubyte squareColor[] = {
        
        
        255, 255, 255, 255, //white
        255, 255, 255, 255,
        255, 255, 255, 255,
        255, 255, 255, 255,
        255, 255, 255, 255,
        255, 255, 255, 255,
        
        
        255, 0,255,255, //purple
        255, 0,255,255,
        255, 0,255,255,
        255, 0,255,255,
        255, 0,255,255,
        255, 0,255,255,
        
        0, 0,255,255, //blue
        0, 0,255,255,
        0, 0,255,255,
        0, 0,255,255,
        0, 0,255,255,
        0, 0,255,255,
        
        255, 0,0,255, //red
        255, 0,0,255,
        255, 0,0,255,
        255, 0,0,255,
        255, 0,0,255,
        255, 0,0,255,
   
        255, 255,0,255, //yellow
        255, 255,0,255,
        255, 255,0,255,
        255, 255,0,255,
        255, 255,0,255,
        255, 255,0,255,
        
        0, 255,0,255, //green
        0, 255,0,255,
        0, 255,0,255,
        0, 255,0,255,
        0, 255,0,255,
        0, 255,0,255,
    };
    glDisable(GL_CULL_FACE);
    glEnableClientState(GL_COLOR_ARRAY);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColor);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glDrawArrays(GL_TRIANGLES, 0, 36); 
// for(int i = 0; i < num_teapot_indices; i += new_teapot_indicies[i] + 1)
// {
// glDrawElements(GL_TRIANGLE_STRIP, new_teapot_indicies[i], GL_UNSIGNED_SHORT, &new_teapot_indicies[i+1]);
// }

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

// If our view is resized, we'll be asked to layout subviews.
// This is the perfect opportunity to also update the framebuffer so that it is
// the same size as our display area.
-(void)layoutSubviews
{
[EAGLContext setCurrentContext:context];
[self destroyFramebuffer];
[self createFramebuffer];
[self drawView];
}

- (BOOL)createFramebuffer
{
// Generate IDs for a framebuffer object and a color renderbuffer
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
// This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
// allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
// For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer.
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
{
NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
return NO;
}
return YES;
}

// Clean up any buffers we have allocated.
- (void)destroyFramebuffer
{
glDeleteFramebuffersOES(1, &viewFramebuffer);
viewFramebuffer = 0;
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
viewRenderbuffer = 0;
if(depthRenderbuffer)
{
glDeleteRenderbuffersOES(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
}

- (NSInteger) animationFrameInterval
{
return animationFrameInterval;
}

- (void) setAnimationFrameInterval:(NSInteger)frameInterval
{
// Frame interval defines how many display frames must pass between each time the
// display link fires. The display link will only fire 30 times a second when the
// frame internal is two on a display that refreshes 60 times a second. The default
// frame interval setting of one will fire 60 times a second when the display refreshes
// at 60 times a second. A frame interval setting of less than one results in undefined
// behavior.
if (frameInterval >= 1)
{
animationFrameInterval = frameInterval;
if (animating)
{
[self stopAnimation];
[self startAnimation];
}
}
}

- (void) startAnimation
{
if (!animating)
{
if (displayLinkSupported)
{
// CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
// if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
// not be called in system versions earlier than 3.1.
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView)];
[displayLink setFrameInterval:animationFrameInterval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
else
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView) userInfo:nil repeats:TRUE];
animating = TRUE;
}
}

- (void)stopAnimation
{
if (animating)
{
if (displayLinkSupported)
{
[displayLink invalidate];
displayLink = nil;
}
else
{
[animationTimer invalidate];
animationTimer = nil;
}
animating = FALSE;
}
}

- (void)dealloc
{
free(accel);
if([EAGLContext currentContext] == context)
{
[EAGLContext setCurrentContext:nil];
}
[context release];
[super dealloc];
}

@end

댓글 없음:

댓글 쓰기