4.投影矩阵

2016-03-31  本文已影响650人  花神Flora

一般情况下,显示3D场景的窗口并不是正方形的,需要通过投影矩阵进行变换。

#import "GameViewController.h"
#import <OpenGLES/ES2/glext.h>
//顶点类型:顶点坐标、UV贴图坐标、顶点法向量
typedef struct {
    GLKVector3 positionCoords;
    GLKVector2 textureCoords;
    GLKVector3 normalVector;
} VertexType;

//三角形类型,逆时针
typedef struct {
    VertexType vertices[3];
} TriangleType;

TriangleType triangleTypeMake(VertexType v1, VertexType v2, VertexType v3) {
    TriangleType triangle = {v1, v2, v3};
    return triangle;
}

//计算单位法向量
GLKVector3 unitNormal(GLKVector3 v1, GLKVector3 v2) {
    return GLKVector3Normalize(GLKVector3CrossProduct(v1, v2));
}

VertexType vA = {{-0.5f, 0.5f, -0.5f}, {0.0f, 1.0f}, {0.0f, 0.0f, 1.0f}};
VertexType vB = {{-0.5f, 0.0f, -0.5f}, {0.0f, 0.5f}, {0.0f, 0.0f, 1.0f}};
VertexType vC = {{-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
VertexType vD = {{0.0f, -0.5f, -0.5f}, {0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}};
VertexType vE = {{0.5f, -0.5f, -0.5f}, {0.1f, 0.0f}, {0.0f, 0.0f, 1.0f}};
VertexType vF = {{0.5f, 0.0f, -0.5f}, {1.0f, 0.5f}, {0.0f, 0.0f, 1.0f}};
VertexType vG = {{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}};
VertexType vH = {{0.0f, 0.5f, -0.5f}, {0.5f, 1.0f}, {0.0f, 0.0f, 1.0f}};
VertexType vI = {{0.0f, 0.0f, 0.0f}, {0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}};

@interface GameViewController () {
    GLuint vertexBufferID;
    
    TriangleType triangles[8];
}
//用于设置通用的OpenGL ES环境
@property (strong, nonatomic) GLKBaseEffect *baseEffect;
//OpenGL ES上下文
@property (strong, nonatomic) EAGLContext *context;
@end

@implementation GameViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.preferredFramesPerSecond = 60;
    
//    GLKVector3 v1 = {0.75, 0.25, -0.25};
//    GLKVector3 v2 = {-0.25, 0.75, 0.5};
//    GLKVector3 v3 = unitNormal(v1, v2);
//    NSLog(@"%@", NSStringFromGLKVector3(v3));
    
    triangles[0] = triangleTypeMake(vA, vB, vH);
    triangles[1] = triangleTypeMake(vH, vB, vI);
    triangles[2] = triangleTypeMake(vG, vH, vF);
    triangles[3] = triangleTypeMake(vH, vI, vF);
    triangles[4] = triangleTypeMake(vB, vD, vI);
    triangles[5] = triangleTypeMake(vB, vC, vD);
    triangles[6] = triangleTypeMake(vD, vF, vI);
    triangles[7] = triangleTypeMake(vD, vE, vF);
    
    //使用支持OpenGL ES2的上下文
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    if (!self.context) {
        NSLog(@"Failed to create ES context");
    }
    
    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

    [EAGLContext setCurrentContext:self.context];
    
    self.baseEffect = [[GLKBaseEffect alloc] init];
    //启用元素的默认颜色
    self.baseEffect.useConstantColor = GL_TRUE;
    //设置默认颜色
    self.baseEffect.constantColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
    
    //生成纹理,由于UIKit的坐标系Y轴与OpenGL ES的Y轴刚好上下颠倒,因此如果图片不加任何处理的话,在贴图后将是颠倒显示
    CGImageRef imageRef = [UIImage imageNamed:@"1.jpg"].CGImage;
    GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithCGImage:imageRef options:nil error:nil];
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"grid" ofType:@"png"];
//    GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:nil error:nil];
    //贴图纹理
    self.baseEffect.texture2d0.name = textureInfo.name;
    self.baseEffect.texture2d0.target = textureInfo.target;
    //光照:设置漫反射灯光
    self.baseEffect.light0.enabled = YES;
//    self.baseEffect.light0.diffuseColor = GLKVector4Make(1.0f, 1.0f, 0.0f, 1.0f);
    self.baseEffect.light0.ambientColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
    //第四个参数非0表示从该位置发散光线,0表示无穷远处光线的发射方向
    self.baseEffect.light0.position = GLKVector4Make(1.0f, 1.0f, 0.5f, 0.0f);
    
    //变换矩阵
    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(-60.0f), 1.0f, 0.0f, 0.0f);
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(-30.0f), 0.0f, 0.0f, 1.0f);
    modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, 0.0f, 0.0f, 0.25f);
    self.baseEffect.transform.modelviewMatrix = modelViewMatrix;
    
    //设置背景色
    glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
    
    //1. 生成缓存ID
    glGenBuffers(1, &vertexBufferID);
    //2. 
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
    //3. 
    glBufferData(GL_ARRAY_BUFFER, sizeof(TriangleType) * 8, triangles, GL_STATIC_DRAW);
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

#pragma mark - GLKView and GLKViewController delegate methods

//- (void)update {
//    NSLog(@"----");
//}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    [self.baseEffect prepareToDraw];
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    //2. 
//    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
    //4. 启用顶点坐标(x, y, z)
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    //5. 类型,成员个数,类型,规范化,间隔,偏移
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexType), NULL + offsetof(VertexType, positionCoords));
    
    //2. 
//    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
    //4.1 启用纹理坐标(u, v)
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    //5.1 设置纹理坐标
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(VertexType), NULL + offsetof(VertexType, textureCoords));
    
    //2. 
//    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
    //4.2 启用法向量
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    //5.2 设置法向量
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexType), NULL + offsetof(VertexType, normalVector));

    //6. 绘制
    glDrawArrays(GL_TRIANGLES, 0, 3 * 8);
    
    //投影矩阵
    const CGFloat aspectRatio = view.frame.size.width / view.frame.size.height;
    self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeScale(1.0f, aspectRatio, 1.0f);
}

- (void)dealloc
{
    if ([EAGLContext currentContext] == self.context) {
        [EAGLContext setCurrentContext:nil];
    }
    
    glDeleteBuffers(1, &vertexBufferID);
    vertexBufferID = 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
    if ([self isViewLoaded] && ([[self view] window] == nil)) {
        self.view = nil;
        
        if ([EAGLContext currentContext] == self.context) {
            [EAGLContext setCurrentContext:nil];
        }
        self.context = nil;
        
        glDeleteBuffers(1, &vertexBufferID);
        vertexBufferID = 0;
    }
}
@end
上一篇下一篇

猜你喜欢

热点阅读