Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How can I assign texture to 3D buildings made with VertexArrays?  (Read 4280 times)

0 Members and 2 Guests are viewing this topic.

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
How can I assign texture to 3D buildings made with VertexArrays?
« on: December 18, 2019, 07:48:43 pm »
Hello ladies and guys!

I would like that Quads (that are the 2D parts of my 3D buildings) have a texture, so the 3D buildings look a bit more like buildings ... I tried this but they just show the same old Color but darker

        private void AssignVertices(VertexArray array, Color color, float[] coords)
        {
            int a;
            array.Clear();            
            for (a = 0; a < coords.Length / 2; a++)
                array.Append(new Vertex(new Vector2f(coords[a * 2], coords[a * 2 + 1] + this.ship.Scroll), color, new Vector2f(0, 0)));
            array.Append(new Vertex(new Vector2f(coords[0], coords[1]), color, new Vector2f(0, 0)));
            RenderStates states = new RenderStates(Graphics.building);  // this is the Texture
            this.window.Draw(array, states);
        }
 

Maybe those who remember my Massacre space shooter game will understand better ...

Thanks in advance
Pablo

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How can I assign texture to 3D buildings made with VertexArrays?
« Reply #1 on: December 18, 2019, 08:04:14 pm »
All your texture coordinates (3rd parameter of sf::Vertex) are 0, 0

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: How can I assign texture to 3D buildings made with VertexArrays?
« Reply #2 on: December 18, 2019, 10:53:51 pm »
What should they be? I put the same coords than the Quad's vertices and it is still the same ... just the old colors but darker ... I can't see the texture in the parallelepiped's faces ...

Thanks for replying

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: How can I assign texture to 3D buildings made with VertexArrays?
« Reply #3 on: December 19, 2019, 04:59:22 am »
I saw in another post in the forum that Laurent said that texCoords are like a Sprite's TextureRect ... I have no doubt he's right, but I can't understand how ...

The TextureRect is a Rectangle which has a Vector2f Position and a Vector2f Size, like ( (x, y), (width, height) ) ... but, as I have Quads in my VertexArray, then I have 4 Vertices and then 8 numbers ... those 8 numbers, what should they be?

I want the Texture, which is 80x80, covers al the parallelepiped's faces (the 3D buildings), the faces are Quads

I will apreciate very much if someone solves my mistery  :)


G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How can I assign texture to 3D buildings made with VertexArrays?
« Reply #4 on: December 19, 2019, 09:24:40 am »
Instead of having a position and a size like in a texture rect, you give the texture coordinates of the 4 corner of your texture rect. (x, y) (x + width, y) (x + width, y + height) and (x, y + height)

Here's a random texture from google images.
Let's say you want to apply the little orange building on your quads (3rd building on the 2nd row).
Your 4 texture coordinates would be (256, 128) (top left corner of the wall), (384, 128) (top right corner), (384, 256) (bottom right corner), and (256, 256) (bottom left corner).

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: How can I assign texture to 3D buildings made with VertexArrays?
« Reply #5 on: December 19, 2019, 04:09:31 pm »
Thank you very much for your time, G.!

My error was that I added a 5th Vertex to the quad

You solved my issue


 

anything