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

Author Topic: SFML 2.0 Using shaders together with rendering text  (Read 14487 times)

0 Members and 2 Guests are viewing this topic.

r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
SFML 2.0 Using shaders together with rendering text
« on: June 19, 2012, 05:22:04 am »
Hi,

I just started using SFML and I am thoroughly impressed. =D

I am having a bit of a problem with the shaders though. It seems that after rendering a shader, when you render text, the texture that was passed to the shader doesnt render anymore.

My shader is just a basic vertex and fragment shader, no fancy stuff, just straight transform and compute the color.

Is there a rule of thumb when working with shaders that use textures? Based on the Shader class, the SetParameter function does not have a parameter to set the texture slot number, so 0 is always used. Could there be a conflict and the texture in the shader is overwritten?

Thanks,
r0undh0u53

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 Using shaders together with rendering text
« Reply #1 on: June 19, 2012, 08:07:53 am »
Can you show your code?

And please next time post in the appropriate forum (Help > Graphics), not on the first one that you see.
Laurent Gomila - SFML developer

r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2.0 Using shaders together with rendering text
« Reply #2 on: June 19, 2012, 02:14:00 pm »
Oh sorry about that. I got confused by all the SFML 2.0 inquiries in the General Discussion.

Here is my test code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

using SFML.Graphics;
using SFML.Window;

namespace VertexArrayTest
{
    static class Program
    {
        static TimeSpan elapsedTime;
        static TimeSpan totalTime;

        // Create a clock for measuring the time elapsed
        static Stopwatch stopwatch = new Stopwatch();

        static TimeSpan ComputeTimeSlice()
        {
            stopwatch.Stop();
            TimeSpan timeslice = stopwatch.Elapsed;
            stopwatch.Reset();
            stopwatch.Start();
            return timeslice;
        }

        static Text text;
        static Shader shader;
        static VertexArray vertexArray;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            elapsedTime = totalTime = TimeSpan.Zero;
            stopwatch.Start();

            // Create the main window
            RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML Shader");
            window.SetVerticalSyncEnabled(true);

            // Setup event handlers
            window.Closed += new EventHandler(OnClosed);
            window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);

            Texture texture = new Texture("sampleTiles.png");
            Font font = new Font("sansation.ttf");
            text = new Text("Test", font, 20);
            text.Position = new Vector2f(10, 10);

            vertexArray = new VertexArray(PrimitiveType.Quads);
            vertexArray.Append(new Vertex(new Vector2f(0, 0), Color.White, new Vector2f(0, 0)));
            vertexArray.Append(new Vertex(new Vector2f(100, 0), Color.White, new Vector2f(1, 0)));
            vertexArray.Append(new Vertex(new Vector2f(100, 100), Color.White, new Vector2f(1, 1)));
            vertexArray.Append(new Vertex(new Vector2f(0, 100), Color.White, new Vector2f(0, 1)));

            // Load the shader
            shader = new Shader("basic.vert", "basic.frag");
            shader.SetParameter("texture", texture);
            RenderStates states = new RenderStates(shader);

            // Start the game loop
            while(window.IsOpen())
            {
                var elapsed = ComputeTimeSlice();

                elapsedTime = elapsed;
                totalTime += elapsed;

                var floatElapsed = (float)totalTime.TotalSeconds;

                // Process events
                window.DispatchEvents();

                // Clear the window
                window.Clear(Color.Blue);

                // Draw the vertices
                window.Draw(vertexArray, states);

                // Draw the text
                window.Draw(text);

                // Finally, display the rendered frame on screen
                window.Display();
            }
        }

        /// <summary>
        /// Function called when the window is closed
        /// </summary>
        static void OnClosed(object sender, EventArgs e)
        {
            RenderWindow window = (RenderWindow)sender;
            window.Close();
        }

        /// <summary>
        /// Function called when a key is pressed
        /// </summary>
        static void OnKeyPressed(object sender, KeyEventArgs e)
        {
            RenderWindow window = (RenderWindow)sender;

            switch(e.Code)
            {
                // Escape key: exit
                case Keyboard.Key.Escape:
                    window.Close();
                    break;

                default:
                    break;
            }
        }
    }
}
 

And here is my shader :

basic.vert :
void main()
{
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
    gl_FrontColor = gl_Color;
}
 

basic.frag :
uniform sampler2D texture;

void main()
{
    gl_FragColor = texture2D(texture, gl_TexCoord[0].xy) * gl_Color;   
}
 

r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2.0 Using shaders together with rendering text
« Reply #3 on: June 19, 2012, 02:16:28 pm »
Removing the call to

// Draw the text
window.Draw(text);
 

will show the texture.

Edit: I forgot to mention that I am using the .NET binding of SFML 2.0 RC.
« Last Edit: June 19, 2012, 02:31:16 pm by r0undh0u53 »

