OAG库(OpenGL)第1部分-为MFC应用程序设置库(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/oag-library-opengl-part-1-setting-up-the-library-f-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2939 个词 阅读量 0OAG库(OpenGL)第1部分-为MFC应用程序设置库(译文)
原文地址:https://www.codeproject.com/Articles/85921/OAG-Library-OpenGL-Part-1-Setting-Up-the-Library-f
原文作者:Eduardo Tucci
译文由本站 robot-v1.0 翻译
前言
OAG is a library written in C++. With this library, you can create OpenGL based applications.
OAG是用C ++编写的库.使用此库,您可以创建基于OpenGL的应用程序.
介绍(Introduction)
该库是为创建基于OpenGL的应用程序而编写的.在本教程中,我们将设置库.(This library has been written for creating OpenGL based applications. In this tutorial, we will setup the library.)
关于图书馆(About the Library)
该库与插件一起使用,您可以保存和打开XML格式的对象.在本教程中,我们将使用一些类来绘制Geometry 2D.该库具有用于Geometries 2D,纹理和文本(True Type字体)的插件.文件(The library works with plug-ins, and you can save and open the objects in XML format. In this tutorial, we are going to work with some classes to draw Geometry 2D. The library has plug-ins for Geometries 2D, textures, and text (True Type Font). The file)**OAG_2D_Demo.zip(OAG_2D_Demo.zip)**显示这一点.该库可从以下位置下载(shows this. The library is available for download at) SourceForge(SourceForge) .(.)
样品(Sample)
此示例演示如何使用MFC设置库,设置目录的目录以及设置库的目录.对于示例,项目名称为(This sample demonstrates how to setup the library with MFC, setup the directories for directories, and setup the directories for libraries. For the sample, the project name is) RenderGraphicMFC
.该项目教程是使用Visual Studio 2008创建的.(. This project tutorial has been created with Visual Studio 2008.)
为项目的include设置目录:文件夹(Setting up the directories for includes for the project: The folder)**OAG图书馆(OAGLibrary)**是存储库的文件夹.文件夹(is the folder where the library is stored. The folder)**的OpenGL(OpenGL)**存储OpenGL应用程序的类.(stores the classes for the OpenGL applications.)
设置项目库的目录:(Setting up the directories for libraries for the project: the)**箱子(bin)**文件夹存储库.文件夹(folder stores the libraries. The folder)**VC9(VC9)**使用Visual Studio 2008存储库.(stores the libraries with Visual Studio 2008.)
设置视图类(Setting up the View Class)
班级(The class) oag::Win32Renderer
是一个渲染器,而该类(is a renderer, and the class) oag::WinGraphicContext
在MFC应用程序中设置OpenGL.(sets up OpenGL in an MFC application.)
oag::WinRenderer* m_pRender;
oag::WinGraphicContext* m_pWinGraphicContext;
现在我们将创建方法(Now we will create the method) OnInitialUpdate
初始化库和方法(to initialize the library and the method) EraseBkgnd
.因此,OpenGL将被初始化并准备在屏幕上绘制对象.(. So OpenGL will be initialized and prepared to draw objects on the screen.)
void COAGMFCView::OnInitialUpdate()
{
CView::OnInitialUpdate();
//Delete the current m_pWinGraphicContext,
//m_pRender and tool if a new document is called
UnloadData();
m_pWinGraphicContext = new oag::WinGraphicContext(this);
m_pRender = new oag::WinRenderer(m_pWinGraphicContext, false);
m_pRender->SetBackGroundColor(0, 0, 0);
if ( m_pWinGraphicContext->MakeCurrent() )
{
GetDocument()->m_pObjectsMappingTable->LoadOpenGLExtensions();
m_pWinGraphicContext->DeleteCurrent();
}
}
BOOL COAGMFCView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return FALSE;
}
现在我们将创建方法(Now we will create the method) OnSize
更改窗口大小时更新窗口大小和屏幕上的对象.我们需要调用方法(to update the window size and the objects on the screen when the window size is changed. We need call the method) RenderScene
渲染器在函数中使用的(used by the renderer in the function) OnDraw
在屏幕上绘制对象.(to draw the objects on the screen.)
void COAGMFCView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if ( m_pWinGraphicContext )
m_pWinGraphicContext->OnSize(cx, cy);
}
void COAGMFCDoc::OnDraw(CDC* pDC)
{
COAGMFCDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
if( m_pRender )
{
m_pRender->SetScene( pDoc->m_pScene );
m_pRender->RenderScene();
}
}
设置文档类别(Setting up the Document Class)
班级(The class) oag::OAGScene
存储对象并将其绘制在屏幕上.(stores the objects and draws them on the screen.)
oag::OAGScene* m_pScene;
oag::ObjectsMappingTable* m_pObjectsMappingTable;
实现构造函数和析构函数:(Implementing the constructor and destructor:)
COAGMFCDoc::COAGMFCDoc()
:m_pScene(NULL)
,m_pObjectsMappingTable(NULL)
{
CreateLibraryObjects();
}
删除物件(Deleting Objects)
我们有两种删除对象的方法.你可以打电话(We have two ways to delete objects. You can either call) m_pScene->DeleteAllObjects()
,或在用户要添加对象的类中删除对象.对于演示,我使用(, or delete the objects in the class where the user is adding the objects. For the demo, I use) m_pScene->DeleteAllObjects()
因为对象仅添加到场景中.(because the objects are being adding to the scene only.)
COAGMFCDoc::~COAGMFCDoc()
{
UnloadLibraryObjects();
}
void COAGMFCDoc::CreateLibraryObjects()
{
if( m_pObjectsMappingTable == NULL)
m_pObjectsMappingTable = new oag::ObjectsMappingTable();
if( m_pScene == NULL )
{
m_pScene = new oag::OAGScene();
m_pScene->SetFontMappingTable( m_pObjectsMappingTable->GetFontMappingTable() );
m_pScene->SetTextureMappingTable( m_pObjectsMappingTable->GetTextureMappingTable() );
}
}
void COAGMFCDoc::UnloadLibraryObjects()
{
//Deletes the scene and all objects from the memory
if ( m_pScene )
{
m_pScene->DeleteAllObjects();
delete m_pScene;
m_pScene = NULL;
}
if ( m_pObjectsMappingTable )
{
delete m_pObjectsMappingTable;
m_pObjectsMappingTable = NULL;
}
}
编译应用程序,您将看到一个黑色背景的窗口:(Compile the application, and you will see a window with a black background:)
在屏幕上显示对象(Showing Objects on the Screen)
首先要做的就是更改功能(The first thing to do is to change the function) OnInitialUpdate
在视图上.下面的代码段显示了该功能的所有行.我们只需要添加一行(on the view. The snippet below shows all the lines of the function. We just need to add a line for) CreateObjects()
.(.)
void COAGMFCView::OnInitialUpdate()
{
CView::OnInitialUpdate();
m_pWinGraphicContext = new oag::WinGraphicContext(this);
m_pRender = new oag::WinRenderer(m_pWinGraphicContext, false);
m_pRender->SetBackGroundColor(0, 0, 0);
if ( m_pWinGraphicContext->MakeCurrent() )
{
GetDocument()->m_pObjectsMappingTable->LoadOpenGLExtensions();
m_pWinGraphicContext->DeleteCurrent();
}
CreateObjects();
}
第二件事是创建函数(The second thing to is to create the function) CreateObjects
创建对象并将其添加到场景中.因此,场景在屏幕上绘制了对象.(that creates objects and adds them to the scene. So the scene draws the objects on the screen.)
void COAGMFCView::CreateObjects()
{
//Creating a lines
oag::OAGPrimitives* pLine = new oag::OAGPrimitives();
pLine->SetGeometryType(GL_LINES);
GetDocument()->m_pScene->AddObject( pLine );
pLine->SetArraySize( 4 );
pLine->AddVertex(450, 100, 0 );
pLine->AddVertex(450, 200, 0 );
pLine->AddVertex(400, 150, 0 );
pLine->AddVertex(500, 150, 0 );
//Creating a rectangle
oag::OAGPrimitives* pQuad = new oag::OAGPrimitives();
pQuad->SetGeometryType(GL_QUADS);
GetDocument()->m_pScene->AddObject( pQuad );
pQuad->SetArraySize( 4 );
pQuad->AddVertex( 100, 100, 0);
pQuad->AddVertex( 100, 200, 0);
pQuad->AddVertex( 200, 200, 0);
pQuad->AddVertex( 200, 100, 0);
}
通过命令行构建几何(Building Geometries by Command Line)
要使用该库构建几何,您需要创建一个新的(To build geometries using the library, you need to create a new instance of) OAGPrimitives
.使用该类,可以绘制OpenGL支持的图元,例如线,折线,矩形和三角形.创建新的实例后(. With the class, you can draw primitives supported by OpenGL such as line, polylines, rectangles, and triangles. After creating a new instance of) OAGPrimitives
,您必须使用命令(, you must use the command) SetGeometryType
更改要绘制的基本类型.支持的类型(to change the primitive type that you want to draw. The supported types by) SetGeometryType
是(are) GL_LINES
,(,) GL_LINE_STRIP
,(,) GL_QUADS
和(, and) GL_TRIANGLES
.的(. The) CreateNewGeometry2D
下面的函数显示了如何创建基元并将其添加到场景中.功能(function below shows how to create primitives and add them to the scene. The function) CreateNewGeometry2D
在本教程中未显示.(is not shown in this tutorial.)
void COAGMFCDoc::CreateNewGeometry2D(int nType)
{
switch( nType )
{
case 1: //Line
{
oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
pGeo->SetGeometryType( GL_LINES );
m_pScene->AddObject( pGeo );
}
break;
case 2: //Polyline
{
oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
pGeo->SetGeometryType( GL_LINE_STRIP );
m_pScene->AddObject( pGeo );
}
break;
case 3: //Rectangle
{
oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
pGeo->SetGeometryType( GL_QUADS );
m_pScene->AddObject( pGeo );
}
break;
case 4: //Triangle
{
oag::OAGPrimitives* pGeo = new oag::OAGPrimitives();
pGeo->SetGeometryType( GL_TRIANGLES );
m_pScene->AddObject( pGeo );
}
break;
}
}
历史(History)
-
5(5)日(th)2010年6月(June, 2010)
- 初始版本(Initial version)
-
11(11)日(th)2010年6月(June, 2010)
- 添加了图像插件(BMP,JPG,TGA,PCX)(Added plug-ins for images (BMP, JPG, TGA, PCX))
-
19(19)日(th)2010年6月(June, 2010)
- 为True Type字体添加了插件(Added plug-ins for True Type font)
- 添加了用于纹理和字体的XML(Added XML for texture and font)
-
23(23)rd(rd)2010年7月(July, 2010)
- 纹理变化(Changes in textures)
- 添加了纹理贴图((Added Texture Mapping ()
RectangleMapping
和(and)TriangleMapping
)()) - 添加了对象表的纹理和字体(Added object table for texture and font)
-
15(15)日(th)2010年8月(August, 2010)
- 添加了简单对象3D((Added simple objects 3D ()
Cube
,(,)Pyramid
和(, and)Quadrics
)()) - 添加了纹理贴图((Added Texture Mapping ()
CubeMapping
和(and)PyramidMapping
)和纹理的二次曲面() and Textures for Quadrics)
- 添加了简单对象3D((Added simple objects 3D ()
-
20(20)日(th)2010年10月(October, 2010)
- 添加了纹理的滤镜和包装(Added Filters and Wrap for texture)
- 添加了多纹理(Added Multitexture)
- 添加了反射和反射场景(Added Reflection and Scene for Reflection)
- 为渲染器添加了多个场景(Added Multiple scene for the renderer)
-
1个(1)圣(st)2011年8月(August, 2011)
- 为图片PNG添加了插件(Added plug-in for image PNG)
- 使用矩阵添加转换.命令(Add Transformations using Matrix. The commands)
SetTranslation
,(,)SetScale
,(,)SetRotation
被替换(were replace by)SetTransform
.(.) - 对于基元,该函数(For Primitives, the function)
AddVertex
去掉了.该命令被替换为(was removed. This command was replaced by)SetArrayVertices
它使用向量列表作为参数.(that uses a list of vector as parameter.) - 添加广告牌((Added Billboard ()
oag::OAGBillboard
,(,)OAGBillboardTextureItems
,(,)OAGBillboardGroup
).广告牌支持类的Texture(). The Billboard has support for Texture for the classes)RectangleMapping
和(and)TriangleMapping
.(.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VC9.0 C++ Vista Windows Win7 WinXP Win32 Visual-Studio VS2008 Dev 新闻 翻译