21

I realize this is probably a ridiculous question, but before trying to figure out what libraries to use for which projects, I think it makes sense to really understand the purpose of such libraries first.

A lot of video games use libraries like OpenGL. All the tutorials I've seen of such libraries demonstrate how to write code that tells the computer to draw something. Thing is, in games these days everything is modeled using software such as Zbrush, Maya, or 3ds Max. The models are textured and are good to go. It seems like all you'd need to do is write an animation loop that draws the models and updates repeatedly rather than actually program the code to draw every little thing. That would be both extremely time consuming and would make the models useless. So where does OpenGL or Direct 3D come in in relation to video games and 3d art? What is so crucial about them when all the graphics are already created and just need to be loaded and drawn? Are they used mainly for shaders and effects?

This question may just prove how new I am to this, but it's one I've never heard asked. I'm just starting to learn programming and I'm understanding the code and logic fairly well, but I don't understand graphics libraries or certain frameworks at all and tutorials are not helping.

user1558737
  • 229
  • 1
  • 2
  • 3
  • 2
    Have you read http://en.wikipedia.org/wiki/OpenGL? Modelling software serves a completely different purpose. – Matt Ball Jul 27 '12 at 21:18
  • I have read that. I know what modelling software does. The problem is the only tutorials for graphics libraries I can find are demonstrating how to model things with code, which doesn't make sense in real projects when you're using external software to get your graphics. I simply don't understand what part graphics libraries play in graphics. – user1558737 Jul 27 '12 at 21:21
  • OpenGL or Direct3D are fairly low-level abstractions. They're the building blocks that higher level tools are built upon. – Flexo Jul 27 '12 at 21:24
  • An example of why I'm confused as to it's purpose, is that to implement graphics for a 2D game, you essentially constantly load images and then draw them to the screen. The only things involved are the programming language and the images you want to load. I don't understand why you can't do this with 3D. What makes things like OpenGL such a necessity? – user1558737 Jul 27 '12 at 21:25
  • So it's just for optimization? – user1558737 Jul 27 '12 at 21:26
  • loading and drawing are totally different operations. OpenGL is very good at drawing 3D (or 2D, or ...) "things". It has no loading code. Higher level libraries bridge that gap. You could draw entirely with software and skip OpenGL, but it'd be exceedingly slow since modern graphics hardware is exceedingly good at pushing the polygons OpenGL gives it around very fast. Given the coordinates (and other attributes) of a polygon there's *a lot* of work to be done to convert that into a set of pixels that makes sense on the screen. OpenGL is an API for doing that work. – Flexo Jul 27 '12 at 21:30
  • @user1558737: "The only things involved are the programming language and the images you want to load." And the ***drawing of the image***. A loaded image is just a block of bits; it doesn't get on the screen until you tell *something* to put it on the screen. – Nicol Bolas Jul 27 '12 at 21:31
  • OpenGL graphics library is a kind of firmware library which takes care of GPU functionalities for displaying your well defined model. – Karthik Krish Aug 24 '18 at 10:57

5 Answers5

11

It seems like all you'd need to do is write an animation loop that draws the models and updates repeatedly rather than actually program the code to draw every little thing.

Everything that happens in a computer does so because a program of some form tells it exactly what to do. The letters that this message is composed of only appear because your web-browser of choice downloaded this file via TCP/IP over an HTTP protocol, decoded its UTF-8-encoded text, interpreted that text as defined by the XML, HTML, JavaScript, and so forth standards, and then displayed the visible portion as defined by the Unicode standard for text layout and in accord with HTML et al, using the displaying and windowing abilities of your OS or window manager or whatever.

Every single part of that operation, from the downloading of the file to its display, is governed by a piece of code. Every pixel you are looking at on the screen is where it is because some code put it there.

HTML alone doesn't mean anything. You cannot just take an HTML file and blast it to the screen. Some code must interpret it. You can interpret HTML as a text file, but if you do, it loses all formatting, and you get to see all of the tags. A web browsers can interpret it as proper HTML, in which case you get to see the formatting. But in every case, the meaning of the HTML file is determined by how it is used.

The "draws the model" part of your proposed algorithm must be done by someone. If you don't write that code, then you must be using a library or some other system that will cause the model to appear. And what does that library do? How does it cause the model to appear?

A model, like an HTML web page, is meaningless by itself. Or to put it another way, your algorithm can be boiled down to this:

  1. Animate the model.
  2. ????
  3. Profit!

You're missing a key component: how to actually interpret the model and cause it to appear on the screen. OpenGL/D3D/a software rasterizer/etc is vital for that task.

