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

Author Topic: VertexArray with Texture not working - nothing drawn  (Read 1806 times)

0 Members and 1 Guest are viewing this topic.

smurf

  • Newbie
  • *
  • Posts: 36
    • View Profile
VertexArray with Texture not working - nothing drawn
« on: September 16, 2022, 04:01:53 pm »
See below for a minimal example. I've tried lots of variations of this code as well.
Nothing is drawn on the screen (except the green background color). If I instead create sprites from this same texture, I can draw those no problem.
If I add a white color to the vertices, and don't pass the RenderStates to Draw(), then it draws a white square of the expected size. So it seems to be just the texture association with the vertex array thats broken.

using SFML.Graphics;
using SFML.System;
using SFML.Window;

namespace VAD
{
    class Program
    {      
        static void Main(string[] args)
        {
            VideoMode videoMode = new VideoMode(500, 500);
            RenderWindow window = new RenderWindow(videoMode, "vertex arrays");

            VertexArray va = new VertexArray(PrimitiveType.Quads);            
            Texture theTexture = new Texture(@"C:\pics\house.png");
            RenderStates renderStates = new RenderStates();
            renderStates.Texture = theTexture;

            va.Append(new Vertex(new Vector2f(0, 0), new Vector2f(0, 0)));
            va.Append(new Vertex(new Vector2f(500, 0), new Vector2f(100, 0)));
            va.Append(new Vertex(new Vector2f(500, 500), new Vector2f(100, 100)));
            va.Append(new Vertex(new Vector2f(0, 500), new Vector2f(0, 100)));

            while (true)
            {
                window.DispatchEvents();
                window.Clear(Color.Green);
                window.Draw(va, renderStates);
                window.Display();
            }
        }
    }
}

 


This is C# obviously, using the SFML 2.5 nuget package. .NET6, Windows 10, Visual Studio 2022.
« Last Edit: September 16, 2022, 04:08:40 pm by smurf »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: VertexArray with Texture not working - nothing drawn
« Reply #1 on: September 20, 2022, 08:54:14 pm »
The issue isn't very obvious and stems from some constraints towards the CSFML interop.

The solution is, turning this
            RenderStates renderStates = new RenderStates();
            renderStates.Texture = theTexture;

into this
            RenderStates renderStates = new RenderStates(theTexture);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

smurf

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: VertexArray with Texture not working - nothing drawn
« Reply #2 on: September 23, 2022, 05:15:17 am »
Oh wow that is super tricky. Thank you!!

It seems that when using the constructor, other properties are set besides just the texture that I passed in.
But when using the parameterless ctor and just setting the texture property, those other properties (like transform) are NOT set.

maybe an improvement would be, in the setter for the properties, the transform property is also set to Identity matrix, if it has not already been set to something.

 

anything