zoukankan      html  css  js  c++  java
  • CG基础学习笔记(Lecture5以及对作业1的理解)

    视口变换

    作业1 基础版

    模型变换:get_model_matrix(float rotation_angle)

    //视图转换
    Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos)
    {
        // 定义 4 * 4 的单位矩阵
        Eigen::Matrix4f view = Eigen::Matrix4f::Identity();
    
        Eigen::Matrix4f translate;
        // 初始化视角变换矩阵
        translate << 1, 0, 0, -eye_pos[0], 
                     0, 1, 0, -eye_pos[1], 
                     0, 0, 1,-eye_pos[2], 
                     0, 0, 0, 1;
    
        view = translate * view;
    
        return view;
    }

    透视投影变换矩阵

    1 .将视锥压缩成正交投影变换矩阵可处理的形式

    2 .正交投影

    
    // 绕 z 轴旋转的变换矩阵
    Eigen::Matrix4f get_model_matrix(float rotation_angle)
    {
        // 初始化一个单位矩阵model
        Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
        // TODO: Implement this function
        // Create the model matrix for rotating the triangle around the Z axis.
        // Then return it.
        // --------------------
        float r = rotation_angle * MY_PI / 180; //转化为弧度
        Eigen::Matrix4f rotation;
    
        // 围绕z轴进行旋转
        rotation << cos(rotation_angle), -sin(rotation_angle), 0, 0,
            sin(rotation_angle), cos(rotation_angle), 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1;
    
        model *= rotation;
    
        // --------------------
        return model;
       
    }
    
    //eye_fov:纵向视野(度), aspect_ratio:宽高比
    Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
        float zNear, float zFar)
    {
    
        Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
    
        // TODO: Implement this function
        // Create the projection matrix for the given parameters.
        // Then return it.
        Eigen::Matrix4f M_perspToOrtho(4, 4);
        Eigen::Matrix4f M_ortho_scale(4, 4);
        Eigen::Matrix4f M_ortho_trans(4, 4);
    
        float half_angle = eye_fov * MY_PI / 360.0; // 纵向视野的一半,eye_fov*MY_PI/2 / 180.0
        float top = zNear * tan(half_angle);
        float bottom = -top;
        float right = top * aspect_ratio;
        float left = -right;
        
        M_ortho_trans << 1, 0, 0, -(right + left) / 2,
            0, 1, 0, -(top + bottom) / 2,
            0, 0, 1, -(zNear + zFar) / 2,
            0, 0, 0, 1;
        M_ortho_scale << 2 / (right - left), 0, 0, 0,
            0, 2 / (top - bottom), 0, 0,
            0, 0, 2 / (zNear - zFar), 0,
            0, 0, 0, 1;
        M_perspToOrtho << zNear, 0, 0, 0,
            0, zNear, 0, 0,
            0, 0, zNear + zFar, -zNear * zFar,
            0, 0, 1, 0;
                        
        Eigen::Matrix4f M_ortho = M_ortho_scale *M_ortho_trans;
    
        projection = M_ortho * M_perspToOrtho;
    
        return projection;
    }

    效果(测试数据改了改)

     

    本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/15798993.html

  • 相关阅读:
    HTTP Handlers and HTTP Modules Overview
    NTLM Detail
    How to get report service instance name by wmi
    How to remove the history credential in IE8 .(OP is Win 7 64)
    Session 机制详解
    ES6map对象,for of 遍历,class类,extends继承
    MongoDB 安装及命令指令符基本操作
    nodeIO,path,http , url模块
    ES6promise
    node模块化简介, process全局对象, fs模块,Buffer对象 ,
  • 原文地址:https://www.cnblogs.com/Knight02/p/15798993.html
Copyright © 2011-2022 走看看