Fix texture mapping

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-09-02 17:20:08 +02:00
parent cea1e07552
commit 195817f2d9
+88 -24
View File
@@ -270,20 +270,6 @@ void drawRays2D()
int shiftSize = (int)(log2f((float)mapSize));
glColor3f(0, 1, 1);
glBegin(GL_QUADS);
glVertex2i(526, 0);
glVertex2i(1006, 0);
glVertex2i(1006, 160);
glEnd();
glColor3f(0, 0, 1);
glBegin(GL_QUADS);
glVertex2i(526, 160);
glVertex2i(1006, 160);
glVertex2i(1006, 320);
glEnd();
// Ray x, y, angle
float rayX, rayY, rayAngle;
@@ -516,6 +502,22 @@ void drawRays2D()
// * Draws the top-down map with white alls and black floors
void drawMap2D()
{
// glColor3f(0, 1, 1);
// glBegin(GL_QUADS);
// glVertex2i(0, 0);
// glVertex2i(TOTAL_WIDTH, 0);
// glVertex2i(TOTAL_WIDTH, (int)(TOTAL_HEIGHT / 2));
// glVertex2i(0, (int)(TOTAL_HEIGHT / 2));
// glEnd();
// glColor3f(0, 0, 1);
// glBegin(GL_QUADS);
// glVertex2i(0, (int)(TOTAL_HEIGHT / 2));
// glVertex2i(TOTAL_WIDTH, (int)(TOTAL_HEIGHT / 2));
// glVertex2i(TOTAL_WIDTH, TOTAL_HEIGHT);
// glVertex2i(0, TOTAL_HEIGHT);
// glEnd();
int gridMapSize = mapSize;
int x, y, xo, yo;
for (y = 0; y < mapY; y++)
@@ -585,6 +587,9 @@ void drawRays3D()
rayAngle -= 2 * PI;
}
glPointSize(2.0f);
glBegin(GL_POINTS);
// * Cast n rays
for (r = 0; r < 60; r++)
{
@@ -804,8 +809,12 @@ void drawRays3D()
// The further away, the larger the distance and the shorter the final line
// height will be.
float lineHeight = (mapSize * gameHeight) / disT;
float textureYstep = mapSize / (float)lineHeight;
float textureYoffset = 0;
if (lineHeight > gameHeight)
{
textureYoffset = (lineHeight - gameHeight) / 2.0;
lineHeight = gameHeight;
}
@@ -821,16 +830,56 @@ void drawRays3D()
// Calculate dynamic column width and horizontal starting offset
float rayWidth = (float)gameWidth / 60.0f;
int xStart = (int)floorf(r * rayWidth);
int xEnd = (int)floorf((r + 1) * rayWidth);
float rayX3D = MAP_WIDTH + GAP + (r * rayWidth);
float shade = 1;
glColor3f(0, 0.8, 0);
if (distanceVert < distanceHor)
{
shade = 0.5;
rayX = vx;
rayY = vy;
distanceHor = distanceVert;
glColor3f(0, 0.6, 0);
}
// * Draw the "wall" line
// Draw solid wall column using QUADS instead of GL_LINES
glBegin(GL_QUADS);
glVertex2f(rayX3D, lineOffset);
glVertex2f(rayX3D + rayWidth, lineOffset);
glVertex2f(rayX3D + rayWidth, lineOffset + lineHeight);
glVertex2f(rayX3D, lineOffset + lineHeight);
glEnd();
int x, y;
int textureX;
if (shade == 1) // North-south walls (PI -> (2 * PI))
{
textureX = (int)(rayX / 2.0) % mapSize;
if (rayAngle < PI) // South walls
{
textureX = mapSize - 1 - textureX;
}
}
else // East-west walls (PI2 -> PI3)
{
textureX = (int)(rayY / 2.0) % mapSize;
if (rayAngle > PI2 && rayAngle < PI3) // West
{
textureX = mapSize - 1 - textureX;
}
}
for (x = xStart; x < xEnd; x++)
{
float textureY = textureYoffset * textureYstep;
for (y = 0; y < lineHeight; y++)
{
float c = All_Textures[((int)textureY * mapSize) + textureX] * shade;
glColor3f(c, c, c);
glVertex2i(MAP_WIDTH + GAP + x, (int)(y + lineOffset));
textureY += textureYstep;
}
}
// * Increase the ray angle to draw the next ray
rayAngle += DR;
@@ -843,6 +892,7 @@ void drawRays3D()
rayAngle -= 2 * PI;
}
}
glEnd();
}
// * Draw ceiling
@@ -1078,7 +1128,21 @@ void ButtonUp(unsigned char key, int x, int y)
// * Window resizing
void resize(int w, int h)
{
glutReshapeWindow(1024, 512);
// Prevent division by zero
if (h == 0)
h = 1;
// Map the orthographic scene bounds to fill the entire window frame
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set logical coordinate space: (left, right, bottom, top)
gluOrtho2D(0, TOTAL_WIDTH, TOTAL_HEIGHT, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// * Check for key presses and move the "player" accordingly
@@ -1091,7 +1155,7 @@ void init()
// Sets the window to be 2D at a given size
// gluOrtho2D(0, 1024, 512, 0);
gluOrtho2D(0, TOTAL_WIDTH, TOTAL_HEIGHT, 0);
// gluOrtho2D(0, TOTAL_WIDTH, TOTAL_HEIGHT, 0);
// Spawn player centered in grid tile (1, 1)
playerX = 1.5f * mapSize;
@@ -1109,7 +1173,7 @@ int main(int argc, char *argv[])
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
// glutInitWindowSize(TOTAL_WIDTH, TOTAL_HEIGHT);
glutInitWindowSize(1024, 510);
glutInitWindowSize(1024, 512);
// Initialize the window to the center of the screen
glutInitWindowPosition(200, 200);