工控编程吧
标题:
144上位机VC MFC使用OPenGL绘制三维矩形
[打印本页]
作者:
qq263946146
时间:
2016-1-6 21:44
标题:
144上位机VC MFC使用OPenGL绘制三维矩形
(, 下载次数: 3)
上传
点击文件名下载附件
144上位机VC MFC使用OPenGL绘制三维矩形
功能展示
OPenGL可以很方便的绘制三维方面的动画,我们当前使用实现OPenGL环境的初始化,并绘制一个三维的矩形,效果如图;
要点提示
使用OPENGL前得向工程加载它的静态文件及所在的头文件,通过 我们例程中几行代码便可#include"gl\gl.h“ #include "gl\glu.h“ #include "gl\glaux.h"#pragmacomment(lib,"opengl32.lib")#pragmacomment(lib,"glu32.lib") #pragma comment(lib,"glaux.lib")
另外窗口得具备属性WS_CLIPSIBLINGS |WS_CLIPCHILDREN才能更好显示效果;
在glPushMatrix()数据入栈操作, glPopMatrix()数据出栈操作两函数之间设置矩形的顶点数据,然后将glBigin()函数设置为GL_QUADS.函数glNormal3fv用于设置当前点,glVertex3fv用于设置矩形顶点,函数glColor3fv用于设置顶颜色值;
实现功能
1.新建基于对话框的应用程序
2.初始化OPENGL环境,在主对话框头文件添加#include "gl\gl.h"
#include "gl\glu.h“ #include"gl\glaux.h"#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib") #pragmacomment(lib,"glaux.lib")
3.添加变量HGLRC m_hOpenglRC; 变量在OnInitDialog()初始化,在OnDestroy()销毁
ModifyStyle(0,WS_CLIPSIBLINGS | WS_CLIPCHILDREN,0);//设置窗口属性;
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), //
1, //
PFD_DRAW_TO_WINDOW | //
PFD_SUPPORT_OPENGL | //
PFD_DOUBLEBUFFER, //
PFD_TYPE_RGBA, //
24, //
0, 0, 0, 0, 0, 0, //
0, //
0, //
0, //
0, 0, 0, 016, //
0, //
0, //
PFD_MAIN_PLANE, //
0, //
0, 0, 0 //
};
复制代码
int pixelformat=::ChoosePixelFormat(GetDC()->m_hDC,&pfd);
if(pixelformat==0)
{
AfxMessageBox("初始化失败");
return -1;
}
SetPixelFormat(GetDC()->m_hDC,pixelformat,&pfd);
m_hOpenglRC=wglCreateContext(GetDC()->m_hDC);
wglMakeCurrent(GetDC()->m_hDC,m_hOpenglRC);
void CGkbc8Dlg::OnDestroy()
{
wglDeleteContext(m_hOpenglRC);
CDialog::OnDestroy();
}
复制代码
在对话框源文件顶部添加静态变量
static GLfloat p1[]={0.5,-0.5,-0.5};
static GLfloat p2[]={0.5,0.5,-0.5};
static GLfloat p3[]={0.5,0.5,0.5};
static GLfloat p4[]={0.5,-0.5,0.5};
static GLfloat p5[]={-0.5,-0.5,0.5};
static GLfloat p6[]={-0.5,0.5,0.5};
static GLfloat p7[]={-0.5,0.5,-0.5};
static GLfloat p8[]={-0.5,-0.5,-0.5};
static GLfloat m1[]={1.0,0.0,0.0};
static GLfloat m2[]={-1.0,0.0,0.0};
static GLfloat m3[]={0.0,1.0,0.0};
static GLfloat m4[]={0.0,-1.0,0.0};
static GLfloat m5[]={0.0,0.0,1.0};
static GLfloat m6[]={0.0,0.0,-1.0};
static GLfloat c1[]={0.0,0.0,1.0};
static GLfloat c2[]={0.0,1.0,1.0};
static GLfloat c3[]={1.0,1.0,1.0};
static GLfloat c4[]={1.0,0.0,1.0};
static GLfloat c5[]={1.0,0.0,0.0};
static GLfloat c6[]={1.0,1.0,0.0};
static GLfloat c7[]={0.0,1.0,0.0};
static GLfloat c8[]={1.0,1.0,1.0};
复制代码
在OnPaint() 函数中绘制在OnSize()调整大小
void CGkbc8Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if ( 0 >= cx || 0 >= cy )
{
return;
}
::glViewport(0,0,cx,cy);
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity();
GLdouble dblaspect = (GLdouble)cx/(GLdouble)cy;
::gluPerspective(40.0f,dblaspect,.1f, 20.0f);
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
::glTranslatef(0.0f,0.0f,-5.0f);
::glRotatef(20.0f, 1.0f, 0.0f, 0.0f);
}
复制代码
我们来演示下功能实现的整个过程
(, 下载次数: 0)
上传
点击文件名下载附件
[note]1[/note]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4