opengl笔记—— glMultMatrixf() 区别 glLoadMatrixf()

本文探讨了OpenGL中的glPush/PopMatrix与glLoadMatrixf的区别。glPushMatrix会将当前矩阵复制到堆栈上,而不会与堆栈中的其他矩阵交互。glLoadMatrixf则会替换当前矩阵。通过实例代码阐述了这些函数的使用场景。

能找到最好的解释来自:http://www.gamedev.net/topic/489879-glpushmatrixglpopmatrix--glloadmatrixf/

原理:

glPushMatrix didn't fail to push onto the stack; it's job is to push a copy of the current matrix onto a stack of matrices. Those matrices on the stack don't interact at all. You only manipulate the current, top-most, matrix at any given time.

Example 1:

   command                 result

glLoadMatrixf(A)       stack = [A]
glPushMatrix()         stack = [A, A]
glLoadMatrixf(B)       stack = [B, A]
glPopMatrix()          stack = [A]

Example 2:
   command                 result

glLoadMatrixf(A)       stack = [A]
glPushMatrix()         stack = [A, A]
glMultMatrixf(B)       stack = [AB, A]
glPopMatrix()          stack = [A]

注意: glTranslate* 等都是实际调用的是glMultMatrixf

实例:

看下面的代码:

743     glPushMatrix();
744 
745     // tramsform camera
746     matrixView.identity();
747     matrixView.rotate(cameraAngleY, 0, 1, 0);
748     matrixView.rotate(cameraAngleX, 1, 0, 0);
749     matrixView.translate(0, 0, -cameraDistance);
750     //@@ the equivalent code for using OpenGL routine is:
751     //@@ glTranslatef(0, 0, -cameraDistance);
752     //@@ glRotatef(cameraAngleX, 1, 0, 0);   // pitch
753     //@@ glRotatef(cameraAngleY, 0, 1, 0);   // heading
754 
755     // copy view matrix to OpenGL
756     glLoadMatrixf(matrixView.getTranspose());
757 
758     drawGrid();                         // draw XZ-grid with default size
759 
760     // compute model matrix
761     matrixModel.identity();
762     //matrixModel.rotateZ(45);        // rotate 45 degree on Z-axis
763     matrixModel.rotateY(10);        // rotate 45 degree on Y-axis
764     matrixModel.translate(0, 1, 0); // move 2 unit up
765 
766     // compute modelview matrix
767     matrixModelView = matrixView * matrixModel;
768 
769     // copy modelview matrix to OpenGL
770     glLoadMatrixf(matrixModelView.getTranspose());
771 
772     drawAxis();
                              

770     glLoadMatrixf(matrixModelView.getTranspose());

不能是glMultMatrixf

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值