我要发帖 回复

正式会员

9

主题

51

积分

0

专家分

:

私信
发表时间 : 2008-10-16 19:34:22 | 浏览 : 1675    评论 : 1
这是我在单文档显示OSG模型的代码,编译成功了可是在再想分栏出问题了
// MFCOpenGLView.cpp : implementation of the CMFCOpenGLView class
//

#include "stdafx.h"
#include "MFCOpenGL.h"

#include "MFCOpenGLDoc.h"
#include "MFCOpenGLView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMFCOpenGLView

IMPLEMENT_DYNCREATE(CMFCOpenGLView, CView)

BEGIN_MESSAGE_MAP(CMFCOpenGLView, CView)
        //{{AFX_MSG_MAP(CMFCOpenGLView)
        ON_WM_CREATE()
        ON_WM_DESTROY()
        ON_WM_ERASEBKGND()
        ON_WM_PAINT()
        ON_WM_SIZE()
        //}}AFX_MSG_MAP
        // Standard printing commands
        ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
        ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
        ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCOpenGLView construction/destruction

CMFCOpenGLView::CMFCOpenGLView()
{
        // TODO: add construction code here
        frameNumber = 0;
        start_tick = timer.tick();
        sceneViewer = new osgUtil::SceneView();
        frameStamp = new osg::FrameStamp();
}

CMFCOpenGLView::~CMFCOpenGLView()
{
}

BOOL CMFCOpenGLView::PreCreateWindow(CREATESTRUCT& cs)
{
        // TODO: Modify the Window class or styles here by modifying
        //  the CREATESTRUCT cs

        // chyojn<
        cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CS_OWNDC;
        // >chyojn

        return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMFCOpenGLView drawing

void CMFCOpenGLView::OnDraw(CDC* pDC)
{
        CMFCOpenGLDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        // TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMFCOpenGLView printing

BOOL CMFCOpenGLView::OnPreparePrinting(CPrintInfo* pInfo)
{
        // default preparation
        return DoPreparePrinting(pInfo);
}

void CMFCOpenGLView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
        // TODO: add extra initialization before printing
}

void CMFCOpenGLView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
        // TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMFCOpenGLView diagnostics

#ifdef _DEBUG
void CMFCOpenGLView::AssertValid() const
{
        CView::AssertValid();
}

void CMFCOpenGLView::Dump(CDumpContext& dc) const
{
        CView::Dump(dc);
}

CMFCOpenGLDoc* CMFCOpenGLView::GetDocument() // non-debug version is inline
{
        ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCOpenGLDoc)));
        return (CMFCOpenGLDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMFCOpenGLView message handlers

int CMFCOpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
        if (CView::OnCreate(lpCreateStruct) == -1)
                return -1;
       
        // TODO: Add your specialized creation code here
        Init();
        return 0;
}

void CMFCOpenGLView::Init()
{
        hWnd = GetSafeHwnd();
        hDC = ::GetDC(hWnd);
        if (SetWindowPixelFormat(hDC)==FALSE)
                return;
        if (CreateViewGLContext(hDC)==FALSE)
                return;
}

BOOL CMFCOpenGLView::SetWindowPixelFormat(HDC hDC)
{
        PIXELFORMATDESCRIPTOR pixelDesc;
        pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
        pixelDesc.nVersion = 1;
        pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
        //PFD_DRAW_TO_BITMAP |
        PFD_SUPPORT_OPENGL |
        PFD_DOUBLEBUFFER,
        //PFD_SUPPORT_GDI |
        //PFD_STEREO_DONTCARE;
        pixelDesc.iPixelType = PFD_TYPE_RGBA;
        pixelDesc.cColorBits = 32;
        pixelDesc.cRedBits = 8;
        pixelDesc.cRedShift = 16;
        pixelDesc.cGreenBits = 8;
        pixelDesc.cGreenShift = 8;
        pixelDesc.cBlueBits = 8;
        pixelDesc.cBlueShift = 0;
        pixelDesc.cAlphaBits = 0;
        pixelDesc.cAlphaShift = 0;
        pixelDesc.cAccumBits = 64;
        pixelDesc.cAccumRedBits = 16;
        pixelDesc.cAccumGreenBits = 16;
        pixelDesc.cAccumBlueBits = 16;
        pixelDesc.cAccumAlphaBits = 0;
        pixelDesc.cDepthBits = 32;
        pixelDesc.cStencilBits = 8;
        pixelDesc.cAuxBuffers = 0;
        pixelDesc.iLayerType = PFD_MAIN_PLANE;
        pixelDesc.bReserved = 0;
        pixelDesc.dwLayerMask = 0;
        pixelDesc.dwVisibleMask = 0;
        pixelDesc.dwDamageMask = 0;
        m_GLPixelIndex = ChoosePixelFormat( hDC, &pixelDesc);
        if (m_GLPixelIndex==0) // Let's choose a default index.
        {
                m_GLPixelIndex = 1;
                if (DescribePixelFormat(hDC, m_GLPixelIndex,
                        sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc)==0)
                {
                        return FALSE;
                }
        }
        if (SetPixelFormat( hDC, m_GLPixelIndex, &pixelDesc)==FALSE)
        {
                return FALSE;
        }
        return TRUE;
}

