Home games How I Implemented Shadows in my Game Engine

How I Implemented Shadows in my Game Engine

by admin



Devlog video about “Homegrown”, a casual farming game I’m creating using my own engine.

Support the channel on Patreon and get access to the code for this game, the city-builder, and Equilinox:
https://www.patreon.com/thinmatrix

My previous game “Equilinox”:
https://store.steampowered.com/app/853550/Equilinox/

You can follow the progress of the game on my social media:
Twitter: https://twitter.com/ThinMatrix
Instagram: https://www.instagram.com/thinmatrix/
Facebook: https://www.facebook.com/thinmatrix
Trello: https://trello.com/b/W3zkIJTM/farm

Email: thinmatrix@gmail.com

Background music by Jamal Green:
https://open.spotify.com/artist/50jTMgIPZrjKFgHbCvoeRt?si=Z-xfUXEXQCuGdGRy7qf9Ug&dl_branch=1

#devlog #Homegrown

source

Related Videos

50 comments

@bitwise4996 March 23, 2024 - 5:46 am

You should really create an editor for your engine. It looks amazing.

@Retsih March 23, 2024 - 5:46 am

why are you working on a game that has an unwilling farming concept , isn't 3rd person and has bad graphics ? don't you think its better forsomeone with your knowledge of coding to make a better game on those aspects ?

@AlexTuduran March 23, 2024 - 5:46 am

You can try shadow cascades to avoid computing the shadow coverage. 4 cascades that blend nicely could work fine.

@Rhodochrone March 23, 2024 - 5:46 am

your computer setup looks wonderful…

@scewps March 23, 2024 - 5:46 am

Late comment, but I can definitely recommend using shadow samplers, they make shadows look noticably smoother without needing more samples. Also I've made good experiences with using a 16 sample poisson disk (I just hardcoded the sampling offsets in the shader and looped through them), and the result looks quite nice

@atomictraveller March 23, 2024 - 5:46 am

ok i just solved stable texels –
my first build had camera translation in the model matrix and my texels "rolled" in the screen. to solve this i removed the translation from the model matrix sent to both shadow and scene shaders and made a new clean (identity) translation matrix then premultiply that with the view matrix, in the right order of course or the rotations in the view matrix go first.
then when cam moves enough, recompute my height field and shift the camera and new height field by an amount quantised with the shadow map resolution and the size of my height field grid 🙂 this makes texels appear to be in a fixed place.

…the other thing i haven't built yet (ate dinner watched video) is.. use the map resolution to determine the decimal portion of the texture read location, then use bilinear interpolation to produce smooth shading without high resolution. use 9 read positions and determine each corner of the square for interpolation by summing the four texels on that corner 🙂

it might be a little less expensive than using bigger filter kernels, definitely more performant than higher resolution maps i expect.

@cvspvr March 23, 2024 - 5:46 am

i've implemented hybrid frustum traced shadows in my game engine, and they work great. you could also try shadows volumes to get rid of a lot of the visual artifacts

@DarkFazy March 23, 2024 - 5:46 am

This video was great, thanks so much. Both relaxing and also explained some ideas behind implementing shadows quite well.

@question_mark March 23, 2024 - 5:46 am

thank u sooooooooo much =)

@JesseNerio March 23, 2024 - 5:46 am

Written in Java?

@Dannnneh March 23, 2024 - 5:46 am

I very much appreciate the technical insight.

@nhuvunguyen4525 March 23, 2024 - 5:46 am

The only bad thing about watching your dev logs is that they inspire me to drop everything and make my own game engine

@chuckmchale March 23, 2024 - 5:46 am

update on this pls 🙂 great work!

@zurrutikGames March 23, 2024 - 5:46 am

Great explanation! Only thing is…why did you say that the depth bias didnt work for you?

The problem is that we need the bias to be as small as possible to reduce the "peter panning" but also big enough to solve the shadow acne problem which means that it'll be this magic number that will be different for every scene, which is a REAL PAIN IN THE NECK, but it should always work right?

The trick to draw the backfaces in the shadowmap works BUT if you later use objects that have no backfaces(e.g. a terrain or a wall that is just a quad) they will not cast shadows…

@irissupercoolsy March 23, 2024 - 5:46 am

1:02 you screens are in front of a window??

@XCSme March 23, 2024 - 5:46 am

It has a RuneScape vibe

@IogaMaster March 23, 2024 - 5:46 am

I'm curious, have you have ever tried kotlin with lwjgl?

@crabking0 March 23, 2024 - 5:46 am

Please update equinox it is one of my favorite games

@henryford8433 March 23, 2024 - 5:46 am

Ok, I am a botanist, a retired plant population biologist, and I and a botanical artist wondered why people are plant blind, and discussed the potentiality of games based on plants, and Albert Durer’s turf, and the immersive Van Gogh exhibition in Bristol. Your ideas are interesting and I might come back for a chat some time. Henry F

@BanditBloodwyn March 23, 2024 - 5:46 am

What video editing tool do you use? Maybe I want to create my own devlogs 🙂

@johnsnow4108 March 23, 2024 - 5:46 am

hey, nice video! are u writing this project on JAVA + GLFW? what are u thinking about library libGDX, that has a cool methods to help with OpenGL, is it better or not?)

@Skeffles March 23, 2024 - 5:46 am

Fascinating to see how you approach shadows!

@user-mx2ss1sn5o March 23, 2024 - 5:46 am

КРАСОТА !!!

@sparklydavid March 23, 2024 - 5:46 am

This give me Sebastian vibe, both channel make me realized how dumb I am, 10/10 would hit that like button.

@valaas13II March 23, 2024 - 5:46 am

Really work

@SpikeStudio March 23, 2024 - 5:46 am

The solutions you come up with are always incredible

