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

Author Topic: VertexArray displaying Dominant color, not the actual image  (Read 2300 times)

0 Members and 2 Guests are viewing this topic.

JTeck

  • Newbie
  • *
  • Posts: 35
    • View Profile
VertexArray displaying Dominant color, not the actual image
« on: April 06, 2014, 11:34:34 am »
Hi, I've recently learn't how to use VertexArray's as I read that it was much faster than loading each image individually.

I have my code for the VertexArray working, just when I goto load the image to display as a the 'tile' it shows the most dominant color used in the image, which is green, not the image.

Can you explain why it's doing this?

sf::Texture t_grass;//Called when program is initialized

        while (window.isOpen()){
                sf::Event event;
                while (window.pollEvent(event)){
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                t_grass.loadFromFile("images/grass.png");//Loads the image
                window.clear();

                sf::VertexArray lines(sf::Quads, 4);
                lines[0].position = sf::Vector2f(0, 0);
                lines[1].position = sf::Vector2f(0, 50);
                lines[2].position = sf::Vector2f(50, 50);
                lines[3].position = sf::Vector2f(50, 0);
                window.draw(lines, &t_grass);
                window.display();
        }
 

The image loads fine, so I'm lost with all of it.
Cheers
« Last Edit: April 07, 2014, 09:40:15 am by JTeck »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: VertexArray displaying Dominant color, not the actual image
« Reply #1 on: April 06, 2014, 11:46:06 am »
Hi, I've recently learn't how to use VertexArray's as I read that it was much faster than loading each image individually.
You can't compare vertex arrays with image loading. You probably mean drawing sprites vs. drawing vertex arrays.

You also have to set the texture coordinates of the vertices, not only the position. Have you read the tutorials and documentation?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

JTeck

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: VertexArray displaying Dominant color, not the actual image
« Reply #2 on: April 06, 2014, 11:53:04 am »
Ohh, I thought I was forgetting something :)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: VertexArray displaying Dominant color, not the actual image
« Reply #3 on: April 06, 2014, 12:00:51 pm »
Why are you loading the image inside the loop?
Just load it once.

JTeck

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: VertexArray displaying Dominant color, not the actual image
« Reply #4 on: April 06, 2014, 12:10:56 pm »
Ok, I've got that working now, just I added it to my loops for my tile map, and it draws it all, but this happens.


This is the loop being to draw it, doing x*x instead of x+x crashes with a First chance exception.
        for (int x = 0; x < 250; x++){
                for (int y = 0; y < 250; y++){
                        sf::VertexArray lines(sf::Quads, 4);
                        lines[0].position = sf::Vector2f(x, y);
                        lines[1].position = sf::Vector2f(x, y+y);
                        lines[2].position = sf::Vector2f(x+x, y+y);
                        lines[3].position = sf::Vector2f(x+x, y);

                        lines[0].texCoords = sf::Vector2f(0, 0);
                        lines[1].texCoords = sf::Vector2f(0, 16);
                        lines[2].texCoords = sf::Vector2f(16, 16);
                        lines[3].texCoords = sf::Vector2f(16, 0);
                        window.draw(lines, &t_grass);
                }
        }
 

Did I type something wrong?
Cheers

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: VertexArray displaying Dominant color, not the actual image
« Reply #5 on: April 06, 2014, 12:23:35 pm »
Why x+x or even x*x? Multiplication doesn't make sense here.

Try to start in small steps. First draw a single quad without a loop. If it works, then add the loop, and so on...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

JTeck

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: VertexArray displaying Dominant color, not the actual image
« Reply #6 on: April 06, 2014, 12:36:28 pm »
I have done that, but I haven't been able to get the drawing going. I've set the points, but It doesn't do it correctly, I cant see what i'm doing wrong.

JTeck

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: VertexArray displaying Dominant color, not the actual image
« Reply #7 on: April 06, 2014, 12:43:12 pm »
Solved, didn't look far enough into http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php it fixed my problem :)

JTeck

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: [Solved] VertexArray displaying Dominant color, not the actual image
« Reply #8 on: April 07, 2014, 09:38:49 am »
Got it working, but when drawing it is still slow, am I missing something?

        t_grass.loadFromFile("images/grass.png");

        for (int x = 0; x < 150; x++){
                for (int y = 0; y < 150; y++){
                        sf::VertexArray lines(sf::Quads, 4);
                        lines[0].position = sf::Vector2f(x * 16, y * 16);
                        lines[1].position = sf::Vector2f((x + 1) * 16, y * 16);
                        lines[2].position = sf::Vector2f((x + 1) * 16, (y + 1) * 16);
                        lines[3].position = sf::Vector2f(x * 16, (y + 1) * 16);

                        lines[0].texCoords = sf::Vector2f(0 * 16, 0 * 16);
                        lines[1].texCoords = sf::Vector2f((0 + 1) * 16, 0 * 16);
                        lines[2].texCoords = sf::Vector2f((0 + 1) * 16, (0 + 1) * 16);
                        lines[3].texCoords = sf::Vector2f(0 * 16, (0 + 1) * 16);
                        window.draw(lines, &t_grass);
                }
        }
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: VertexArray displaying Dominant color, not the actual image
« Reply #9 on: April 07, 2014, 09:43:43 am »
Yes, you're missing the most important point. You should read the tutorial, it explains very well how and why you can get better performances with a VertexArray, and the examples at the end should be easy to understand.
Laurent Gomila - SFML developer

 

anything