English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 23 July 2020, 17:10   #1
JoeJoe
Registered User
 
Join Date: Feb 2020
Location: Germany
Posts: 178
What is the easiest way to calculate hidden faces of an cube

Hello,


What is the easiest or fastest way to calculate which faces are covered by a cube.
If the cube rotates only around one axis, you can determine this simply by the angle, for example a>180° face 1,2,3,4 are not visible.
How does the whole thing look like if you rotate the cube on several axes. As far as I understood you have to determine the vector of the face. Then you have to calculate the angle of the vector to the observer. If this angle is greater than 90° the face is not visible. But this kind of calculation correct?

This is how my cube looks like until now.


Thanks
JoeJoe
Attached Thumbnails
Click image for larger version

Name:	Cube.png
Views:	148
Size:	6.7 KB
ID:	68264  
JoeJoe is offline  
Old 23 July 2020, 18:30   #2
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,216
Quote:
Originally Posted by JoeJoe View Post
How does the whole thing look like if you rotate the cube on several axes. As far as I understood you have to determine the vector of the face. Then you have to calculate the angle of the vector to the observer. If this angle is greater than 90° the face is not visible.
If you mean "hidden" instead of "covered", then this is correct. Compute the angle between the surface normal vector and the vector that points into the Z direction, i.e. the observer direction. Since that is just given by the arccos of the scalar product of both vectors, it is sufficient to look at the Z coordinate of the surface vector.


Depending on how you normalize your view vector, it is thus sufficient to check whether this coordinate is positive (pointing towards the observer) or negative (pointing into the opposide direction).


However, this works only if the surface has no holes. In general, you sort the surfaces from front to back, i.e. by the Z coordinate of its center, and then draw them from back to front. This always works.
Thomas Richter is offline  
Old 23 July 2020, 22:00   #3
DanScott
Lemon. / Core Design
 
DanScott's Avatar
 
Join Date: Mar 2016
Location: Tier 5
Posts: 1,212
https://www.cubic.org/docs/backcull.htm
DanScott is offline  
Old 24 July 2020, 08:03   #4
JoeJoe
Registered User
 
Join Date: Feb 2020
Location: Germany
Posts: 178
Thanks for the information. I will implement it and then report when i have success.
JoeJoe is offline  
Old 24 July 2020, 09:33   #5
deimos
It's coming back!
 
deimos's Avatar
 
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
If you plan on more complex objects than cubes (i.e. anything not a single purely convex object) then you may want to look at Binary Space Partitioning (BSP) trees next. This uses the surface normal work that you're about to do, but also gives you the order in which to draw the surfaces, rather than just using the painters algorithm (drawing from back to front) which only works in simple cases.

Last edited by deimos; 24 July 2020 at 09:42.
deimos is offline  
Old 24 July 2020, 13:58   #6
JoeJoe
Registered User
 
Join Date: Feb 2020
Location: Germany
Posts: 178
It´s work now
Attached Thumbnails
Click image for larger version

Name:	cubev2.png
Views:	132
Size:	8.3 KB
ID:	68271  
JoeJoe is offline  
Old 26 July 2020, 09:27   #7
Antiriad_UK
OCS forever!
 
Antiriad_UK's Avatar
 
Join Date: Mar 2019
Location: Birmingham, UK
Posts: 418
JoeJoe, which method did you end up using? My code has always used the 2D way but I recently added multi-point light sources so had to dig into all the normal/dot product math. No matter which way I tried to code it the 2D method was always faster for me.

My notes from the time were concerned with doing visibility check, followed by calculating normal for the visible face for lighting.

Code:
Rotate a point 3-axis: 			9 muls
Working out surface normal:	 	6 muls
Working out normal magnitude:		3 muls, 1 sqrt
Normalizing (dont have magnitude): 	3 muls, 1 sqrt, 3 divs
Normalizing (already have magnitude): 	3 divs
Working out dot product of unit vectors	3 muls
Face visibility check/2d method:	2 muls
Face visibility check/3d method:	9 muls (surface non-unit normal + dot product (sign only))

muls = 70 cycles
divs = 158
sqrt = 500

2D vs 3D Face Visibility
========================
As in all methods we end up doing the surface normal for visible faces is it quicker
to do the 2D check first and then only do the surface normal? Again assuming 20 faces, with
8 being visible on average.

Face 2D:
2d visibility check (20 faces)				2 * 20 muls
get surface normal for lighting visible faces (8 faces)		6 * 8 muls
							88 muls, 6160? cycles

Face 3D:
get surface normal (20 faces)				6 * 20 muls
3d visibility check,dot product with first vertex (20 faces)		3 * 20 muls
Get surface normal for lighting visible faces FREE
						180 muls, 12600? cycles
No contest. Use 2D method.
Another method I've seen is to just calculate the surface normal before you start drawing and rotate that new point along with all the other vertices but that's 9 muls per vertex. Looking at my notes in hindsight I've not included the perspective function in those calcs (2 shifts + 2 divs or 2 shifts + 1 div + 2 muls) - but can't off the top of my head think of how to skip that in the 3d method for only doing it on visible faces. In the end I calculated the surface normal magnitude once (or every time the object changed) and stored that with the face. Then doing 2d visibility check followed by lighting using pre-stored normal magnitude to remove sqrt. Be good if any of you vector geniuses can says how to do it better

Last edited by Antiriad_UK; 26 July 2020 at 09:58.
Antiriad_UK is offline  
Old 13 August 2020, 11:35   #8
JoeJoe
Registered User
 
Join Date: Feb 2020
Location: Germany
Posts: 178
Sorry for the delay. I use the "old 2D" way from post 3 to do this.
JoeJoe is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to calculate possible blit times? Tigerskunk Coders. Asm / Hardware 32 11 January 2022 08:24
Games with hidden/un-hidden symbolism? markpjd Retrogaming General Discussion 8 06 December 2017 16:56
Demo name? - Talking fish/human faces in tank & rotating Coke can Higgy request.Demos 0 08 May 2017 09:54
Calculate a color gradient. AGS Coders. Asm / Hardware 13 11 February 2015 11:20
Faces which defined the Amiga Tolls Nostalgia & memories 25 02 June 2011 22:02

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 18:08.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.07377 seconds with 14 queries