现在的位置: 首页 > 综合 > 正文

A Scene-Graph based Ray Tracer (2) – MFC, Scene Graph API, Maths

2013年04月06日 ⁄ 综合 ⁄ 共 2605字 ⁄ 字号 评论关闭

This post includes program setup/architecture topics.

[OpengGL in MFC]

I refered to one article to establish the MFC Dialog based OGL application framework. But there exists one bug: when app exists, a memory access violation will happen. I guess it is from the OGL libs I imported – to be solved. Actually there are many successful setup over there on the Internet. Um, I wasn’t wise enough to choose the right one :P

We will use Windows OpengGL extention to connect MFC device context with OGL. First we need to choose the window for us to draw – we use GetDC() to retrieve its device context. Next step is critical. We need to use SetPixelFormat() to setup the graphics properties of the DC we got. The passed-in param of struct PIXELFORMATDESCRIPTOR would be like this:

static PIXELFORMATDESCRIPTOR pfd =
{
   sizeof(PIXELFORMATDESCRIPTOR),
   1,
   PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
   PFD_TYPE_RGBA,
   32,    // bit depth
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   16,    // z-buffer depth
   0, 0, 0, 0, 0, 0, 0,
};

Now we have setup the window graphic properties. Another step is needed before starting coding OGL:

HGLRC hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);

We need to use Windows OpenGL extension to (1) create the GL context using wglCreateContext() on the DC we got; (2) indicate the current OGL context using wglMakeCurrent() which API would enable us to create multi-windows for rendering if needed.

OK, now we can call regular OGL API to render what you need in OnPaint()!

[Scene Graph API]

Considering that our scene to render involves a lot of elements as camera, lights, objects etc., we need a framework to manage all these stuff. We call it Scene Graph. Basically there are 3 parts in the scene graph: 1. Scene manager 2. objects hierarchy. 3. camera & lights

~ Scene Manager

A scene manager takes in charge of manipulating all discrete elements we will use, as camera setup, lights setup and objects manipulations. Generally speaking a scene graph consists of many settor & gettor, addor & deletor interfaces. Also, scene graph drives the whole rendering process and handles the anti-aliasing issue.

~ Objects Hierarchy

We need a base class for all concrete geometry objects. The base class handles all common tasks for an object like color setup, mirror ratio config., light model implementation and the key point of ray tracing: ray hit judgment (ray-objects collision)

~ Camera & Lights

These two classes are relatively easy. For camera, it is responsible for manager view point, view direction, view volume; while lights will involve colors, positions/directions. Different light type as point light, directional light or spot light.

[Mathematics]

I remember one sentence from “Fundamental of Computer Graphics” Preface: For most cases graphics programmes are just mathematics expression. It’s true dude. Basically speaking 3D geometry and Linear algebra are enough for simple applications.

For advanced graphics applications, you definitely need more advanced maths. knowledge and skills. One graphics developer has to be a mathematics fan.

抱歉!评论已关闭.