0
\$\begingroup\$

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);


    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.

enter image description here

This is what it looks like when I'm far back enough. If I get closer to the wall, the image becomes more distorted so to say. The thing is, the texture is being drawn per ray instead of per wall.

I apologize if I was being too vague with my question.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ Please fix the indentation of your code, it's currently unreadable. \$\endgroup\$ – Vaillancourt Dec 10 '20 at 15:48
  • \$\begingroup\$ After reading this question, it's not clear to me what specific problem you need help with. How does your texture mapping look at present? How does this differ from what you want? Or what step in the process of texture mapping do you need help implementing? \$\endgroup\$ – DMGregory Dec 10 '20 at 15:53
  • \$\begingroup\$ Can you show us the distorting you're seeing? Looking at your picture, it's still not clear to me what the problem is with this visual result. \$\endgroup\$ – DMGregory Dec 11 '20 at 16:12
  • \$\begingroup\$ Welcome to GDSE. Please double check the code you've posted & your markdown formatting. I attempted to vertically align your code & was unable to do so because it's not clear if it is intended to be 1 block of code or 2. You have some plain text in the middle that might have be intended as a comment or as question text. It also looks like you're missing at least one closing }. Posting clean, complete & consistently formatted code makes it easier for the community to help you find solutions. \$\endgroup\$ – Pikalek Dec 13 '20 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.