工控编程吧
标题:
143上位机VC MFC使用OPenGL绘制三维茶壶
[打印本页]
作者:
qq263946146
时间:
2016-1-6 21:40
标题:
143上位机VC MFC使用OPenGL绘制三维茶壶
(, 下载次数: 4)
上传
点击文件名下载附件
143上位机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才能更好显示效果;
使用OPENGL画动画的过程 是ChoosePixelFormat()选择像素格式再通过SetPixelFormat()进行设置; wglCreateContext()创建设备上下文; wglMakeCurrent()设置上下文; glViewport()设置浏览视图的大小
glMatrixMode()设置矩阵模式; gluPerspective()设置透视坐标; glRotatef()设置观看坐标; glPushMatrix()数据入栈操作, glPopMatrix()数据出栈操作; SwapBuffers()交换缓存内容;可以 glPushMatrix (); glPopMatrix ();之间绘制动画;
实现功能
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, 0, 16, 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);
glClearColor (0.0, 0.0, 0.0, 0.0);
int j;
for (j = 0; j < 64; j++)
{
stripeImage[3*j] = 255;
stripeImage[3*j+1] =255-2*j;
stripeImage[3*j+2] =255;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage1D(GL_TEXTURE_1D, 0, 3,64, 0, GL_RGB, GL_UNSIGNED_BYTE, stripeImage);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, sgenparams);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_1D);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glMaterialf (GL_FRONT, GL_SHININESS, 64.0);
复制代码
void CGkbc8Dlg::OnDestroy()
{
wglDeleteContext(m_hOpenglRC);
CDialog::OnDestroy();
}
在OnPaint() 函数中绘制在OnSize()调整大小
static GLfloat xangle = 15.0f;
static GLfloat yangle = 10.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
glTranslatef(0.0f, 0.0f, -8.0);
glRotatef(xangle, yangle, yangle, 0.0f);
xangle+= 10.0f;
yangle+=10.0f;
auxSolidTeapot(2.5);
glPopMatrix ();
glFlush();
if(FALSE==::SwapBuffers(GetDC()->m_hDC))
AfxMessageBox("交换缓冲区失败");
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,0.1f,60.0f);
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
::glTranslatef(0.0f,0.0f,-5.0f);
::glRotatef(20.0f, 10.0f, 0.0f, 0.0f);
复制代码
我们来演示下功能实现的整个过程
[iqiyi]http://player.video.qiyi.com/e2f42258eb8b86acd177d2f1184104f8/0/0/w_19rtdp20pp.swf-albumId=5145258609-tvId=5145258609-isPurchase=0-cnId=12[/iqiyi]
(, 下载次数: 0)
上传
点击文件名下载附件
[note]1[/note]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4