|
http://picasaweb.google.com/lh/p ... sbQ?feat=directlink
- #include<hgl/win.h>
- #include<hgl/OpenGL.H>
- #include<hgl/FlowObject.H>
- #include<hgl/Matrix.H>
- #include<hgl/TileFont.H>
-
- const float corners[]= //立方体的8个点原始坐标
- {
- -25.0f, 25.0f, 25.0f,
- 25.0f, 25.0f, 25.0f,
- 25.0f,-25.0f, 25.0f,
- -25.0f,-25.0f, 25.0f,
- -25.0f, 25.0f,-25.0f,
- 25.0f, 25.0f,-25.0f,
- 25.0f,-25.0f,-25.0f,
- -25.0f,-25.0f,-25.0f
- };
-
- enum WORK_MODE //工作模式枚举
- {
- wmMove=0, //移动
- wmScale, //缩放
- wmRotate //旋转
- };
-
- wchar_t *WORK_NAME[]=
- {
- L"Move",
- L"Scale",
- L"Rotate"
- };
-
- using namespace hgl;
- class TestObject:public FlowObject
- {
- TileFont *fnt;
-
- Matrix m; //定义一个变换矩阵
- Vertex3f move,scale,rotate;
-
- Vertex3f coord[8];
- int index[24];
-
- WORK_MODE mode;
- wchar_t axis;
-
- bool use_hard;
-
- /* 4 5
- *---------------*
- /| /|
- 0 / | 1 / |
- *--+------------* |
- | | | |
- | | | | 6
- | *------------+--*
- | / 7 | /
- |/ |/
- *---------------*
- 3 2
- */
-
- private:
-
- void DrawBox()
- {
- int *p=index;
-
- for(int i=0;i<6;i++)
- {
- glBegin(GL_LINE_LOOP);
- for(int j=0;j<4;j++)
- glVertex3fv(coord[*p++]);
- glEnd();
- }
- }
-
- bool KeyDownProc(uint key)
- {
- switch(key)
- {
- case kbM:
- case kbT: mode =wmMove;break; // T/M 进入移动模式
- case kbS: mode =wmScale;break; // S 进入缩放模式
- case kbR: mode =wmRotate;break; // R 进入旋转模式
-
- case kbX: axis ='X';break; // X 处理X轴
- case kbY: axis ='Y';break; // Y 处理Y轴
- case kbZ: axis ='Z';break; // Z 处理Z轴
-
- case kbSpace:
- case kbEnter: move.Zero(); // 空格/回车 数据复位
- scale.One();
- rotate.Zero();
- break;
-
- case kbH: use_hard=!use_hard;break; // 是否使用硬件处理
-
- default: return(false);
- }
-
- return(true);
- }
-
- bool KeyPressProc(uint key)
- {
- float value=0;
-
- if(key==kbMinus )value=-1;else // - 减少量
- if(key==kbEquals)value= 1;else // + 增加量
- return(false);
-
- float *p;
-
- if(mode==wmMove )p=move;else
- if(mode==wmScale ){value/=10.0f;p=scale;}else
- if(mode==wmRotate )p=rotate;else
- return(false);
-
- if(axis=='X')*(p+0)+=value;else
- if(axis=='Y')*(p+1)+=value;else
- if(axis=='Z')*(p+2)+=value;else
- return(false);
-
- return(true);
- }
-
- public:
-
- TestObject()
- {
- const int indexes[]=
- {
- 0,1,2,3,
- 4,5,1,0,
- 3,2,6,7,
- 5,4,7,6,
- 1,5,6,2,
- 4,0,3,7
- };
-
- memcpy(index,indexes,sizeof(indexes));
-
- #ifdef __BORLANDC__
- OnKeyDown =KeyDownProc; //设置按键按下处理
- OnKeyPress=KeyPressProc; //设置按键超时处理
- #else
- SetEventCall(OnKeyDown, this,TestObject::KeyDownProc); //设置按键按下处理
- SetEventCall(OnKeyPress,this,TestObject::KeyPressProc); //设置按键超时处理
- #endif//
-
- fnt=GetFont(); //取得默认字体
-
- move.Zero();
- scale.One();
- rotate.Zero();
-
- mode=wmMove;
- axis='X';
-
- OpenLineSmooth(); //开启线条平滑
- }
-
- void Draw()
- {
- ClearScreen();
- To3DMode();
- glTranslatef(0,0,-200);
-
- CloseTexture();
- OpenBlend(GL_SRC_ALPHA,GL_ONE);
- glColor(1,1,1,1);
- {
- memcpy(coord,corners,sizeof(corners)); //恢复立方体坐标原始数据
-
- m.LoadIdentity(); //恢复缺省矩阵
-
- m.Translate(move.x,move.y,move.z); //设置移动
- m.Scale(scale.x,scale.y,scale.z); //设置缩放
-
- m.Rotate(rotate.x,1,0,0); //设置绕X轴旋转
- m.Rotate(rotate.y,0,1,0); //设置绕Y轴旋转
- m.Rotate(rotate.z,0,0,1); //设置绕Z轴旋转
-
- if(use_hard)
- {
- //使用硬件处理矩阵
-
- glMultMatrixf(m); //改变当前OpenGL矩阵
- }
- else
- {
- //软件处理矩阵
-
- for(int i=0;i<8;i++)
- coord[i]*=m; //将所有坐标乘上变换矩阵
- }
-
- DrawBox(); //绘制box
- }
-
- //输出信息到屏幕
-
- To2DMode();
- int row=0;
- int gap=14;
-
- fnt->DrawFormat(0,row,L"Move : %6.2f,%6.2f,%6.2f",move.x, move.y, move.z); row+=gap;
- fnt->DrawFormat(0,row,L"Scale : %6.2f,%6.2f,%6.2f",scale.x, scale.y, scale.z); row+=gap;
- fnt->DrawFormat(0,row,L"Rotate: %6.2f,%6.2f,%6.2f",rotate.x,rotate.y, rotate.z); row+=gap;
- row+=gap;
-
- fnt->DrawFormat(0,row,L"Matrix * Vertex : %s",use_hard?L"OpenGL Hardware":L"Software");row+=gap;
- fnt->DrawFormat(0,row,L"Work Mode: %s",WORK_NAME[mode]);row+=gap;
- fnt->DrawFormat(0,row,L"Work Asix: %c",axis); row+=gap;
- row+=gap;
-
- for(int i=0;i<4;i++)
- {
- float *p=m.m+i*4;
-
- fnt->DrawFormat(0,row,L"Matrix[%d]=%6.2f,%6.2f,%6.2f,%6.2f",i,p[0],p[1],p[2],p[3]);
- row+=gap;
- }
- row+=gap;
-
- for(int i=0;i<8;i++)
- {
- fnt->DrawFormat(0,row,L"Coord[i]=%6.2f,%6.2f,%6.2f",coord[i].x,coord[i].y,coord[i].z);
- row+=gap;
- }
- }
- };
-
- void GameMain(int,wchar_t **)
- {
- SystemInitInfo sii;
-
- sii.info.ProjectName=L"矩阵类测试";
- sii.info.ProjectCode=L"Matrix Test";
-
- if(!Init(&sii))return; //初始化引擎
-
- SetStart(new TestObject); //设置起始对象
-
- RunApplication(); //开始运行程序
- }
复制代码 |
|