Add texture ceiling

This commit is contained in:
Miloslav Číž 2018-09-18 18:09:19 +02:00
parent c97448e9a8
commit c752646293
2 changed files with 58 additions and 20 deletions

View file

@ -33,6 +33,10 @@
/* ^ Turns on computation of texture coordinates for the floor in raycastlib
and makes this demo render the textures. */
//#define TEXTURE_CEILING
/* ^ Turns on texture on ceiling (RCL_COMPUTE_FLOOR_TEXCOORDS must be turned
on as well for this to work). */
#define FLOOR_TEXTURE_SCALE 2
/* ^ Scales the floor texture if RCL_COMPUTE_FLOOR_TEXCOORDS is on. */
@ -83,6 +87,8 @@ Player player;
#define INFO_BAR_START 70
#define TEXTURE_MAX_DISTANCE (RCL_UNITS_PER_SQUARE * 6)
#define RESOLUTION_Y (INFO_BAR_START + 1)
// temporary defines for better visibility of walls and floors below
#define D 6
#define o 0
@ -897,39 +903,55 @@ inline void pixelFunc(RCL_PixelInfo *pixel)
if (pixel->position.y == MIDDLE_ROW)
zBuffer[pixel->position.x] = pixel->depth;
uint8_t c = 0;
uint8_t color = 0;
#ifndef NO_MIRROR
if (pixel->position.y == 0)
mirror = 0;
#endif
bool drawCeiling = false;
int16_t intensity = 0;
if (!pixel->isWall)
{
#if RCL_COMPUTE_FLOOR_TEXCOORDS == 1
if (pixel->isFloor)
{
if (pixel->texCoords.y > 3 * RCL_UNITS_PER_SQUARE) // leave astrip of untextured floor
c = pixel->depth > RCL_UNITS_PER_SQUARE * 5 ?
color = pixel->depth > RCL_UNITS_PER_SQUARE * 5 ?
textureAverageColors[1] :
sampleImage(textures[1],pixel->texCoords.x / FLOOR_TEXTURE_SCALE,pixel->texCoords.y / FLOOR_TEXTURE_SCALE);
else
c = cFloor;
color = cFloor;
#ifdef TEXTURE_CEILING
drawCeiling = true;
#endif
}
else
c = 0; // looks better :)
{
#ifdef TEXTURE_CEILING
// don't draw ceiling here, we'll draw it along with floor
return;
#else
color = 0; // looks better :)
#endif
}
#else
c = pixel->isFloor ? cFloor: cCeiling;
color = pixel->isFloor ? cFloor: cCeiling;
#endif
#ifndef NO_MIRROR
int16_t intensity = pixel->isFloor ?
intensity = pixel->isFloor ?
-1 * (pixel->depth - mirror * 64) / RCL_UNITS_PER_SQUARE : 0;
#else
int16_t intensity = -1 * pixel->depth / (RCL_UNITS_PER_SQUARE * 2);
intensity = -1 * pixel->depth / (RCL_UNITS_PER_SQUARE * 2);
#endif
c = addIntensity(c,intensity);
color = addIntensity(color,intensity);
mirror++;
}
@ -938,9 +960,9 @@ inline void pixelFunc(RCL_PixelInfo *pixel)
RCL_Unit textureScroll = pixel->hit.type != 4 ? 0 : 16 * pokitto.frameCount;
#ifdef NO_TEXTURES
c = textureAverageColors[pixel->hit.type];
color = textureAverageColors[pixel->hit.type];
#else
c = pixel->depth < TEXTURE_MAX_DISTANCE ?
color = pixel->depth < TEXTURE_MAX_DISTANCE ?
sampleImage(textures[pixel->hit.type],pixel->texCoords.x + textureScroll,pixel->texCoords.y) :
textureAverageColors[pixel->hit.type];
#endif
@ -948,20 +970,36 @@ inline void pixelFunc(RCL_PixelInfo *pixel)
#ifndef NO_SHADING
if (previousColumn == pixel->position.x)
{
c = addIntensity(c,pixelIntensity);
color = addIntensity(color,pixelIntensity);
}
else
{
// optimization: precompute intensity for the whole column
pixelIntensity = 1 - pixel->depth / (RCL_UNITS_PER_SQUARE * 2) + (pixel->hit.direction % 2 == 0 ? 2 : 0);
previousColumn = pixel->position.x;
c = addIntensity(c,pixelIntensity);
color = addIntensity(color,pixelIntensity);
}
#endif
}
putSubsampledPixel(pixel,c);
putSubsampledPixel(pixel->position.x,pixel->position.y,color);
#ifdef TEXTURE_CEILING
if (drawCeiling)
{
/* here we sample a different texture for ceiling, but if the texture is
same as floor, just reuse the color sampled above to save on the
sampling and intensity addition */
color = pixel->depth > RCL_UNITS_PER_SQUARE * 5 ?
textureAverageColors[0] :
sampleImage(textures[0],pixel->texCoords.x / FLOOR_TEXTURE_SCALE,pixel->texCoords.y / FLOOR_TEXTURE_SCALE);
color = addIntensity(color,intensity);
putSubsampledPixel(pixel->position.x,RESOLUTION_Y - pixel->position.y - 1,color);
}
#endif
}
void draw()
@ -1008,7 +1046,7 @@ int main()
player.setPositionSquare(6,5);
player.mCamera.height = RCL_CAMERA_COLL_HEIGHT_BELOW;
player.mCamera.resolution.y = INFO_BAR_START + 2;
player.mCamera.resolution.y = RESOLUTION_Y;
uint32_t previousTime = 0;
uint32_t dt;

View file

@ -77,19 +77,19 @@ RCL_RayConstraints defaultConstraints;
unsigned short palette[256];
#ifdef POK_SIM
inline void putSubsampledPixel(RCL_PixelInfo *pixel, uint8_t color)
inline void putSubsampledPixel(int32_t x, int32_t y, uint8_t color)
{
pokitto.display.drawPixel(pixel->position.x * SUBSAMPLE,pixel->position.y,color);
pokitto.display.drawPixel(pixel->position.x * SUBSAMPLE + 1,pixel->position.y,color);
pokitto.display.drawPixel(x * SUBSAMPLE,y,color);
pokitto.display.drawPixel(x * SUBSAMPLE + 1,y,color);
}
#else
// This code breaks the simulator.
inline void putSubsampledPixel(RCL_PixelInfo *pixel, uint8_t color)
inline void putSubsampledPixel(int32_t x, int32_t y, uint8_t color)
{
uint8_t *buf = pokitto.display.screenbuffer;
buf += pixel->position.x * SUBSAMPLE;
buf += pixel->position.y * SCREEN_WIDTH;
buf += x * SUBSAMPLE;
buf += y * SCREEN_WIDTH;
for (uint8_t i = 0; i < SUBSAMPLE - 1; ++i)
*buf++ = color;