glm::perspective explanation

前端 未结 2 769
梦谈多话
梦谈多话 2020-12-24 11:58

I am trying to understand what the following code does:

glm::mat4 Projection = glm::perspective(35.0f, 1.0f, 0.1f, 100.0f);

Does it create

2条回答
  •  悲哀的现实
    2020-12-24 12:43

    This is a c standalone version of the same function. This is roughly a copy paste version of the original.

    # include 
    # include 
    # include 
    
    typedef struct s_mat {
        float *array;
        int width;
        int height;
    } t_mat;
    
    t_mat *mat_new(int width, int height)
    {
        t_mat *to_return;
    
        to_return = (t_mat*)malloc(sizeof(t_mat));
        to_return->array = malloc(width * height * sizeof(float));
        to_return->width = width;
        to_return->height = height;
        return (to_return);
    }
    
    void mat_zero(t_mat *dest)
    {
        bzero(dest->array, dest->width * dest->height * sizeof(float));
    }
    
    void mat_set(t_mat *m, int x, int y, float val)
    {
        if (m == NULL || x > m->width || y > m->height)
            return ;
        m->array[m->width * (y - 1) + (x - 1)] = val;
    }
    
    t_mat *mat_perspective(float angle, float ratio,
            float near, float far)
    {
        t_mat *to_return;
        float tan_half_angle;
    
        to_return = mat_new(4, 4);
        mat_zero(to_return);
        tan_half_angle = tan(angle / 2);
        mat_set(to_return, 1, 1, 1 / (ratio * tan_half_angle));
        mat_set(to_return, 2, 2, 1 / (tan_half_angle));
        mat_set(to_return, 3, 3, -(far + near) / (far - near));
        mat_set(to_return, 4, 3, -1);
        mat_set(to_return, 3, 4, -(2 * far * near) / (far - near));
        return (to_return);
    }
    

提交回复
热议问题