乐之者体感开发

好知者不如乐之者,体感开发请点击:http://www.enjoythis.net/

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

OpenGL中的栈的作用的作用是保存当前矩阵

 pushMatrix() 作用

声明一下 图形学中的三维坐标变化:

OpenGL当中的坐标变化过程中保存当前矩阵(包括投影矩阵、变换矩阵,等等)下面代码简单模仿了矩阵的保存与恢复。

代码:(应用了STL中的栈数据结构,自己实现请跟帖)

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include <stack>
 4 #include "Windows.h"
 5 using namespace std;
 6 /************************************************************************/
 7 /* 对于opengl中栈操作的模拟,简单模拟了 其中的世界坐标系的操作。
 8 其中:压栈出栈 为了保存当前的矩阵变换
 9 本例子用  initFunc() 模仿初始化 transformFunc() 表示坐标变换 */
10 /************************************************************************/
11 
12 struct Vector3f{
13 
14     float x,y,z;//表示坐标 x,y,z
15 };
16 
17 Vector3f currentVector;//全局的坐标表示 用来表示世界坐标(全局)
18 stack<Vector3f> saveVectorStatic;
19 //初始化函数
20 void showVector();
21 void initFunc()
22 {
23     currentVector.x=0;
24     currentVector.y=0;
25     currentVector.z=0;
26 }
27 //变换函数
28 void transformFunc(float x,float y,float z)
29 {
30     currentVector.x=currentVector.x+x;
31     currentVector.y=currentVector.y+y;
32     currentVector.z=currentVector.z+z;
33 }
34 
35 int _tmain(int argc, _TCHAR* argv[])
36 {
37     initFunc();
38     cout<<"我想看看目前的坐标位置:"<<endl;
39     showVector();
40 
41     cout<<"我要向上平移6个单位,保存当前矩阵!!"<<endl;
42     cout<<"执行:“saveVectorStatic.push(currentVector); 来保存!”"<<endl;
43     cout<<"执行:“transformFunc(0.0f,6.0f,0.0f);” 来移动!"<<endl;
44     saveVectorStatic.push(currentVector);
45     transformFunc(0.0f,6.0f,0.0f);
46 
47     cout<<"我想看看目前的坐标位置:"<<endl;
48     showVector();
49 
50     cout<<"我想按照原点的位置向左移动3各单位!!"<<endl;
51     currentVector=saveVectorStatic.top();
52     transformFunc(3.0f,0.0f,0.0f);
53 
54     cout<<"执行:“currentVector=saveVectorStatic.top(); ”来恢复!"<<endl;
55     cout<<"执行:“transformFunc(3.0f,0.0f,0.0f); ”来移动!"<<endl;
56 
57 
58 
59     cout<<"我想看看目前的坐标位置:"<<endl;
60     showVector();
61     return 0;
62 }
63 void showVector()
64 {
65     cout<<"当前栈内元素数目: "<<saveVectorStatic.size()<<endl;
66     cout<<"坐标:"<<"   X"<<"   Y"<<"   Z"<<endl;
67     cout<<"       "<<currentVector.x<<"    "<<currentVector.y<<"   "<<currentVector.z<<endl;
68 }

原文连接:(我们博创论坛):http://bochuang.sinaapp.com/forum.php?mod=viewthread&tid=55

 

 

 

posted on 2012-05-20 21:43  伟征  阅读(322)  评论(0编辑  收藏  举报