Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Texturing disappearing after applying shader

  1. #1
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    2

    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?

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    You are supplying a texture and texture coodinates but you shader is not using them.

  3. #3
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    3
    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.

  4. #4
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    2
    Thx for the replies,

    I've tried what you said but I'm still not seeing any textures

    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

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

  5. #5
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    1
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •