0
\$\begingroup\$

I want to display a texture on a full screen quad. What I get is a completely green quad and after a week of debugging I cannot understand why.

I am currently using OpenTK 1.1.2225 and Magick.NET-Q16-HDRI-AnyCPU.

My class extends GameWindow. I report here the methods I use.

class SelectiveEnvironmentMapping : GameWindow
{
    int textureID, shaderProgram, quadVAO;
    float exposure = 1.0f;

    float[] quadVertices = {
        // Positions        // Texture Coords
        -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        -1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
        1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
        1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
    };

    public SelectiveEnvironmentMapping() : base(800,600,new GraphicsMode(),"My Title",GameWindowFlags.Default)
    { }

    protected int LoadShaderProgram(string VSSource, string FSSource)
    {
        int program = GL.CreateProgram();
        int vshader = GL.CreateShader(ShaderType.VertexShader);
        int fshader = GL.CreateShader(ShaderType.FragmentShader);
        GL.ShaderSource(program, VSSource);
        GL.ShaderSource(program, FSSource);
        GL.CompileShader(vshader);
        GL.CompileShader(fshader);
        int success;
        GL.GetShader(vshader, ShaderParameter.CompileStatus, out success);
        if(success != 1)
        {
            Console.WriteLine("vshader not compiled");
            Console.WriteLine(GL.GetShaderInfoLog(vshader));
        }
        GL.GetShader(fshader, ShaderParameter.CompileStatus, out success);
        if (success != 1)
        {
            Console.WriteLine("fshader not compiled");
            Console.WriteLine(GL.GetShaderInfoLog(fshader));
        }
        Console.WriteLine(GL.GetShaderInfoLog(vshader));
        Console.WriteLine(GL.GetShaderInfoLog(fshader));
        GL.AttachShader(program, vshader);
        GL.AttachShader(program, fshader);
        GL.LinkProgram(program);
        return program;
    }

    protected override void OnLoad(System.EventArgs e)
    {
        try
        {
            Console.WriteLine(ClientSize.Width + " " + ClientSize.Height);
            Console.WriteLine(GL.GetString(StringName.Version));
            Console.WriteLine(GL.GetString(StringName.ShadingLanguageVersion));
            GL.Viewport(0, 0, ClientSize.Width, ClientSize.Height);
            VSync = VSyncMode.On;
            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);

            // Setup quad VAO
            quadVAO = GL.GenVertexArray();
            int quadVBO = GL.GenBuffer();
            GL.BindVertexArray(quadVAO);
            GL.BindBuffer(BufferTarget.ArrayBuffer, quadVBO);
            GL.BufferData(BufferTarget.ArrayBuffer, quadVertices.Length * sizeof(float), quadVertices, BufferUsageHint.StaticDraw);
            GL.EnableVertexAttribArray(0);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);
            GL.EnableVertexAttribArray(1);
            GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));

            MagickImage img = new MagickImage(@"../../Cubemaps/GravelPlaza_REF.jpg");

            float[] data = img.GetPixels().ToArray();

            textureID = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, textureID);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, width, height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, data);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, Convert.ToInt32(TextureWrapMode.Repeat));
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, Convert.ToInt32(TextureWrapMode.Repeat));
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, Convert.ToInt32(TextureMinFilter.Linear));
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, Convert.ToInt32(TextureMagFilter.Linear));

            GL.BindTexture(TextureTarget.Texture2D, 0);

            string vsource = File.ReadAllText(@"../../Shaders/MyVShader.glsl");
            string fsource = File.ReadAllText(@"../../Shaders/MyFShader.glsl");
            shaderProgram = LoadShaderProgram(vsource, fsource);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

    }

    protected override void OnRenderFrame(FrameEventArgs e)
    {
        GL.ClearColor(Color.Purple);
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        GL.UseProgram(shaderProgram);
        GL.ActiveTexture(TextureUnit.Texture0);
        GL.BindTexture(TextureTarget.Texture2D,textureID);

        GL.BindVertexArray(quadVAO);
        GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4);
        GL.BindVertexArray(0);
        SwapBuffers();

    }

    public static void Main()
    {
        try
        {
            using (SelectiveEnvironmentMapping p = new SelectiveEnvironmentMapping())
            {
                p.Run(60);
            }
      }
}

My Vertex Shader:

#version 400 
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 texCoords;

out vec2 TexCoords;


void main()
{
    gl_Position = vec4(position.x, position.y, 0.0f, 1.0f);
    TexCoords = texCoords;
}

And my Fragment Shader:

#version 400 
out vec4 color;
in vec2 TexCoords;


uniform sampler2D text;

void main()
{
    color = vec4(texture(text,TexCoords),1.0);
}

EDIT: there was a typo in the LoadShaderProgram function (on this post, not on my code).

EDIT2: Found out that my shaders don't compile and no infolog is showed, i tried this

int success; GL.GetShader(vshader, ShaderParameter.CompileStatus, out success); if(success != 1) { Console.WriteLine("vshader not compiled"); Console.WriteLine(GL.GetShaderInfoLog(vshader)); }

and the output was "vshader not compiled" and that's it. No apparent reasons.

\$\endgroup\$
3
  • \$\begingroup\$ Does it change to white if you do color = vec4(1.0,1.0,1.0,1.0); in the fragment shader instead of what you have there now? \$\endgroup\$
    – Lasse
    Apr 28, 2016 at 15:16
  • \$\begingroup\$ Just tried, no sir. \$\endgroup\$
    – Alakanu
    Apr 28, 2016 at 18:23
  • \$\begingroup\$ You can and should delete the shaders after you linked the program. You don't need them. \$\endgroup\$
    – Bálint
    Apr 29, 2016 at 17:02

2 Answers 2

1
\$\begingroup\$

In glsl, the texture function returns a vec4, thus you provide too much arguments to the vec4.

It should be

color = texture(sampler, texCoord);

To avoid issues like this, always print out what glGetError returns.

Second problem:

Guess what GL.ShaderSource takes in as arguments. A shader, and you provided a shader program to it.

\$\endgroup\$
0
\$\begingroup\$

Line 5 and 6 of LoadShaderProgram() contain a copy/paste error.

    GL.ShaderSource(program, VSSource);
    GL.ShaderSource(program, FSSource);

Should be instead:

    GL.ShaderSource(vshader, VSSource);
    GL.ShaderSource(fashader, FSSource);
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I don't see any difference in the posted snippets. \$\endgroup\$
    – Bálint
    Apr 29, 2016 at 16:51
  • \$\begingroup\$ Changed it, as you can see copy/paste errors kinda dooming me this last week. \$\endgroup\$
    – Alakanu
    May 1, 2016 at 8:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .