9 ms·
Olive.c: a simple graphics library that does not have any dependencies
- m00dy 4y agoI have been wondering what he is going to do under the recently declared partial mobilisation. I hope he could find a way to get out the country. He needs to know he is always welcomed in Turkiye.
- TheMode 4y agoLooks great but the API itself doesn't seem very innovative (which is probably not the goal anyway). I'd personally be interested in a graphics API that do not try to render the whole buffer every frame, but update it given a list of changes (e.g. sprite moving/rotating). I believe that such approach could work very well for a 2D software renderer given the likeliness of spatial redundancy, and possibly for video encoding without going through a clueless encoder querying pixels (and having the ability to exploit hardware efficiency to decode/render the stream).
- dlivingston 4y agoCool project! What’s the motivation? Since it eschews any graphics API, is it safe to assume this only does CPU-based rendering?
- skulk 4y agoFrom demos/triangle.c (https://github.com/tsoding/olive.c/blob/master/demos/triangle.c https://github.com/tsoding/olive.c/blob/master/demos/triangl...) // This idea is that you can take this code and compile it to different platforms with different rendering machanisms: // native with SDL, WebAssembly with HTML5 canvas, etc. The goal seems to be to maximize portability.
- p1necone 4y agoUnfortunately only portable to systems with fast CPUs though. Although I guess rendering simple 3d shapes probably runs just fine on low end hardware.
- keeglin 4y agoUh, right? The demo page renders fine on this decade old core i5-3337u, with all the vulnerabilities patched.
- unsafecast 4y agoIt depends how fast “fast” is. PCs in the 90s could run games like quake in lower quality mode with lower framerates, but they ran. CPUs that we have now are many orders of magnitude more powerful, so imagine what you can do with that. Though most games don't bother so we don't really have actual evidence.
- mysterydip 4y agoI was thinking about this just yesterday, it would be neat to have a game jam to make something run in dosbox at modern cpu speeds.
- Someone 4y agoQuake used hand-coded assembly for blitting pixels. That could be a bit less efficient when using this engine. I would expect multi+GHz CPUs hardware to be able to overcome that, though, especially if you’re happy with the tiny (for today) screen resolution of Quake. Quake also did a few hacks to stay performant (https://en.wikipedia.org/wiki/Quake_engine#Engine_design_and_milestones https://en.wikipedia.org/wiki/Quake_engine#Engine_design_and...). If necessary you would have to do the same for something that uses this library.
- unsafecast 4y agoI'm not giving this library as an example of very fast code. I mean, it's really cool, but at the moment it doesn't even use SIMD (which dramatically improves performance). Also, I think handmade hero started with a software renderer. Not sure how far they went with that, but I remember Casey mentioning once that software rendering is viable if you do it right. Certain art styles are easier to do as well — I'm not expecting PBR to be fast, but you could pull off a cel shaded look with simple lighting, and make it look good. It's also worth mentioning that even with portable APIs like OpenGL, drivers are terrible and porting to other operating systems (and even other GPUs!) can be a chore. Software rendering can be an asset in those cases.
- nspattak 4y agoi understand that it is more portable but if it is cpu based, i guess its power performance is terrible since it is not using gpu acceleration which is a priori more efficient
- feiss 4y agoyes, this is only cpu based
- jandrese 4y agoIt pains me to see #include <olive.c> at the top of the file. It makes the project feel like a toy.
- KMnO4 4y agoJust wait till you find out that you can do: const char *s = #include "test.txt" ;
- unwind 4y agoSure, but only if the text file looks like a C string literal, i.e. starts and ends with double quotes (which would make it into a weird text file). Doing const char *s = " #inclued "test.txt" "; won't work, since the preprocessor won't interpret directives inside string literals of course. In many assemblers, there is a directive called "incbin" which pastes in unstructured binary data at the point of usage. I just found a very clever C and C++ wrapper [1] for that, which gives you an INCBIN() macro. Nice! [1]: https://github.com/graphitemaster/incbin https://github.com/graphitemaster/incbin
- cygx 4y agoNote that C23 will include a variant of incbin spelled #embed: Semantically, the preprocessor will insert a list of integers which you can use to initialize an array.
- jsmith45 4y agoAlso, for clarity, it is fully expected that the compiler will use the as-if rule to optimize it. Most likely by having a dedicated token/ast node that only decomposes to comma separated values if if actually needs to, with common usage in initalizers being handled by simply copying the data directly into a static data segment without ever creating an integer list at all.
- a_e_k 4y agoAn incbin-like facility is coming to the standard: https://thephd.dev/finally-embed-in-c23 https://thephd.dev/finally-embed-in-c23
- WoodenChair 4y agoI started something superficially similar but for SVG in an intro C class: https://github.com/davecom/SuperSimpleGraphics https://github.com/davecom/SuperSimpleGraphics
- billsix 4y agoNice! PortableGL is also great, OpenGL api that is defined in entirely in C and writes the “framebuffer” into an SDL window https://github.com/rswinkle/PortableGL https://github.com/rswinkle/PortableGL
- rswinkle 4y agoMinor correction/clarification, the library doesn't depend on SDL, it just writes into a 32 bit color frame buffer, but all the demos/examples use SDL for getting it to the screen since that's the best option. The test suite just writes frames to disk as png images to compare against expected for example.
- jstimpfle 4y agoIt's an implementation, not an API.
- sylware 4y ago... and it is opengl-ish, namely seriously castrated (shaders). Borrowing maths from Mr. Bellard's tinygl.
- rswinkle 4y agoDepends on how you define castrated. It's not like using C or C++ is really limiting and using a C++ math library (like my own rsw_math or more complete glm) lets you basically write it in GLSL. It's not like there's anything you can't do. And the only math from Bellard's TinyGL that I use is his clipping code, maybe 80 lines of code give or take. Not to diminish Bellard at all, if anything I'm saying any problems with PortableGL are mine not his.
- sylware 4y agoMy bad, I used "castrated", it is not what I really wanted to say. I should have said a subset of opengl features. Namely, it is not a software GL implementation you can run an opengl4 3D game on (only those horrible c++ diarehas which are llvm with things like llvmpipe or the other one from intel are supposed to be able to do so).
- andai 4y agoDoes this use WebGL2? It doesn't run on my phone (which does support WebGL 1). Edit: According to the JS source file it appears to be a plain 2D canvas. Now I am even more confused, those should definitely load! https://github.com/tsoding/olive.c/blob/master/js/vc.js https://github.com/tsoding/olive.c/blob/master/js/vc.js
- MobiusHorizons 4y agoIf you read the linked article, it is a c cpu rendering library compiled to web assembly.
- unsafecast 4y agoDoes your browser version support webassembly?
- jeroenhd 4y agoDepending on your privacy settings, your anti fingerprinting configuration may block canvas operations. I know some stringent Firefox fingerprinting protection used to do this, not sure if that has been refined since.
- treuks 4y agoPlease do note that it is in an extremely early stage of development, so if it may seem raw and unpolished, it is because it is.
- simonw 4y agoThis is so nice. It feels a bit like Logo using C syntax - I love that it's totally safe to run software written in this in a browser thanks to WebAssembly.
- dezmou 4y agoNice to see tsoding on the top of HN, this guy does amazing streams and youtube video
- NeutralForest 4y agoDude is insanely talented and patient enough to learn new stuff, it's always a pleasure to watch the videos even though they're usually really long.
- mikedelago 4y agoI agree, he's pretty great. Really intelligent guy. It's a damn shame that he's unable to get paid from Twitch and Youtube due to the war.
- _qua 4y agoI only follow about 20% of what he does because of my limited skill but he's certainly entertaining.
- abainbridge 4y agoHow can I find them?
- rofrol 4y agohttps://twitch.tv/tsoding https://twitch.tv/tsoding
- SiempreViernes 4y agoUsing the search field on youtube is probably easiest.
- abainbridge 4y agoGee thanks. I tried typing olive.c there. That didn't help. The string "tsoding" is the magic I needed.
- sph 4y ago
- synergy20 4y agothis is very impressive, how is this done? the executable is only a few hundred KB and it runs for me, wasm is doing the magic here but I don't know too much about how the process works. I added -I/usr/include/SDL2 to build.sh, made sure wasm-ld is in place, and it builds and runs smoothly.
- Jasper_ 4y agoIt's a simple software rasterizer. If you're interested in writing your own, I've written a tutorial explaining the basics. https://magcius.github.io/xplain/article/rast1.html https://magcius.github.io/xplain/article/rast1.html
- dazhbog 4y agoNice website. Looking forward to the next rasterization article!
- mvladic 4y agoUnfortunately, repository https://github.com/magcius/xplain https://github.com/magcius/xplain is archived and the latest update is from 5 years ago.
- mvladic 4y agoActually, there is already rast2 article: https://magcius.github.io/xplain/article/rast2.html https://magcius.github.io/xplain/article/rast2.html. Also there is the menu article: https://magcius.github.io/xplain/article/menu.html https://magcius.github.io/xplain/article/menu.html.
- Jasper_ 4y agoFYI, these are unfinished, and I have no plans to work on this series again.
- sowbug 4y agoAlso look at the source for original Quake (https://github.com/id-Software/Quake https://github.com/id-Software/Quake), one of the last pure software-rasterizing AAA 3D PC games. Michael Abrash's Graphics Programming Black Book (https://github.com/jagregory/abrash-black-book https://github.com/jagregory/abrash-black-book) explains many of the critical parts of the rendering pipeline. By the way, quake.exe for DOS was 404,480 bytes.
- an1sotropy 4y agoThe TODO section at the end of olive.c shows that there's lots more to do, and decisions to make about how much to do (e.g. what about bezier curves?), but that's quibbling: it is refreshing (and educational) to see everything here done in such a perfectly self-contained way. Godspeed!
- shultays 4y agoHell, half the letters in alphabet is not implemented in that glyph list. That is a weird place to leave a work halfway done. I wonder if he only added the letters for his hello world
- urjjhjx 4y ago
- altilunium 4y agoYears ago, i made something similar as a coursework (Computer Graphics) https://github.com/rtnF14/ComputerGraphics https://github.com/rtnF14/ComputerGraphics
- arriu 4y agoAny plans for text rendering?
- tyingq 4y agoThere's a fairly simple olivec_text() function there that uses a built-in bitmap font.
- bearon 4y agoNice! An awesome retrospective.
- two_almonds 4y agoThe name is pronounced as "olivets" which is a Ukrainian word for "pencil" ("олівець"). From the readme.
- SotCodeLaureate 4y agoNeat.. of course it seems obvious now, but that's funny - I'm Ukrainian and I think that without explicit explanation it would have never occurred to me to read, let alone pronounce it this way - the dot before the extension somehow puts an insurmountable boundary after "olive".
- lioeters 4y agoAs a long-time beginner student of several Slavic languages, I like that word play too, with ".c" being "ts". It makes me wonder what missed opportunities there are for C library names with a funny pun.
- ofrzeta 4y agoFrom the build.sh I figured out that all it takes is to compile with the wasm target, I think: clang $COMMON_CFLAGS -Os -fno-builtin --target=wasm32 ...
- tleb_ 4y agoIdea for improvement: avoiding color blending in sRGB. A first step would be to work in linear space. https://github.com/tsoding/olive.c/blob/43a67dd76760f915516a5e8e2529ec46dcdf1f22/olive.c#L270-L287 https://github.com/tsoding/olive.c/blob/43a67dd76760f915516a...
- osener 4y agoI also found this Cairo-like vector graphics library that runs in a shader interesting: https://www.shadertoy.com/view/lslXW8 https://www.shadertoy.com/view/lslXW8
- stoeckley 4y agoThat is truly one of the coolest things I've seen in a while.
- quickthrower2 4y agoThe combination of mapping pixels to memory and rendering individually and then that appearing on webgl and looking great on a not too expensive phone, where I am pinch zooming while it flawlessly animates, sort of blows my mind in how much technology has come in 25 years. Connects the old with the new in a way I haven’t seen for a while (and yeah seen loads of C64 emulators and such)
- sriram_malhar 4y agoAt first I thought the demos were gifs! After seeing your comment, I went back and remained slack-jawed!
- darkwinx 4y agothose are canvas with 2d context, not webgl
- TazeTSchnitzel 4y agoIt's cool that you can get away with software rendering these days, but it's a shame to not make use of the power of the GPU.
- jesse__ 4y agoThat's the point. Also, not all devices have dedicated graphics hardware
- nottorp 4y agoAnd even if they do, if they're portable please think of my battery :)
- TazeTSchnitzel 4y agoOnly embedded systems and servers lack it these days. Server-side rendering is an interesting use-case, I guess.
- tambourine_man 4y agoAmazing that software rendering can be this smooth on a phone.
- erwincoumans 4y agoCool use of Webassemly! See also the 500 line https://github.com/ssloy/tinyrenderer https://github.com/ssloy/tinyrenderer or the 100 line Python/numpy version https://github.com/rougier/tiny-renderer https://github.com/rougier/tiny-renderer Both cpu renderers with texture mapping and Wavefront obj import without further dependencies.
- sylware 4y ago