Skip to content

Commit

Permalink
renderer: integrate texture-mapping with level and texture -loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Nov 27, 2018
1 parent 196f123 commit 69e103c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 deletions.
9 changes: 6 additions & 3 deletions renderer/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ Level *level_load(int levelId)
UNUSED(levelId);
Wall walls_template[] = {
{.startCorner = xz(real_from_int(70), real_from_int(50)),
.color = GColorFromRGB(0, 255, 0)},
.texture = 0,
.texCoord = { xy_zero, xy_one}},
{.startCorner = xz(real_from_int(0), real_from_int(50)),
.color = GColorFromRGB(255, 0, 255)},
.texture = 0,
.texCoord = { xy_zero, xy_one}},
{.startCorner = xz(real_from_int(0), real_from_int(-30)),
.color = GColorFromRGB(0, 255, 255)}
.texture = 0,
.texCoord = { xy_zero, xy_one}},
};
Sector sectors_template[] = {
{.wallCount = 3,
Expand Down
3 changes: 2 additions & 1 deletion renderer/level.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ typedef struct Location {

typedef struct Wall {
xz_t startCorner;
GColor color;
TextureId texture;
TexCoord texCoord;
} Wall;

typedef struct Sector {
Expand Down
45 changes: 11 additions & 34 deletions renderer/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,6 @@ void renderer_clipByFov(const Renderer* me, lineSeg_t* wallSeg, TexCoord* texCoo
texCoord->end.x = real_add(real_mul(wallPhaseLeft, texCoordAmpl), texCoordStart);
}

typedef struct {
GColor* colors;
int width, height;
} Bild;

static const Bild* genBild()
{
static GColor pixels[64*64];
static Bild bild;
bild.width = 64;
bild.height = 64;
bild.colors = pixels;
for (int i = 0; i <64*64; i++)
{
int x = i % 64;
int y = i / 64;
int c = x ^ y;
pixels[i].r = (c >> 0) & 2;
pixels[i].g = (c >> 2) & 2;
pixels[i].b = (c >> 4) & 2;
pixels[i].a = 3;
}
return &bild;
}

void renderer_project(const Renderer* me, const Sector* sector, const lineSeg_t* transformedSeg, WallSection* projected)
{
const real_t halfHeight = real_div(real_from_int(sector->height), real_from_int(2));
Expand All @@ -173,14 +148,14 @@ void renderer_project(const Renderer* me, const Sector* sector, const lineSeg_t*
void renderer_renderWall(Renderer* me, GColor* framebuffer, const Sector* sector, int wallIndex)
{
const Wall* wall = &sector->walls[wallIndex];
const Bild* bild = genBild();
const Texture* texture = texture_load(me->textureManager, wall->texture);

lineSeg_t wallSeg;
renderer_transformWall(me, sector, wallIndex, &wallSeg);
if (real_compare(wallSeg.start.xz.z, real_zero) < 0 && real_compare(wallSeg.end.xz.z, real_zero) < 0)
return;

TexCoord texCoord = (TexCoord) { xy_zero, xy_one };
TexCoord texCoord = wall->texCoord;
renderer_clipByFov(me, &wallSeg, &texCoord);

WallSection p;
Expand All @@ -201,13 +176,13 @@ void renderer_renderWall(Renderer* me, GColor* framebuffer, const Sector* sector

real_t xNormalized = real_div(real_from_int(x - renderLeft), renderAmpl);
int texCol = real_to_int(
real_mul(real_lerp(xNormalized, texCoord.start.x, texCoord.end.x), real_from_int(bild->width))
real_mul(real_lerp(xNormalized, texCoord.start.x, texCoord.end.x), real_from_int(texture->size.w))
);
real_t yNormalized = yCurStart >= 0 ? real_zero
: real_div(real_from_int(-yCurStart), real_from_int(yCurEnd - yCurStart));
real_t texRow = real_mul(real_lerp(yNormalized, texCoord.start.y, texCoord.end.y), bild->height);
real_t texRow = real_mul(real_lerp(yNormalized, texCoord.start.y, texCoord.end.y), real_from_int(texture->size.h));
real_t texRowIncr = real_div(
real_mul(real_sub(texCoord.end.y, texCoord.start.y), real_from_int(bild->height)),
real_mul(real_sub(texCoord.end.y, texCoord.start.y), real_from_int(texture->size.h)),
real_from_int(yCurEnd - yCurStart + 1));

int y;
Expand All @@ -216,15 +191,17 @@ void renderer_renderWall(Renderer* me, GColor* framebuffer, const Sector* sector
for (; y <= min(RENDERER_HEIGHT - 1, yCurEnd); y++)
{
int texRowI = real_to_int(texRow);
*(curPixel++) = bild->colors[
(texRowI % bild->height) * bild->width +
(texCol % bild->width)
*(curPixel++) = texture->pixels[
(texRowI % texture->size.h) * texture->size.w +
(texCol % texture->size.w)
];
texRow += texRowIncr;
texRow = real_add(texRow, texRowIncr);
}
for (; y < RENDERER_HEIGHT; y++)
*(curPixel++) = sector->ceilColor;
}

texture_free(me->textureManager, texture);
}

void renderer_renderSector(Renderer* renderer, GColor* framebuffer, const Sector* sector)
Expand Down
2 changes: 1 addition & 1 deletion renderer/renderer_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void renderer_renderDebugSector(Renderer* me, SDL_Renderer* sdlRenderer, xz_t of
lineSeg_t wallLine;
wallLine.start.xz = curWall->startCorner;
wallLine.end.xz = sector->walls[(i + 1) % sector->wallCount].startCorner;
renderer_setDebugColor(sdlRenderer, curWall->color);
renderer_setDebugColor(sdlRenderer, GColorFromRGB((i % 3 == 0) * 255, (i % 3 == 1) * 255, (i % 3 == 2) * 255));
renderer_renderSDLDebugLine(me, sdlRenderer, offset, wallLine, opts);
}
}
Expand Down

0 comments on commit 69e103c

Please sign in to comment.