- OpenGL·Vulkan·Metal·DirectX के अंदरूनी काम करने के तरीके को समझने के लिए बिना किसी बाहरी graphics library के software renderer को शुरुआत से इम्प्लीमेंट किया गया है
- यह triangle mesh और texture से बने 3D model को image में बदलता है, और GUI या GPU application बनाना इसमें शामिल नहीं है
- पूरा कोड लगभग 500 लाइनों का है, और छात्र आमतौर पर 10~20 घंटे लगाकर काम करने वाला renderer बनाना शुरू करते हैं
- RGB·RGBA·grayscale को सपोर्ट करने वाली TGA processing class और सिर्फ single-pixel setting की सुविधा दी जाती है; line segment और triangle drawing आपको खुद इम्प्लीमेंट करनी होती है
- तैयार कोड को कॉपी करने के बजाय खुद लिखने पर ही rendering concepts और 3D library के अंदरूनी काम को ठीक से समझा जा सकता है
Rendering pipeline को खुद बनाने की प्रक्रिया
- आधुनिक 3D graphics library की संरचना का ढीला-ढाला अनुसरण करते हुए rendering pipeline के काम करने का तरीका सीखा जाता है
- GPU application लिखने के बजाय software renderer से उसके internal behavior को दोबारा बनाया जाता है
- input एक 3D model है जो triangle mesh और texture से बना है, और output एक rendered image है
- graphics interface के बिना प्रोग्राम image file बनाता है
- बाहरी dependency कम रखने के लिए सरल image format TGA का उपयोग किया गया है
- शुरुआत में दी गई सुविधा सिर्फ image load/save और एक pixel का color set करना है
- line segment या triangle draw करने के लिए कोई built-in function नहीं है, इसलिए सब कुछ खुद लिखना पड़ता है
- शुरुआती उदाहरण
64x64 RGB framebuffer बनाता है, तीन coordinates के pixel को सफेद सेट करता है, और फिर उसे framebuffer.tga के रूप में सेव करता है
- color value BGRA order में दी जाती है
कोड build और run करना
git clone https://github.com/ssloy/tinyrenderer.git &&
cd tinyrenderer &&
cmake -Bbuild &&
cmake --build build -j &&
build/tinyrenderer obj/diablo3_pose/diablo3_pose.obj obj/floor.obj
- run का परिणाम
framebuffer.tga में सेव होता है
- पूरा कोड लगभग 500 लाइनों का है, लेकिन concepts को समझने के लिए खुद इम्प्लीमेंट करना ज़रूरी है, इसलिए दिए गए कोड को ज्यों का त्यों इस्तेमाल करने की सिफारिश नहीं की जाती
1 टिप्पणियां
Hacker News की राय
https://github.com/kshitijl/tinyrenderer-rs
repository में development process और मज़ेदार visual bugs के बहुत सारे screenshots हैं। इससे सिर्फ rendering के सिद्धांत ही नहीं, बल्कि यह भी अच्छी तरह सीखा कि modern CPU बहुत तेज़ हैं, और single-thread CPU renderer से भी flashy special effects वाले interactive 3D game चलाए जा सकते हैं
यह LLM से पहले की बात थी, इसलिए कम से कम दो महीने लगे, और ज़्यादातर समय computer graphics की maths समझने और C segmentation faults को trace करने में गया
concepts को refresh करने के लिए यह GitHub lecture note ज़्यादा अच्छा लगा। repository का code style मुझे पसंद नहीं है, और पुराना rasterizer भी बहुत ज़्यादा simple और inefficient है, लेकिन फिर भी Foley की किताब से ज़्यादा पढ़ने योग्य है
editions बदलने के साथ इस्तेमाल की गई language भी Pascal से C, फिर C और C++ तक बढ़ी, और latest edition में थोड़ा C# भी है। कई नए concepts गायब हैं, लेकिन फिर भी इसमें अब भी काफ़ी मूल्यवान सामग्री है
frustum clipping को local tile के point selection से handle किया जा सकता है, और primitive composition में inverse-transformed clipping rectangle को barycentric coordinate system में संभालना आसान होता है। double precision या fixed-point से rounding error नियंत्रित किए जा सकते हैं, और मुख्य कठिनाई नए vertices के Z और 1/Z values को फिर से बनाना है। अगर rasterizer delayed attribute composition वाला है, तो बाकी हिस्सा स्वाभाविक रूप से pipeline से गुजर जाता है, और OpenSWR.org के open source implementation में इसके example देखे जा सकते हैं
सबसे बड़ा मानसिक अवरोध projection space और homogeneous coordinates की अपरिचितता है। clip space की छह planes बस
x = ±w,y = ±w,z = ±wहैं, और polygon की हर edge पर चलते हुए दोनों endpoints के अंदर/बाहर होने का निर्णय लेकर boundary intersection की position और vertex attributes को linear interpolation से निकालना होता है। यह प्रक्रिया हर plane पर क्रम से लागू करने पर triangle अधिकतम 9 vertices वाला convex polygon बन जाता है, जिसे आसानी से फिर triangles में बाँटा जा सकता है। outcode को पहले से calculate कर लें तो पूरी तरह अंदर या बाहर मौजूद triangles के clipping step को छोड़ा जा सकता हैशोधपत्र: https://dl.acm.org/doi/10.1145/360767.360802
अगर fixed pipeline जैसा रूप बनाए रखें, तो काफ़ी simple drawing functions से भी हैरान कर देने वाली संख्या में triangles process किए जा सकते हैं