Skip to content

Instantly share code, notes, and snippets.

@hackergrrl
Created May 25, 2012 23:47
Show Gist options
  • Save hackergrrl/2791264 to your computer and use it in GitHub Desktop.
Save hackergrrl/2791264 to your computer and use it in GitHub Desktop.
awful awful rendering code
float ang_inc = (cam->fov / (float)SCR_WIDTH);
float ang = -cam->fov / 2.0f - ang_inc;
for(int i=0; i < SCR_WIDTH; i++)
{
ang += ang_inc;
float cos_corr = cos(ang*D2R);
float ovx = i - SCR_WIDTH/2;
float ovy = cam->projDist;
float mag = sqrt( ovx*ovx + ovy*ovy );
ovx /= mag; ovy /= mag;
float vx = cos_v * ovy - sin_v * ovx;
float vy = cos_v * ovx + sin_v * ovy;
if(vx == 0) vx = 0.1f; // HACK: :(
bool xhit = false, yhit = false;
float m, b;
int Hxi, Hyi, Hxs, Hys;
int Vxi, Vyi, Vxs, Vys;
m = vy / vx;
b = cam->y - m * cam->x;
// For horizontal-line intersections
if(vy > 0) { Hyi = (((int)cam->y/32)*32 + 32.1f) * 65536; Hys = 32 << 16; }
else { Hyi = (((int)cam->y/32)*32-0.001f) * 65536; Hys = -32 << 16; }
Hxi = 65536 * (((Hyi>>16) - b) / m);
Hxs = 65536 * ((vx > 0 ? 32.0f : -32.0f) / absf(m));
// For vertical-line intersections
if(vx > 0) { Vxi = (((int)cam->x/32)*32 + 32.1f) * 65536; Vxs = 32 << 16; }
else { Vxi = (((int)cam->x/32)*32-0.001f) * 65536; Vxs = -32 << 16; }
Vyi = 65536 * (m * (Vxi>>16) + b);
Vys = 65536 * ((vy > 0 ? 32.0f : -32.0f) * absf(m));
bool noobX=true, noobY=true;
while((noobX && !xhit) || (noobY && !yhit))
{
noobX = !((Hxi>>16) < 0 || (Hyi>>16) < 0 || (Hxi>>16) >= mapWidth*32 || (Hyi>>16) >= mapHeight*32);
noobY = !((Vxi>>16) < 0 || (Vyi>>16) < 0 || (Vxi>>16) >= mapWidth*32 || (Vyi>>16) >= mapHeight*32);
if(noobX && mapdata[Hxi>>21][Hyi>>21].solid)
xhit = true;
else if(noobX && !xhit)
{
Hxi += Hxs; Hyi += Hys;
}
if(noobY && mapdata[Vxi>>21][Vyi>>21].solid)
yhit = true;
else if(noobY && !yhit)
{
Vxi += Vxs; Vyi += Vys;
}
}
float HxiF = ((float)Hxi) / 65536.0f, HyiF = ((float)Hyi) / 65536.0f;
float VxiF = ((float)Vxi) / 65536.0f, VyiF = ((float)Vyi) / 65536.0f;
float pd1 = xhit ? (HxiF-cam->x)*(HxiF-cam->x) + (HyiF-cam->y)*(HyiF-cam->y) : 10000000;
float pd2 = yhit ? (VxiF-cam->x)*(VxiF-cam->x) + (VyiF-cam->y)*(VyiF-cam->y) : 10000000;
if(xhit || yhit)
{
float pd;
int tx, ty;
int u;
if(pd1 < pd2)
{
pd = 1.0f / (sqrt(pd1) * cos_corr);
tx = (Hxi >> 21);
ty = (Hyi >> 21);
u = (Hxi >> 16) & 31;
}
else
{
pd = 1.0f / (sqrt(pd2) * cos_corr);
tx = (Vxi >> 21);
ty = (Vyi >> 21);
u = (Vyi >> 16) & 31;
}
zbuffer[i] = 1.0f / pd;
float h = (16.0f * cam->projDist) * pd + 1;
float sy = (-cam->z * cam->projDist) * pd + SCR_HEIGHT/2 - cam->zRot;
int topy = sy;
int boty = sy + 2*h;
int v_inc = (32 * 65536) / (h*2);
int v = topy < 0 ? v_inc * -topy : 0;
float fog_mult = maxf(0.0f, minf(1.0f, (1.0f - (1.0f/pd) / fog_dist)));
int br = mapdata[tx][ty].brightness * fog_mult;
if(topy < 0) topy = 0;
if(boty >= SCR_HEIGHT) boty = SCR_HEIGHT;
SDL_Surface *tex = textures[ mapdata[tx][ty].wallTex ];
for(int j=topy; j < boty; j++)
{
pixel col = getpixel(tex,u,v>>16);
byte r = (col >> 16) & 255;
byte g = (col >> 8) & 255;
byte b = col & 255;
r = (r * br) >> 16;
g = (g * br) >> 16;
b = (b * br) >> 16;
col = (r << 16) | (g << 8) | b;
putpixel(screen,i,j, col);
v += v_inc;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment