Skip to content

Instantly share code, notes, and snippets.

@iodiot
Created October 4, 2012 14:16
Show Gist options
  • Save iodiot/3833785 to your computer and use it in GitHub Desktop.
Save iodiot/3833785 to your computer and use it in GitHub Desktop.
void Renderer::RenderWall(Vector2 v1, Vector2 v2)
{
static const double zClip = 0.2;
// 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;
// Near plane clipping
if (z1 < zClip && z2 < zClip) return;
if (z1 < zClip) {
double p = (zClip - z1) / (z2 - z1);
z1 = zClip;
x1 = x1 + (x2 - x1) * p;
}
if (z2 < zClip) {
double p = (zClip - z1) / (z2 - z1);
z2 = zClip;
x2 = x1 + (x2 - x1) * p;
}
// 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