📄 openglview.cpp
字号:
// OpenGLView.cpp : implementation file
//
#include "stdafx.h"
#include "MagicScissors.h"
#include "MagicScissorsDoc.h"
#include "OpenGLView.h"
#include "Image/Image.h"
#include "Image/Video.h"
#include "EdgeList.h"
#include "EdgePoint.h"
#include "FrameInfo.h"
#include "ViewAngleDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COpenGLView
IMPLEMENT_DYNCREATE(COpenGLView, CScrollView)
COpenGLView::COpenGLView()
{
m_hGLContext = NULL;
m_pQuadObj = NULL;
m_pTexture = NULL;
m_CX = 0;
m_CY = 0;
m_CZ = 100;
m_ptAnchor = CPoint(0,0);
m_bClick = FALSE;
m_fZoom = 0.5f;
}
COpenGLView::~COpenGLView()
{
}
BEGIN_MESSAGE_MAP(COpenGLView, CScrollView)
//{{AFX_MSG_MAP(COpenGLView)
ON_WM_CLOSE()
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_COMMAND(IDM_DEPTH_MAP, OnDepthMap)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_COMMAND(IDM_FRONT_VIEW, OnFrontView)
ON_COMMAND(IDM_DOWN_VIEW, OnDownView)
ON_WM_CONTEXTMENU()
ON_COMMAND(IDM_ORIGINAL, OnOriginal)
ON_COMMAND(IDM_HALF, OnHalf)
ON_COMMAND(IDM_MAKE_STEREO, OnMakeStereo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COpenGLView drawing
void COpenGLView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CMagicScissorsDoc* pDoc = GetDocument();
CSize sizeTotal;
// TODO: calculate the total size of this view
if( pDoc->m_nMode == 0 )
sizeTotal = pDoc->m_pVideo->GetVideoSize();
else
sizeTotal = pDoc->m_ImgSize;
sizeTotal.cx /= 2;
sizeTotal.cy /= 2;
SetScrollSizes(MM_TEXT, sizeTotal);
ResizeParentToFit(FALSE);
ResizeParentToFit(TRUE);
pDoc->m_pSecond = this;
CRect rect;
pDoc->m_pMain->GetWindowRect(&rect);
//SetWindowPos( &wndTop, rect.right+5, rect.top, 0, 0, SWP_NOSIZE|SWP_SHOWWINDOW );
}
void COpenGLView::OnDraw(CDC* pDC)
{
CMagicScissorsDoc* pDoc = GetDocument();
// TODO: add draw code here
if( m_hGLContext )
{
wglMakeCurrent(pDC->GetSafeHdc(), m_hGLContext);
Render();
glFinish();
SwapBuffers(wglGetCurrentDC());
wglMakeCurrent(NULL,NULL);
}
}
/////////////////////////////////////////////////////////////////////////////
// COpenGLView diagnostics
#ifdef _DEBUG
void COpenGLView::AssertValid() const
{
CScrollView::AssertValid();
}
void COpenGLView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CMagicScissorsDoc* COpenGLView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMagicScissorsDoc)));
return (CMagicScissorsDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COpenGLView message handlers
void COpenGLView::OnClose()
{
// TODO: Add your message handler code here and/or call default
CMagicScissorsDoc* pDoc = GetDocument();
pDoc->m_pSecond = NULL;
CScrollView::OnClose();
}
int COpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CScrollView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
if( !InitGL() ) return -1;
return 0;
}
void COpenGLView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
if( cx == 0 || cy == 0 ) return;
// TODO: Add your message handler code here
CRect rect;
GetClientRect(&rect);
cx = rect.Width();
cy = rect.Height();
CDC* pDC = GetDC();
wglMakeCurrent(pDC->GetSafeHdc(), m_hGLContext);
glClearColor( 0.f, 0.f, 0.f, 1.f );
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_FILL);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0, cx, 0, cy, -100, 512 );
//glFrustum( 0, cx, 0, cy, 0, 512 );
glViewport(0, 0, cx, cy);
//gluLookAt(m_CX,m_CY,m_CZ,0,-10,-100,0,1,0);
glMatrixMode(GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
GLfloat ambientProperties[] = {0.2f, 0.2f, 0.2f, 1.f};
GLfloat diffuseProperties[] = {0.4f, 0.4f, 0.4f, 1.f};
GLfloat specularProperties[] = {1.0f, 1.0f, 1.0f, 1.f};
GLfloat position[] = {0.f, 0.f, 50.f, 0.f};
GLfloat ambientProperties2[] = {0.3f, 0.3f, 0.3f, 1.f};
GLfloat diffuseProperties2[] = {0.5f, 0.5f, 0.5f, 1.f};
GLfloat specularProperties2[] = {1.0f, 1.0f, 1.0f, 1.f};
GLfloat position2[] = { (float)cx/2, (float)cy, 100.f, 0.f};
glLightfv( GL_LIGHT0, GL_AMBIENT, ambientProperties);
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseProperties);
glLightfv( GL_LIGHT0, GL_SPECULAR, specularProperties);
glLightfv( GL_LIGHT0, GL_POSITION, position );
glLightfv( GL_LIGHT1, GL_AMBIENT, ambientProperties2);
glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuseProperties2);
glLightfv( GL_LIGHT1, GL_SPECULAR, specularProperties2);
glLightfv( GL_LIGHT1, GL_POSITION, position2 );
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
///// texture mapping setting ///////
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
/////////////////////
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseProperties);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
wglMakeCurrent(NULL, NULL);
ReleaseDC(pDC);
}
void COpenGLView::OnDestroy()
{
CloseGL();
CScrollView::OnDestroy();
// TODO: Add your message handler code here
CMagicScissorsDoc* pDoc = GetDocument();
pDoc->m_pSecond = NULL;
}
BOOL COpenGLView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
//return CScrollView::OnEraseBkgnd(pDC);
}
BOOL COpenGLView::InitGL()
{
CDC* pDC = GetDC();
if (!SetupPixelFormat(pDC))
return FALSE;
m_hGLContext = wglCreateContext(pDC->GetSafeHdc());
if( !m_hGLContext ) return FALSE;
//gluLookAt( 30, 20, 100, 0, 0, -1 , 0, 1, 0 );
CreateQuadricObject();
wglMakeCurrent(pDC->GetSafeHdc(), m_hGLContext);
gluLookAt( 0, 0, 0, 0, 0, -100 , 0, 1, 0 );
wglMakeCurrent(NULL, NULL);
ReleaseDC(pDC);
m_pTexture = new CImage( CSize(1024,512), 24, 0 );
return TRUE;
}
void COpenGLView::CloseGL()
{
if( wglGetCurrentContext() != NULL )
wglMakeCurrent(NULL,NULL);
if(m_hGLContext)
wglDeleteContext( m_hGLContext );
if( m_pQuadObj )
gluDeleteQuadric(m_pQuadObj);
if( m_pTexture ) delete m_pTexture;
}
BOOL COpenGLView::SetupPixelFormat(CDC* pDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1; // version number
pixelDesc.dwFlags =
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER; // double buffered
//PFD_STEREO_DONTCARE;
pixelDesc.iPixelType = PFD_TYPE_RGBA; // RGBA type
pixelDesc.cColorBits = 24; // Color depth
pixelDesc.cRedBits = 0;
pixelDesc.cRedShift = 0;
pixelDesc.cGreenBits = 0;
pixelDesc.cGreenShift = 0;
pixelDesc.cBlueBits = 0;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 0;
pixelDesc.cAccumRedBits = 0;
pixelDesc.cAccumGreenBits = 0;
pixelDesc.cAccumBlueBits = 0;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 16;
pixelDesc.cStencilBits = 0;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
int GLPixelIndex = ChoosePixelFormat(pDC->GetSafeHdc(),&pixelDesc);
if(GLPixelIndex==0) // Choose default
{
GLPixelIndex = 1;
if(!DescribePixelFormat(pDC->GetSafeHdc(),GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc))
return FALSE;
}
if(!SetPixelFormat(pDC->GetSafeHdc(),GLPixelIndex,&pixelDesc))
return FALSE;
return TRUE;
}
BOOL COpenGLView::CreateQuadricObject()
{
m_pQuadObj = gluNewQuadric();
gluQuadricDrawStyle(m_pQuadObj, GLU_FILL);
gluQuadricNormals(m_pQuadObj, GLU_SMOOTH);
gluQuadricOrientation(m_pQuadObj, GLU_OUTSIDE);
gluQuadricTexture(m_pQuadObj, GL_FALSE);
return TRUE;
}
void COpenGLView::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
CMagicScissorsDoc* pDoc = GetDocument();
CSize size;
if( pDoc->m_nMode == 0 )
size = pDoc->m_pVideo->GetVideoSize();
else
size = pDoc->m_ImgSize;
/// background plane
glPushMatrix();
glScalef(m_fZoom,m_fZoom,m_fZoom);
glTranslatef( 0, 0, -500 );
glBegin(GL_POLYGON);
glColor3f(1,1,1);
glNormal3f(0,0,1);
glVertex3f( 0, 0, 0 );
glVertex3f( 0, (float)size.cy, 0 );
glVertex3f( (float)size.cx, (float)size.cy, 0 );
glVertex3f( (float)size.cx, 0, 0 );
glEnd();
glPopMatrix();
////
//CMagicScissorsDoc* pDoc = GetDocument();
CFrameInfo* pInfo = pDoc->m_pCurFrame;
glPushMatrix();
//glTranslatef( 0, 0, -100 );
glScalef(m_fZoom,m_fZoom,m_fZoom);
pInfo->Render( m_pQuadObj, m_pTexture, TRUE );
glPopMatrix();
}
void COpenGLView::MakeDepthMap(LPCTSTR lpszPathName)
{
if( !m_hGLContext ) return;
CMagicScissorsDoc* pDoc = GetDocument();
CDC* pDC = GetDC();
wglMakeCurrent(pDC->GetSafeHdc(), m_hGLContext);
CRect rect;
GetClientRect(&rect);
int i,j;
int w,h;
w = rect.Width();
h = rect.Height();
float* buf = new float[w*h];
glReadPixels(0,0,w,h,GL_DEPTH_COMPONENT, GL_FLOAT, buf );
CImage* pImage = new CImage( CSize(w,h), 8, 0 );
BYTE** ptr = pImage->GetPtr();
float* fptr;
for( j = 0 ; j < h ; j++ )
{
fptr = buf+w*j;
for( i = 0 ; i < w ; i++ )
ptr[h-j-1][i] = (BYTE)((1-fptr[i])*255);
}
if( lpszPathName )
pImage->SaveImage(lpszPathName);
delete [] buf;
if( pDoc->m_pDepthmap )
delete pDoc->m_pDepthmap;
pDoc->m_pDepthmap = pImage;
wglMakeCurrent(NULL, NULL);
ReleaseDC(pDC);
}
void COpenGLView::OnDepthMap()
{
// TODO: Add your command handler code here
CFileDialog dlg(FALSE, ".bmp", NULL, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_EXPLORER|OFN_OVERWRITEPROMPT, "Depth map(*.bmp)|*.bmp||", NULL);
if( dlg.DoModal() == IDOK )
{
MakeDepthMap(dlg.GetPathName());
}
}
void COpenGLView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bClick = TRUE;
m_ptAnchor = point;
CScrollView::OnLButtonDown(nFlags, point);
}
void COpenGLView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/*if( nFlags == MK_LBUTTON )
{
int dx = point.x - m_ptAnchor.x;
int dy = point.y - m_ptAnchor.y;
m_ptAnchor = point;
m_CX += dx/10.f;
m_CY += dy/10.f;
CDC* pDC = GetDC();
wglMakeCurrent(pDC->GetSafeHdc(), m_hGLContext);
glLoadIdentity();
gluLookAt(m_CX,m_CY,0,0,0,-100,0,1,0);
wglMakeCurrent(NULL, NULL);
ReleaseDC(pDC);
Invalidate(FALSE);
}*/
CScrollView::OnMouseMove(nFlags, point);
}
void COpenGLView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bClick = FALSE;
CScrollView::OnLButtonUp(nFlags, point);
}
void COpenGLView::OnFrontView()
{
// TODO: Add your command handler code here
CDC* pDC = GetDC();
wglMakeCurrent(pDC->GetSafeHdc(), m_hGLContext);
glLoadIdentity();
gluLookAt( 0, 0, 0, 0, 0, -100 , 0, 1, 0 );
wglMakeCurrent(NULL, NULL);
Invalidate(FALSE);
ReleaseDC(pDC);
}
void COpenGLView::OnDownView()
{
// TODO: Add your command handler code here
CDC* pDC = GetDC();
wglMakeCurrent(pDC->GetSafeHdc(), m_hGLContext);
glLoadIdentity();
gluLookAt(0,180,0,0,0,-100,0,1,0);
wglMakeCurrent(NULL, NULL);
Invalidate(FALSE);
ReleaseDC(pDC);
}
void COpenGLView::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CMenu menu;
if (menu.LoadMenu(IDR_GL_VIEW))
{
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
pPopup->TrackPopupMenu(TPM_RIGHTBUTTON | TPM_LEFTALIGN, point.x, point.y, this);
}
}
void COpenGLView::OnOriginal()
{
// TODO: Add your command handler code here
CMagicScissorsDoc* pDoc = GetDocument();
m_fZoom = 1.f;
CSize sizeTotal;
// TODO: calculate the total size of this view
if( pDoc->m_nMode == 0 )
sizeTotal = pDoc->m_pVideo->GetVideoSize();
else
sizeTotal = pDoc->m_ImgSize;
SetScrollSizes(MM_TEXT, sizeTotal);
ResizeParentToFit(FALSE);
ResizeParentToFit(TRUE);
}
void COpenGLView::OnHalf()
{
// TODO: Add your command handler code here
CMagicScissorsDoc* pDoc = GetDocument();
m_fZoom = 0.5;
CSize sizeTotal;
// TODO: calculate the total size of this view
if( pDoc->m_nMode == 0 )
sizeTotal = pDoc->m_pVideo->GetVideoSize();
else
sizeTotal = pDoc->m_ImgSize;
sizeTotal.cx /= 2;
sizeTotal.cy /= 2;
SetScrollSizes(MM_TEXT, sizeTotal);
ResizeParentToFit(FALSE);
ResizeParentToFit(TRUE);
}
void COpenGLView::OnMakeStereo()
{
// TODO: Add your command handler code here
CMagicScissorsDoc* pDoc = GetDocument();
OnFrontView();
OnOriginal();
UpdateWindow();
CViewAngleDlg dlg;
dlg.m_VPos = 5000;
dlg.m_Diff = 20;
if( dlg.DoModal() == IDOK )
{
BeginWaitCursor();
pDoc->MakeStereoImage(dlg.m_VPos,dlg.m_Diff);
EndWaitCursor();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -