rendering simple 3d planes (with clipping)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
mk8
Prole
Posts: 35
Joined: Thu Apr 22, 2021 7:11 am

rendering simple 3d planes (with clipping)

Post by mk8 »

so i've given up after 2 hours of googling and figured i'll just ask here
what i would like is to just render multiple rectangular planes (possibly rotated), that clip correctly
i've looked everywhere, but i can't seem to find a good tutorial/demo and stealing code from other ppl's 3d libraries just isn't my thing
any help will be appreciated, if you could just point me towards how to check where a 3d plane and a line intersect, even that would be helpful

thanc in advanc
hippity hoppity ur code is my property (thanc in advanc)
mk8
Prole
Posts: 35
Joined: Thu Apr 22, 2021 7:11 am

Re: rendering simple 3d planes (with clipping)

Post by mk8 »

so i finally found the line vs plane intersection code:

Code: Select all

--lx, ly, lz: 'starting point' of the line
--lnx, lny, lnz: the direction vector of the line
--px, py, pz: the 'center' of the plane
--pnx, pny, pnz: the normal vector of the plane (perpendicular)
function castPlane(lx,ly,lz, lnx,lny,lnz, px,py,pz, pnx,pny,pnz)
    local dx,dy,dz = lx-px,ly-py,lz-pz
    --if the dot product of the normals is 0, it means they are perpendicular and thus the line never intersects the plane
    if (lnx*pnx+lny*pny+lnz*pnz)==0 then return false end
    local dist = -(dx*pnx+dy*pny+dz*pnz)/(lnx*pnx+lny*pny+lnz*pnz)
    local rx,ry,rz = lx+dist*lnx,ly+dist*lny,lz+dist*lnz
    return true,rx,ry,rz,dist
end
--local doesIntersect,px,py,pz,distance = castPlane(...)
(did a bit of translating so might have a typo) [got this from rosettacode its there in like 40 other languages as well lol]
note: the line extends to both directions, so checking if dist is > 0 or smth is needed for raycasts

tho calling this 480*270 times a second for every plane in the game might not be very efficient...

so the question still remains, help greatly appreciated!
hippity hoppity ur code is my property (thanc in advanc)
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: rendering simple 3d planes (with clipping)

Post by pgimeno »

Depth buffers were invented for a reason :)

Yes, it's crazy to calculate an intersection per pixel, or more than once per pixel if you have more than one plane. The usual solution to this is a depth buffer. It consists of an array of numbers with the same size as the screen, in addition to the surface where you render the result.

The idea is that the array contains the depth of each point; it is initialized to all infinite before rendering anything, because the depth of vacuum is infinite :) Now you start rendering a plane, and for each pixel that the plane touches, you check its depth (normally the Z coordinate of the camera coordinates, which is why it's also called a Z buffer). If it's less than what is already in the buffer, it means it's closer to the camera than what was previously drawn, therefore it's visible and you draw it, and store the new depth in the buffer; otherwise you skip it because it's behind something else.

Even with that trick, things could get pretty calculation-intensive; it has been used in assembler-coded demos in the 80's, with little geometry, but in Lua it's not a good idea even with nowadays' processing power.

Enter OpenGL. Graphics cards are designed for doing that kind of tasks; they do it quickly and efficiently. To use a depth buffer, the easiest way is to activate it in conf.lua, but you also need to have your 3D geometry passed to OpenGL, for which you need to have it as meshes, using custom vertex data with 3 dimensions instead of Löve's default of 2, and you also need a custom shader.
mk8
Prole
Posts: 35
Joined: Thu Apr 22, 2021 7:11 am

Re: rendering simple 3d planes (with clipping)

Post by mk8 »

thanks! i'm going to take a look
pgimeno wrote: Wed Mar 30, 2022 7:53 am Enter OpenGL. Graphics cards are designed for doing that kind of tasks; they do it quickly and efficiently. To use a depth buffer, the easiest way is to activate it in conf.lua, but you also need to have your 3D geometry passed to OpenGL, for which you need to have it as meshes, using custom vertex data with 3 dimensions instead of Löve's default of 2, and you also need a custom shader.
i don't suppose there is a good tutorial anywhere, is there? i don't wanna bother, but could you just give a quick example of how to do this? i have looked at meshes before, but i didn't quite understand the attributes/vertex data part. thx
hippity hoppity ur code is my property (thanc in advanc)
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: rendering simple 3d planes (with clipping)

Post by pgimeno »

mk8 wrote: Wed Mar 30, 2022 1:38 pmi don't suppose there is a good tutorial anywhere, is there? i don't wanna bother, but could you just give a quick example of how to do this? i have looked at meshes before, but i didn't quite understand the attributes/vertex data part. thx
I don't know about tutorials, but I made a simple demo where the code tries to show the principles involved:

viewtopic.php?p=219345#p219345
mk8
Prole
Posts: 35
Joined: Thu Apr 22, 2021 7:11 am

Re: rendering simple 3d planes (with clipping)

Post by mk8 »

pgimeno wrote: Sat Apr 02, 2022 9:28 pm
mk8 wrote: Wed Mar 30, 2022 1:38 pmi don't suppose there is a good tutorial anywhere, is there? i don't wanna bother, but could you just give a quick example of how to do this? i have looked at meshes before, but i didn't quite understand the attributes/vertex data part. thx
I don't know about tutorials, but I made a simple demo where the code tries to show the principles involved:

viewtopic.php?p=219345#p219345
sorry about late reply and thanks alot! i know how basic sorting works, so i should be at least able to implement it into your code. the question is, how about performance? would it run with at least a reasonable framerate (~40 FPS) on an average computer? i'm sure mine would handle it, since it's pretty strong, but i have no way to test it out on an average one.

the reason i'm stubbornly holding on to this and keep asking questions is that i really want to use Lua as the language for my game, because it makes it very easily user-customizable, and i really want to use a framework, not an engine (i have my reasons, i know about Godot and Defold), and there are only 2 3D Lua frameworks i know about: LOVR, which is really not good for desktop apps (or so i've heard) and Urho3D, which i just didn't like at all (yes i just judged a book by its cover what u gonna do about it), and LOVE is the closest to my requirements, its cross-platform and is capable of 3D

even if there are 3D Lua frameworks i don't know about, i'd still prefer to use LOVE, if the performance was good. if it's not, anyone who knows about a good 3D Lua framework, i'd love to hear

thanks again
hippity hoppity ur code is my property (thanc in advanc)
User avatar
GVovkiv
Party member
Posts: 686
Joined: Fri Jan 15, 2021 7:29 am

Re: rendering simple 3d planes (with clipping)

Post by GVovkiv »

sorry about late reply and thanks alot! i know how basic sorting works, so i should be at least able to implement it into your code. the question is, how about performance? would it run with at least a reasonable framerate (~40 FPS) on an average computer? i'm sure mine would handle it, since it's pretty strong, but i have no way to test it out on an average one.
You can later, when you done code, post it here, since members here have various pc configurations, so we will tell you about fps
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: rendering simple 3d planes (with clipping)

Post by zorg »

mk8 wrote: Sun Apr 03, 2022 5:44 pm ...there are only 2 3D Lua frameworks i know about: LOVR, which is really not good for desktop apps (or so i've heard) and Urho3D, which i just didn't like at all (yes i just judged a book by its cover what u gonna do about it), and LOVE is the closest to my requirements, its cross-platform and is capable of 3D.

even if there are 3D Lua frameworks i don't know about, i'd still prefer to use LOVE, if the performance was good. if it's not, anyone who knows about a good 3D Lua framework, i'd love to hear
Löve *is* a 3D framework, if you consider the bar for that being able to make 3D games with proper 3D accelerated graphics; the "love2d" this is just the domain name, after all.

It uses OpenGL (with vulkan/metal possibly coming in the future) so whatever you implement in shaders will be as performant as what you'd be able to do elsewhere by hand.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
mk8
Prole
Posts: 35
Joined: Thu Apr 22, 2021 7:11 am

Re: rendering simple 3d planes (with clipping)

Post by mk8 »

zorg wrote: Mon Apr 04, 2022 3:19 am
mk8 wrote: Sun Apr 03, 2022 5:44 pm ...there are only 2 3D Lua frameworks i know about: LOVR, which is really not good for desktop apps (or so i've heard) and Urho3D, which i just didn't like at all (yes i just judged a book by its cover what u gonna do about it), and LOVE is the closest to my requirements, its cross-platform and is capable of 3D.

even if there are 3D Lua frameworks i don't know about, i'd still prefer to use LOVE, if the performance was good. if it's not, anyone who knows about a good 3D Lua framework, i'd love to hear
Löve *is* a 3D framework, if you consider the bar for that being able to make 3D games with proper 3D accelerated graphics; the "love2d" this is just the domain name, after all.

It uses OpenGL (with vulkan/metal possibly coming in the future) so whatever you implement in shaders will be as performant as what you'd be able to do elsewhere by hand.
ok, so it is possible! thanks!

i have a couple more questions about pgimeno's example (the link he posted above):
1. would

Code: Select all

love.graphics.setDepthMode("lequal", true)
do the sorting, or do i need to mess with the canvas' pixel format or am i completely off with this?
2. i would obviously like to have more than just 1 texture in the game. would it be faster to create a new 'mesh' of pgimeno's example sort for every texture, or create a single 'mesh' with a spritesheet texture, and set different faces to different uv values to match the desired tile
(how the example works: it creates a single mesh with the "triangles" mode and you can just add new rhomboid faces to it, which look like independent meshes but are all part of the single big mesh)
3. how expensive is mesh:setVertex() and mesh:setVertices()? could i call this function for every moving object's face each frame? (there's not gonna be that many moving objects, but each will probably have more than just a few faces)
4. and lastly another question about the example code: how many faces would a regular PC be ok with? i know it depends on how heavy the rest of my game is, but could i just get an approximation? in the example the limit is set to 3000 vertices, which means 750 faces, but i would obviously like to get as close to the actual limit as possible. i'm even thinking of using "stream" mode for the mesh and resetting the vertices on every frame, which would basically allow me to do love.graphics.draw in 3d, but that would surely be more expensive. thoughts?

i'm sorry if i'm bothering thanks for your time and have a great day!
hippity hoppity ur code is my property (thanc in advanc)
mk8
Prole
Posts: 35
Joined: Thu Apr 22, 2021 7:11 am

Re: rendering simple 3d planes (with clipping)

Post by mk8 »

bump, can someone pls answer? i am sorry, not experienced with this graphics stuff, but i would really like to learn. thanks!
hippity hoppity ur code is my property (thanc in advanc)
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests