Click here to Skip to main content
12,827,765 members (46,801 online)
Click here to Skip to main content
Add your own
alternative version


151 bookmarked
Posted 30 Nov 1999

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 

    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 


    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

    You may also be interested in...


    Comments and Discussions

    Generalproblem of runing the program Pin
    freeman1825-Mar-04 18:22
    memberfreeman1825-Mar-04 18:22 
    QuestionReverse sub-division? Pin
    GernotFrisch4-Nov-03 11:01
    sussGernotFrisch4-Nov-03 11:01 
    General4x4 Matrix Pin
    macmac388-Oct-03 11:13
    membermacmac388-Oct-03 11:13 
    GeneralRe: 4x4 Matrix Pin
    pxp13-Feb-05 1:28
    memberpxp13-Feb-05 1:28 
    GeneralMemory allocation error Pin
    Frontman2-Oct-03 6:44
    memberFrontman2-Oct-03 6:44 
    GeneralOpengl Color and select Pin
    mcalves@coc.ufrj.br7-Apr-03 7:07
    membermcalves@coc.ufrj.br7-Apr-03 7:07 
    GeneralRe: Opengl Color and select Pin
    differ121-May-03 19:25
    memberdiffer121-May-03 19:25 
    GeneralRe: Opengl Color and select Pin
    mcalves@coc.ufrj.br22-May-03 3:26
    membermcalves@coc.ufrj.br22-May-03 3:26 
    GeneralRe: Opengl Color and select Pin
    differ122-May-03 18:12
    memberdiffer122-May-03 18:12 
    GeneralBad to da bone Pin
    Jeffrey Dutschke3-Apr-03 20:53
    memberJeffrey Dutschke3-Apr-03 20:53 
    GeneralNothing show if i load other .wrl files Pin
    Man6-Nov-01 1:15
    memberMan6-Nov-01 1:15 
    GeneralRe: Nothing show if i load other .wrl files Pin
    mattie10-Dec-01 11:49
    membermattie10-Dec-01 11:49 
    GeneralRe: Nothing show if i load other .wrl files Pin
    Anonymous10-Oct-02 22:45
    sussAnonymous10-Oct-02 22:45 
    GeneralRe: Nothing show if i load other .wrl files Pin
    Anonymous3-Apr-02 0:44
    memberAnonymous3-Apr-02 0:44 
    GeneralRe: Nothing show if i load other .wrl files Pin
    powerdigital29-May-02 18:55
    memberpowerdigital29-May-02 18:55 
    GeneralRe: Nothing show if i load other .wrl files Pin
    Anonymous31-May-02 1:38
    memberAnonymous31-May-02 1:38 
    GeneralRe: Nothing show if i load other .wrl files Pin
    Anonymous24-Jul-02 5:26
    sussAnonymous24-Jul-02 5:26 
    GeneralRe: Nothing show if i load other .wrl files Pin
    John Ulvr15-Nov-06 16:09
    memberJohn Ulvr15-Nov-06 16:09 
    GeneralRe: Nothing show if i load other .wrl files Pin
    garaber9-Mar-05 7:39
    membergaraber9-Mar-05 7:39 
    GeneralRe: Nothing show if i load other .wrl files Pin
    Huy Le Xuan20-Feb-06 0:44
    memberHuy Le Xuan20-Feb-06 0:44 
    GeneralRe: Nothing show if i load other .wrl files Pin
    scupid1-Mar-06 19:49
    memberscupid1-Mar-06 19:49 
    GeneralRe: Nothing show if i load other .wrl files Pin
    zhodj14-Nov-11 22:47
    memberzhodj14-Nov-11 22:47 
    GeneralCrashes on vertex/line/face changes Pin
    Jason Douglas6-Jul-01 9:47
    memberJason Douglas6-Jul-01 9:47 

    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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.170326.1 | Last Updated 1 Dec 1999
    Article Copyright 1999 by Pierre Alliez
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid