Created
June 4, 2013 18:22
-
-
Save gszauer/5708230 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define WIN32_LEAN_AND_MEAN | |
#define WIN32_EXTRA_LEAN | |
#include <windows.h> | |
#include <stdio.h> | |
#include "rasterizer.h" | |
extern HDC hdcMemory; | |
extern int g_nWindowWidth; | |
extern int g_nWindowHeight; | |
void sort(tri* t, int* order) { | |
// Find the lowest y | |
if (t->a.y < t->b.y) { | |
if (t->a.y < t->c.y) { | |
order[0] = 0; | |
} else { | |
order[0] = 2; | |
} | |
} else { | |
if (t->b.y < t->c.y) { | |
order[0] = 1; | |
} else { | |
order[0] = 2; | |
} | |
} | |
// Find the highest y | |
if (t->a.y > t->b.y) { | |
if (t->a.y > t->c.y) { | |
order[2] = 0; | |
} else { | |
order[2] = 2; | |
} | |
} else { | |
if (t->b.y > t->c.y) { | |
order[2] = 1; | |
} else { | |
order[2] = 2; | |
} | |
} | |
order[1] = 3 - (order[0] + order[2]); | |
} | |
void draw_triangle(vert v1, vert v2, vert v3) { | |
tri t = {v1, v2, v3}; | |
draw_triangle(&t); | |
} | |
void draw_triangle(float x1, float y1, float x2, float y2, float x3, float y3) { | |
tri t = { x1, y1, x2, y2, x3, y3 }; | |
draw_triangle(&t); | |
} | |
void draw_triangle(ptri t) { // The triangle should be ordered by the time it gets here | |
#define x1 t->vtx[0].x | |
#define x2 t->vtx[1].x | |
#define x3 t->vtx[2].x | |
#define y1 t->vtx[0].y | |
#define y2 t->vtx[1].y | |
#define y3 t->vtx[2].y | |
float slope_left, slope_right; | |
float left, right; | |
float height = y3 - y1; | |
/*char buffer[512]; | |
memset(buffer, 0, 512); | |
sprintf(buffer, "Second\n{%d, %d}\n{%d, %d}\n%{%d, %d}", | |
(int)x1, (int)y1, (int)x2, (int)y2, (int)x3, (int)y3); | |
MessageBox(0, buffer, "Debug", MB_OK);*/ | |
if (height == 0) | |
return; | |
slope_left = (x2 - x1) / height; | |
slope_right = (x3 - x1) / height; | |
left = right = x1; | |
if (y1 < y3) { | |
for (int y = y1; y < y3; ++y) { | |
draw_line(left, right, y); | |
left += slope_left; | |
right += slope_right; | |
} | |
} else { | |
for (int y = y1; y >= y3; --y) { | |
draw_line(left, right, y); | |
left -= slope_left; | |
right -= slope_right; | |
} | |
} | |
#undef x1 | |
#undef x2 | |
#undef x3 | |
#undef y1 | |
#undef y2 | |
#undef y3 | |
} | |
void draw_line(float x1, float x2, float y) { | |
if (x1 < 0) x1 = 0; | |
if (x1 > g_nWindowWidth) x1 = g_nWindowWidth; | |
if (x2 < 0) x1 = 0; | |
if (x2 > g_nWindowWidth) x2 = g_nWindowWidth; | |
if (y < 0) y = 0; | |
if (y > g_nWindowHeight) y = g_nWindowHeight; | |
POINT pntArray[2] = { x1, y, x2, y }; | |
Polyline(hdcMemory, pntArray, 2); | |
} | |
void triangle(vert v1, vert v2, vert v3) { | |
tri t = {v1, v2, v3}; | |
triangle(&t); | |
} | |
void triangle(float x1, float y1, float x2, float y2, float x3, float y3) { | |
tri t = { x1, y1, x2, y2, x3, y3 }; | |
triangle(&t); | |
} | |
void triangle(ptri t) { | |
int order[3]; | |
sort(t, order); | |
#define x1 t->vtx[order[0]].x | |
#define x2 t->vtx[order[1]].x | |
#define x3 t->vtx[order[2]].x | |
#define y1 t->vtx[order[0]].y | |
#define y2 t->vtx[order[1]].y | |
#define y3 t->vtx[order[2]].y | |
// If this triangle has no horizontal edges, split it into two triangles | |
if (y2 != y1 && y3 != y1 && y3 != y2) { | |
float new_x = (x3 - x1) * ((y2 - y1) / (y3 - y1)) + x1; | |
if (new_x < x2) { | |
draw_triangle(x1, y1, new_x, y2, x2, y2); | |
draw_triangle(x3, y3, x2, y2, new_x, y2); | |
} else { | |
/*char buffer[512]; | |
memset(buffer, 0, 512); | |
sprintf(buffer, "Top\n{%d, %d}\n{%d, %d}\n%{%d, %d}", | |
(int)x1, (int)y1, (int)x2, (int)y2, (int)new_x, (int)y2); | |
MessageBox(0, buffer, "Triangle debug", MB_OK);*/ | |
draw_triangle(x1, y1, x2, y2, new_x, y2); | |
/*memset(buffer, 0, 512); | |
sprintf(buffer, "Bottom\n{%d, %d}\n{%d, %d}\n%{%d, %d}", | |
(int)x3, (int)y3, (int)x2, (int)y2, (int)new_x, (int)y2); | |
MessageBox(0, buffer, "Triangle debug", MB_OK);*/ | |
draw_triangle(x3, y3, x2, y2, new_x, y2); | |
} | |
} | |
// If the top is flat | |
else if (y1 == y2) { | |
if (x1 < x2) | |
draw_triangle(x3, y3, x2, y2, x1, y2); | |
else | |
draw_triangle(x3, y3, x1, y2, x2, y2); | |
} | |
// If the bottom is flat | |
else if (y2 == y3) { | |
if (x2 < x3) | |
draw_triangle(x1, y1, x2, y2, x3, y2); | |
else | |
draw_triangle(x1, y1, x3, y2, x2, y2); | |
} | |
#undef x1 | |
#undef x2 | |
#undef x3 | |
#undef y1 | |
#undef y2 | |
#undef y3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment