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

Author Topic: [solved] RenderStates causing blank images to appear  (Read 5338 times)

0 Members and 1 Guest are viewing this topic.

Radnen

  • Newbie
  • *
  • Posts: 17
    • View Profile
[solved] RenderStates causing blank images to appear
« on: July 09, 2013, 10:35:37 am »
Can someone help me with a weird issue? I'm trying to draw a transformed image. An image where the x's and y's can be whatever so it can look skewed, pinched, etc. with a texture.

The code I do that is here:
        [JSFunction(Name = "transformBlit")]
        public void TransformBlit(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
        {
            Vertex[] array = new Vertex[4];
            Color c = new Color(255, 255, 255);

            array[0] = new Vertex(new Vector2f((float)x1, (float)y1), c, new Vector2f(0, 0));
            array[1] = new Vertex(new Vector2f((float)x2, (float)y2), c, new Vector2f((float)_image.Size.X, 0));
            array[2] = new Vertex(new Vector2f((float)x3, (float)y3), c, new Vector2f((float)_image.Size.X, (float)_image.Size.Y));
            array[3] = new Vertex(new Vector2f((float)x4, (float)y4), c, new Vector2f(0, (float)_image.Size.Y));

            Program._window.Draw(array, PrimitiveType.Quads, state);
        }
 

Excuse the float conversions, I can only get doubles from the JS environment. The state object has the Texture property set to the texture I'm using. It should work! I am loading the texture before setting the state, and making sure it's not disposed in any way. Drawing the texture alone as a sprite works, but not this method. And this is AFAIK the only method where you can skew the drawn image.

You can try the code out at: https://github.com/Radnen/sphere-sfml
Just download, run in visual studio and have at it. The demo code should draw an image in the upper-left that is not a spaceship, but a face. As you can see, it's invisible even though transformBlit is being used. Removing the state will make it draw a white rectangle. So I know it's working, but just not applying the textured state.
« Last Edit: July 09, 2013, 11:38:10 pm by Radnen »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: RenderStates causing blank images to appear
« Reply #1 on: July 09, 2013, 09:53:42 pm »
So if you can see a white square when you remove the render states then its obvious that your vertex array is working correctly. That means the problem is then with how you load and apply the texture to your render state. So please show the code of how you load and apply a texture to your render state.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Radnen

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: RenderStates causing blank images to appear
« Reply #2 on: July 09, 2013, 10:25:54 pm »
The code to do that is here:

        private Texture _image;
        private Sprite _sprite;
        private RectangleShape _shape = new RectangleShape();
        private RenderStates state = new RenderStates();

        public ImageInstance(ObjectInstance proto, string filename)
            : base(proto)
        {
                _image = new Texture(GlobalProps.BasePath + "\\images\\" + filename);
                _sprite = new Sprite(_image);
                PopulateFunctions();
                state.Texture = _image;
                DefineProperty("width", new PropertyDescriptor(_image.Size.X, PropertyAttributes.Sealed), true);
                DefineProperty("height", new PropertyDescriptor(_image.Size.Y, PropertyAttributes.Sealed), true);
        }
 

Drawing the sprite works, the texture loads just fine.

In fact just doing this makes the image invisible:
Program._window.draw(_sprite, new RenderStates());
 
« Last Edit: July 09, 2013, 10:33:02 pm by Radnen »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderStates causing blank images to appear
« Reply #3 on: July 09, 2013, 10:35:40 pm »
Laurent Gomila - SFML developer

Radnen

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: RenderStates causing blank images to appear
« Reply #4 on: July 09, 2013, 11:37:28 pm »
Thank you! I was reading the docs and forgot about that C# idiom. I set up default parameters and now it's working just fine.

Also another strange thing:
VertexArray array = new VertexArray(PrimitiveTypes.Quad, 4);

array[0].Position = new Vector2f(0, 0);
 

Is an error due to not being able to set a returned value. You really can't fix that it's just another weird C# thing that makes coding a bit more interesting. :P So that means for the most part VertexArray is just about useless in .NET.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [solved] RenderStates causing blank images to appear
« Reply #5 on: July 10, 2013, 07:52:20 am »
Really? I thought this was supposed to work.
Laurent Gomila - SFML developer

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: RenderStates causing blank images to appear
« Reply #6 on: July 10, 2013, 02:39:17 pm »
You really can't fix that it's just another weird C# thing that makes coding a bit more interesting. :P So that means for the most part VertexArray is just about useless in .NET.

Really? I thought this was supposed to work.

It does work, just not in that usage of it. This is another case where a weird C# thing gets in the way again. Basically you can never modify the return value of a struct, but you can make a copy of it and then assign it back again. The reason being because when you modify the returned value all you are doing is modifying a copy that would be created and thrown away in a single line. Hence the compiler doesn't allow you to waste resources that way.

Notice that I used an index access property just to show the similarity to the vertex array, just note that the same thing happens even if you use plain property with get/set.

    class Program
    {
        static void Main()
        {
            TestStruct teststruct = new TestStruct("Test String Text");
            teststruct.TestString = "This will work";
            TestIndexerClass dummyindexerclass = new TestIndexerClass();
            dummyindexerclass[1].TestString = "This will not work"; // changing the returned struct is not valid C# - comment this line to make the code compile
            TestStruct secondteststruct = dummyindexerclass[1]; //secondteststruct is a copy from our test indexer class
            secondteststruct.TestString = "Now we can change our copy";
            dummyindexerclass[1] = secondteststruct; //now we can assign our copy back to replace the original
        }
    }
    public struct TestStruct
    {
        public string TestString;
        public TestStruct(string NewTestString)
        {
            TestString = NewTestString;
        }
    }
    public class TestIndexerClass
    {
        TestStruct ourstruct = new TestStruct("Unchangeable Text");
        public TestStruct this[int i]
        {
            get
            {
                return ourstruct;
            }
            set
            {
                ourstruct = value;
            }
        }
    }


Now to make it work with vertex arrays just do the following.

VertexArray array = new VertexArray(PrimitiveTypes.Quad, 4);

Vertex vtx = array[0];
vtx.Position = new Vector2f(0, 0);
array[0] = vtx;


And a few sources...
http://stackoverflow.com/questions/13036702/weird-struct-behavior-explanation-would-be-great
http://answers.unity3d.com/questions/425510/cant-modify-struct-variables-c.html
« Last Edit: July 10, 2013, 02:50:57 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [solved] RenderStates causing blank images to appear
« Reply #7 on: July 10, 2013, 02:59:35 pm »
I hate C# :D
Laurent Gomila - SFML developer