PDA

View Full Version : Curiosity About OpenGL



newbie472
01-30-2004, 05:16 PM
I understand OpenGL is a graphics library. I also understand it is often used with c/c++.

OK, how was this library developed? With what languages or tools? My understanding of c/c++ is that the languages have no inherent support for graphics programming. Which is why you must use a library like OpenGL.

This is a chicken or egg type of question. If you can't do graphic programming with c/c++, then how would you develop a graphics library with it.

I've asked this question in a number of newsgroups and people blow me off or say "it depends".

Well, I want to know!!! I've been curious about this for a couple of years now and still don't know. Which is why I figured I ask here. Because if anyone should know how a graphics library is developed, it should be the people around here, right?

dorbie
01-30-2004, 06:03 PM
The answer is hardware, and the interfaces to hardware.

OpenGL is an API, it defines an interface, now you could go out and implement that interface to render a bitmap to memory. This has infact been done, for example the Mesa 3D library did this, it requires nothing beyond simple C to render to memory.
http://www.mesa3d.org/

Now what would you have then.... an rectangular image in an array of memory that represented a view of your 3D scene, you couldn't see this image, but you could pretty much write this code on any hardware with a C programming manual and no other knowledge.

You could then display that rectangle of image data on a screen with a simple blit, which on some hardware would simply involve writing it to known memory addresses where the screen display is stored and the hardware takes care of the rest, because on that hardware *specific to that hardware and operating system* and nothing to do with C, those memory addresses contain the screen contents.

Someone else on the other hand may have hardware that is much more sophisticated than a 2D memory mapped display. They might have hardware that can draw polygons, and if you write certain command streams to memory, (a set of hardware specific instructions) and then send a signal to the hardware (by for example writing to certain mapped memory addresses) the hardware would go and read from the memory and interpret the command stream and draw stuff. Now you package all this up with a C api called OpenGL that everyone can use and we can all write compatible portable code and not worry about the implementation details or how it all works.

This is basically how it's done although the details are pretty horrific w.r.t. protected operating system support, memory models, bus interfaces and api frameworks, etc.

I hope that makes it a bit clearer.

[This message has been edited by dorbie (edited 01-30-2004).]

Aldacron
01-31-2004, 03:02 AM
Originally posted by newbie472:

OK, how was this library developed? With what languages or tools? My understanding of c/c++ is that the languages have no inherent support for graphics programming. Which is why you must use a library like OpenGL.


When you say 'no inherent support for graphics programming', you're a bit wrong. It's semantics really, but we've always been able to do graphics 'programming' with C/C++. What the languages don't provide is a way to communicate directly with the graphics hardware, or built-in functions to put the graphics on screen.

There is no single implementation of OpenGL. There are some software-only implementations (such as the default OpenGL32.dll which ships with windows and SGI's version of the same, or Mesa). But the graphics card manufacturers make their own implementations of OpenGL (when you install the drivers for an Nvidia card, you get an Nvidia OpenGL implementation). The driver implementations handle all of the hardware access, so that we don't have to. These are developed with C as far as I know, but could be developed with C++ or assembly, provided they provided a C-style interface.

So you see, graphics programming is possible in C, but access to the graphics hardware and algorithms (for drawing lines, triangles, texturing, etc...) are not built in. You can however use C and C++ to develop the algorithms, and hardware manufacturers can provide access to the hardware using C or C++.

nexusone
01-31-2004, 05:03 AM
Mabye you do understand what they mean by C/C++ does not support graphics, they just mean there is not standard C/C++ graphics commands.

One reason for there being no standard C/C++ graphics commands is that the idea of standard C/C++ to be able to compile on any computer system and not all systems support graphics.

Now the nice thing about C/C++ is you can create your own commands to add to the standard set of commands.

Each OS has is own graphics librarys with commands for drawing, but they have diffrent command names. So you can not use say a Microsoft graphics library with a Linux one.

Here is where openGL comes in, it set's a standard set of commands in which all others use to create a version of the library for there system.

Microsoft creates a openGL library for windows, using the standard set by SGI, linux also does the same thing.

So now any program using standard C/C++ and openGL will compile on any computer system that supports these standards.

Now there is one more part to this is that openGL does not write direct to the graphics card, it calls the openGL driver to do the direct calls to the card. That is why in order to get Hardware rendering you need a video card that has openGL drivers for it. And those are written by the hardware maker.

Little over view of openGL

You call a openGL command from your program, it in turn calls a openGL library function.
The openGL library routine then calls the openGL video card driver. Note if no video driver, then openGL emulates hardware rendering with software.