Texturing disappearing after applying shader

I’m currently working on Windows with c# using OpenTK.

I was able to load a texture fine until I implemented a fragment and vertex shader of my own.

I tested around to see if the fragment buffer actually contained any of my texture data and that failed as well.

Here’s my shader code:

        // GLSL for fragment shader
        String fragSource = @"

            varying vec3 N;
            varying vec3 v;

            void main (void)
            {
                vec3 L = normalize(gl_LightSource[0].position.xyz + v);
                vec3 E = normalize(v); // we are in Eye Coordinates, so EyePos is (0,0,0)
                vec3 R = normalize(reflect(-L,N));

                //calculate Ambient Term:
                vec4 Iamb = gl_FrontLightProduct[0].ambient;

                //calculate Diffuse Term:
                vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0);
                Idiff = clamp(Idiff, 0.0, 1.0);

                // calculate Specular Term:
                vec4 Ispec = gl_FrontLightProduct[0].specular * pow(max(dot(R,E),0.0), 0.3 * gl_FrontMaterial.shininess);
                Ispec = clamp(Ispec, 0.0, 1.0);

                // write Total Color:
                gl_FragColor = gl_FrontLightModelProduct.sceneColor + Iamb + Idiff + Ispec;
            }
        ";    

        // GLSL for vertex shader
        String vertSource = @"

            varying vec3 N;
            varying vec3 v;

            void main(void)
            {
                v = vec3(gl_ModelViewMatrix * gl_Vertex);       
                N = normalize(gl_NormalMatrix * gl_Normal);
                gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
            }
        ";

This is where I’m loading my texture:

        #region LoadTexture
        GL.Enable(EnableCap.Texture2D);
        Texture = LoadTexture("dotcode_image" + img_num + ".tiff");

        string save_path = @"c:\NDAC_Simulator";
        if (!Directory.Exists(save_path))
        {
            DirectoryInfo di = Directory.CreateDirectory(save_path);
        }

        GL.ActiveTexture(TextureUnit.Texture0);
        //GL.BindTexture(TextureTarget.Texture2D, Texture);

        #endregion

    static int LoadTexture(string filename)
    {
        string path = System.IO.Directory.GetCurrentDirectory() + "\\" + filename;

        if (String.IsNullOrEmpty(path))
            throw new ArgumentException(path);

        int id = GL.GenTexture();
        GL.BindTexture(TextureTarget.Texture2D, id);

        Bitmap bmp = new Bitmap(path);
        BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0,
            OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);

        bmp.UnlockBits(bmp_data);
        
        // We haven't uploaded mipmaps, so disable mipmapping (otherwise the texture will not appear).
        // On newer video cards, we can use GL.GenerateMipmaps() or GL.Ext.GenerateMipmaps() to create
        // mipmaps automatically. In that case, use TextureMinFilter.LinearMipmapLinear to enable them.
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

        return id;
    }

And this is my draw:

    protected override void OnRenderFrame(FrameEventArgs e)
    {
        base.OnRenderFrame(e);

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        
        Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadIdentity();
        GL.LoadMatrix(ref modelview);

        GL.Rotate(pitch, 1.0f, 0.0f, 0.0f);
        GL.Rotate(yaw, 0.0f, 1.0f, 0.0f);
        GL.Rotate(roll, 0.0f, 0.0f, 1.0f);
        GL.Translate(x, y, depth);

        GL.BindTexture(TextureTarget.Texture2D, Texture);
        GL.Begin(BeginMode.Quads);
 
        GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-2657.0f, -3683.5f);
        GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(2657.0f, -3683.5f);
        GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(2657.0f, 3683.5f);
        GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-2657.0f, 3683.5f);
        GL.End();

        GL.LoadIdentity();

Can anybody help me find what seems to be the problem here?

You are supplying a texture and texture coodinates but you shader is not using them.

Well though I am not an expert, still in your shader you should have something called Sampler2D which is an uniform and that should have your texture loaded in it. Since you have moved to shaders,
So you will have to perform shading in frag shader and there you have to look up the texture value for each fragment. Assuming you have your texture coordinates in frag shader you will do something like:

vec4 texColor = texture(Tex1,texCoordinate); // calculate color from texture

Now assign this color to current pixel. Here the texCoordinate is per fragment. In my code I passed texCoordinate to vertex shader and then passed them as varying to frag shader. So, I have an interpolated texCoordinate for each fragment. I just do the look up and assign the result to the current pixel. Hope this clarifies your doubt.

Thx for the replies,

I’ve tried what you said but I’m still not seeing any textures :frowning:

        String fragSource = @"

            varying vec3 N;
            varying vec3 v;
            uniform sampler2D tex;

            uniform sampler2D textSampler;

            void main (void)
            {
                vec3 L = normalize(gl_LightSource[0].position.xyz + v);
                vec3 E = normalize(v); // we are in Eye Coordinates, so EyePos is (0,0,0)
                vec3 R = normalize(reflect(-L,N));
                    vec4 color = texture2D(tex, gl_TexCoord[0].st);

                //calculate Ambient Term:
                vec4 Iamb = gl_FrontLightProduct[0].ambient;

                //calculate Diffuse Term:
                vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0);
                Idiff = clamp(Idiff, 0.0, 1.0);

                // calculate Specular Term:
                vec4 Ispec = gl_FrontLightProduct[0].specular * pow(max(dot(R,E),0.0), 0.3 * gl_FrontMaterial.shininess);
                Ispec = clamp(Ispec, 0.0, 1.0);

                // write Total Color:
                gl_FragColor = color + Iamb + Idiff + Ispec;
            }
        ";    

to make sure I also tried setting gl_FragColor = color to see if i was actually getting the texture pixels but only gray pixels showed up :frowning:

Am I supposed to do something in the vertex shader as well?

I thinks you will have to perform shading in frag shader and there you have to look up the texture value for each fragment.Thanks kizi.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.