How to use Open Inventor in an MFC ProgramThis article describes how to use the Open Inventor (OIV) Development Library in an MFC program. In this article, we will use VC 2003 as the development environment and the coin3d OIV provided by SIM Corporation (www.coin3d.org) as the Open Inventor development library. For OIV settings in Visual C ++, read the content in the open Inventor-Coin3D Development Environment website.
1
. Create a projectFirst, start VC 2003 and use MFC wizards to create an MDI Application (this article only discusses the application of the MDI type, but the SDI or dialog program only needs to make some modifications, OIV can also be used ). We assume that the name of the MDI application is "mfcviewer ".
2
. Add
Coin
And
Sowin
Code1. Open the mfcviewer. cpp file and add the following header files at the beginning of the file:
# Include <inventor/win/sowin. h>
Edit cmfcviewerapp: initinstance () and add:
Sowin: Init ("");This line of code initializes sowin and coin libraries and must call this line of code before calling any coin or sowin function. 2. Open the mfcviewerview. h file and add the following header file list (after the precompiled header file-# ifdef/# pragma once/# endif sequence)
# Include <inventor/win/sowin. h>
# Include <inventor/win/viewers/sowinexaminerviewer. h>
Add a public data member variable
Sowinexaminerviewer * viewer;Sowinexaminerviewer is mainly used to render our scenario and perform interactive operations with the displayed content (such as rotating objects and selecting some scenes)
3. Open the mfcviewerview. cpp file and modify the constructor:
Cmfcviewerview: cmfcviewerview ()
{
Viewer = NULL;
}
Modify the Destructor:
Cmfcviewerview: cmfcviewerview ()
{
If (viewer! = NULL)
Delete viewer;
}
3
. Create a scenarioWe hope that when you create a new document (click File | new), an OIV scenario will appear in the newly created window. 1. Open the mfcviewerdoc. h file and add the following code on the definition of the cmfcviewerdoc class:
Class soseparator;This line of code mainly tells the compiler that we want to use a class called soseparator. 2. Add a public data member variable to the cmfcviewerdoc class:
Soseparator * root;This is the root node of our scenario. 3. Open the mfcviewerdoc. cpp file and add the following header files:
# Include <inventor/nodes/soseparator. h>
# Include <inventor/nodes/somaterial. h>
# Include <inventor/nodes/socone. h>
# Include <inventor/nodes/sotranslation. h>
# Include <inventor/nodes/sotext2.h>These are the nodes we are going to use in the scenario. Therefore, we need to first include their header files. 4. Modify the onnewdocument () function ()
Bool cmfcviewerdoc: onnewdocument ()
{
If (! Cdocument: onnewdocument ())
Return false;
Root = new soseparator;
Root-> REF ();
Somaterial * mymaterial;
Root-> addchild (mymaterial = new somaterial );
Mymaterial-> diffusecolor. setvalue (1.0, 0.0, 0.0 );
Root-> addchild (New socone );
Soseparator * instructsep = new soseparator;
Root-> addchild (instructsep );
Instructsep-> addchild (mymaterial = new somaterial );
Mymaterial-> diffusecolor. setvalue (0.5, 1.0, 1.0 );
Sotranslation * instructtrans = new sotranslation;
Instructtrans-> translation = sbvec3f (-2.0f, 1.3f, 2.0f );
Instructsep-> addchild (instructtrans );
Sotext2 * instructions = new sotext2;
Const char * STR [] = {
"Instructions for the mfcviewer tutorial ",
"",
"Left mouse button = rotate ",
"Middle mouse button = move ",
"Ctrl + middle mouse button = zoom ",
"Right mouse button = options"
};
Instructions-> string. setvalues (0, sizeof (STR)/sizeof (char *), STR );
Instructions-> justification = sotext2: left;
Instructsep-> addchild (Instructions );
Return true;
}We create a scenario by adding a subnode to the root node. Remember that OIV will delete all the scenario objects by itself, so you do not need to remember the pointers of these objects here and delete them later. (In fact, a memory error will occur if you do this ). OIV implements this function through a technology called reference counting. 5. Modify the constructor and destructor and add the following code:
Cmfcviewerdoc: cmfcviewerdoc ()
{
Root = NULL;
}
Cmfcviewerdoc ::~ Cmfcviewerdoc ()
{
If (Root)
Root-> unref ();
}
Call
Root-> unref (), Tells OIV that we no longer use the Root Node object. Therefore, OIV will automatically delete the root node. 6. Open the mfcviewerview. cpp file and add the following code:
# Include <inventor/nodes/soseparator. h>
Modify cmfcviewerview: ondraw (CDC * PDC ):
Void cmfcviewerview: ondraw (CDC * PDC)
{
Cmfcviewerdoc * pdoc = getdocument ();
Assert_valid (pdoc );
If (viewer = NULL)
{
Viewer = new sowinexaminerviewer (m_hwnd );
Viewer-> setdecoration (false );
Soseparator * root = getdocument ()-> root;
Viewer-> setscenegraph (Root );
}
}
The above code embeds winexaminerviewer into the cmfcviewerview window. The toolbar (decoration) Around winexaminerviewer is not displayed. Compile and execute the www.openinventor.cnopeninventor@gmail.comopeninventor for this application @ 126.com