Click here to Skip to main content
15,861,172 members
Articles / Multimedia / OpenGL
Article

A small VRML viewer using OpenGL and MFC

Rate me:
Please Sign up or sign in to vote.
4.96/5 (57 votes)
30 Nov 19992 min read 426K   16.8K   159   49
  • Download files - 669 Kb
  • Image 1

    FIG. 1. You can easily display a wrl-based terrain using OpenGL and MFC.
     

    This contribution is a small MFC sample to learn how to :

    • display a VRML file
    • use OpenGL display lists
    • superimpose wireframe on a flat or smoothly shaded mesh
    • smoothly subdivide a 3D triangular mesh (from Charles Loop)
    • implement mouse interaction (rotation and translation)
    • build a scene graph from a vrml 2.0 file (hand-made and not lex-based)

    DISPLAY LIST

    Using display lists is a nice way to accelerate your rendering application. A display list compiles a sequence of gl drawings using standard OpenGL calls, then can be recalled later using a simple list id number. The resulting list is thus resident in the main memory in a precompilated mode, the which greatly accelerates rendering loops. A good command sequence to build a display list may be :
    int list = ::glGenLists(1); <font color="#009900">// ask for a free id number</font> 
    ::glNewList(list,GL_COMPILE_AND_EXECUTE); 
      ::glBegin(GL_TRIANGLES); 
      <font color="#009900">// std gl calls here... fill vertices, normals, colors</font> 
      ::glEnd(); 
    ::glEndList();
    A good command sequence to use a display list may be :
    if(::glIsList(list) == GL_TRUE)
      ::glCallList(m_ListOpenGL);
    The sample builds a scene graph from a vrml 2.0 file (exported via 3D Studio Max only), then uses display lists. Each 3D mesh contains a list number, and use a glCallList command instead of standards glBegin(GL_TRIANGLES) commands when its list is built. A flag m_Modified permits to rebuild the list when the mesh is modified.
    //******************************************** 
    // The 3D mesh class definition 
    //******************************************** 
    class CMesh2d : public CObject3d 
    { 
    private : 
    
     // Std datas 
     CArray<CVertex3d> m_ArrayVertex; 
     CArray<CFace3d>   m_ArrayFace; 
    
     // OpenGL-specific 
     unsigned int m_ListOpenGL; 
     BOOL m_ListDone; 
     BOOL m_Modified; 
     .../... 
    
    public : 
     BOOL glDraw(); 
     .../... 
    } 
    
    //******************************************** 
    // Mesh drawing 
    //******************************************** 
    BOOL CMesh2d::glDraw() 
    { 
     // Build list at first 
     if(!m_ListDone || m_Modified) 
      glBuildList(); 
    
     // Is the list valid ? 
     if(::glIsList(m_ListOpenGL)==GL_TRUE) 
     { 
       ::glCallList(m_ListOpenGL); 
      return TRUE; 
     } 
     return FALSE; 
    }

    SUPERIMPOSING WIREFRAME

    Sometime you would like to view the wireframe superimposing the flat or smooth shaded mesh. A good way to do this is to use the glPolygonOffset command, which creates a z-buffer offset. The following code shows the RenderScene function of the document, if one resumes two rendering passes are necessary, the first render the mesh using lighted flat mode, the second cut off the light, set the line mode, set a z-buffer offset, then draw the mesh again.
    <font color="#009900">//*********************************************** 
    // RenderScene 
    //***********************************************</font> 
    void CMeshDoc::RenderScene() 
    { 
     // Main drawing 
     m_SceneGraph.glDraw(); 
    
     // Add wireframe (no light, and line mode) 
     if(m_AddWireframe) 
     { 
      // Set state 
      ::glDisable(GL_LIGHTING); 
      ::glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); 
      ::glEnable(GL_POLYGON_OFFSET_LINE); 
      ::glPolygonOffset(m_PolygonOffset,-1.0f); 
    
      // Draw again... 
      m_SceneGraph.glDraw(TYPE_MESh2D); 
    
      // Restore light and mode 
      ::glDisable(GL_POLYGON_OFFSET_LINE); 
      ::glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); 
      ::glEnable(GL_LIGHTING); 
     } 
    
     ::glFlush(); 
    }

    Image 2

    FIG. 2. You can superimpose a wireframe on a flat-rendered mesh, using a second rendering loop with the line option.

    SMOOTH SUBDIVISION

    From a given 3D mesh, how can we improve the geometric appearence on smooth models ? The Charles Loop smooth subdivision comes here to help us. Each triangle is divided in four triangles (see figure 3), and a filtering function permits the mesh to be smoothed. The command is easy-to-use in the document, and I let you discover the details in the mesh's code.
    <img SRC="wrl_viewer/wrl_viewer3.jpg" height=191 width=600>
    FIG.3. The one-to-four triangle subdivision scheme used by method.
    <font color="#006600">//*********************************************** 
    // Smooth subdivision 
    </font><font color="#009900">//***********************************************</font> 
    void CMeshDoc::OnMeshLoop() 
    { 
     BeginWaitCursor(); 
     int NbObject = m_SceneGraph.NbObject(); 
     for(int i=0;i<NbObject;i++) 
     { 
       CObject3d *pObject3d = m_SceneGraph[i]; 
       if(pObject3d->GetType() == TYPE_MESh2D) 
      { 
       CMesh2d *pMesh  = (CMesh2d *)pObject3d; 
       pMesh->SubdivisionLoop(); 
      } 
     } 
     UpdateAllViews(NULL); 
     EndWaitCursor(); 
    }

    Image 3

    FIG.4. Two successives iterations of one-to-four subdivision scheme.

    Image 4

    FIG.5. See the visual enhancement obtained by a smooth subdivsion scheme..
     

    MOUSE INTERACTION

    A few variables and commands inserted in the view permit mouse interaction.
    <font color="#009900">//*********************************************** 
    // Left button -> x/y translation 
    //*********************************************** 
    </font>void CMeshView::OnLButtonDown(UINT nFlags, CPoint point) 
    { 
     m_LeftButtonDown = TRUE; 
     m_LeftDownPos = point; 
     SetCapture(); 
     CView::OnLButtonDown(nFlags, point); 
    } 
    void CMeshView::OnLButtonUp(UINT nFlags, CPoint point) 
    { 
     m_RightButtonDown = FALSE; 
     m_LeftButtonDown = FALSE; 
     ReleaseCapture(); 
     CView::OnLButtonUp(nFlags, point); 
    } 
    
    <font color="#009900">//*********************************************** 
    // Right button : z translation 
    //*********************************************** 
    </font>void CMeshView::OnRButtonDown(UINT nFlags, CPoint point) 
    { 
     m_RightButtonDown = TRUE; 
     m_RightDownPos = point; 
     SetCapture(); 
     CView::OnRButtonDown(nFlags, point); 
    } 
    void CMeshView::OnRButtonUp(UINT nFlags, CPoint point) 
    { 
     m_RightButtonDown = FALSE; 
     m_LeftButtonDown = FALSE; 
     ReleaseCapture(); 
     CView::OnRButtonUp(nFlags, point); 
    } 
    
    <font color="#009900">//*********************************************** 
    // Mouse move 
    // Both : rotation 
    // Left : x / y translation 
    // Right : z translation 
    //*********************************************** 
    </font>void CMeshView::OnMouseMove(UINT nFlags, CPoint point) 
    { 
     // Both : rotation 
     if(m_LeftButtonDown && m_RightButtonDown) 
     { 
      if(m_xyRotation) 
      { 
       m_yRotation -= (float)(m_LeftDownPos.x - point.x) * m_SpeedRotation; 
       m_xRotation -= (float)(m_LeftDownPos.y - point.y) * m_SpeedRotation; 
      } 
      else 
      { 
       m_zRotation -= (float)(m_LeftDownPos.x - point.x) * m_SpeedRotation; 
       m_xRotation -= (float)(m_LeftDownPos.y - point.y) * m_SpeedRotation; 
      } 
      m_LeftDownPos = point; 
      m_RightDownPos = point; 
      InvalidateRect(NULL,FALSE); 
     } 
    
     else 
    
     // Left : x / y translation 
     if(m_LeftButtonDown) 
     { 
      m_xTranslation -= (float)(m_LeftDownPos.x - point.x) * m_SpeedTranslation; 
      m_yTranslation += (float)(m_LeftDownPos.y - point.y) * m_SpeedTranslation; 
      m_LeftDownPos = point; 
      InvalidateRect(NULL,FALSE); 
     } 
    
     else 
    
     // Right : z translation 
     if(m_RightButtonDown) 
     { 
      m_zTranslation += (float)(m_RightDownPos.y - point.y) * m_SpeedTranslation; 
      m_RightDownPos = point; 
      InvalidateRect(NULL,FALSE); 
     } 
    
     CView::OnMouseMove(nFlags, point); 
    }

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    France France
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralMy vote of 4 Pin
    Trí Huỳnh Minh Nov202320-Nov-23 13:25
    Trí Huỳnh Minh Nov202320-Nov-23 13:25 
    GeneralMy vote of 5 Pin
    Sergey Chepurin22-May-12 3:04
    Sergey Chepurin22-May-12 3:04 
    GeneralThank you Pin
    Darryl Bryk7-Jan-11 9:05
    Darryl Bryk7-Jan-11 9:05 
    GeneralSolidWorks VRML files Pin
    DerrekCurtis2-Aug-10 9:03
    DerrekCurtis2-Aug-10 9:03 
    GeneralI can complie the code without error, but the graphic is NOT draw on the screen. Pin
    kamanwu19-Oct-09 8:48
    kamanwu19-Oct-09 8:48 
    Generalthank you Pin
    woshisiguigui9-Jul-09 4:22
    woshisiguigui9-Jul-09 4:22 
    Questionmouse interaction.. how to make each obkect "clickable" (or selectable)? Pin
    mimosa24-Jun-08 9:20
    mimosa24-Jun-08 9:20 
    Generalunable to render other .wrl files Pin
    Rajani Kamath28-Apr-08 23:13
    Rajani Kamath28-Apr-08 23:13 
    AnswerRe: unable to render other .wrl files Pin
    chaoguodong25-Jun-12 22:41
    chaoguodong25-Jun-12 22:41 
    GeneralWont compile in VisStudio 2005 Pin
    Member 460001413-Mar-08 14:31
    Member 460001413-Mar-08 14:31 
    AnswerRe: Wont compile in VisStudio 2005 Pin
    CDO18-Mar-08 22:35
    CDO18-Mar-08 22:35 
    Generalupdating surface Pin
    Olaf Petersen25-Oct-07 3:43
    Olaf Petersen25-Oct-07 3:43 
    Questionhow's that supposed to work? Pin
    Quarkette20-Aug-07 8:19
    Quarkette20-Aug-07 8:19 
    AnswerRe: how's that supposed to work? Pin
    DucThanh8-Oct-07 20:49
    DucThanh8-Oct-07 20:49 
    GeneralChange Alpha Value Pin
    Digvijay Gupta25-May-07 23:16
    Digvijay Gupta25-May-07 23:16 
    Generalthank you! Pin
    kru_pl9-Jan-07 1:13
    kru_pl9-Jan-07 1:13 
    Questioncan not draw Pin
    dapson.dps7-Sep-06 22:27
    dapson.dps7-Sep-06 22:27 
    GeneralSome suggest Pin
    Papyna11-Mar-06 20:09
    Papyna11-Mar-06 20:09 
    Generalnothing happens when i upload .wrl in openGL Pin
    vijethas20-Nov-05 13:09
    vijethas20-Nov-05 13:09 
    GeneralThanks Pin
    coder@coder.gr6-Apr-05 9:42
    coder@coder.gr6-Apr-05 9:42 
    QuestionSpecifying a texture image onto the mesh surface? Pin
    TBiker24-Feb-05 11:33
    TBiker24-Feb-05 11:33 
    Generalwow Pin
    Yves5-Oct-04 17:38
    Yves5-Oct-04 17:38 
    QuestionHow to save GDI+ bmp file as PDF File Format. Pin
    pubududilena13-Jul-04 20:03
    pubududilena13-Jul-04 20:03 
    Hi everybody,
    I want to export GDI+ bmp files as PDF File format.
    I have created GDI+ bmp Objects like this.
    Bitmap bmp7(L"myImage.jpg");

    Now I want to save this bmp7 Object as PDF File format. u please help me if any one know this.Frown | :(
    Thanks.
    AnswerRe: How to save GDI+ bmp file as PDF File Format. Pin
    azonenberg4-Mar-08 4:09
    azonenberg4-Mar-08 4:09 
    Questionhow to display following files using opengl and mfc? Pin
    buaa200310-May-04 15:39
    buaa200310-May-04 15:39 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.