Friday 15 March 2013

java - Z-buffering algorithm not drawing 100% correctly -



java - Z-buffering algorithm not drawing 100% correctly -

i'm programming software renderer in java, , trying utilize z-buffering depth calculation of each pixel. however, appears work inconsistently. example, utah teapot illustration model, handle draw perhaps half depending on how rotate it.

my z-buffer algorithm:

for(int = 0; < m_triangles.size(); i++) { if(triangleisbackfacing(m_triangles.get(i))) continue; //backface culling for(int y = miny(m_triangles.get(i)); y < maxy(m_triangles.get(i)); y++) { if((y + getheight()/2 < 0) || (y + getheight()/2 >= getheight())) continue; //getheight/2 , getwidth/2 moving model centre of screen for(int x = minx(m_triangles.get(i)); x < maxx(m_triangles.get(i)); x++) { if((x + getwidth()/2 < 0) || (x + getwidth()/2 >= getwidth())) continue; rayorigin = new point2d(x, y); if(pointwithintriangle(m_triangles.get(i), rayorigin)) { zdepth = zvalueofpoint(m_triangles.get(i), rayorigin); if(zdepth > zbuffer[x + getwidth()/2][y + getheight()/2]) { zbuffer[x + getwidth()/2][y + getheight()/2] = zdepth; colour[x + getwidth()/2][y + getheight()/2] = m_triangles.get(i).getcolour(); g2.setcolor(m_triangles.get(i).getcolour()); drawdot(g2, rayorigin); } } } } }

method calculating z value of point, given triangle , ray origin:

private double zvalueofpoint(triangle triangle, point2d rayorigin) { vector3d surfacenormal = getnormal(triangle); double = surfacenormal.x; double b = surfacenormal.y; double c = surfacenormal.z; double d = -(a * triangle.getv1().x + b * triangle.getv1().y + c * triangle.getv1().z); double rayz = -(a * rayorigin.x + b * rayorigin.y + d) / c; homecoming rayz; }

method calculating if ray origin within projected triangle:

private boolean pointwithintriangle(triangle triangle, point2d rayorigin) { vector2d v0 = new vector2d(triangle.getv3().projectpoint(modelviewer), triangle.getv1().projectpoint(modelviewer)); vector2d v1 = new vector2d(triangle.getv2().projectpoint(modelviewer), triangle.getv1().projectpoint(modelviewer)); vector2d v2 = new vector2d(rayorigin, triangle.getv1().projectpoint(modelviewer)); double d00 = v0.dotproduct(v0); double d01 = v0.dotproduct(v1); double d02 = v0.dotproduct(v2); double d11 = v1.dotproduct(v1); double d12 = v1.dotproduct(v2); double invdenom = 1.0 / (d00 * d11 - d01 * d01); double u = (d11 * d02 - d01 * d12) * invdenom; double v = (d00 * d12 - d01 * d02) * invdenom; // check if point in triangle if((u >= 0) && (v >= 0) && ((u + v) <= 1)) { homecoming true; } homecoming false; }

method calculating surface normal of triangle:

private vector3d getnormal(triangle triangle) { vector3d v1 = new vector3d(triangle.getv1(), triangle.getv2()); vector3d v2 = new vector3d(triangle.getv3(), triangle.getv2()); homecoming v1.crossproduct(v2); }

example of incorrectly drawn teapot:

what doing wrong? sense must little thing. given triangles draw @ all, uncertainty it's pointwithintriangle method. backface culling appears work correctly, uncertainty it's that. culprit me zvalueofpoint method, don't know plenty know what's wrong it.

this must slow

so much redundant computations per iteration/pixel iterate coordinates you should compute 3 projected vertexes , iterate between them instead look here: triangle/convex polygon rasterization

i dislike zvalueofpoint function

can not find utilize of x,y coordinates main loops in it so how can compute z value correctly ? or computes average z value per whole triangle ? or missing something? (not java coder myself) in anyway seems main problem if z-value wrongly computed z-buffer can not work properly to test @ depth buffer image after rendering if not shaded teapot incoherent or constant mess instead clear ...

z buffer implementation

it looks ok

[hints]

you have much times terms x + getwidth()/2 why not compute them 1 time variable? i know modern compilers should anyway code more readable , shorter... at to the lowest degree me

java algorithm graphics 3d zbuffer

No comments:

Post a Comment