|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
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 LISTUsing 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); // ask for a free id number ::glNewList(list,GL_COMPILE_AND_EXECUTE); ::glBegin(GL_TRIANGLES); // std gl calls here... fill vertices, normals, colors ::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 WIREFRAMESometime 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.//*********************************************** // RenderScene //*********************************************** 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(); } |
||||||||||||||||||||||||||||||
|
PermaLink |
Privacy |
Terms
of Use
Last Updated: 30 Nov 1999 Editor: Chris Maunder |
Copyright 1999 by Pierre Alliez Everything else Copyright © CodeProject, 1999-2008 Web18 | Advertise on the Code Project |