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

Author Topic: VertexArray is not drawn  (Read 5725 times)

0 Members and 1 Guest are viewing this topic.

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
VertexArray is not drawn
« on: May 27, 2014, 12:40:09 am »
Hi,

i basically took the examples code of the tile map and copied it almost identically in my testproject.
Sadly the loaded texture is not drawn, i only see a white square instead of the tiles.

Here is my code:

Note that all needed values for the VertexArray functions have valid values (i.e. the texture was definatly loaded and is drawn just fine when i test it with a sprite, height, width etc. have their proper value).
Also this was just made ad hoc, so its pretty unclean.

 See below for the code

Apparently it is the texture that does not get bound to the vertices properly?! Because i get a white square of the expected size, which means the vertexarray is somewhat drawn, just without texture attached.
« Last Edit: May 28, 2014, 03:27:12 pm by kingcools »

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: VertexArray is not drawn
« Reply #1 on: May 28, 2014, 03:26:38 pm »

        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
            // apply the tileset texture
            states.texture = &tileset;

            // draw the vertex array
            target.draw(m_vertices, states);
        }

        void populateVertexArray() // Fill the VertexArray
        {
            // resize the vertex array to fit the level size
            m_vertices.setPrimitiveType(sf::Quads);
            m_vertices.resize(mapwidth * mapheight * 4);

            // populate the vertex array, with one quad per tile
            for (unsigned int i = 0; i < mapwidth; ++i)//width of the map in number of tiles (10 for me)
            {
                for (unsigned int j = 0; j < mapheight; ++j)
                {
                    // get the current tile number
                    int tileNumber = tiles[i + j * mapwidth]-1; //-1 because the editor im using starts with 1, not 0
                    // find its position in the tileset texture
                    int tu = tileNumber % (tileset.getSize().x / tilewidth); // Width of a tile in pixel (32 for me)
                    int tv = tileNumber / (tileset.getSize().x / tilewidth);

                    // get a pointer to the current tile's quad
                    sf::Vertex* quad = &m_vertices[(i + j * mapwidth) * 4];

                    // define its 4 corners
                    quad[0].position = sf::Vector2f(i * tilewidth, j * tileheight);
                    quad[1].position = sf::Vector2f((i + 1) * tilewidth, j * tileheight);
                    quad[2].position = sf::Vector2f((i + 1) * tilewidth, (j + 1) * tileheight);
                    quad[3].position = sf::Vector2f(i * tilewidth, (j + 1) * tileheight);

                    // define its 4 texture coordinates
                    quad[0].texCoords = sf::Vector2f(tu * tilewidth, tv * tileheight);
                    quad[1].texCoords = sf::Vector2f((tu + 1) * tilewidth, tv * tileheight);
                    quad[2].texCoords = sf::Vector2f((tu + 1) * tilewidth, (tv + 1) * tileheight);
                    quad[3].texCoords = sf::Vector2f(tu * tilewidth, (tv + 1) * tileheight);

                }
            }
        }

};

