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

Author Topic: How to set texture for vertices array  (Read 11489 times)

0 Members and 1 Guest are viewing this topic.

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to set texture for vertices array
« on: March 15, 2012, 12:32:44 pm »
I tried to search but could not find what i need.
How to set texture for vertices array:
Code: [Select]

sf::Vertex* vert = new sf::Vertex[6];
...//fill vertices
...
??? SetTexture(???) ; // WHAT?
window.Draw( vert, 6, sf::Triangles);


Thanks for your time.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to set texture for vertices array
« Reply #1 on: March 15, 2012, 12:35:26 pm »
Code: [Select]
window.Draw(vert, 6, sf::Triangles, &texture);
Which is in fact a shortcut for this:
Code: [Select]
sf::RenderStates states;
states.texture = &texture;
window.Draw(vert, 6, sf::Triangles, states);

sf::RenderStates allow to set the texture, shader, transform and blend mode to use for the current draw operation (even when drawing sprites or text -- although some states are overriden in this case).
Laurent Gomila - SFML developer

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to set texture for vertices array
« Reply #2 on: March 15, 2012, 12:44:22 pm »
Thanks.

Richy19

  • Full Member
  • ***
  • Posts: 190
    • View Profile
How to set texture for vertices array
« Reply #3 on: March 15, 2012, 01:36:57 pm »
Why use sf::Vertex* vert = new sf::Vertex[6];

And not just an sf::VertexArray?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to set texture for vertices array
« Reply #4 on: March 15, 2012, 02:02:22 pm »
Quote
Why use sf::Vertex* vert = new sf::Vertex[6];

And not just an sf::VertexArray?

It sounds strange, the question is usually "why sf::VertexArray and not a simple Vertex[]?" ;)

sf::VertexArray is a wrapper around std::vector<sf::Vertex> + sf::PrimitiveType. Nothing more.

However, std::vector<sf::Vertex> (at least) would be better than this raw array of vertices managed manually.
Laurent Gomila - SFML developer

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to set texture for vertices array
« Reply #5 on: March 15, 2012, 06:32:09 pm »
Hmmm. I can't get this right. if image has alpha channel nothing is displayed, and if it does not then it only shows vertex color:

Code: [Select]

