工控编程吧
标题:
145上位机VC MFC使用OPenGL绘制三维NURBS曲线
[打印本页]
作者:
qq263946146
时间:
2016-1-6 21:48
标题:
145上位机VC MFC使用OPenGL绘制三维NURBS曲线
(, 下载次数: 5)
上传
点击文件名下载附件
145上位机VC MFC使用OPenGL绘制三维NURBS曲线
功能展示
OPenGL可以很方便的绘制三维方面的动画,我们当前使用实现OPenGL环境的初始化,并绘制一个 三维NURBS曲线,效果如图;
要点提示
使用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才能更好显示效果;
绘制三维NURBS曲线主要用到函数有:gluBeginSurface()gluNurbsSurface() gluEndSurface();gluNewNurbsRenderer()生成对象;
实现功能
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);
void CGkbc8Dlg::OnDestroy()
{
wglDeleteContext(m_hOpenglRC);
CDialog::OnDestroy();
<div style="text-align: center;"><span style="line-height: 1.5;"><font size="4">}</font></span></div>
复制代码
在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);
}
复制代码
//<>
GLUnurbsObj *nurb;
GLfloat point[4][4][3];
GLfloat diffuse[]={0.88,0.66,0.22,1.0};
GLfloat specular[]={0.92,0.9,0.0,1.0};
GLfloat shininess[]={80.0};
glClearColor(0.0,0.0,0.0,1.0);
glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,specular);
glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
nt u, v;
for (u=0;u<4;u++)
{
for(v=0;v<4;v++)
{
point[u][v][0]=2.0*((GLfloat)u-1.5);
point[u][v][1]=2.0*((GLfloat)v-1.5);
if((u==1||u==2)&&(v==1||v==2))
point[u][v][2]=3.0;
else
point[u][v][2]=-3.0;
}
}
nurb=gluNewNurbsRenderer();
gluNurbsProperty(nurb,GLU_SAMPLING_TOLERANCE,25.0);
gluNurbsProperty(nurb,GLU_DISPLAY_MODE,GLU_FILL);
GLfloat knots[8]={0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0};
复制代码
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(330.0,1.0,0.0,0.0);
glScalef(0.5,0.5,0.5);
gluBeginSurface(nurb);
gluNurbsSurface(nurb,
8,
knots,
8,
knots,
4*3,
3,
&point[0][0][0],
4,4,
GL_MAP2_VERTEX_3);
gluEndSurface(nurb);
glPopMatrix();
glFlush();
if(FALSE==::SwapBuffers(GetDC()->m_hDC))
AfxMessageBox("交换缓冲区失败");
复制代码
我们来演示下功能实现的整个过程
[iqiyi]http://player.video.qiyi.com/3e46a048b3c01d70e4877e102eb58bac/0/0/w_19rtdp1ml9.swf-albumId=5145338609-tvId=5145338609-isPurchase=0-cnId=12[/iqiyi]
(, 下载次数: 0)
上传
点击文件名下载附件
[note]1[/note]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4