Nicol Bolas
  • 378,677
  • 53
  • 635
  • 829
  • I appreciate the answer. So is it accurate to say a graphics library is just a collection of functions/methods that let you choose how and where to draw things to the screen? I think the source of my confusion is that the very first programming I did was some Java stuff, and Java includes methods like paint() with Swing and AWT. It didn't occur to me that the GUI toolkits included in Java actually had graphics libraries in them so I was taking those methods for granted. – user1558737 Jul 27 '12 at 21:43
  • Yes. Something, somewhere, has to actually draw the things. OpenGL is a portable, relatively low-level API to the code that does that for 3D graphics. – comingstorm Jul 27 '12 at 21:49
9

A lot of video games use libraries like OpenGL.

First and foremost: OpenGL is not a library per-se, but an API (specification). The OpenGL API may be implemented in form as a software library, but these days is much more common to implement OpenGL in form of a driver that turns OpenGL function calls into control commands to a graphics processor sitting on a graphics card (GPU).

All the tutorials I've seen of such libraries demonstrate how to write code that tells the computer to draw something.

Yes. This is because things need to be drawn to make any use of them.

Thing is, in games these days everything is modeled using software such as Zbrush, Maya, or 3ds Max.

At this point the models just consist of a large list of numbers, and further numbers that tell, how the other numbers form some sort of geometry. Those numbers are not some sort of ready to use image.

The models are textured and are good to go.

They are a bunch of numbers, and what they have is some additional numbers controlling texturing. The textures themself are in turn just numbers.

It seems like all you'd need to do is write an animation loop that draws the models

And how do you think this drawing is going to happen? There's no magic "here you have a model, display it" function. Because for one the way in which the numbers making up a model may have any kind of meaning. So some program must give meaning to those numbers. And that is a renderer.

and updates repeatedly rather than actually program the code to draw every little thing.

Again, there is no magic "draw it" function. Drawing a model involves going through each of its numbers, it consists of, and turning those into drawing commands to the GPU.

That would be both extremely time consuming and would make the models useless.

How are the models useless, when they are what is controlling the issuing of commands to OpenGL. Or do you think OpenGL is used to actually "create" models?

So where does OpenGL or Direct 3D come in in relation to video games and 3d art?

It is used to turn the numbers a 3D model, as it is saved away from a modeller, into something pleasant to look at.

What is so crucial about them when all the graphics are already created

The graphics is not yet created, when the model is done. What's created is a model, and some auxilliary data in form of textures and shaders, which are then turned into graphics in realtime, at the execution time of the program.

and just need to be loaded and drawn?

Again, after being loaded, a model is just a bunch of numbers. And drawing means, turning those numbers into something to look at, which requires sending drawing commands to the graphics processor (GPU), which happens using a API like OpenGL or Direct3D

Are they used mainly for shaders and effects?

They are used to turn the numbers generated by a 3D modelling program (Blender, Maya, ZBrush) into an actual picture.

datenwolf
  • 149,702
  • 12
  • 167
  • 273
  • I'm aware that there is no magic draw it function. I started with Java which comes with GUI toolkits that contains various methods to draw things. Since that's how I began (which wasn't long ago), I wasn't aware that other languages didn't come bundled with such methods already in available. – user1558737 Jul 28 '12 at 21:00
  • @user1558737: Even Java's GUI toolkits SWING and AWT are merely built on the graphics primitives of the OS. And on some OS's this actually is OpenGL. Java itself (the language) diesn't have graphics capabilities. – datenwolf Jul 29 '12 at 00:34
3

You have data. Like a model, with vertices, normals, and textures. As @datenwolf stated above, those are all just numbers sitting on the hard drive or in RAM, not colors on the screen.

Your CPU (which is where the program you write runs) can't talk to the screen directly. Instead, you send the data you want to draw to the GPU. The GPU then draws the data. Graphics APIs like OpenGL and Direct3D allow programs running on the CPU to send data to the GPU and customize how the GPU draws it. This is a gross simplification, but it sounds like you just need an overview.

Ultimately, every graphics program must go through a graphics API. When you draw an image, for example, you send the GPU the image, and the GPU draws it on the screen. Draw some text? Send the data to the GPU. The GPU draws it. Remember, your code can't talk to the screen. It CAN talk to the GPU through OpenGL or Direct3D, and the GPU then draws the data.

imallett
  • 13,288
  • 9
  • 49
  • 115
2

Before OpenGL and DirectX, the games had to use special instructions depending on what graphics card you had. When you bought a new game, you had to check carefully if your card was supported, or you couldn't use the game.

OpenGL and DirectX is a standardized API to the grapics cards. A library is delivered by the manufacturer of the card. If they follow the specification, you are guaranteed that games will work (if they also follow the same specification).

Lars Pensjö
  • 502
  • 2
  • 8
-2

Open Graphics Library (OpenGL) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardware-accelerated rendering.