#### Archived

This topic is now archived and is closed to further replies.

# Filling a triangle...

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

## Recommended Posts

I have a lot of problems it seems. Here is my latest: I am trying to fill a triangle in software using Direct Draw. Everything works well, I can lock, unlock, and flip the buffers. I can plot pixels fine. I cannot fill a triangle. When I code it and let it run, everything runs fine( lock, unlock, etc ), but there is no triangle... This code assumes the triangle''s middle vertice is to the left of the top and bottom vertice, example: v1-> 0 // / / v2-> 0 / \ / \ / \ / \ / \/ v3-> 0 Here is the code: void softDrawTriangle( RSTRIANGLE* Tri ) { BLOCK t, m, b, temp; float zFix = Tri->v1->z; if( zFix == 0 ) zFix = 1; t.x = ( int )( ( Tri->v1->x / zFix * Perspective ) + xOffset ); t.y = ( int )( ( -Tri->v1->y / zFix * Perspective ) + yOffset ); m.x = ( int )( ( Tri->v2->x / zFix * Perspective ) + xOffset ); m.y = ( int )( ( -Tri->v2->y / zFix * Perspective ) + yOffset ); b.x = ( int )( ( Tri->v3->x / zFix * Perspective ) + xOffset ); b.y = ( int )( ( -Tri->v3->y / zFix * Perspective ) + yOffset ); if( t.y > m.y ) { temp = t; t = m; m = temp; } if( t.y > b.y ) { temp = t; t = b; b = temp; } if( m.y > b.y ) { temp = m; m = b; b = temp; } // THE CODE ABOVE WORKS. TROUBLE AREAS BELOW. float xSlope_t2m, xSlope_t2b, xSlope_m2b; if( t.y - m.y == 0 ) xSlope_t2m = 0; else xSlope_t2m = float( ( t.x - m.x ) / ( t.y - m.y ) ); xSlope_t2b = float( ( t.x - b.x ) / ( t.y - b.y ) ); if( m.y - b.y == 0 ) xSlope_m2b = 0; else xSlope_m2b = float( ( m.x - b.x ) / ( m.y - b.y ) ); int xStart = t.x; int xEnd = t.x; int y_lPitch; if( t.y != m.y ) { if( xSlope_t2m < xSlope_t2b ) { for( int CurrY = t.y; CurrY < m.y; CurrY++ ) { y_lPitch = CurrY * ScreenlPitch; for( int CurrX = xStart; CurrX < ( xEnd + 1 ); CurrX++) { Buffer[ CurrX + y_lPitch ] = Color; } xStart = ( int )( xStart + xSlope_t2m ); xEnd += ( int )( xEnd + xSlope_t2b ); } for( CurrY = m.y; CurrY < ( b.y + 1 ); CurrY++ ) { y_lPitch = CurrY * ScreenlPitch; for( int CurrX = xStart; CurrX < ( xEnd + 1 ); CurrX++) { Buffer[ CurrX + y_lPitch ] = Color; } xStart = ( int )( xStart + xSlope_m2b ); xEnd += ( int )( xStart + xSlope_t2b ); } } } }

##### Share on other sites
I hate these message boards. Lost all my tabs... Also, that first thing is supposed to be an ASCII triangle showing how v2 is to the left of v1 and v3...

##### Share on other sites
Letting the accelerator card fill the triangles for you would be much faster, but I don''t know what you''re overall plan is. Typcasting within the drawing loops will slow the thing down when it performs all of those conversions, I recommend using a single type in the innermost loops for speed. Drawing a triangle must be fast!

Where did this color come from? I don''t see it anywhere else in the code, except in the drawing loops.
Buffer[ CurrX + y_lPitch ] = Color;

##### Share on other sites
Color is global to that module. I fixed the problem, it was due to lack of accuracy when converting floats to ints. In the end this engine will support at least OpenGL, maybe Direct3D.

• 10
• 13
• 52
• 11
• 15