The internal workings of C ++ graphics libraries

As you probably know, C ++ does not have a standard graphics library. Most games use DirectX or OpenGL.

But how do these libraries work? In other words, how can third-party libraries draw graphics if there is no mechanism in C ++? Are they just written in another language?

+4
source share
3 answers

In particular, DirectX and OpenGL work by invoking the operating system and / or video device driver. The driver, in turn, makes the actual drawing, interacting with the graphics device. The exact details of the interaction vary from one video card to another.

Returning to DOS days, C ++ programmers could work directly with the equipment. This has been achieved in two ways. Firstly, by writing / reading from a special block of memory ("framebuffer") where pixels or text were saved. It was a memory gap at a known address, so to work with it you had to specify an integer constant in a pointer and work with this pointer. The mechanism is pure C ++. Another way to interact is to read / write to I / O ports. Now this is a mechanism that is not directly supported by C unless you want to read the built-in assembly or the internal integrity of the compiler. There were two library functions that would wrap these two operations (literally, wrappers around the two CPU commands - INP and OUTP), but most programmers would simply use a single-line inline assembly fragment.

Even now, most interactions with video equipment boil down to these two paths - framebuffer and I / O ports (DMA and interrupts are usually not used for graphics). But we programmers at the application level do not see them. This is material for drivers.

One modern warning is related to protected mode; in protected mode, the C pointers do not match the underlying physical addresses. Just casting the type 0xA0000 to the pointer will not lead you to the framebuffer even in kernel mode. However, kernel-level code (such as a driver) may ask the memory manager to indicate a pointer corresponding to a specific physical address.

+6
source

They will transfer their calls to the driver, who will send them to the graphics card.

+6
source

DirectX and OpenGL opperate at a fairly low level. That is, you say: "Draw a triangle, now draw a square." Typically, programmers transfer these calls to C ++ classes that provide a higher level of functionality, such as "Draw a model, draw a tree." This is called an engine.

I would recommend taking a look at NeHe tutorials for more information: http://nehe.gamedev.net/


So, for example, the call to draw a triangle in OpenGL looks like this:

GLBegin(BEGIN_POLYGONS); GLVector3(0,0,0); GLVector3(10,10,0); GLVector3(-10,10,0); GLEnd(); 

And, as I said, see the above link to NeHe tutorials, they are all in C / C ++

+1
source

All Articles