opengl如何用键盘实现放大缩小平移的问题
在程序中如何用QW实现X轴旋转,AS轴实现Y轴旋转,ZX实现Z轴旋转,OP实现平移,上下箭头分别实现放大和缩小,求一般代码...
在程序中如何用QW实现X轴旋转,AS轴实现Y轴旋转,ZX实现Z轴旋转,OP实现平移,上下箭头分别实现放大和缩小,求一般代码
展开
展开全部
// 工具界面View.cpp : C工具界面View 类的实现
//
#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "工具界面.h"
#endif
#include "工具界面Doc.h"
#include "工具界面View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// C工具界面View
IMPLEMENT_DYNCREATE(C工具界面View, CView)
BEGIN_MESSAGE_MAP(C工具界面View, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_COMMAND(ID_ceshi1, &C工具界面View::OnCeshi1)
ON_COMMAND(ID_ceshi2, &C工具界面View::OnCeshi2)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
// C工具界面View 构造/析构
C工具界面View::C工具界面View()
{
// TODO: 在此处添加构造代码
m_pDC=new CClientDC(this);
gg=10.0;
jj=1.0;
kk=0.0;
}
C工具界面View::~C工具界面View()
{
}
BOOL C工具界面View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
// C工具界面View 绘制
void C工具界面View::OnDraw(CDC* /*pDC*/)
{
C工具界面Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
}
// C工具界面View 打印
BOOL C工具界面View::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void C工具界面View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}
void C工具界面View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清理过程
}
// C工具界面View 诊断
#ifdef _DEBUG
void C工具界面View::AssertValid() const
{
CView::AssertValid();
}
void C工具界面View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
C工具界面Doc* C工具界面View::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(C工具界面Doc)));
return (C工具界面Doc*)m_pDocument;
}
#endif //_DEBUG
// C工具界面View 消息处理程序
BOOL C工具界面View::SetupPixelFormat(HDC hDC)
{
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,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pixelformat;
//选择像素以索引
if((pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0)
{
return FALSE;
}
//设置像素索引
if(SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
return FALSE;
}
return TRUE;
}
//创建环境RC
BOOL C工具界面View::CreateViewGLContext(HDC hDC)
{
//生成绘制描述表
this->m_hGLContext = ::wglCreateContext(hDC);
if(this->m_hGLContext==NULL)
{
//创建失败
return FALSE;
}
//设置当前绘制描述表
if(::wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
{
//选为当前RC失败
return FALSE;
}
return TRUE;
}
//gl初始化
BOOL C工具界面View::InitOpengl(CDC *pDC)
{
{
m_pDC=new CClientDC(this);
//设置像素格式
if (SetupPixelFormat(hDC) == FALSE)
return 0;
//创建并设置RC
if (CreateViewGLContext(m_pDC->GetSafeHdc()) == FALSE)
{
return FALSE;
}
return TRUE;
}
}
int C工具界面View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
m_pDC=new CClientDC(this);
//使用窗口DC来设置RC
InitOpengl(m_pDC);
return 0;
}
void C工具界面View::OnDestroy()
{
CView::OnDestroy();
// TODO: 在此处添加消息处理程序代码
if(wglGetCurrentContext()!=NULL)
{
wglMakeCurrent(NULL,NULL);
}
if(this->m_hGLContext!=NULL)
{
wglDeleteContext(this->m_hGLContext);
this->m_hGLContext = NULL;
}
if (m_pDC->GetSafeHdc())
{
DeleteObject(m_pDC);
}
}
void C工具界面View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
GLsizei width, height;
GLdouble aspect;
width = cx;
height = cy;
if (cy==0)
aspect = (GLdouble)width;
else
aspect = (GLdouble)width/(GLdouble)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,aspect,0.0,200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void C工具界面View::OnCeshi1()
{
// TODO: 在此添加命令处理程序代码
CPaintDC dc(this);
ceshifun1(0.0,0.0,0.0);
}
bool C工具界面View::ceshifun1(float mm=0.0, float nn=0.0,float oo=0.0)
{
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(100.0,100.0,100.0,0.0,0.0,0.0,0.0,1.0,0.0);
glTranslatef(mm,nn,oo);
glColor3f(0.0,1.0,0.0);
auxWireTeapot(40.00);
glFlush();
::SwapBuffers(m_pDC->GetSafeHdc()); //交换缓冲区
return TRUE;
}
void C工具界面View::OnCeshi2()
{
// TODO: 在此添加命令处理程序代码
ceshifun2(60.0,0.0,0.0,0.0,1.0,1.0,1.0);
}
bool C工具界面View::ceshifun2(float ss=60.0,float aa=0.0,float bb=0.0,float cc=0.0,float dd=1.0,float ee=1.0,float ff=1.0)
{
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(100.0,100.0,100.0,0.0,0.0,0.0,0.0,1.0,0.0);
glRotatef(ss,aa,bb,cc);
glScalef(dd,ee,ff);
glColor3f(0.0,1.0,0.0);
auxWireTeapot(40.00);
glFlush();
::SwapBuffers(m_pDC->GetSafeHdc()); //交换缓冲区
return true;
}
void C工具界面View::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CView::OnPaint()
}
void C工具界面View::OnLButtonDown(UINT nFlags, CPoint point)
{
gg++;
hh=gg/10;
// TODO: 在此添加消息处理程序代码和/或调用默认值
C工具界面View::ceshifun2(60.0,0.0,0.0,0.0,hh,hh,hh);
CView::OnLButtonDown(nFlags, point);
}
void C工具界面View::OnRButtonDown(UINT nFlags, CPoint point)
{
jj++;
ii=10*jj;
// TODO: 在此添加消息处理程序代码和/或调用默认值
C工具界面View::ceshifun2(ii,7.0,7.0,0.0,1.0,1.0,1.0);
CView::OnRButtonDown(nFlags, point);
}
void C工具界面View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
kk++;
ll=10*kk;
switch(nChar)
{
case'w':
case'W':
C工具界面View::ceshifun1(ll,0.0,0.0);
break;
case's':
case'S':
C工具界面View::ceshifun1((-ll),0.0,0.0);
break;
case'a':
case'A':
C工具界面View::ceshifun1(0.0,ll,0.0);
break;
case'd':
case'D':
C工具界面View::ceshifun1(0.0,(-ll),0.0);
break;
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
//
#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "工具界面.h"
#endif
#include "工具界面Doc.h"
#include "工具界面View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// C工具界面View
IMPLEMENT_DYNCREATE(C工具界面View, CView)
BEGIN_MESSAGE_MAP(C工具界面View, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_COMMAND(ID_ceshi1, &C工具界面View::OnCeshi1)
ON_COMMAND(ID_ceshi2, &C工具界面View::OnCeshi2)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
// C工具界面View 构造/析构
C工具界面View::C工具界面View()
{
// TODO: 在此处添加构造代码
m_pDC=new CClientDC(this);
gg=10.0;
jj=1.0;
kk=0.0;
}
C工具界面View::~C工具界面View()
{
}
BOOL C工具界面View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
// C工具界面View 绘制
void C工具界面View::OnDraw(CDC* /*pDC*/)
{
C工具界面Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
}
// C工具界面View 打印
BOOL C工具界面View::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void C工具界面View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}
void C工具界面View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清理过程
}
// C工具界面View 诊断
#ifdef _DEBUG
void C工具界面View::AssertValid() const
{
CView::AssertValid();
}
void C工具界面View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
C工具界面Doc* C工具界面View::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(C工具界面Doc)));
return (C工具界面Doc*)m_pDocument;
}
#endif //_DEBUG
// C工具界面View 消息处理程序
BOOL C工具界面View::SetupPixelFormat(HDC hDC)
{
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,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pixelformat;
//选择像素以索引
if((pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0)
{
return FALSE;
}
//设置像素索引
if(SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
return FALSE;
}
return TRUE;
}
//创建环境RC
BOOL C工具界面View::CreateViewGLContext(HDC hDC)
{
//生成绘制描述表
this->m_hGLContext = ::wglCreateContext(hDC);
if(this->m_hGLContext==NULL)
{
//创建失败
return FALSE;
}
//设置当前绘制描述表
if(::wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
{
//选为当前RC失败
return FALSE;
}
return TRUE;
}
//gl初始化
BOOL C工具界面View::InitOpengl(CDC *pDC)
{
{
m_pDC=new CClientDC(this);
//设置像素格式
if (SetupPixelFormat(hDC) == FALSE)
return 0;
//创建并设置RC
if (CreateViewGLContext(m_pDC->GetSafeHdc()) == FALSE)
{
return FALSE;
}
return TRUE;
}
}
int C工具界面View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
m_pDC=new CClientDC(this);
//使用窗口DC来设置RC
InitOpengl(m_pDC);
return 0;
}
void C工具界面View::OnDestroy()
{
CView::OnDestroy();
// TODO: 在此处添加消息处理程序代码
if(wglGetCurrentContext()!=NULL)
{
wglMakeCurrent(NULL,NULL);
}
if(this->m_hGLContext!=NULL)
{
wglDeleteContext(this->m_hGLContext);
this->m_hGLContext = NULL;
}
if (m_pDC->GetSafeHdc())
{
DeleteObject(m_pDC);
}
}
void C工具界面View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
GLsizei width, height;
GLdouble aspect;
width = cx;
height = cy;
if (cy==0)
aspect = (GLdouble)width;
else
aspect = (GLdouble)width/(GLdouble)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,aspect,0.0,200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void C工具界面View::OnCeshi1()
{
// TODO: 在此添加命令处理程序代码
CPaintDC dc(this);
ceshifun1(0.0,0.0,0.0);
}
bool C工具界面View::ceshifun1(float mm=0.0, float nn=0.0,float oo=0.0)
{
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(100.0,100.0,100.0,0.0,0.0,0.0,0.0,1.0,0.0);
glTranslatef(mm,nn,oo);
glColor3f(0.0,1.0,0.0);
auxWireTeapot(40.00);
glFlush();
::SwapBuffers(m_pDC->GetSafeHdc()); //交换缓冲区
return TRUE;
}
void C工具界面View::OnCeshi2()
{
// TODO: 在此添加命令处理程序代码
ceshifun2(60.0,0.0,0.0,0.0,1.0,1.0,1.0);
}
bool C工具界面View::ceshifun2(float ss=60.0,float aa=0.0,float bb=0.0,float cc=0.0,float dd=1.0,float ee=1.0,float ff=1.0)
{
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(100.0,100.0,100.0,0.0,0.0,0.0,0.0,1.0,0.0);
glRotatef(ss,aa,bb,cc);
glScalef(dd,ee,ff);
glColor3f(0.0,1.0,0.0);
auxWireTeapot(40.00);
glFlush();
::SwapBuffers(m_pDC->GetSafeHdc()); //交换缓冲区
return true;
}
void C工具界面View::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CView::OnPaint()
}
void C工具界面View::OnLButtonDown(UINT nFlags, CPoint point)
{
gg++;
hh=gg/10;
// TODO: 在此添加消息处理程序代码和/或调用默认值
C工具界面View::ceshifun2(60.0,0.0,0.0,0.0,hh,hh,hh);
CView::OnLButtonDown(nFlags, point);
}
void C工具界面View::OnRButtonDown(UINT nFlags, CPoint point)
{
jj++;
ii=10*jj;
// TODO: 在此添加消息处理程序代码和/或调用默认值
C工具界面View::ceshifun2(ii,7.0,7.0,0.0,1.0,1.0,1.0);
CView::OnRButtonDown(nFlags, point);
}
void C工具界面View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
kk++;
ll=10*kk;
switch(nChar)
{
case'w':
case'W':
C工具界面View::ceshifun1(ll,0.0,0.0);
break;
case's':
case'S':
C工具界面View::ceshifun1((-ll),0.0,0.0);
break;
case'a':
case'A':
C工具界面View::ceshifun1(0.0,ll,0.0);
break;
case'd':
case'D':
C工具界面View::ceshifun1(0.0,(-ll),0.0);
break;
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
2013-12-04 · 知道合伙人互联网行家
关注
展开全部
void speckeys( int key, int x, int y )
{
if ( key == GLUT_KEY_LEFT )mm = glm::translate( mm, glm::vec3( -0.1f, 0.0f, 0.0f ) );
else if( key == GLUT_KEY_RIGHT )mm = glm::translate( mm, glm::vec3( 0.1f, 0.0f, 0.0f ) );
else if( key == GLUT_KEY_UP )mm = glm::translate( mm, glm::vec3( 0.0f, 0.0f, -0.1f ) );
else if( key == GLUT_KEY_DOWN )mm = glm::translate( mm, glm::vec3( 0.0f, 0.0f, 0.1f ) );
glutPostRedisplay();
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上的回答真不是一般的详细啊。。。仔细一看对问题没啥帮助啊。。。不过有可以借鉴的地方啊。。。楼主应该重载消息OnKeyDown啊。。。如果用的只是glut那么响应的应该是那个什么keyboard的函数啊。。。我忘了叫什么了啊。。。而且没有什么所谓的一般代码啊。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询