View Full Version : Is there a way to crop a texture?

10-18-2013, 01:20 PM
I have a video stream coming in at 1920x1080 that I want to crop to 2048x1024 with black borders so I can have mipmaps. Is there a command that will do this for me automatically?

10-18-2013, 08:04 PM
When you render a texture it maps to the uv texture coordinates of the triangle (or quad) you are drawing. Normally you would use 0,0 to 1,1 so that the texture completely filled the quad; but if you use 0,0,2,2 and set GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T to GL_CLAMP_TO_BORDER with glTexParameter then the image will be drawn in the bottom left quarter and the rest of the quad will fill with the border colour.In you case I think you want

dx = 2048.9/1920.0 - 1.0;
u for left is dx/2
u for right is 1+dx/2
I am not sure what you want to do with the height since you images is larger than the display area. To crop

dy = 1.0 - 1024.0/1080.0
v left dy/2
v right 1 - dy/2

10-19-2013, 07:45 AM
No, I need to crop the texture before rendering so I can get mipmaps.

carsten neumann
10-19-2013, 09:06 AM
I believe most implementations these days support ARB_texture_non_power_of_two (http://www.opengl.org/registry/specs/ARB/texture_non_power_of_two.txt) so unless you need to support one that does not have this you can get mip maps without doing anything.
Otherwise you should be able to set the GL_UNPACK_* parameters with glPixelStore (http://www.opengl.org/sdk/docs/man/xhtml/glPixelStore.xml) to crop the texture during upload - note this will throw away pixels, not perform any rescaling, but it sounds like that is what you want.

10-20-2013, 11:43 AM
I'm not sure how glPixelStore is used. Is there a tutorial on the site?