This is the DrawGLScene() function called by onPaint()
void COpenGLControl::DrawGLScene()<br />
{<br />
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);<br />
glLoadIdentity();<br />
glRotatef(80.0f,-1.0f,0.0f,0.0f);<br />
<br />
GLfloat xTrans = -xpos;<br />
GLfloat zTrans = -zpos;<br />
GLfloat yTrans = -walkBias-0.25f;<br />
GLfloat sceneRoty = 360.0f - yRotation;<br />
<br />
glRotatef(lookUpDown,1.0f,0.0f,0.0f);<br />
glRotatef(sceneRoty,0.0f,1.0f,0.0f);<br />
<br />
glTranslatef(xTrans, yTrans, zTrans);<br />
<br />
<br />
if(myRightButton==true){<br />
heading -= 10.0f;
yRotation = heading;<br />
::SetFocus(m_hWnd);<br />
<br />
}<br />
if(myLeftButton==true){<br />
heading += 10.0f;
yRotation = heading;<br />
::SetFocus(m_hWnd);<br />
}<br />
<br />
if(myUpButton==true){<br />
xpos -= (float)sin(heading*piover180) * 0.05f;
zpos -= (float)cos(heading*piover180) * 0.05f;<br />
if (walkBiasAngle >= 359.0f)<br />
{<br />
walkBiasAngle = 0.0f;<br />
}<br />
else<br />
{<br />
walkBiasAngle+= 10;<br />
}<br />
walkBias = (float)sin(walkBiasAngle * piover180)/20.0f;<br />
<br />
::MessageBox(0,"Moving Forward","Move Forward",MB_OK|MB_ICONINFORMATION |MB_DEFAULT_DESKTOP_ONLY);<br />
<br />
}<br />
<br />
if(myDownButton==true){<br />
xpos += (float)sin(heading*piover180) * 0.05f;
zpos += (float)cos(heading*piover180) * 0.05f;
if (walkBiasAngle <= 1.0f)<br />
{<br />
walkBiasAngle = 359.0f;<br />
}<br />
else<br />
{<br />
walkBiasAngle-= 10;<br />
}<br />
walkBias = (float)sin(walkBiasAngle * piover180)/20.0f;<br />
<br />
}<br />
<br />
if(myPageUpButton==true){
lookUpDown-=20.0f;<br />
}<br />
<br />
if(myPageDownButton==true){
lookUpDown+=20.0f;<br />
}<br />
<br />
<br />
<br />
HRESULT hr;<br />
CoInitialize(NULL);<br />
<br />
try{<br />
hr = cnSURMPtr2.CreateInstance(__uuidof(ADODB::Connection));<br />
if (FAILED(hr))<br />
{<br />
throw _com_error(hr);<br />
}<br />
<br />
hr = rsObjectsPtr.CreateInstance(__uuidof(ADODB::Recordset));<br />
if (FAILED(hr))<br />
{<br />
throw _com_error(hr);<br />
}<br />
<br />
cnSURMPtr2->CursorLocation = ADODB::adUseClient;<br />
<br />
_bstr_t dbLocation(L"");<br />
dbLocation = (L".\\SURM.mdb");<br />
<br />
cnSURMPtr2->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ dbLocation +";Persist Security Info=False","","",ADODB::adConnectUnspecified);<br />
<br />
rsObjectsPtr->Open("SELECT * from Objects", cnSURMPtr2.GetInterfacePtr(),<br />
ADODB::adOpenDynamic, ADODB::adLockOptimistic, ADODB::adCmdText);<br />
<br />
if(!rsObjectsPtr->ADOEOF){<br />
if(rsObjectsPtr->GetRecordCount()>0){<br />
while (rsObjectsPtr->ADOEOF == false){<br />
_variant_t myCoordinates;<br />
<br />
myCoordinates = rsObjectsPtr->GetCollect(L"Coordinates");<br />
<br />
if(myCoordinates.vt != VT_NULL){<br />
CString myStrCoords;<br />
<br />
myStrCoords = static_cast<char *>(_bstr_t(myCoordinates.bstrVal));<br />
<br />
int myArraySize = (myStrCoords.GetLength()/15);<br />
CString *singleCoord = new CString[myArraySize];<br />
int tokenCounter = 0;<br />
<br />
while(myStrCoords.GetLength()>=15){<br />
singleCoord[tokenCounter] = myStrCoords.Left(15);<br />
myStrCoords.Delete(0,15);<br />
tokenCounter++;<br />
}<br />
<br />
if(tokenCounter==2){<br />
drawLine(singleCoord[0],singleCoord[1]);<br />
}<br />
else if(tokenCounter>2){<br />
drawQuad(singleCoord[0],singleCoord[1],singleCoord[2],singleCoord[3]); <br />
}<br />
}
rsObjectsPtr->MoveNext();<br />
}<br />
if(rsObjectsPtr->BOF){<br />
rsObjectsPtr->MoveFirst();<br />
}<br />
if(rsObjectsPtr->ADOEOF){<br />
rsObjectsPtr->MoveLast();<br />
}<br />
};<br />
}<br />
}<br />
catch(_com_error &e)<br />
{<br />
AfxMessageBox(static_cast<char *>(e.Description()));<br />
}<br />
catch(...)<br />
{<br />
AfxMessageBox("Unhandled Exception");<br />
};<br />
<br />
myRightButton = false;<br />
myLeftButton = false;<br />
myUpButton = false;<br />
myDownButton = false;<br />
myPageUpButton = false;<br />
myPageDownButton = false;<br />
myHomeButton = false;<br />
<br />
SwapBuffers(dc->m_hDC);<br />
}
Since i would like the rotation and translation to happen either by pressing buttons or key-presses, i have the following code: (NB: Without the message boxes it doesn't work, but with them ... it seems to send the focus back to the correct control - which i'm not too sure whether it's the dialog itself or the GLWindow within the dialog)
BOOL COpenGLMFCDlg::PreTranslateMessage(MSG* pMsg) <br />
{<br />
<br />
<br />
if (pMsg->message == WM_KEYDOWN)<br />
{<br />
if (pMsg->wParam == VK_RIGHT){
openGLControl.myRightButton = true;<br />
<br />
m_Txt_ObjectTypeName.SetWindowText("Right fired"+(CString)(openGLControl.yRotation));<br />
}<br />
<br />
if (pMsg->wParam == VK_LEFT){
openGLControl.myLeftButton = true;<br />
::MessageBox(0,"One more Event fired","My fired Event",MB_ICONINFORMATION |MB_SYSTEMMODAL|MB_DEFAULT_DESKTOP_ONLY);<br />
}<br />
<br />
if (pMsg->wParam == VK_UP){
openGLControl.myUpButton = true;<br />
::MessageBox(0,"Up Event Fired","Up Event",MB_ICONINFORMATION |MB_SYSTEMMODAL|MB_DEFAULT_DESKTOP_ONLY);<br />
<br />
}<br />
<br />
if (pMsg->wParam == VK_DOWN){
openGLControl.myDownButton = true;<br />
}<br />
if (pMsg->wParam == VK_PRIOR){
openGLControl.myPageUpButton = true;<br />
::MessageBox(0,"Page Up Event fired","PageUp Event",MB_ICONINFORMATION |MB_SYSTEMMODAL|MB_DEFAULT_DESKTOP_ONLY);<br />
<br />
}<br />
if (pMsg->wParam == VK_NEXT){
openGLControl.myPageDownButton = true;<br />
<br />
}<br />
<br />
}<br />
return CDialog::PreTranslateMessage(pMsg);<br />
}
Above was the code for keypresses... now for the buttons events code
void COpenGLMFCDlg::OnCmdUp() <br />
{<br />
openGLControl.myUpButton = true;<br />
::MessageBox(0,"Up Button Event Fired","Move Forwards",MB_OK|MB_ICONINFORMATION |MB_DEFAULT_DESKTOP_ONLY);<br />
<br />
<br />
}<br />
<br />
void COpenGLMFCDlg::OnCmdRight() <br />
{<br />
<br />
openGLControl.myRightButton = true;<br />
<br />
<br />
::MessageBox(0,"Right Button Event Fired","Turn Right",MB_OK|MB_ICONINFORMATION |MB_DEFAULT_DESKTOP_ONLY);<br />
<br />
}<br />
<br />
void COpenGLMFCDlg::OnCmdLeft() <br />
{<br />
openGLControl.myLeftButton = true;<br />
::MessageBox(0,"Left Button Event Fired","Turn Left",MB_OK|MB_ICONINFORMATION |MB_DEFAULT_DESKTOP_ONLY);<br />
<br />
<br />
}<br />
<br />
void COpenGLMFCDlg::OnCmdDown() <br />
{<br />
openGLControl.myDownButton = true;<br />
::MessageBox(0,"Down Button Event Fired","Move backwards",MB_OK|MB_ICONINFORMATION |MB_DEFAULT_DESKTOP_ONLY);<br />
<br />
<br />
}
This is the onPaint() function
void COpenGLControl::OnPaint() <br />
{ <br />
<br />
glEnable( GL_POLYGON_OFFSET_FILL );<br />
glPolygonOffset( 1.0, 1.0 );<br />
DrawGLScene();<br />
glDisable( GL_POLYGON_OFFSET_FILL );<br />
<br />
glEnable( GL_BLEND );<br />
glEnable( GL_LINE_SMOOTH );<br />
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );<br />
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );<br />
DrawGLScene();<br />
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );<br />
glDisable( GL_LINE_SMOOTH );<br />
glDisable( GL_BLEND );<br />
<br />
<br />
<br />
CWnd::OnPaint();<br />
myRightButton = false;<br />
myLeftButton = false;<br />
}
I think the issue is something to do with the focus being lost somewhere to some control. onPaint an drawGLscene are in the OpenGLControl functions
Hope this helps...
-- modified at 12:16 Thursday 30th March, 2006
|