int main()
{
    sf::RenderWindow window(sf::VideoMode(512, 256),"RPG");

    TileMap t("testmap.tmx");
    t.populateVertexArray();




        window.setFramerateLimit(20);
        while(window.isOpen())
        {

        sf::Event evt;
        //Loop through all window events
        while(window.pollEvent(evt))
        {
            if(evt.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(t);
        window.display();
        }

    return 0;
}
 

Reduced the code to the important parts. Assume all variables to have the right values (checked every single one).

Problem description:
Vertex Array is drawn but the image apparently is not attached to it which resulsts in a white blank square instead of the tilemap being drawn. If i only draw the tileset via window.draw(sf::Sprite(mytileset_tex)) it works.

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: VertexArray is not drawn
« Reply #2 on: May 30, 2014, 05:37:46 pm »
Am i doing it right and nobody knows whats wrong (i even updated my graphic card driver, didnt change anything) or does my post bother you somehow? Is something unclear?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: VertexArray is not drawn
« Reply #3 on: May 30, 2014, 05:57:26 pm »
I don't immediately see a mistake, have you checked with a debugger whether your computations are correct?

And as a general advice, you could still shorten the code so that everything fits into main(). Just use a single tile (not a whole tile map) if that's enough to reproduce the problem. The shorter the code, the more likely you'll get meaningful answers.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: VertexArray is not drawn
« Reply #4 on: May 30, 2014, 07:28:26 pm »
hi, here is a compressed example:

//#include "tilemap.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(512, 256),"RPG");
    //TileMap t("testmap.tmx");

        window.setFramerateLimit(20);

    sf::VertexArray m_vertices;
    sf::Texture m_tileset;
    m_tileset.loadFromFile("Tileset.png");

    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(4);

    m_vertices[0].position = sf::Vector2f(0, 0);
    m_vertices[1].position = sf::Vector2f(32,0);
    m_vertices[2].position = sf::Vector2f(32,32);
    m_vertices[3].position = sf::Vector2f(0,32);

    m_vertices[0].texCoords = sf::Vector2f(0,0);
    m_vertices[1].texCoords = sf::Vector2f(32,0);
    m_vertices[2].texCoords = sf::Vector2f(32,32);
    m_vertices[3].texCoords = sf::Vector2f(0,32);

    sf::RenderStates states;
    states.texture = &m_tileset;
        while(window.isOpen())
        {

        sf::Event evt;
        //Loop through all window events
        while(window.pollEvent(evt))
        {
            if(evt.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(m_vertices,states);
        window.display();
        }
    return 0;
}
 

The file is in the attachment.

Why sfml draws is this:


Im using one of the more recent 2.1 SFML versions.

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: VertexArray is not drawn
« Reply #5 on: May 30, 2014, 07:39:44 pm »
I tried your code on my PC and everything works fine. Try checking if Tileset is correctly load

if( !m_tileset.loadFromFile("Tileset.png") )
{
return -1; //error
}
 

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: VertexArray is not drawn
« Reply #6 on: May 30, 2014, 07:43:44 pm »
it is loaded correctly (it is drawn when i use std::Sprite spr(mytexture) -> draw(spr)).

Damn, so it really is my system. Strange, how can i find out what is wrong, never had any issue with sfml till now :/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: VertexArray is not drawn
« Reply #7 on: May 30, 2014, 08:05:17 pm »
When you compile in debug mode and use debug SFML binaries, is there an error message on the console?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: VertexArray is not drawn
« Reply #8 on: May 30, 2014, 08:14:18 pm »
no, nothing, i always used debug mode and there never was any sort of message in the console.

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: VertexArray is not drawn
« Reply #9 on: June 01, 2014, 03:28:08 pm »
Any way to figure out whats wrong? Or shall i just try out some other versions (im fearing that i need to rebuild thor and sfgui as well^^)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: VertexArray is not drawn
« Reply #10 on: June 01, 2014, 03:53:39 pm »
Maybe sf::Quads is not supported by your graphics card or driver. That would explain why sf::Sprite (which uses sf::TrianglesStrip) works.

Try another primitive type (triangles or triangle strips), and make sure your graphics driver is up-to-date.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: VertexArray is not drawn
« Reply #11 on: June 01, 2014, 05:43:15 pm »
Hi, i recently upgraded my driver so it is supposed to be up to date.
I tried sf::Triangles but with the same result :/ I dont have some esoteric graphics card but an AMD Radeon HD 6900, any known compatibiliy problems for that series?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: VertexArray is not drawn
« Reply #12 on: June 01, 2014, 05:56:12 pm »
Have you adapted the vertex order accordingly? What about triangle strips?

You could also try the latest SFML and Thor revisions (master branch), but I'm not aware of a vertex array problem that has recently been fixed.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: VertexArray is not drawn
« Reply #13 on: June 01, 2014, 06:38:19 pm »
Have you adapted the vertex order accordingly? What about triangle strips?

You could also try the latest SFML and Thor revisions (master branch), but I'm not aware of a vertex array problem that has recently been fixed.

tried both sf::Triangle and Trianglestrips, both show the same graphics as sf::Quad.
I think i will try an older graphic card driver first, i just tried out sfGUI and the rendering didnt work there as well as far as i could tell (first time using), only the text inside the widgets was shown but the widets themselves werent (saw black background instead).
This seriously sucks, i need both sf::VertexArray and sfGUI for my project :/

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: VertexArray is not drawn
« Reply #14 on: June 01, 2014, 09:16:29 pm »
Hi, important update:

i built the master branch just now and it works!  ;D ;D ;D haha, puh, im relieved.
Something was wrong apparently, sadly no idea what.

 

anything