Click here to Skip to main content
Click here to Skip to main content

A small VRML viewer using OpenGL and MFC

, 30 Nov 1999
Rate this:
Please Sign up or sign in to vote.
  • Download files - 669 Kb
  • <!-- Article Starts -->

    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)


    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> 
      <font color="#009900">// std gl calls here... fill vertices, normals, colors</font> 
    A good command sequence to use a display list may be :
    if(::glIsList(list) == GL_TRUE)
    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) 
     // Is the list valid ? 
      return TRUE; 
     return FALSE; 


    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 
    void CMeshDoc::RenderScene() 
     // Main drawing 
     // Add wireframe (no light, and line mode) 
      // Set state 
      // Draw again... 
      // Restore light and mode 

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


    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() 
     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; 

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

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


    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; 
     CView::OnLButtonDown(nFlags, point); 
    void CMeshView::OnLButtonUp(UINT nFlags, CPoint point) 
     m_RightButtonDown = FALSE; 
     m_LeftButtonDown = FALSE; 
     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; 
     CView::OnRButtonDown(nFlags, point); 
    void CMeshView::OnRButtonUp(UINT nFlags, CPoint point) 
     m_RightButtonDown = FALSE; 
     m_LeftButtonDown = FALSE; 
     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) 
       m_yRotation -= (float)(m_LeftDownPos.x - point.x) * m_SpeedRotation; 
       m_xRotation -= (float)(m_LeftDownPos.y - point.y) * m_SpeedRotation; 
       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; 
     // Left : x / y translation 
      m_xTranslation -= (float)(m_LeftDownPos.x - point.x) * m_SpeedTranslation; 
      m_yTranslation += (float)(m_LeftDownPos.y - point.y) * m_SpeedTranslation; 
      m_LeftDownPos = point; 
     // Right : z translation 
      m_zTranslation += (float)(m_RightDownPos.y - point.y) * m_SpeedTranslation; 
      m_RightDownPos = point; 
     CView::OnMouseMove(nFlags, point); 


    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


    About the Author

    Pierre Alliez

    France France
    No Biography provided

    Comments and Discussions

    GeneralMy vote of 5 PinmemberSergey Chepurin22-May-12 3:04 
    GeneralThank you PinmemberDarryl Bryk7-Jan-11 9:05 
    GeneralSolidWorks VRML files PinmemberDerrekCurtis2-Aug-10 9:03 
    GeneralI can complie the code without error, but the graphic is NOT draw on the screen. Pinmemberkamanwu19-Oct-09 8:48 
    Generalthank you Pinmemberwoshisiguigui9-Jul-09 4:22 
    Questionmouse interaction.. how to make each obkect "clickable" (or selectable)? Pinmembermimosa24-Jun-08 9:20 
    Generalunable to render other .wrl files PinmemberRajani Kamath28-Apr-08 23:13 
    AnswerRe: unable to render other .wrl files Pinmemberchaoguodong25-Jun-12 22:41 
    GeneralWont compile in VisStudio 2005 PinmemberMember 460001413-Mar-08 14:31 
    AnswerRe: Wont compile in VisStudio 2005 PinmemberCDO18-Mar-08 22:35 
    Same here, but was easy to fix.
    Problem comes from change in c++ standard scoping rules in for loop:
    replace all
    for(int i; ,,,,,
    for(int j=0;,,
    int i=0,j=0,k=0;

    Generalupdating surface PinmemberOlaf Petersen25-Oct-07 3:43 
    Questionhow's that supposed to work? PinmemberQuarkette20-Aug-07 8:19 
    AnswerRe: how's that supposed to work? PinmemberDucThanh8-Oct-07 20:49 
    GeneralChange Alpha Value PinmemberDigvijay Gupta25-May-07 23:16 
    Generalthank you! Pinmemberkru_pl9-Jan-07 1:13 
    Questioncan not draw Pinmemberdapson.dps7-Sep-06 22:27 
    GeneralSome suggest PinmemberPapyna11-Mar-06 20:09 
    Generalnothing happens when i upload .wrl in openGL Pinmembervijethas20-Nov-05 13:09 
    GeneralThanks Pinmembercoder@coder.gr6-Apr-05 9:42 
    QuestionSpecifying a texture image onto the mesh surface? PinmemberTBiker24-Feb-05 11:33 
    Generalwow PinmemberYves5-Oct-04 17:38 
    QuestionHow to save GDI+ bmp file as PDF File Format. Pinsusspubba13-Jul-04 20:03 
    AnswerRe: How to save GDI+ bmp file as PDF File Format. Pinmemberazonenberg4-Mar-08 4:09 
    Questionhow to display following files using opengl and mfc? Pinmemberbuaa200310-May-04 15:39 
    QuestionHow to run 2 different(or same) OpenGL objects in one DialogBox? Pinmemberwerter13-May-04 21:09 
    Generalproblem of runing the program Pinmemberfreeman1825-Mar-04 17:22 
    QuestionReverse sub-division? PinsussGernotFrisch4-Nov-03 10:01 
    General4x4 Matrix Pinmembermacmac388-Oct-03 10:13 
    GeneralRe: 4x4 Matrix Pinmemberpxp13-Feb-05 0:28 
    GeneralMemory allocation error PinmemberFrontman2-Oct-03 5:44 
    GeneralOpengl Color and select Pinmembermcalves@coc.ufrj.br7-Apr-03 6:07 
    GeneralRe: Opengl Color and select Pinmemberdiffer121-May-03 18:25 
    GeneralRe: Opengl Color and select Pinmembermcalves@coc.ufrj.br22-May-03 2:26 
    GeneralRe: Opengl Color and select Pinmemberdiffer122-May-03 17:12 
    GeneralBad to da bone PinmemberJeffrey Dutschke3-Apr-03 19:53 
    GeneralNothing show if i load other .wrl files PinmemberMan6-Nov-01 0:15 
    GeneralRe: Nothing show if i load other .wrl files Pinmembermattie10-Dec-01 10:49 
    GeneralRe: Nothing show if i load other .wrl files PinsussAnonymous10-Oct-02 21:45 
    GeneralRe: Nothing show if i load other .wrl files PinmemberAnonymous2-Apr-02 23:44 
    GeneralRe: Nothing show if i load other .wrl files Pinmemberpowerdigital29-May-02 17:55 
    GeneralRe: Nothing show if i load other .wrl files PinmemberAnonymous31-May-02 0:38 
    GeneralRe: Nothing show if i load other .wrl files PinsussAnonymous24-Jul-02 4:26 
    GeneralRe: Nothing show if i load other .wrl files PinmemberJohn Ulvr15-Nov-06 15:09 
    GeneralRe: Nothing show if i load other .wrl files Pinmembergaraber9-Mar-05 6:39 
    GeneralRe: Nothing show if i load other .wrl files PinmemberHuy Le Xuan19-Feb-06 23:44 
    GeneralRe: Nothing show if i load other .wrl files Pinmemberscupid1-Mar-06 18:49 
    GeneralRe: Nothing show if i load other .wrl files Pinmemberzhodj14-Nov-11 21:47 
    GeneralCrashes on vertex/line/face changes PinmemberJason Douglas6-Jul-01 8:47 

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

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

    | Advertise | Privacy | Mobile
    Web01 | 2.8.141015.1 | Last Updated 1 Dec 1999
    Article Copyright 1999 by Pierre Alliez
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid