美文网首页
4.投影矩阵

4.投影矩阵

作者: 花神Flora | 来源:发表于2016-03-31 15:33 被阅读650次

一般情况下,显示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

相关文章

  • 4.投影矩阵

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

  • 变换:向量和矩阵

    主要使用了: 矩阵构造(平移、旋转、综合变换) 模型视图矩阵 三角形批次类(创建花托) 投影矩阵(透视投影) 示例...

  • 学习OpenGL ES之透视和正交投影

    本系列所有文章目录 获取示例代码 上一篇介绍了变换矩阵,本篇将介绍两个重要的变换矩阵,透视投影矩阵和正交投影矩阵。...

  • 1.视觉关键名词

    问题 1.什么叫做模型矩阵2.什么叫做投影矩阵3.什么叫做照相机4.什么叫做视景体5.什么叫做管道6.我们所观察到...

  • OpenGL 综合案例

    先看结果 核心代码 栈的机制 在changeSize()函数中,我们加载了投影矩阵,并把投影矩阵压入管道trans...

  • 学习WebGL之透视和正交投影

    本系列所有文章目录 上一篇介绍了变换矩阵,本篇将介绍两个重要的变换矩阵,透视投影矩阵和正交投影矩阵,可以前往我的博...

  • OpenGL 投影矩阵

    OpenGL Projection MatrixOpenGL投影矩阵 概述 透视投影 正交投影 概述 计算机显示器...

  • OpenGL投影矩阵

    概述 电脑显示器是2D平面,一个3D物体通过OpenGL渲染投影到2D显示器平面形成图像,GL_PROJECTIO...

  • 投影矩阵推导

    1、正交投影 上图就是unity中的摄像机,把摄像机设置为正交投影,我们可以在inspector界面调整clipp...

  • 精通数据科学学习笔记:第三章 数学基础

    3.1 矩阵和向量空间 特殊矩阵:单位矩阵、对角矩阵、三角矩阵 向量内积表示向量A在向量B方向上的投影长度。找到一...

网友评论

      本文标题:4.投影矩阵

      本文链接:https://www.haomeiwen.com/subject/qqielttx.html