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

Author Topic: [SOLVED] texture with VertexArray  (Read 5744 times)

0 Members and 1 Guest are viewing this topic.

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
[SOLVED] texture with VertexArray
« on: June 04, 2017, 06:59:52 am »
SFML forum,
 
 window.draw(edges, &texture);
 Program compiles and runs but the texture is never applied to the VertexArray "edges".

 I have successfully used this texture before on a quad.  The only difference is this quad is made with Points.

 I didn't know how to attach the program.

Any suggestions?
jerryd
« Last Edit: June 06, 2017, 10:54:14 pm by jerryd »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: texture with VertexArray
« Reply #1 on: June 05, 2017, 11:20:14 am »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: texture with VertexArray
« Reply #2 on: June 05, 2017, 06:27:40 pm »
eXplOit3r,
 Thanks for the reply.

 I have created a "Minimal, Complete, and Verifiable" example.
 Is there a place I can upload it or is there a way to I attach it
 or do I just put all the code in the post?

 Also to run it you would need the texture file.

jerryd

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: texture with VertexArray
« Reply #3 on: June 05, 2017, 06:32:01 pm »
Since it's minimal, it should be find posting it in the forum.
A good way to provide the texture is to host it on a permanent image hosting site (such as imgur) and then link it here.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: texture with VertexArray
« Reply #4 on: June 05, 2017, 07:06:59 pm »
SFML forum,

 Windows 7 Professional 64 bit,   SFML Version 10.0.4,   mingw-w64


 Here's my code and any texture file should work for testing since
 I know mine works because I've used it with other programs.

#include <SFML/Graphics.hpp>
#include <iostream>
#define XSIZE 600
#define YSIZE 600

class BOX
{
public:
    BOX(unsigned int count) :
    edges(sf::Points, count){}
    ~BOX() {}
   
    sf::VertexArray edges;
    int l, cx, cy;
   
    void load_edges()
    {
        int i; int x = cx-l; int y = cy-l;
        for(i = 0; i<l*2;  i++)
        {edges[i].position = sf::Vector2f(x++,y);}

        for(; i<l*4;  i++)
        {edges[i].position = sf::Vector2f(x,y++);}

        for(; i<l*6;  i++)
        {edges[i].position = sf::Vector2f(x--,y);}

        for(; i<l*8;  i++)
        {edges[i].position = sf::Vector2f(x,y--);}
    }

    void assign_variables(int x, int y, int z)
    {cx = x; cy = y; l  = z;}
   
};


int main()
{
    BOX box(400);
    box.assign_variables(XSIZE/2, YSIZE/2, 50);

    sf::Texture texture;
    if (!texture.loadFromFile("C:/users/Jerry/Desktop/sprites/concrete.jpg"))
    {std::cout << " Error loading concrete texture" << std::endl; system("pause");}
   
    sf::RenderWindow window(sf::VideoMode(XSIZE, YSIZE), "BOX");
    box.load_edges();
    sf::Event event;  
    while(window.isOpen())
    {
        while (window.pollEvent(event))
        { if(event.type == sf::Event::Closed) {window.close();} }

        window.draw(box.edges, &texture);
        window.display();
        window.clear();
    }
        return 0;
}
 

jerryd
« Last Edit: June 05, 2017, 10:58:39 pm by jerryd »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: texture with VertexArray
« Reply #5 on: June 05, 2017, 07:58:12 pm »
What are you trying to achieve? You're using sf::Points, but points can't really be textured. If you just want to display it, you need to use sf::Quad and then define all the points in order.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: texture with VertexArray
« Reply #6 on: June 05, 2017, 08:11:07 pm »
eXpl0it3r,
 I'm relatively new to SFML so I first wrote the same program using "quad"
 and the texture worked.

 Next I wanted to find out if it would also work with Points so I could
 texture any irregular shape.

 If texture won't work with Points it seems like there should have been
 some compile or run time "error" message.

 Is there any way to do it?

Thanks,
jerryd

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: texture with VertexArray
« Reply #7 on: June 05, 2017, 09:44:27 pm »
Sorry but I forgot to mention that you should put your code inside these tags: [code=cpp] [/code]
(you can edit your original post to fix it)

You can texture a (vertex) point but it only represents a single colour from that texture.
You do, however, need to give that vertex some texture coordinates.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: texture with VertexArray
« Reply #8 on: June 05, 2017, 11:08:45 pm »
Hapax,
 Thanks, I fixed my earlier post.

 Not sure what you mean by "it only represents a single colour from that
 texture".   Does it only applies 1 color to 1 Point?

 My array of Points is declared as sf::VertexArray edges; but I don't see
 any way to assign texture coordinates to "edges".

jerryd

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: texture with VertexArray
« Reply #9 on: June 06, 2017, 03:10:23 am »
Not sure what you mean by "it only represents a single colour from that
 texture".   Does it only applies 1 color to 1 Point?
Yes, exactly that. A point (sf::Points) is always one pixel and, as such, can only ever be one colour at a time. The colour, however, can be determined by a position in a texture.

A sf::VertexArray is an array (technically, more of a vector) of sf::Vertex. Each vertex has a position, a colour and, of course, a texture co-ordinate.

An example of setting the texture co-ordinate (10, 20) of the second point (edges[1]):
edges[1].texCoords = sf::Vector2f(10.f, 20.f);

Microsoft Paperclip moment:
It looks like you're trying to draw four (textured, single-pixel-wide) lines around the perimeter of a texture.
Consider using sf::LineStrip or sf::Lines instead of sf::Points. The texture co-ordinate will then just be the corners (the same used for the quad).
https://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php#primitive-types
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: texture with VertexArray
« Reply #10 on: June 06, 2017, 07:26:31 am »
Hapax,

 Thanks I get it.

 My intention is to do it at the lowest possible level(Points).

 I now plot every pixel(Point) in the box and I am able to match
 every Point in the box with the corresponding point in the texture.
 The texture is large enough that I can expand and shrink the box
 over the full size of the window.  Amazingly it seems just as fast
 as when I was just drawing the edges.

 I looked all around this page but couldn't figure out how to make this as SOLVED.

jerryd
« Last Edit: June 06, 2017, 07:28:48 am by jerryd »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: texture with VertexArray
« Reply #11 on: June 06, 2017, 09:53:06 pm »
You're welcome. Glad you got it done how you wanted.

The convention here to mark as solved tends to be editing the original post and changing the subject to start with "[SOLVED]".
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*