flat assembler
Message board for the users of flat assembler.

Index > Projects and Ideas > How fill 3D triangle using XYZ ?

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 2132
Roman 23 Aug 2026, 08:09
I found this code , but 2D coords XY.
Code:
float signed_triangle_area(int ax, int ay, int bx, int by, int cx, int cy) {
    return 0.5*((by-ay)*(bx+ax) + (cy-by)*(cx+bx) + (ay-cy)*(ax+cx));
}

void triangle(int ax, int ay, int bx, int by, int cx, int cy,  TGAColor color) {
    int bbminx = min(min(ax, bx), cx); // bounding box for the triangle
    int bbminy = min(min(ay, by), cy); // defined by its top left and bottom right corners
    int bbmaxx = max(max(ax, bx), cx);
    int bbmaxy = max(max(ay, by), cy);
    float total_area = signed_triangle_area(ax, ay, bx, by, cx, cy);

    for (int x=bbminx; x<=bbmaxx; x++) {
        for (int y=bbminy; y<=bbmaxy; y++) {
            float alpha = signed_triangle_area(x, y, bx, by, cx, cy) / total_area;
            float beta  = signed_triangle_area(x, y, cx, cy, ax, ay) / total_area;
            float gamma = signed_triangle_area(x, y, ax, ay, bx, by) / total_area;
            if (alpha<0 || beta<0 || gamma<0) continue; // negative barycentric coordinate => the pixel is outside the triangle
            PutPixel(x, y, color);
        }
    }
}               
    


How write this code for XYZ ?
I want fill Triangle in 3D space , like 3D voxelization.
Post 23 Aug 2026, 08:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21107
Location: In your JS exploiting you and your system
revolution 23 Aug 2026, 08:21
The screen is 2D so the 3D XYZ position is converted/projected to 2D first.

1. Project XYZ to the 2D view rectangle to give xy (no z).
2. Fill xy triangle.
Post 23 Aug 2026, 08:21
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21107
Location: In your JS exploiting you and your system
revolution 23 Aug 2026, 08:25
BTW: It depends upon which kind of projection is needed, but a common projection method is simply to divide each of X and Y by Z.

x = X/Z
y = Y/Z

This gives the normal "vanishing point" projection.

There other types of projection also, these can all be tried to see which is best.
Post 23 Aug 2026, 08:25
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 2132
Roman 23 Aug 2026, 10:56
Quote:
x = X/Z
y = Y/Z

No.
I want put in my 3d world game, boxes to shape as 3d triangle.
For this reason i needed xyz coords.


Description:
Filesize: 83.22 KB
Viewed: 563 Time(s)

bandicam_2026-03-15_13-13-34-090.jpg


Post 23 Aug 2026, 10:56
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 2132
Roman 23 Aug 2026, 14:44
I do this. 3D Boxes draw shape triangle using coords xyz.


Description:
Filesize: 74.81 KB
Viewed: 546 Time(s)

bandicam 2026-08-23 17-36-49-938.jpg




Last edited by Roman on 26 Aug 2026, 13:06; edited 2 times in total
Post 23 Aug 2026, 14:44
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 2132
Roman 26 Aug 2026, 12:46
I am done voxelization 3D triangle.

t1 dd 0 ,100f ,0f ;triangle vertices
t2 dd 200f ,0 ,0
t3 dd 0f ,0 ,200f


Description:
Filesize: 177.41 KB
Viewed: 443 Time(s)

bandicam 2026-08-26 15-39-50-756.jpg


Post 26 Aug 2026, 12:46
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1151
Location: Russian Federation
comrade 01 Sep 2026, 01:18
It can't be too dissimilar from rasterizing a 3D -> 2D triangle:

1. Project 3D vertex coordinates to 2D space (divide by Z for perspective projection, standard stuff).
2. Then, project the 2D coordinates to the vertical line to understand where the top, bottom, and "middle" point of the triangle is.
3. Iterate scanline by scanline - but instead of drawing pixels, you generate a voxel.
4. Reduce fidelity by making large steps across the scanline, and when walking the vertcal line.
Post 01 Sep 2026, 01:18
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 814
Location: Massachusetts, USA
bitshifter 04 Sep 2026, 06:53
The first code you posted is 1 of 3 popular rendering methods.
Its hard to make it fast even with SIMD instructions.
Madis and Nick did research on this 10+ years ago...
Here is example of bbox triangles (mostly used in HW not SW)
https://github.com/ssloy/tinyrenderer

The other popular two options are scanline rasterization and raytracing.

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 04 Sep 2026, 06:53
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.