1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/raycastlib.git synced 2024-11-21 20:29:59 +01:00

Optimize texture coords

This commit is contained in:
Miloslav Číž 2018-09-16 17:12:09 +02:00
parent e33d02ccec
commit 00e5cff5b1

View file

@ -88,6 +88,13 @@
#define CAMERA_COLL_STEP_HEIGHT (UNITS_PER_SQUARE / 2) #define CAMERA_COLL_STEP_HEIGHT (UNITS_PER_SQUARE / 2)
#endif #endif
#ifndef MIN_TEXTURE_STEP
#define MIN_TEXTURE_STEP 12 /**< Specifies the minimum step in pixels that can
be used to compute texture coordinates in a
fast way. Smallet step will trigger a more
expensive way of computing texture coords. */
#endif
#define logVector2D(v)\ #define logVector2D(v)\
printf("[%d,%d]\n",v.x,v.y); printf("[%d,%d]\n",v.x,v.y);
@ -113,7 +120,6 @@
logVector2D(p.position);\ logVector2D(p.position);\
printf(" depth: %d\n", p.depth);\ printf(" depth: %d\n", p.depth);\
printf(" wall: %d\n", p.isWall);\ printf(" wall: %d\n", p.isWall);\
printf(" textCoordY: %d\n", p.textureCoordY);\
printf(" hit: ");\ printf(" hit: ");\
logHitResult(p.hit);\ logHitResult(p.hit);\
}\ }\
@ -170,7 +176,8 @@ typedef struct
int8_t isHorizon; ///< Whether the pixel is floor going towards horizon. int8_t isHorizon; ///< Whether the pixel is floor going towards horizon.
Unit depth; ///< Corrected depth. Unit depth; ///< Corrected depth.
HitResult hit; ///< Corresponding ray hit. HitResult hit; ///< Corresponding ray hit.
Unit textureCoordY; ///< Normalized (0 to UNITS_PER_SQUARE - 1) tex coord. Vector2D texCoords; /**< Normalized (0 to UNITS_PER_SQUARE - 1) texture
coordinates. */
} PixelInfo; } PixelInfo;
typedef struct typedef struct
@ -902,6 +909,8 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
PixelInfo p; PixelInfo p;
p.position.x = x; p.position.x = x;
p.texCoords.x = 0;
p.texCoords.y = 0;
Unit i; Unit i;
@ -995,7 +1004,11 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
{\ {\
p.position.y = i;\ p.position.y = i;\
p.hit = hit;\ p.hit = hit;\
p.textureCoordY = (wallPosition * UNITS_PER_SQUARE) / wallLength; \ if (COMPUTE_WALL_TEXCOORDS == 1)\
{\
p.texCoords.x = hit.textureCoord;\
p.texCoords.y = (wallPosition * UNITS_PER_SQUARE) / wallLength;\
}\
wallPosition++;\ wallPosition++;\
_pixelFunction(&p);\ _pixelFunction(&p);\
}\ }\
@ -1145,18 +1158,44 @@ void _columnFunctionSimple(HitResult *hits, uint16_t hitCount, uint16_t x,
p.hit.textureCoord -= p.hit.doorRoll; p.hit.textureCoord -= p.hit.doorRoll;
#endif #endif
while (y < wallEnd) Unit coordStep = 1;
{
p.position.y = y;
#if COMPUTE_WALL_TEXCOORDS == 1 #if COMPUTE_WALL_TEXCOORDS == 1
p.textureCoordY = (coordHelper * UNITS_PER_SQUARE) / wallHeightScreen; p.texCoords.x = p.hit.textureCoord;
coordStep = UNITS_PER_SQUARE / wallHeightScreen;
p.texCoords.y = coordStep * coordHelper;
#endif #endif
_pixelFunction(&p); if (coordStep < MIN_TEXTURE_STEP) /* instead of branching inside a critical
loop, have two versions of the loop and
branch early (here) */
{
while (y < wallEnd)
{
// more expensive and accurate version of texture coords computation
++y; p.position.y = y;
#if COMPUTE_WALL_TEXCOORDS == 1
p.texCoords.y = (UNITS_PER_SQUARE * coordHelper) / wallHeightScreen;
#endif
_pixelFunction(&p);
++coordHelper; ++coordHelper;
++y;
}
}
else
{
while (y < wallEnd)
{
// cheaper texture coord computation
p.position.y = y;
_pixelFunction(&p);
#if COMPUTE_WALL_TEXCOORDS == 1
p.texCoords.y += coordStep;
#endif
++y;
}
} }
// draw floor // draw floor