BOOL CMFCOpenGLView::CreateViewGLContext(HDC hDC)
{
        m_hGLContext = wglCreateContext(hDC);
        if (m_hGLContext == NULL)
        {
                return FALSE;
        }
        if (wglMakeCurrent(hDC, m_hGLContext)==FALSE)
        {
                return FALSE;
        }
        return TRUE;
}

void CMFCOpenGLView::OnDestroy()
{
        if(wglGetCurrentContext()!=NULL)
        {
                // make the rendering context not current
                wglMakeCurrent(NULL, NULL);
        }
        if (m_hGLContext!=NULL)
        {
                wglDeleteContext(m_hGLContext);
                m_hGLContext = NULL;
        }

        CView::OnDestroy();
        // TODO: Add your message handler code here
}


BOOL CMFCOpenGLView::OnEraseBkgnd(CDC* pDC)
{
        // TODO: Add your message handler code here and/or call default
       
        //return CView::OnEraseBkgnd(pDC);
        return TRUE;
}

void CMFCOpenGLView::OnPaint()
{
        CPaintDC dc(this); // device context for painting
       
        // TODO: Add your message handler code here
        glLoadIdentity();
        glClear(GL_COLOR_BUFFER_BIT);

       
        glBegin(GL_POLYGON);
                glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
                glVertex2f(100.0f, 50.0f);
                glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
                glVertex2f(450.0f, 400.0f);
                glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
                glVertex2f(450.0f, 50.0f);
        glEnd();
       

        OSG_Render_Frame();

        //glFlush();
        SwapBuffers(hDC);
        // Do not call CView::OnPaint() for painting messages
}

void CMFCOpenGLView::OnSize(UINT nType, int cx, int cy)
{
        CView::OnSize(nType, cx, cy);
       
        // TODO: Add your message handler code here
        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();
        //gluOrtho2D(0.0, 500.0*aspect, 0.0, 500.0);
        glOrtho(-10*aspect, 10*aspect, -10.0, 10.0, -50, 50);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}

void CMFCOpenGLView::OnInitialUpdate()
{
        CView::OnInitialUpdate();

        // TODO: Add your specialized code here and/or call the base class       
        sceneViewer->setDefaults();
        sceneViewer->setComputeNearFarMode( osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR );

        mRoot  = new osg::Group;

    // Load the Model from the model name
    mModel = osgDB::readNodeFile("cow.osg");

    // Add the model to the scene
    mRoot->addChild(mModel.get());

        sceneViewer->setSceneData( mRoot.get() );
        sceneViewer->setLightingMode( osgUtil::SceneView::SKY_LIGHT );
}

// 渲染OSG场景
void CMFCOpenGLView::OSG_Render_Frame(void)
{
        // 保存GL属性
        glPushAttrib( GL_ALL_ATTRIB_BITS );
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glMatrixMode(GL_TEXTURE);
        glPushMatrix();
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();

        // 获得GL属性
        GLint Viewport[4];
        glGetIntegerv( GL_VIEWPORT, Viewport );
        GLfloat ProjMat[16];
        glGetFloatv( GL_PROJECTION_MATRIX, ProjMat );
        GLfloat ViewMat[16];
        glGetFloatv( GL_MODELVIEW_MATRIX, ViewMat );

        // 初始化SCENEVIEWER观察及投影方式
        sceneViewer->setViewport(Viewport[0], Viewport[1], Viewport[2], Viewport[3]);       
        osg::Matrix osgMat;       
        osgMat.set( ProjMat );
        sceneViewer->getProjectionMatrix().set( osgMat );       
        osgMat.set( ViewMat );
        sceneViewer->getViewMatrix().set( osgMat );

        // 绘制OSG场景
        //g_OSG->PreFrameUpdate();
        sceneViewer->update ();
        sceneViewer->cull ();
        sceneViewer->draw ();
        //g_OSG->PostFrameUpdate();

        // 恢复GL属性
        glMatrixMode(GL_TEXTURE);
        glPopMatrix();
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
        glPopAttrib();
}

我用以下代码分栏什么不行啊
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
   
  if   (!m_wndSplitter.CreateStatic(this,   1,   1,WS_CHILD   |   WS_VISIBLE   |   WS_BORDER))   
  return   FALSE;   
  if   (!m_wndSplitter.CreateView(0,   0,   RUNTIME_CLASS(CMyView),   CSize(0,0),   pContext)   )   
  {   
  m_wndSplitter.DestroyWindow();   
  return   FALSE;   
  }   
  if   (!m_wndSplitter.CreateView(0,   1,   RUNTIME_CLASS(CMFCOpenGLView),   CSize(0,0),   pContext)   )   
  {   
  m_wndSplitter.DestroyWindow();   
  return   FALSE;   
  }   
        m_wndSplitter.SetColumnInfo(0,252,50);   
        m_wndSplitter.RecalcLayout();   
        return   TRUE;   
  }
跪求指点

最近VR访客

FlySky 评论于2008-10-16 23:05:55
你的场景渲染比较混乱,,我没看明白~~~不过,,分栏是在MFC中设置的,,我想应该是你的MFC下有什么不对~~~~~

OSG中国官方网站:http://www.osgChina.org
OSG中国官方讨论区:http://bbs.osgChina.org

OSG专业群-OSG地形研究者 49668412
      OSG地形专业研究 45763709

手机版|VR开发网 统计 津ICP备18009691号
网安备12019202000257

GMT+8, 2021-4-14 10:51 PM

返回顶部