-1

So im trying to render a texture to a quad using OpenGL and im getting a really weird artifact and I think it being caused by my loading method but im not truly sure.

When you get close to the quad you can see that it is tring to render the red green and blue next to each other. kinda looks like what a pixel on a tv looks like

Rendered Quad

Close up of quad

Texture

LoadTexture

public static uint LoadTexture(string filePath)
{
    Bitmap bitmap = new Bitmap("Res/" + filePath);

    Gl.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
    uint textureID = Gl.GenTexture();
    Gl.BindTexture(TextureTarget.Texture2d, textureID);

    BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
        ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Rgb8, data.Width, data.Height, 0, OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);
    bitmap.UnlockBits(data);

    Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
    Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

    return textureID;
}

Rendering

public void Render(TexturedModel texturedModel, StaticShader shader)
{
    RawModel model = texturedModel.model;
    using (MemoryLock vertexPositions = new MemoryLock(model.vertexPositions))
    using (MemoryLock texturePositions = new MemoryLock(texturedModel.textureCords))
    {
        Gl.VertexAttribPointer((uint)shader.locationPosition, model.dimension, VertexAttribType.Float, false, 0, vertexPositions.Address);
        Gl.EnableVertexAttribArray((uint)shader.locationPosition);

        Gl.VertexAttribPointer((uint)shader.locationTexturedCoords, 2, VertexAttribType.Float, false, 0, texturePositions.Address);
        Gl.EnableVertexAttribArray((uint)shader.locationTexturedCoords);

        Gl.PixelStore(PixelStoreParameter.UnpackAlignment, 0);
        Gl.ActiveTexture(TextureUnit.Texture0);
        Gl.BindTexture(TextureTarget.Texture2d, texturedModel.texture.TextureID);
        Gl.Uniform1(shader.locationTextureSampler, 0);

        Gl.UniformMatrix4(shader.locationProjectionMatrix, false, projectionMatrix.ToArray());
        viewMatrix = Maths.CreateViewMatrix(camera);
        Gl.UniformMatrix4(shader.locationViewMatrix, false, viewMatrix.ToArray());
        Gl.UniformMatrix4(shader.locationTransformationMatrix, false, model.modelMatrix.ToArray());

        //Gl.DrawArrays(PrimitiveType.Triangles, 0, model.vertexPositions.Length / model.dimension);
        Gl.DrawElements(PrimitiveType.Triangles, model.indices.Length, DrawElementsType.UnsignedInt, model.indices);
    }
}

Shader program

public class StaticShader : ShaderProgram
{
    private static string VERTEX_FILE = @"Shader/VertexShader.txt";
    private static string FRAGMENTSHADER = @"Shader/FragmentShader.txt";

    public int locationPosition;
    public int locationTexturedCoords;
    public int locationTransformationMatrix;
    public int locationProjectionMatrix;
    public int locationViewMatrix;
    public int locationTextureSampler;

    public StaticShader() : base(VERTEX_FILE, FRAGMENTSHADER)
    {
        locationTransformationMatrix = this.GetUniformLocation("transformationMatrix");
        locationProjectionMatrix = this.GetUniformLocation("projectionMatrix");
        locationViewMatrix = this.GetUniformLocation("viewMatrix");
        locationTexturedCoords = this.GetAttribLocation("textureCords");
        locationPosition = this.GetAttribLocation("position");
        locationTextureSampler = this.GetUniformLocation("textureSampler");
    }
}

Fragment shader

varying vec2 outTextureCords;

uniform sampler2D textureSampler;

void main() {
    gl_FragColor = texture(textureSampler, outTextureCords);
}

vertices

public float[] vertexPositions = new float[] {
            -0.5f,0.5f,0,   //V0
            -0.5f,-0.5f,0,  //V1
            0.5f,-0.5f,0,   //V2
            0.5f,0.5f,0     //V3
    };

public int[] indices = new int[] {
            0,1,3,  //Top left triangle (V0,V1,V3)
            3,1,2   //Bottom right triangle (V3,V1,V2)
    };

public float[] textureCords = {

            0,0,
            0,1,
            1,1,
            1,0
    };
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    You are asking for a `32bppArgb` format and then tell OpenGL it is a `Bgr` data. That doesn't sound right. – BDL Feb 08 '18 at 09:11
  • That pointed me into the right direction. When i switch to RGB the problem didn't go away but when i changed to RGBA i get it to render but with the wrong colors [link] (https://i.gyazo.com/5c8c7a668c981bd64e8c0724532e1da3.png) – Tyler Gregorcyk Feb 08 '18 at 09:15
  • @BDL I am not using OpenTK so i do not have access to the Texture class – Tyler Gregorcyk Feb 08 '18 at 09:26
  • Did you try what the other answer suggests? Using `OpenGL.PixelFormat.Bgra`? Which OpenGL wrapper are you using if not OpenTK? – BDL Feb 08 '18 at 09:35
  • Ahh i thought it was suggesting Bgr witch i was already useing. this fixed the problem thank you very much – Tyler Gregorcyk Feb 08 '18 at 09:40
  • @BDL https://github.com/luca-piccioni/OpenGL.Net – Luca Feb 10 '18 at 21:41

1 Answers1

1

Change the opengl format in the TexImage2d to B and switch internalFormat to rgba

Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Rgba, data.Width, data.Height, 0, OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);