Skip to main content
gamedev.net
🔒 Locked

3d lines...

Started by Vlion Sep 15, 2000 at 1:50 PM 8 replies 300+ views
Original Post
Vlion
Vlion
Or taking a virtual 3d line, and rastreizing it to the screen... This particualr function is giving me errors.... could ya''ll take a gander ? FYI, i`m using QC to do work out this algorithim.
void line3D(short x1, short y1, short z1, short x2, short y2, short z2)
{
    short x,y;//final vars...
    
    x = abs(640 * x1 / z1);//this is a formula i dug up to use
    y = abs(640 * y1 / z1);//i added the abs...
    _moveto(x,y);          //sets the start point
    
    x = 640 * x2 / z2;//end point
    y = 640 * y2 / z2;//end point
    _lineto(x,y);     //draws the line/
}
 
I came, I saw, I got programmers block. ~V''lion
~V'lionBugle4d
Rataploum
Rataploum
Try using 480 instead of 640 for the y coordinate..
Meduzza
Meduzza
I dont think your formula will work.
You probably mean classic projection formula that goes like this:

xScreen = ( x3D / z3D * focus ) + screenWidthHalf;
yScreen = ( y3D / z3D * focus ) + screenHeightHalf;

Where "focus" is some constant (try 320).

----
Salut!
Meduzza
Vlion
Vlion
Hm....okay...
*tests it*
Doesnt work.
Uhm, you said ''classic projection formula''
There`s others ?
Where can I find them ?
~V''lion

I came, I saw, I got programmers block.
~V''lion
~V'lionBugle4d
Meduzza
Meduzza
Dude, it is working, i use it all the time.
Did you forget to convert your 3D vector into camera space?
Vlion
Vlion
Dont mean to rain on your parade or anything, but heres the program. I dont have a camera, 3d vector or anything.
I simply have straight non-page-flipping code, horribly simple....
Heres my program in the entirty, with the unused function edited out.

#include
#include
void setup(){_setvideomode(_MAXRESMODE);}
void line3D(short x1, short y1, short z1, short x2, short y2, short z2)
{
short x,y;
//x = abs(640 * x1 / z1);
//y = abs(480 * y1 / z1);
x = ( x1 / z1 * 320 ) + 480/2;
y = ( y1 / z1 * 320) + 640/2;

_moveto(x,y);
x = 640 * x2 / z2;
y = 640 * y2 / z2;
_lineto(x,y);
}
void RETURN()
{
getch();
_setvideomode(_DEFAULTMODE);
}
void main()
{
int i;
setup();
for(i=1;i<=360;i++)
{
line3D(100,200,i,100,100,i);
}
RETURN();
}


I came, I saw, I got programmers block.
~V''lion
~V'lionBugle4d
Heraldin
Heraldin
OK. I''ll explain what you should be doing.

To get perspective, multiply both x and y by d/z, where d is the distance of your eye (or camera) from the monitor. In your original code, this was 640, which was great, but it needed conversion to screen space. Looking at the new code, you''re using different algorithms for each end of the line! Point 1 is ok (with d=320), but point 2 is using the old code. Try writing a generic ''3D-point to 2D screen pixel'' function and calling it for both ends of the line; you can use:

x = ( x1 / z1 * 320 ) + 480/2;
y = ( y1 / z1 * 320) + 640/2;

(which I stole from your code above ;-) )

You''re almost there!
Vlion
Vlion
Ecellent. Everyone talks about working with virtual lines in memory, but the final 3d to 2d transformation is not near as commonly covered I`ve found.

I came, I saw, I got programmers block.
~V''lion
~V'lionBugle4d
Meduzza
Meduzza
OK, first of all:

x = ( x1 / z1 * 320 ) + 640/2;
y = ( y1 / z1 * 320 ) + 480/2;

You have 640 and 480 swapped...
Second of all, your coordinates must be in camera space.
For example, (0,0,100) - Right in front of camera.
Vlion
Vlion
Like this ?
Ive tried it and it works a TON better than my previous algorithims did, but Im not sure what are acceptable co-ords for the camera space.
Can you give me ideas ?
Also...
line3d(0,0,100,50,0,100);
That just gives me a pixel.
line3d(0,0,100,50,50,100);
This too simply outputs a pixel...
Why ?

void line3D(short x1, short y1, short z1, short x2, short y2, short z2)
{
short x,y;
x=(x1 / z1 * 640) + 640/2;
y=(y1 / z1 * 640) + 480/2;
_moveto(x,y);
x=(x2 / z2 * 640) + 640/2;
y=(y2 / z2 * 640) + 480/2;
_lineto(x,y);
}


I came, I saw, I got programmers block.
~V''lion
~V'lionBugle4d

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.