Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (223)
games submitted by our members
Games in WIP (813)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Z-buffer algorithm implementation problem  (Read 1170 times)
0 Members and 1 Guest are viewing this topic.
Offline bestwarrior2001

Senior Newbie


Exp: 3 years



« Posted 2017-04-18 09:42:24 »

Hi guys, now I'm making a 3d game engine in Java from scratch.

Now I'm working at the Z-buffer because I didn't like the effect of the Painter's Algorithm(Z-sorting).

My question is: what is the easiest way to determine the Z value of each pixel? Please include a source code if you can  Wink

P.S.: The renderer contains just triangles, something like that:

class Vector3d {
double x,y,z;
}
class Triangle {
Vector3d v1,v2,v3;
Color color;
}
class Renderer {
public Triangle[] triangles;
[ . . . ]
}
Offline ClickerMonkey

JGO Coder


Medals: 26
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2017-04-18 12:43:59 »

When you transform the 3d points into 2d screen coordinares the z value of the transformed point is the z value you use.

Offline bestwarrior2001

Senior Newbie


Exp: 3 years



« Reply #2 - Posted 2017-04-18 12:48:53 »

When you transform the 3d points into 2d screen coordinares the z value of the transformed point is the z value you use.

1. So don't I need the z value of the points inside the triangle?
EDIT
2. if so, what if the triangle's vertices have different Z values?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 442



« Reply #3 - Posted 2017-04-18 14:33:48 »

You need to do perspective-correct interpolation of the three vertices' Z values for each fragment generated within the triangle.
Offline bestwarrior2001

Senior Newbie


Exp: 3 years



« Reply #4 - Posted 2017-05-22 06:51:55 »

You need to do perspective-correct interpolation of the three vertices' Z values for each fragment generated within the triangle.
OK, I understand a bit.
Can someone give me a function like that:
1  
2  
3  
4  
5  
public void getZ(point3d v1,point3d v2,point3d v3, // triangle vertices
                        point2d screen) // x y coordinates
{
    // [what I need] (calculate the Z value of the pixel located at screen.x X screen.y)
}

PLS answer as soon possible
Offline Archive
« Reply #5 - Posted 2017-05-22 16:00:18 »

Are you familiar with linear interpolation?

Consider the function X1 = X1 + F(X2-X1)
Where X1 is the start value, X2 is the end value, and F is the fraction between 0 and 1.
This fraction represents how far along in between X1 and X2 you want to go. For example,
if F = 0 you'll get X1, if F = 1, you'll get X2, and if F = 0.5, you'll get a value half way in between X1 and X2.

You use linear interpolation to find values along the triangle's edges, and across the triangle's surface. So for your Z problem, you interpolate between the Z values of vertex 1 (X1) and vertex 2 (X2). An F value of 0 will give you the Z value of vertex 1 and an F value of 1 will give you the Z value of vertex 2.

In order to find the F value, you need to find the change of Z over the change in Y. This dz/dy is the F value and is constant for the entire edge of the triangle.

Loop over each of the Y values between vertex 1 and vertex 2, incrementing a counter by F.

1  
2  
3  
4  
5  
6  
7  
float F = (vertex2.z - vertex1.z) / (vertex2.y - vertex1.y);
float interpZ = vertex1.z;
while (vertex1.y < vertex2.y) {
     // interpolate across X using a similar method
     // interpZ is the Z value in between the edges.
     interpZ += F;
}

Offline bestwarrior2001

Senior Newbie


Exp: 3 years



« Reply #6 - Posted 2017-05-22 19:28:13 »

I'VE FINALLY FOUND WHAT I NEED!
Thank you a lot, you made my day!

And your 3d renderer is very cool. It seems that you wanted to remake the OpenGL. Nice work!
Offline Archive
« Reply #7 - Posted 2017-05-22 21:16:00 »

I'VE FINALLY FOUND WHAT I NEED!
Thank you a lot, you made my day!

And your 3d renderer is very cool. It seems that you wanted to remake the OpenGL. Nice work!
Haha yup, no problem. And thanks, you're doing the same stuff I was doing when I started working on my renderer. So keep going! :p

Pages: [1]
  ignore  |  Print  
 
 

 
cybrmynd (117 views)
2017-08-02 12:28:51

cybrmynd (139 views)
2017-08-02 12:19:43

cybrmynd (136 views)
2017-08-02 12:18:09

Sralse (152 views)
2017-07-25 17:13:48

Archive (619 views)
2017-04-27 17:45:51

buddyBro (730 views)
2017-04-05 03:38:00

CopyableCougar4 (1255 views)
2017-03-24 15:39:42

theagentd (1235 views)
2017-03-24 15:32:08

Rule (1213 views)
2017-03-19 12:43:22

Rule (1265 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!