r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2.0 Using shaders together with rendering text
« Reply #4 on: June 19, 2012, 02:40:37 pm »
I may have found a solution to my issue...

I wrapped my render text call with Push/Pop state calls:

// Draw the text
window.PushGLStates();
window.Draw(text);
window.PopGLStates();
 

But I am wondering though if that is really how you would do it if you are using Shaders. So would that mean I have to group & wrap all of my non shader draw calls with push/pop state calls?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 Using shaders together with rendering text
« Reply #5 on: June 19, 2012, 02:54:12 pm »
I think this bug is already fixed. Could you try the latest sources?
Laurent Gomila - SFML developer

r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2.0 Using shaders together with rendering text
« Reply #6 on: June 19, 2012, 03:27:42 pm »
I just downloaded the latest SFML.Net source and was able to build. My question is what version of SFML should I use with it? can I use the SFML 2.0 RC C++ DLLs with it or should I also download and build the latest SFML codes?

r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2.0 Using shaders together with rendering text
« Reply #7 on: June 19, 2012, 03:28:33 pm »
Ooops! Nevermind! I just saw the "extlibs" folder. =P

r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2.0 Using shaders together with rendering text
« Reply #8 on: June 19, 2012, 03:35:50 pm »
I tried it with the latest codes but it's the same. The texture still doesn't show.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 Using shaders together with rendering text
« Reply #9 on: June 19, 2012, 03:41:03 pm »
Texture coordinates in Vertex must be pixels (0 .. texture size), not normalized coordinates (0 .. 1).
Laurent Gomila - SFML developer

r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2.0 Using shaders together with rendering text
« Reply #10 on: June 19, 2012, 03:46:43 pm »
Hmmm... I'm getting some weird results, but I see what you mean.

My texture is 32x32 pixels long. Using just the Shader, I can use floating point coordinates ala OpenGL (0.0 ... 1.0). So when using with Text, it seems that the upper left part of the texture is being shown. So upon trying your suggestion it seemed to work, but I noticed something weird. My texture is 32x32 but I needed to treat it like it was 128x128 just to see the whole texture:

            vertexArray = new VertexArray(PrimitiveType.Quads);
            vertexArray.Append(new Vertex(new Vector2f(0, 0), Color.White, new Vector2f(0, 0)));
            vertexArray.Append(new Vertex(new Vector2f(100, 0), Color.White, new Vector2f(128, 0)));
            vertexArray.Append(new Vertex(new Vector2f(100, 100), Color.White, new Vector2f(128, 128)));
            vertexArray.Append(new Vertex(new Vector2f(0, 100), Color.White, new Vector2f(0, 128)));
 

I don't understand why is this the case?


r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2.0 Using shaders together with rendering text
« Reply #11 on: June 19, 2012, 03:53:25 pm »
Ok so I did a bit of experimentation. Instead of using the Shader, I ditched the shader and applied the texture directly into the RenderStates :

// Load the shader
//shader = new Shader("basic.vert", "basic.frag");
//shader.SetParameter("texture", texture);
//RenderStates states = new RenderStates(shader);
RenderStates states = new RenderStates(texture);
 

And then modified the texture coordinates to use 32:

vertexArray = new VertexArray(PrimitiveType.Quads);
vertexArray.Append(new Vertex(new Vector2f(0, 0), Color.White, new Vector2f(0, 0)));
vertexArray.Append(new Vertex(new Vector2f(100, 0), Color.White, new Vector2f(32, 0)));
vertexArray.Append(new Vertex(new Vector2f(100, 100), Color.White, new Vector2f(32, 32)));
vertexArray.Append(new Vertex(new Vector2f(0, 100), Color.White, new Vector2f(0, 32)));
 

It worked!

But still, I'd like to understand what happened when I used the Shader. Why are the textures seem to be scaled 4x larger when passed on to the shader?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 Using shaders together with rendering text
« Reply #12 on: June 19, 2012, 03:55:05 pm »
Have you tried the Shader example? If it works (and it should!) then have a look at its code and shaders to see what's wrong with yours.
Laurent Gomila - SFML developer

r0undh0u53

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: SFML 2.0 Using shaders together with rendering text
« Reply #13 on: June 19, 2012, 04:16:55 pm »
Yup. It was my springboard to start with. It works and I understand the code, but there is no sample wherein a Shader, rendering a texture, is mixed with Text in a separate draw call. In the Shader sample shaders are applied to Text.

So based on the Shader sample, I created my test code to see how a shader would behave with Text.

Could this be a bug in SFML?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 Using shaders together with rendering text
« Reply #14 on: June 19, 2012, 04:38:45 pm »
Quote
It works and I understand the code, but there is no sample wherein a Shader, rendering a texture, is mixed with Text in a separate draw call. In the Shader sample shaders are applied to Text.
There is the UI text, which is drawn in a separate call with no shader. So it's very similar to what you do.
Laurent Gomila - SFML developer

 

anything