Greetings,
I know this isn't exactly C++ related, however, I was just wondering if anyone knew anything about texture mapping onto a raycaster. I'm currently trying to write my own and I'm struggling to map textures onto the walls. I know what it's doing since it's mapping the texture per ray instead of per wall, but I'm not sure how to fix it. Here's the code:
The code for the walls
void Draw3DScene()
{
sf::Color color;
sf::Texture texture;
sf::Sprite sprite;
for(int i = 0; i < NUM_OF_RAYS; i+= constant)
{
float correctedDistance = rays[i].totalDistance * cos(rays[i].angle - player.angle);
float wallHeight = (TILE_SIZE / correctedDistance) * projectionPlane;
int wallTop = (WINDOW_HEIGHT / 2) - (wallHeight / 2);
if(wallTop < 0)
wallTop = 0;
int wallBottom = (WINDOW_HEIGHT / 2) + (wallHeight / 2);
if(wallBottom > WINDOW_HEIGHT)
wallBottom = WINDOW_HEIGHT;
if(rays[i].wallContent == 1)
{
color = sf::Color::White;
texture = texture_t[0].myTexture;
if(rays[i].wasHitVertical == true)
color = sf::Color(150,150,150);
}
if(rays[i].wallContent == 2)
{
color = sf::Color::Red;
texture = texture_t[1].myTexture;
if(rays[i].wasHitVertical == true)
color = sf::Color(150,0,0);
}
if(rays[i].wallContent == 3)
{
color = sf::Color::Blue;
texture = texture_t[2].myTexture;
if(rays[i].wasHitVertical == true)
color = sf::Color(0,0,150);
}
if(rays[i].wallContent == 4)
{
color = sf::Color::Yellow;
texture = texture_t[3].myTexture;
if(rays[i].wasHitVertical == true)
color = sf::Color(150,150,0);
}
DrawLineWithColor(&gameWindow, sf::Vector2f(i, wallTop), sf::Vector2f(i, wallBottom), sf::Vector2f(i + 32,wallBottom), sf::Vector2f(i + 32, wallTop), color);
// DrawLineWithTextureTEST(&gameWindow, sf::Vector2f(i, wallTop), sf::Vector2f(i, wallBottom), sf::Vector2f(i + constant,wallBottom), sf::Vector2f(i + constant, wallTop), texture);
// DrawLineWithTextureTEST2(&gameWindow, sf::Vector2f(i, wallTop), sf::Vector2f(i, wallBottom), sf::Vector2f(i + constant,wallBottom), sf::Vector2f(i + constant, wallTop), texture, sprite);
DrawTexture(sf::Vector2f(i, wallTop), sf::Vector2f(i, wallBottom), sf::Vector2f(i + 1, wallBottom), sf::Vector2f(i + 1, wallTop), texture);
}
}
The code for drawing the textures
void DrawTexture(sf::Vector2f point1, sf::Vector2f point2, sf::Vector2f point3, sf::Vector2f point4, sf::Texture texture)
{
sf::VertexArray vertices(sf::Quads, 4);
vertices[0].position = point1;
vertices[1].position = point2;
vertices[2].position = point3;
vertices[3].position = point4;
//Test
vertices[0].texCoords = sf::Vector2f(0,0);
vertices[1].texCoords = sf::Vector2f(0,TEXTURE_HEIGHT);
vertices[2].texCoords = sf::Vector2f(TEXTURE_WIDTH,TEXTURE_HEIGHT);
vertices[3].texCoords = sf::Vector2f(TEXTURE_WIDTH,0);
gameWindow.draw(vertices, &texture);
}
I got the images from this site
https://lodev.org/cgtutor/raycasting.html
Let me know if you want the whole code instead of just snippets.