Perfect texture mapping using modern OpenGL

After deciding to try programming in modern OpenGL, I left behind the pipeline with a fixed function, and I'm not quite sure how to get the same functionality as before.

I am trying to texture ATVs with a pixel size corresponding to the size of the texture. For example, a 128x128 texture displays a 128x128 square.

This is my vertex shader.

#version 110 uniform float xpos; uniform float ypos; uniform float tw; // texture width in pixels uniform float th; // texture height in pixels attribute vec4 position; varying vec2 texcoord; void main() { mat4 projectionMatrix = mat4( 2.0/600.0, 0.0, 0.0, -1.0, 0.0, 2.0/800.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0); gl_Position = position * projectionMatrix; texcoord = (gl_Position.xy); } 

This is my fragment shader:

 #version 110 uniform float fade_factor; uniform sampler2D textures[1]; varying vec2 texcoord; void main() { gl_FragColor = texture2D(textures[0], texcoord); }
#version 110 uniform float fade_factor; uniform sampler2D textures[1]; varying vec2 texcoord; void main() { gl_FragColor = texture2D(textures[0], texcoord); } 

My vertex data is as such, where w and h are the width and height of the texture.

 [ 0, 0, w, 0, w, h, 0, h ]
[ 0, 0, w, 0, w, h, 0, h ] 

I load a 128x128 texture and with these shaders I see that the image repeats 4 times: http://i.stack.imgur.com/UY7Ts.jpg

Can anyone offer advice on the correct way to translate and scale based on the twth, xpos, xpos form?

+6
source share
1 answer

The problem with this:

 mat4 projectionMatrix = mat4( 2.0/600.0, 0.0, 0.0, -1.0, 0.0, 2.0/800.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0); gl_Position = position * projectionMatrix; 

Transformation matrices are correct associative, i.e. you must multiply the opposite order. Also, you usually don’t specify the projection matrix in the shader, you pass it as a unified one. OpenGL provides you with ready-to-use forms for designing and viewing the model. In the core of OpenGL-3, you can reuse unified names to stay compatible.

 // predefined by OpenGL version < 3 core: #if __VERSION__ < 400 uniform mat4 gl_ProjectionMatrix; uniform mat4 gl_ModelviewMatrx; uniform mat4 gl_ModelviewProjectionMatrix; // premultiplied gl_ProjectionMatrix * gl_ModelviewMatrix uniform mat4 gl_ModelviewInverseTranspose; // needed for transformin normals attribute vec4 gl_Vertex; varying vec4 gl_TexCoord[]; #endif void main() { gl_Position = gl_ModelviewProjectionMatrix * gl_Vertex; } 

Then you should understand that texture coordinates do not address texture pixels (texels), but that texture should be understood as an interpolation function with given sample points; the coordinates of texture 0 or 1 do not fall into the centers of the texel, but lie exactly between the wrapper, thereby blurring. As long as your square-sized box exactly matches the texture size, that's fine. But as soon as you want to show just something, something interesting (I leave it as an exercise for the reader to find out the exact comparison; a hint: you will have the conditions 0.5 / size and (dimension-1) / dimension in the solution )

+6
source

Source: https://habr.com/ru/post/886528/


All Articles