Search code examples
openglglsltexturesopentk

Why is my texture interpolated after rendering it on a surface with sampler2D


I'm writing a program with OpenTK in C# and I currently tried to get a texture on a group of cubes.

My problem is that when i use the given texture with 3 pixels in it and give the bitmap into my shader to a sampler2d it interpolates the colors of the texture while rendering instead of giving me 3 lines with the 3 colors on each side of each cube.

To give it into the shader i used the code similar to the one given on this side.

Here the exact code in my fragment shader:

#version 430 core

uniform sampler2D materialTexture;

in vec2 uvPos;

out vec4 color;

void main() 
{
    vec4 materialColor = texture2D(materialTexture, uvPos);
    color = materialColor;
}

Here the image used as texture shown in gimp: The 3 pixel texture

Here the current result The result after rendering

What would i have to do so the image would be rendered on the cubes without interpolating? Are there maybe any flags or calls in OpenTK i can set to prevent this?


Solution

  • You need to use nearest-neighbor filtering for the magnification function.

    You have a 3x1 texture here, which needs to be magnified to stretch across your scene. The default magnification filter (GL_TEXTURE_MAG_FILTER) in OpenGL is GL_LINEAR, which interpolates the four closest texels.

    GL_NEAREST will produce the behaivor you want. Minification (when your texture resolution is too high) does not apply here, but also defaults to linear filtering.