PDA

View Full Version : How to define OpenGL camera movement using MFC OnKeyDown and OnKeyPress..??



Geethanjali
05-23-2017, 09:06 AM
Hello, I am doing real time movement of object using OpenGL. I have created OpenGL window using MFC framework. I want to implement MFC OnKeyDown() and OnKeyUp() in order to rotate camera around an object. For example if the left arrow key is down , start to rotate the camera to left in OnKeyDown() and if the key is released, stop the rotation in OnKeyUp(). I have declared OnKeyDown() and OnkeyUp(), but I am finding difficulty in defining the OpenGL camera within this. Can you please suggest me an answer for this. Below is the Code.. Can Somebody please help me with this. Thank You



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


// CubeWnd

CubeWnd::CubeWnd()
{
}

CubeWnd::~CubeWnd()
{
}


BEGIN_MESSAGE_MAP(CubeWnd, CWnd)
//{{AFX_MSG_MAP(CubeWnd)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_KEYDOWN()
ON_WM_KEYUP()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()



// CubeWnd message handlers

int CubeWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;

CClientDC dc(this);

PIXELFORMATDESCRIPTOR pfd ;
memset(&pfd,0, sizeof(PIXELFORMATDESCRIPTOR)) ;

pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1 ; // Version number
pfd.dwFlags = PFD_DOUBLEBUFFER | // Use double buffer
PFD_SUPPORT_OPENGL | // Use OpenGL
PFD_DRAW_TO_WINDOW ; // Pixel format is for a window.
pfd.iPixelType = PFD_TYPE_RGBA ;
pfd.cColorBits = 24; // 24-bit color
pfd.cDepthBits = 32 ; // 32-bit depth buffer
pfd.iLayerType = PFD_MAIN_PLANE ; // Layer type

int nPixelFormat = ChoosePixelFormat(dc.m_hDC, &pfd);
if (nPixelFormat == 0)
{
TRACE("ChoosePixelFormat Failed %d\r\n",GetLastError()) ;
return -1 ;
}
TRACE("Pixel Format %d\r\n", nPixelFormat) ;

BOOL bResult = SetPixelFormat(dc.m_hDC, nPixelFormat, &pfd);
if (!bResult)
{
TRACE("SetPixelFormat Failed %d\r\n",GetLastError()) ;
return -1 ;
}

//
// Create a rendering context.
//
m_hrc = wglCreateContext(dc.m_hDC);
if (!m_hrc)
{
TRACE("wglCreateContext Failed %x\r\n", GetLastError()) ;
return -1;
}

return 0;
}

void CubeWnd::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);

if ( (cx <= 0) || (cy <= 0) ) return ;

CClientDC dc(this) ;

//
// Make the rendering context m_hrc current
//
BOOL bResult = wglMakeCurrent(dc.m_hDC, m_hrc);
if (!bResult)
{
TRACE("wglMakeCurrent Failed %x\r\n", GetLastError() ) ;
return ;
}

//
// Set up the mapping of 3-space to screen space
//
double gldAspect = static_cast<double>(cx)/ static_cast<double>(cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, gldAspect, 2.0, 40.0);
glViewport(0, 0, cx, cy);
glMatrixMode(GL_MODELVIEW);

//
// No rendering context will be current.
//
wglMakeCurrent(NULL, NULL);

}
void CubeWnd::OnkeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
if (nChar = VK_LEFT)
{
What has to be defined to set OpenGL camera.??????

}

if (nChar = VK_RIGHT)
{

}
}

void CubeWnd::OnkeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CWnd::OnKeyUp(nChar, nRepCnt, nFlags);
if (nChar = VK_LEFT)
{


}

if (nChar = VK_RIGHT)
{

}
}
void CubeWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting

dc.RealizePalette();

// Make the HGLRC current.
BOOL bResult = wglMakeCurrent (dc.m_hDC, m_hrc);
if (!bResult)
{
TRACE("wglMakeCurrent Failed %x\r\n", GetLastError() ) ;
}

// Draw.
DrawWindow();

// Swap buffers.
SwapBuffers(dc.m_hDC) ;

wglMakeCurrent(NULL, NULL);

// Do not call CWnd::OnPaint() for painting messages
}


void CubeWnd:DrawWindow()
{
CRect space;
GetClientRect(&space);

// Clear the color and depth buffers
glClearColor(.96,.96,.96,0) ; //this sets the background color, (RGB, alpha)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//Move the camera back some so we get a good view of the sensors.
glTranslatef(0,0,-10);


for(int i=0;i<static_cast<int>(m_CubesPO.size());++i)
{
if (m_DrawCube[i])
{

glPushMatrix();
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
if (m_DrawCube[i])
{
glClear(GL_DEPTH_BUFFER_BIT);

if (i == 0) // TRANSDUCER
{
glColor3f(1, 0, 0);


}

else if (i == 1) // NEEDLE
{
glColor3f(.5, 0, 1);
}
else if (i == 2)
glColor3f(0, 0, 1);

else
glColor3f(.5, 1, 1);
}
glMultMatrixd(&m_CubesPO[i].m_mat[0][0]);
glPopMatrix(); //go back to the location when we pushed
}

glFlush();

}

}

Brokenmind
05-31-2017, 02:37 AM
Why not just define a member variable that saves the angle, and increment/decrement the angle on left/right press? Then either in the OnKeyDown (you might need a thread for continuous update) or in OnPaint, update your camera position:


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//Move the camera back some so we get a good view of the sensors.
float radius = 10;
glTranslatef(0,0,-10);
gluLookAt(radius * sin(angle), radius * cos(angle), 12, 0, 0, -10, 0, 0, 1);

Maybe give this a try. Not sure how to combine it with the code in your other thread though..
Maybe take a look at this (http://nehe.gamedev.net/article/camera_class_tutorial/18010/) or this (http://nehe.gamedev.net/tutorial/beautiful_landscapes_by_means_of_height_mapping/16006/) (search for "lookat")