int main() {
...
sf::Texture tex;
if(!tex.LoadFromFile("smoke.tga"))
{
return 1;
}

sf::RenderStates rs;
rs.Texture = &tex;
rs.BlendMode = sf::BlendNone;

float size = 50.0f;
sf::Vector2f pos(400.0f, 300.0f);
sf::Vertex vertices[6];
vertices[0].Color        = sf::Color(255,255,255,255);
vertices[0].Position.x   = pos.x + size;
vertices[0].Position.y   = pos.y + size;
vertices[0].TexCoords    = sf::Vector2f(1.0f, 1.0f);

vertices[1].Color        = sf::Color(255,255,255,255);
vertices[1].Position.x   = pos.x + size;
vertices[1].Position.y   = pos.y - size;
vertices[1].TexCoords    = sf::Vector2f(1.0f, 0.0f);

vertices[2].Color        = sf::Color(255,255,255,255);
vertices[2].Position.x   = pos.x - size;
vertices[2].Position.y   = pos.y - size;
vertices[2].TexCoords    = sf::Vector2f(0.0f, 0.0f);

vertices[3].Color        = sf::Color(255,255,255,255);
vertices[3].Position.x   = pos.x + size;
vertices[3].Position.y   = pos.y + size;
vertices[3].TexCoords    = sf::Vector2f(1.0f, 1.0f);

vertices[4].Color        = sf::Color(255,255,255,255);
vertices[4].Position.x   = pos.x - size;
vertices[4].Position.y   = pos.y - size;
vertices[4].TexCoords    = sf::Vector2f(0.0f, 0.0f);

vertices[5].Color        = sf::Color(255,255,255,255);
vertices[5].Position.x   = pos.x - size;
vertices[5].Position.y   = pos.y + size;
vertices[5].TexCoords    = sf::Vector2f(0.0f, 1.0f);
...
wnd.Draw( vertices, 6, sf::Triangles, rs);


here is a image "smoke.tga"(32bits, uncompressed tga):
http://fs10u.sendspace.com/processupload.html

Is just a green circle with

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to set texture for vertices array
« Reply #6 on: March 15, 2012, 07:02:57 pm »
Read the doc: TexCoords are in pixels, they are not normalized.
Laurent Gomila - SFML developer

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to set texture for vertices array
« Reply #7 on: March 15, 2012, 07:07:55 pm »
Thats it hanks.

But its strange that i have specified BlendNone yet it shows no white "borders" aroung green circle? Whats wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to set texture for vertices array
« Reply #8 on: March 15, 2012, 08:17:51 pm »
Quote
But its strange that i have specified BlendNone yet it shows no white "borders" aroung green circle? Whats wrong?

I don't get it. Maybe you could show a screenshot?
Laurent Gomila - SFML developer

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to set texture for vertices array
« Reply #9 on: March 15, 2012, 08:58:39 pm »
Ok. Here is a test src:
Code: [Select]

#include <iostream>
#include <SFML/Graphics.hpp>

#pragma comment(lib, "sfml-graphics-d.lib")
#pragma comment(lib, "sfml-window-d.lib")
#pragma comment(lib, "sfml-system-d.lib")

int main()
{
try
{
sf::RenderWindow wnd(sf::VideoMode( 200, 200, 32 ), "Test vertices");

sf::Texture tex;
if(!tex.LoadFromFile("smoke.tga"))
{
throw std::runtime_error("Failed to load texture!");
}

float tw = (float)tex.GetWidth();
float th = (float)tex.GetHeight();

sf::Transform transform;
transform.Translate(100.0f, 100.0f);
sf::RenderStates rs;
rs.Transform = transform;
rs.Texture   = &tex;
rs.BlendMode = sf::BlendNone;

float size = 50.0f;
sf::Vertex vertices[6];
vertices[0].Color        = sf::Color(255,255,255,255);
vertices[0].Position.x   = size;
vertices[0].Position.y   = size;
vertices[0].TexCoords    = sf::Vector2f(tw, th);

vertices[1].Color        = sf::Color(255,255,255,255);
vertices[1].Position.x   = size;
vertices[1].Position.y   = -size;
vertices[1].TexCoords    = sf::Vector2f(tw, 0.0f);

vertices[2].Color        = sf::Color(255,255,255,255);
vertices[2].Position.x   = -size;
vertices[2].Position.y   = -size;
vertices[2].TexCoords    = sf::Vector2f(0.0f, 0.0f);

vertices[3].Color        = sf::Color(255,255,255,255);
vertices[3].Position.x   = size;
vertices[3].Position.y   = size;
vertices[3].TexCoords    = sf::Vector2f(tw, th);

vertices[4].Color        = sf::Color(255,255,255,255);
vertices[4].Position.x   = -size;
vertices[4].Position.y   = -size;
vertices[4].TexCoords    = sf::Vector2f(0.0f, 0.0f);

vertices[5].Color        = sf::Color(255,255,255,255);
vertices[5].Position.x   = -size;
vertices[5].Position.y   = size;
vertices[5].TexCoords    = sf::Vector2f(0.0f, th);


while(wnd.IsOpen())
{
sf::Event event;
while( wnd.PollEvent(event) )
{
switch(event.Type)
{
case sf::Event::Closed:
{
wnd.Close();
return false;
}break;
};
}
wnd.Clear(sf::Color(0, 0, 0));

wnd.Draw( vertices, 6, sf::Triangles, rs);

wnd.Display();
}
}
catch( const std::runtime_error& e)
{
std::cout << e.what() << std::endl;
std::cin.ignore();
return EXIT_FAILURE;
}
std::cin.ignore();
return EXIT_SUCCESS;
}


render screenshot:


tga image in Photoshop (saved as 32bits uncompressed). i have recolored "borders" to be blue for easier explaination:


Image uploaded to sendspace:
http://www.sendspace.com/file/zqgnlg

I expected render (with BlendNone) to show blue "borders" aroun green circle.

I expected this with BlendNone (edit in Photoshop):

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to set texture for vertices array
« Reply #10 on: March 15, 2012, 10:36:56 pm »
It was a bug, it's fixed now. Thanks for your feedback :)
Laurent Gomila - SFML developer

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to set texture for vertices array
« Reply #11 on: March 15, 2012, 10:54:01 pm »
Why don't you release prebuilt binaries?
Why do everybody likes this stupid CMake? Its junk generator!

Oh god why are you tortuering me??? O why did you make my life harder?

I dont understand this from tutorial (I am using VS2010):
Quote

With Visual C++, you can either run CMake from the "Visual Studio command prompt" available from the start menu, or call the vcvars32.bat file of your Visual Studio installation; it will setup the environment variables properly.

> your_visual_studio_folder\VC\bin\vcvars32.bat
> cmake


Do i need both of these programs? Whats with this combination?

What to choose here for VS2010 c++

a vs 2010 or NMake makefiles?

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to set texture for vertices array
« Reply #12 on: March 15, 2012, 11:11:18 pm »
Quote

For example, to generate a Visual Studio 10 solution, you must choose "Visual Studio 10". To generate makefiles usable with Visual C++, select "NMake makefiles".


What is a difference beetwen vs2010 and Visual c++ in this context?
Now i don't know what to choose?
What is NMake makefiles JOM?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to set texture for vertices array
« Reply #13 on: March 15, 2012, 11:21:31 pm »
Quote
Why don't you release prebuilt binaries?

Because it's a development version, it's not finished yet.

Quote
Why do everybody likes this stupid CMake? Its junk generator!

What do you suggest instead?
Don't denigrating something just because you don't understand it ;)

Quote
Oh god why are you tortuering me??? O why did you make my life harder?

If you take some time to understand what you do, not only you'll learn useful stuff, but you'll realize that it's much simpler than what you think. Blindly following steps in a tutorial is good if you want things done quickly, but it's not enough in the long run.

Quote
I dont understand this from tutorial (I am using VS2010)

vcvars32.bat will set the appropriate environment variables so that you can then use the VC++ compiler. You need to call it before doing anything with VC++ (except running the Visual Studio IDE, of course).
Then, "cmake" (or "cmake-gui") simply runs CMake.
So yes you need to call both, in this order.

Quote
a vs 2010 or NMake makefiles?

Whatever you like. I recommend NMake makefiles, so that you can compile with a simple "nmake" in the console.
"Visual Studio 2010" will create a solution that you can open in the IDE.

Quote
What is a difference beetwen vs2010 and Visual c++ in this context?

Visual Studio is the IDE, Visual C++ is the compiler.

Quote
What is NMake makefiles JOM?

JOM is a tool written by the Qt team to enable multi-core compilation with nmake (which doesn't support it natively).
Laurent Gomila - SFML developer

morando

  • Newbie
  • *
  • Posts: 16
    • View Profile
How to set texture for vertices array
« Reply #14 on: March 15, 2012, 11:39:50 pm »
Ok. i manage somehow to make solution for vs2010 and compiled debug & release libs of SMFL. Now to replace all function names to lowercase   :cry:

 

anything