Skip to content

Instantly share code, notes, and snippets.

@iodiot
Created October 4, 2012 13:50
Show Gist options
  • Save iodiot/3833648 to your computer and use it in GitHub Desktop.
Save iodiot/3833648 to your computer and use it in GitHub Desktop.
void Renderer::RenderWall(Vector2 v1, Vector2 v2)
{
// Translate input vectors into view frame
v1 -= cameraPosition;
v2 -= cameraPosition;
// Rotate input vectors
v1.Rotate(cameraRotation);
v2.Rotate(cameraRotation);
double x1 = v1.x;
double z1 = v1.y;
double x2 = v2.x;
double z2 = v2.y;
// Projection
int startX = width / 2 - int(nearPlaneDistance * x1 / z1);
int endX = width / 2 - int(nearPlaneDistance * x2 / z2);
if (startX >= endX) return;
int lineHeight1 = int(fabs(double(height) / z1));
int lineHeight2 = int(fabs(double(height) / z2));
int dx = endX - startX;
for (int x = max(0, startX); x < min(width, endX); ++x)
{
double d = double(x - startX) / double(dx);
// Depth test
double lineDepth = z1 + d * (z2 - z1);
if (zBuffer[x] > lineDepth)
zBuffer[x] = lineDepth;
else
continue;
// Interpolate line height
int lineHeight = int(lineHeight1 + d * (lineHeight2 - lineHeight1));
int startY = height / 2 - lineHeight / 2;
int endY = height / 2 + lineHeight / 2;
int dy = endY - startY;
for (int y = max(0, startY); y < min(height, endY); ++y)
{
// Draw pixel
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment