r/code • u/saj1441 • Jun 01 '20
How the hell is anyone supposed to actually understand programming graphics?
Ok so I have been trying to understand graphics programming for awhile now. I want to understand how to do so without using a library ( in other words to actually understand it). I started learning lower level languages such as C/ c++ Only to find out you cant code graphics in pure C/C++, so I moved to assembly. If assembler cant do it then what? hex?? Im so sick of hearing "Anything you can do in one language you can do in another" well apparently that is not the case.
2
u/Poddster Jun 03 '20 edited Jun 03 '20
I started learning lower level languages such as C/ c++ Only to find out you cant code graphics in pure C/C++, so I moved to assembly. If assembler cant do it then what? hex?? Im so sick of hearing "Anything you can do in one language you can do in another" well apparently that is not the case.
When it comes to the discipline of software engineering it turns out it's very easy to write a tiny sentence that seems straightforward enough to the layman, but is in fact monumentally difficult.
Guess what job I, along with 300 other people, spread across various teams and disciplines, once had? That's right, we were making graphics without a library. (Intact, we were.making the libraries!) Although even that statement is a lie: I was in the wddm/directx team, and we worked in C and assembly, and even we had to relay on a library, namely the Window directx interface. So even the pros can't escape it ;)
So I'm not sure how you've "found out" it's impossible to do in C/C++/assembly, as these are the traditional languages to do so.
Still, it should be possible to understand the concept, much in the same way that I can understand how to build a house, or a bridge, despite never having done so, and might even be able to implement a toy version myself in my own garden, e.g. a scale model, or a simple version like a Wendy house, or a bridge over a tiny brook.
So what's the graphics equivalent of a Wendy house? Well, I guess it depends what you want to know, which is unclear from your question. Perhaps you don't even know yourself what you want to know, as "understand graphics" can mean quite a lot of different things to different people. So here's a quick example of the different things it could mean:
Ok so I have been trying to understand graphics programming for awhile now. I want to understand how to do so without using a library ( in other words to actually understand it)
I would say that your "in other words" is not equivalent to the first part. Traditionally, when people want to "understand computer graphics" they buy something like Principles and Practice and then, using basic C/C++ and using a library such as OpenGL, they would implement the algorithms shown in the book and explore the ideas that way.
But the people doing that have the aim of understanding computer graphics from a mathematical, computer sciencey point of view, e.g. they want to know how the lighting equation works or what a Monte Carlo raytracer would be. So for such people "not using a library" would be insane madness and would actively undermine their education, as they'd waste a lot of time programming things to standards and specifications that aren't relevant and they would be the equivalent of the guy with a rivert gun on the golden gate bridge when they actuslly want to learn how to be the design engineer who calculates the stress factors.
The software engineers that learn computer graphics in this manner go on to get jobs working on e.g. computer games, game engines, CAD tools, movie FX tools, etc. The majority of these will do it in C++. If you want to learn the type of computer graphics you get taught in a computer science degree or get one of those jobs then I suggest you buy that book (or take an online equivalent course) and use a library, such as OpenGL. You can go on to Google things like "implement my own graphics rendered / Rasterizer / game engine".
Perhaps, however, you question is more about understanding the fundamental concept of how any arbitrary computer system might interact with a piece of computer peripheral hardware, such as a computer screen? If so, again C/C++/assembly are the correct way to do so, and again you're using a library, only this time you're interfacing with either the BIOS (if you want to do raw VGA, dos style), your OS's syscall API, or, if you want to go so far as to write a driver, your OS''s driver interface.
In our bridge analogy, this is very much the rivert-gun end of the spectrum.
For dos/vga, look up int 13h on Google, e.g. http://www.wagemakers.be/english/doc/vga
For the syscall level, look up something like Linux's /dev/fb0.
For writing a more modern driver, I don't know of a handy guide, there's probably one on OSdev.org, but I suggest you start off with a generic Linux driver tutorial (i.e. one that isn't a graphics driver) as that will probably teach you more about what you want to know. Or, if you insist on writing a graphics driver, then you can read this tedious, dry stuff : https://docs.microsoft.com/en-us/windows-hardware/drivers/display/windows-vista-display-driver-model-design-guide
https://www.kernel.org/doc/html/v4.14/gpu/index.html
You should also look into the Microsoft Reference Rasterizer (either WARP or the old dx9 era one). I don't know if the source code for that is public or not and can't be bothered to Google it. You can also look it to WINEs DX translate stuff. And there are some open source OpenGL driver out there, e.g. the MESA ones.
Or perhaps you want to understand how, in a very simple computer system, one might design and implement a piece of graphics hardware? Well, without getting into VHDL and FPGAs I would recommend:
Read Code by Charles Petzold
Take the nand2tetris course, aka The Elements of Computing Systems.
Again look at the MS refrast.
This should satisfy everything you want to know in that regards.
So obviously there's a lot of stuff that a simple sentence like "I want to understand computer graphics without a library" could actually resolve into meaning. Hopefully I've illustrated a few major areas and you can now articulate which area it is that you were interested in learning?
(You can then also explain how you came to the conclusion that C/C++/assembly were the wrong tools for this job...)
1
u/saj1441 Jun 04 '20
I was just asking if libraries are written in languages such as C c++ then it MUST be possible to do it without it. I guess from your response its not possible. I guess these magical libraries just magically spawn into existence.
2
u/Poddster Jun 05 '20
I said in my response I said it's certainly possible, it's just a lot of work, and it depends what you want to learn.
What about computer graphics do you wish to learn?
I guess these magical libraries just magically spawn into existence.
Did you miss the part where I said I was employed, at one point in my life, to write these libraries? There's nothing magic about it.
1
u/saj1441 Jun 05 '20
you said you used a library.
2
u/Poddster Jun 05 '20
Yes, the one's Microsoft wrote for their operating system. But that's still not magic, someone had to write it.
What about computer graphics is it you wish to learn? Why are you so obsessed with the concept of a library?
1
u/saj1441 Jun 06 '20
So how was THAT written?
1
u/saj1441 Jun 06 '20
Essentially what Im trying to understand is how did the chicken come before the egg?
2
u/Poddster Jun 06 '20
Microsoft's graphics API is just code. And it interacts with the Kernel and the hardware vensor's drivers.
The kernel itself is also just code that a bunch of people wrote.
I don't feel that actually answers your original question though. What is it you actually want to know about computer graphics?
Or do you just want to understand how an operating system works at the lowest levels?
5
u/StochasticTinkr Jun 02 '20 edited Jun 02 '20
What you're talking about ("without using a library") means writing your own video card driver. You would need at least a little assembly to do that. Now, if you wanted to go (slightly) retro, it is relatively easy to write a VGA graphics driver in MSDOS.
Of course you can't do graphics in "pure" C++, because C++ doesn't define the abstractions needed to communicate with hardware that way. Depending on your compiler of choice, there are probably extensions that do let you communicate with the graphics hardware, but they depend on the operating system to some degree.
As far as "assembler cant do it", what exactly are you hoping for? Something like a `doGraphicsButLetMeUnderstandIt()` function? Maybe instead of learning a language in hopes that it'll give you better understanding, find a resource about graphics hardware to learn how to interface with it. THEN find a language that lets you drive that interface.
BTW, Assembly and HEX are just both notations that are as close to machine-code as you are going to get.
Edit: The book Zen of Graphics Programming taught me most of what I know about the VGA hardware and shape-drawing algorithms.
3
Jun 01 '20
I advise you take a look at a youtube channel called bisqwit, I believe it would help you understand some things in computer graphics programming or give an overview on how things are done. The channel/person does a lot of programming videos including but not limited to computer graphics.
0
u/shadows1123 Jun 02 '20
One reason you are having trouble writing it even in assembly is because most of the memory allocation for displays is locked down in some fashion, only accessible via sanctioned mediums. This is because back in the old days when nothing was locked, a virus could come by and completely hijack your visuals.
Good luck
5
u/[deleted] Jun 01 '20
You can do it any language you just need the skill and knowledge of how it works to do it. At the end of the day, it's more or less mapping 2d textures to a 3d space using vectors and other fancy things you can make yourself. Hell, someone made doom using excel. You can do graphics on anything with a screen and memory you can adjust to change the screen