@rawanahmed9604 March 23, 2024 - 5:46 am

Lol

@Phvli March 23, 2024 - 5:46 am

Good, concise explanations for the basic concepts! You'll probably want to look into Cascaded Shadow Mapping if your single map resolution is not enough.

@samuilxd March 23, 2024 - 5:46 am

Hello. First of all, you are making your video in a great and instructive way, I wish you success in your new game (I wish I could solve the logic) 🙂

@dragonminz602 March 23, 2024 - 5:46 am

"To technical and boring" Thats why i am here.
Love the technical stuff

@qb1482 March 23, 2024 - 5:46 am

what is equilinox and your new game built in? Also do you care if I mod it?

@silwalanish March 23, 2024 - 5:46 am

Anyone knows how to fix the issue with shadow mapping that @thinmatrix shows at 6:40?
https://youtu.be/uueB2kVvbHo?t=400

@notnullnotvoid March 23, 2024 - 5:46 am

Using bilinear shadow sampling (sampler2DShadow in GLSL with GL_LINEAR sampling and GL_COMPARE_REF_TO_TEXTURE​) is very useful, it allows you to use texture sampler hardware to get rid of the pixelation issue basically for free, while allowing you to significantly reducing the PCF filter size and get rid of the random sampling, or even eliminate the filter entirely if you don't need particularly soft shadows. And it's a trivial change, just a few lines of code. I strongly recommend doing that first, before you spend more time doing further work on shadow calculations, because it informs all the other tweaks and optimizations you might decide to do (or not) in the shaders. Speaking from experience.

@longbowman2306 March 23, 2024 - 5:46 am

HI there!. long time subscriber here, love what you're doing.
i was wondering how you designed you engine, specifically what to put public/protected/private. have your watched/read stuff on project architecture?
or have you just refactored so much that its not quite flexible, at least enough for your usage?
seems like im refactoring a handful of classes constantly 😅
p.s. we need a cooking episode!!!!

@himselfe March 23, 2024 - 5:46 am

No sheep were affected by shadow in the making of this video.

@ThylineTheGay March 23, 2024 - 5:46 am

"technical and boring" is exactly what i like :3

@leoingson March 23, 2024 - 5:46 am

One of your best videos of late! Tech content rules 🙂

@simon-wt March 23, 2024 - 5:46 am

I haven't commented yet but this time I really want to just show you my gratitude for your work. This video is actually one of the most interesting videos I have seen yet! In graduate school I studied a lot of computer graphics theory and I really appreciate watching you implement these techniques and algorithms into your game. I Especially love the commentary you give and it's really nice to have the direct feedback on the game. Thank you Karl for taking us with you on this journey ❤
PS: I only watch around 2-5 videos a month but I won't ever miss on watching any of yours!

@hernestpoissin6964 March 23, 2024 - 5:46 am

Hello ThinMatrix,

I have started game dev with your OpenGL series and have learned a lot with you. Thank you! You do great work.

For your shadow shimmering, what you need to do is to move the shadow camera by fixed increments, which correspond to the projection of the shadow texels on a horizontal plane. This completely removes the issue. Don't hesitate if I can help you back.

@haydenwarrington2951 March 23, 2024 - 5:46 am

Good

@frostfire-1404 March 23, 2024 - 5:46 am

ti (the instrunt into the channel rack) and then it crashes the soft soft… Can soone help please?

@jlebrech March 23, 2024 - 5:46 am

Use color keys for your shadow map.

@kiki-drawer2669 March 23, 2024 - 5:46 am

I don't code or create games at all but the way you talk about your work and explain simply means your videos are never boring no matter what element you focus on! I enjoyed this video just as much as all your others! Ty for working so hard!

@hopperpl March 23, 2024 - 5:46 am

We use an additional approach with 2 shadow maps… the first is the wider scene which creates the pixely shadow map (the one you use), and a second is a narrower map which only contains a small part of the scene using a distance field around the camera (not the light source). Both maps are generated during the same rendering pass, the shaders just use 2 rendering targets. The 2nd map has a much higher resolution, you can think of it like a 2nd zoomed shadow map. During the final render, the shadow is then, based on distance again, sampled from the narrower or the wider map. This generates nice sharp shadows around the "camera" while having only blurry shadows far away, which you wont notice in the distance anyway. As a disclaimer, this will only work with 3rd person or 1st person rendering. For this game with a wide/far camera, the distance value for the narrow shadow map will be too large and the difference in shadow map resolution too small.

@_CJ_ March 23, 2024 - 5:46 am

Well it's devlog so we expect all the "boring" bits of it 🙂 I think all the things you are working on and need some decision of which approach you use are interesting and should be in video. Technical or not it is fun to watch and learn some advanced stuff so you know what is hard and what is some programming trick 🙂 So don't be afraid to mix it up with some more technical episodes than others and with some more life related things (like cooking and exploring nature around). It is great. Thank you for video <3

@OGLDEV March 23, 2024 - 5:46 am

Great video! It's such a coincidence that I've just released a tutorial on how to do PCF and random sampling using the same NVIDIA article. If you don't mind, I'll put the link here in case anyone is interested: https://youtu.be/NCptEJ1Uevg. Thank you 🙂

@codakairos8082 March 23, 2024 - 5:46 am

do you plan on adding ambient occlusion to your engine ? Great video btw 🙂

@igz March 23, 2024 - 5:46 am

Fascinating, and the game is looking great!

@kennywestone12 March 23, 2024 - 5:46 am

The best but I need more ti to make 100% best soft hehehehe thanks for the video bro.

@wyzenmodff4099 March 23, 2024 - 5:46 am

more complicated one. your lody needs to soft nice. 808(app) should also fit with the lody and the rythm. snare and percussion

Comments are closed.