# is this linear?

This topic is 2075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Could maybe someone help me in answering simple question?

I do raytracing of the simple quad - this is i send the rays

(my screen is 38 cm width, 30 cm height, 100 cm far and

those are starting coordinates of my ray, (it is from (-19, -15, +100) left up

to (+19 ,+15, +100) right down,)

this rays meets the quad, I wonder if the edges of this quad are perfectly linear

with this approach, or are they not 100% linear?

i couldnt answet this

when i raytrace spheres they are sometimes dostorted here, not round

(distortion depends on the window width and height / z-distance, for small

z distance it is much visible like here

[attachment=19843:balls.jpg]

so for spheres for sure this is distorted, quads are also distorted but

i dont know if the edges stays perfec linear here or not?

##### Share on other sites

Yes, you are using a projective mapping, which maps lines into lines (and conic sections into conic sections, so the oblong contours of your spheres after projection are actually ellipses).

Edited by Álvaro

##### Share on other sites

Yes, you are using a projective mapping, which maps lines into lines (and conic sections into conic sections, so the oblong contours of your spheres after projection are actually ellipses).

much tnx for answer, so depth buffer values i can also interpolate in linear?

Has you or maybe some other person give the formula of the radius for the outcome 2d elipsis (made from x,y,z,R ball)? I am a bit shocked becouse previously i wass assuming im my rasterizer that ball transforms to circle only the radius was shrinked? You say that with triangles i should no worry at all, only transform vertices?

tnx much for info

##### Share on other sites

No, you can't interpolate depth-buffer values linearly. Lines map to lines, but distances are not preserved (so the middle point of a segment will not generally map to the middle point of the projection of the segment). I believe for depth values you can lineally interpolate 1/z.

You can mark the points in the sphere where the ray to the camera is tangential to the sphere. They form a circle. Since a circle is a conic section, its projection will also be a conic section. I am not sure what you intend to do with the "radius" you are asking about, and I am not even sure what the radius of an ellipse is. Note you could also get a parabola or a piece of a hyperbola, depending on whether the sphere has points with z=0 or points with z<0.

Triangles project to triangles as long as all three vertices have z>0.

##### Share on other sites

No, you can't interpolate depth-buffer values linearly. Lines map to lines, but distances are not preserved (so the middle point of a segment will not generally map to the middle point of the projection of the segment). I believe for depth values you can lineally interpolate 1/z.

You can mark the points in the sphere where the ray to the camera is tangential to the sphere. They form a circle. Since a circle is a conic section, its projection will also be a conic section. I am not sure what you intend to do with the "radius" you are asking about, and I am not even sure what the radius of an ellipse is. Note you could also get a parabola or a piece of a hyperbola, depending on whether the sphere has points with z=0 or points with z<0.

Triangles project to triangles as long as all three vertices have z>0.

allright,tnx,  i didnt understand the thing with a sphere, now it seem to be a little hard to write  rsaterization routine for ball :C though i will be neded that - could someone maybe help with that yet more ? (proper ball software rasterization routine with its eliptic distrtion and proper depth)

same with triangle though triangle probably i get now if the only change is to do interpolation of 1/z instead z

also question do opengl make it corecctly (with 1/z depth interpolation for triangles) ?

##### Share on other sites

I am sure OpenGL does the right thing for all quantities being interpolated.

I have never tried to rasterize a sphere before. Traditionally you divide the sphere up into triangles and rasterize those.

##### Share on other sites

I have never tried to rasterize a sphere before. Traditionally you divide the sphere up into triangles and rasterize those.

probably this would be very slow compared to this approach i used

(i did a bressenham in xy then scanline-bressenham in yz to write

to depth - it works very fast i think but is not distortion correct and make it distortion correct would be probably a bit hard (?), im not sure, maybe will try it later but need some time to rethink it)

##### Share on other sites

Well, you could compute the leftmost and rightmost tangent to the sphere contained in the plane determined by the camera and the scanline. You might be able to reduce that to a Bressenham-style algorithm, but even if you have to take one square root per scanline it should be fast enough.

##### Share on other sites

Well, you could compute the leftmost and rightmost tangent to the sphere contained in the plane determined by the camera and the scanline. You might be able to reduce that to a Bressenham-style algorithm, but even if you have to take one square root per scanline it should be fast enough.

my code for 'rasterizing ball ' probably was like here (i got mess in old sources but probably this was that), this is maybe naive c code but I am not able to go much better - it is bressenham X bressenham of my own idea, as far as i remember it worked ok


//xs means xstart , xk - xend here, CLIENT_X, CLIENT_Y is pixelport size

void drawBallScanlineWithDepth(int y, int xs, int xk, int z, unsigned color)
{
if(y<0)         return;
if(y>=CLIENT_Y) return;

if(xk==xs)
{
SetPixelWithDepthUnsafe(xs, y, z, color);
return;
}

if(xs>xk) { int temp = xs; xs=xk; xk=temp; }
if(xs<0)  { if(xk<0) return;  xs=0;  }
if(xk>=CLIENT_X)  { if(xs>=CLIENT_X) return; xk = CLIENT_X-1;  }

int x = (xs+xk)/2;

int x_  = 0;
int d1 = 3 - (2 * radius);

do
{
if (d1 < 0)      d1 += (x_<<2) + 6;
else             d1 += ((x_-(y_--))<<2) + 10;
SetPixelWithDepthUnsafe(x+x_, y, z-y_, color);
SetPixelWithDepthUnsafe(x-x_, y, z-y_, color);
SetPixelWithDepthUnsafe(x+y_, y, z-x_, color);
SetPixelWithDepthUnsafe(x-y_, y, z-x_, color);
x_++;
}
while(x_<y_);

}

void DrawBallWithDepth(int x1, int y1, float z, int radius, unsigned color)
{

if(radius==0) { SetPixelWithDepth(x1,y1, z, color); return; }
if(radius==1) { SetPixelWithDepth(x1,y1, z, color); return; }

if(!_rectanglesOverlap(0, 0, CLIENT_X, CLIENT_Y,
left, up, right, down ) )
return;

int x  = 0;
int d1 = 3 - (2 * radius);

do
{
if (d1 < 0)      d1 += (x<<2) + 6;
else             d1 += ((x-(y--))<<2) + 10;

drawBallScanlineWithDepth(y1+y, x1-x, x1+x, z, color);
drawBallScanlineWithDepth(y1+x, x1-y, x1+y, z, color);
drawBallScanlineWithDepth(y1-x, x1-y, x1+y, z, color);
drawBallScanlineWithDepth(y1-y, x1-x, x1+x, z, color);

x++;
}
while(x<y);

// XXX
// XXXXX
// XXXXXX
// XXXXXXX
// XXXXXXXX
// XXXXXXXX
// XXXXXXXXX
// XXXXXXXXX
// XXXXXXXXX

// 2??
// 4????
// 5?????
// 6??????
// 7???????
// 7???????
// 8????????
// 8????????
// 888776542

}


would be modyfing this to raytracing compatible state (with correct elipsoid distortion and correct depth writing/testing) possible or easy?

I do not understand this thing with tangent

##### Share on other sites

I do not understand this thing with tangent

Here's how you can rasterize a sphere:

* Among the lines that pass through the camera and are contained in a plane that contains the center of the sphere and the vertical direction, select the two that are tangent to the sphere. Use them to compute the top row and the bottom row of the rasterization.

* For each row between those two:

* Among the horizontal lines that pass through the camera and are contained in a plane that contains the row we are rasterizing, select the two that are tangent to the sphere. Use them to compute the left end and the right end of the scanline.

The steps of computing the tangent lines can be reduced to solving a quadratic equation, so you'll have to use a square root. Unless you can come up with a Bressenham-style algorithm, which is perhaps possible.

I haven't thought about how to compute the depth of each pixel. Computing a square root might be unavoidable, but you can always come up with some approximation that is good enough for this purpose.

Edited by Álvaro

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 18
• 15
• 10
• 9