How to crop and scale texture in OpenGL

I have an input texture that is 852x640 and the output texture is 612x612. I pass the input through the shader and I want the result to scale and crop properly. I'm having trouble getting squareCoordinates , textureCoordinates and viewPorts to work together.

I don’t want to just crop, I want to scale it to maximize the maximum possible number of images. If I used Photoshop, I would do it in two steps (in OpenGL I try to do it in one step):

  • Zoom in to 612x814
  • Trim excess 101px on each side

I use standard square vertices and texture vertices:

 static const GLfloat squareVertices[] = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, }; static const GLfloat squareTextureVertices[] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f } 

I don’t know exactly what viewPort should be.

+4
source share
1 answer

Viewport will have a size of 612x612 pixels.

To scale and crop the original square, the easiest way would be to set the vertices on a 612x612 rectangle (in your case, we leave squareVertices unchanged), but set the texture coordinates so that the left and right sides are cropped:

 static const GLfloat squareTextureVertices[] = { (852.0f-640.0f)/852.0f*0.5f, 0.0f, 1.0f - (852.0f-640.0f)/852.0f*0.5f, 0.0f, (852.0f-640.0f)/852.0f*0.5f, 1.0f, 1.0f - (852.0f-640.0f)/852.0f*0.5f, 1.0f } 
+6
source

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