opengl 投影 内部实现代码

// This creates a symmetric frustum.// It converts to 6 params (l, r, b, t, n, f) for glFrustum()// from given 4 params (fovy, aspect, near, far)

void makeFrustum(double fovY, double aspectRatio, double front, double back)

{  

const double DEG2RAD = 3.14159265 / 180; 

double tangent = tan(fovY/2 * DEG2RAD);  // tangent of half fovY  

double height = front * tangent;// half height of near plane  

double width = height * aspectRatio;// half width of near plane// params: left, right, bottom, top, near, far  

glFrustum(-width, width, -height, height, front, back);

}



再贴

///// set a perspective frustum with 6 params similar to glFrustum()// (left, right, bottom, top, near, far)// Note: this is for row-major notation. OpenGL needs transpose it///

void ModelGL::setFrustum(float l, float r, float b, float t, float n, float f)

{  

matrixProjection.identity();  

matrixProjection[0] = 2 * n / (r - l);  

matrixProjection[2] = (r + l) / (r - l); 

matrixProjection[5] = 2 * n / (t - b);  

matrixProjection[6] = (t + b) / (t - b);  

matrixProjection[10] = -(f + n) / (f - n);  

matrixProjection[11] = -(2 * f * n) / (f - n); 

matrixProjection[14] = -1;  

matrixProjection[15] = 0;

}

///// set a orthographic frustum with 6 params similar to glOrtho()// (left, right, bottom, top, near, far)// Note: this is for row-major notation. OpenGL needs transpose it///

void ModelGL::setOrthoFrustum(float l, float r, float b, float t, float n,float f)

{  

matrixProjection.identity(); 

matrixProjection[0] = 2 / (r - l);  

matrixProjection[3] = -(r + l) / (r - l);  

matrixProjection[5] = 2 / (t - b);  

matrixProjection[7] = -(t + b) / (t - b);  

matrixProjection[10] = -2 / (f - n);  

matrixProjection[11] = -(f + n) / (f - n);}...// pass projection matrx to OpenGL before draw

glMatrixMode(GL_PROJECTION);

glLoadMatrixf(matrixProjection.getTranspose());...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值