eBehbahani
eBehbahani

Reputation: 1564

Using vertex shader to move texture to pixel center

I am trying to move my texture to the center of the pixel as stated here OpenGL Texture Coordinates in Pixel Space but am struggling to do so. Are the pixels we are trying to center on in the texture or on the screen? How do we access them? Here is my vertex shader:

"uniform mat4 " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + ";\n" +
"attribute vec4 " + ShaderProgramConstants.ATTRIBUTE_POSITION + ";\n" +
"attribute vec4 " + ShaderProgramConstants.ATTRIBUTE_COLOR + ";\n" +
"attribute vec2 " + ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES + ";\n" +
"varying vec4 " + ShaderProgramConstants.VARYING_COLOR + ";\n" +
"varying vec2 " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + ";\n" +

"void main() {\n" +
"   " + ShaderProgramConstants.VARYING_COLOR + " = " + ShaderProgramConstants.ATTRIBUTE_COLOR + ";\n" +
"   " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + " = " + ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES + ";\n" +
    "   gl_Position = " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + " * " + ShaderProgramConstants.ATTRIBUTE_POSITION + ";\n" +
"}"

I am new to open GL and have just started doing research on vertex shaders, so any suggestion or tip would be much appreciated.

EDIT: The problem I experience, is that the pixels from my texture get deformed when scaled. Normal textures look fine, but when they are scaled using the scaleM command on the 4x4 Matrix (Model view matrix), then it looks like there are two columns or two rows of the same pixels in random rows and columns.

Upvotes: 0

Views: 1141

Answers (1)

eBehbahani
eBehbahani

Reputation: 1564

Turns out the scale factor was causing the distortion. All I had to do was make sure that each pixel in the texture had the same amount of pixels on the display, and the distortions disappeared.

Upvotes: 0

Related Questions