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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - r0undh0u53

Pages: [1]
1
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« on: June 19, 2012, 09:45:19 pm »
Yeah, It was partly my gut feel too that there must be some sort of conflict in the texture index seeing that there is no way to set which index you want to bind the texture to. If you run the code, you will notice that the texture shows but only for a split second. So it seems that the draw call to render the text does something to the underlying render states.

At least there is a way to work around it, but the problem is if we extend the shader code to use gl_TexCoord[1..n].

2
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« on: June 19, 2012, 08:05:24 pm »
Ok so I experimented with the idea a bit. I modified my fragment shader to accept 2 textures. The second texture has some parts that are transparent and will be applied as a decal on top of the first texture:

uniform sampler2D texture;
uniform sampler2D texture2;

void main()
{
    vec4 color1 = texture2D(texture, gl_TexCoord[0].st);
    vec4 color2 = texture2D(texture2, gl_TexCoord[0].st);

    vec3 temp = mix(color1.rgb, color2.rgb, color2.a);

    gl_FragColor = vec4(temp, 1) * gl_Color;   
}
 

And here is my updated shader initialization code:

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

It works! So I guess as long as I use "Shader.CurrentTexture" for my first texture, there won't be any problems with regards to texture coordinates introduced by Text rendering (and possibly other SFML built in drawables). This means I can now go wild in the fragment shader.  ;D

Thanks Laurent for a very very fascinating library! A special thank you too for continually updating the .NET bindings! Keep up the excellent work!

3
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« on: June 19, 2012, 07:37:00 pm »
Ahhh ok.... So assigning a texture to a shader needs to be done by passing Shader.CurrentTexture and passing the texture to RenderStates.

How about if I have a 2nd texture that I would need to send to the shader? Would that be possible? Sorry for complicating a bit, it's just a curiosity. I plan on implementing some fullscreen transitions, I was thinking of sending 2 RenderTargets to a shader and do some interesting transitions like fade in/out, screen wipes, etc. Is that possible?

4
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« on: June 19, 2012, 05:10:43 pm »
Yes. But specifically, what I am doing is rendering a Quad using a shader and applying a texture to it. Then after that, render the Text.

I see an almost similar code in the "Edge effect" sample but the Texture was rendered as a Sprite. What I am really aiming for is similar to what you illustrated in a previous post of yours (http://en.sfml-dev.org/forums/index.php?topic=6426.msg42450#msg42450) wherein you used a VertexArray. The difference is that instead of passing the Texture directly into the draw call (in C# that would be in the RenderStates), I would pass the texture into the shader as a parameter and then pass the shader into the draw call.

So it looks like the shader method, wherein a texture passed to a shader and then passed to the states, is broken. But I do understand that this can easily be alleviated by passing the texture directly to the draw call and not bother with shaders. And just apply shaders directly to a RenderTexture if post processing is the goal.

I guess it's a case of the API allowing such a scenario (by way of the broad nature of supporting shaders), but internally, it doesn't really support it or at least in this case, with unexpected results in the texture coordinates.

5
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« 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?

6
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« 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?

7
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« 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?


8
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« 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.

9
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« on: June 19, 2012, 03:28:33 pm »
Ooops! Nevermind! I just saw the "extlibs" folder. =P

10
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« 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?

11
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« 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?

12
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« 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.

13
DotNet / Re: SFML 2.0 Using shaders together with rendering text
« 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;   
}
 

14
DotNet / 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

Pages: [1]