Dan Collins
Dan Collins

Reputation: 1018

openGL pixel coordinate

I am trying to work with textures in openGL to do parallel computation (openCL is not an option). I understand that the domain is 0.0 to 1.0 on each axis from the bottom left. However, is the coordinate of a pixel during rendering map to the pixel's upper left corner, or the middle of the pixel?

Ex. If I have a texture that is 100x100px. Is the coordinate of the upper left most pixel (0.0,1.0) or is it (0.005,0.995)? I assumed it was the former, but have been getting some unexpected results. I am wondering if I am accessing data on "pixel boundaries" and openGL is interpolating to a value that is averaging the two neighboring pixels.

Upvotes: 2

Views: 3146

Answers (2)

wcochran
wcochran

Reputation: 10896

OpenGL pixels/fragments are conceptually 1x1 squares centered on half-integer pixels. The OpenGL 4.5 specification states:

A fragment is located by its lower left corner, which lies on integer grid coordinates. Rasterization operations also refer to a fragment’s center, which is offset by (1/2,1/2) from its lower left corner (and so lies on half-integer coordinates).

So for 100x100 px the horizontal distance between texels is 1/100 = 0.01. Therefore the distance between the left edge and the first texel center is 0.01/2 = 0.005.

The first vertical texel center from the lower left corner is also 0.005 so from the top the first texel center is 1.0 - 0.005 = 0.995.

That's why you get those numbers.

You can see my post here for more detail (tex coordinate stuff toward the end).

Upvotes: 4

Hannesh
Hannesh

Reputation: 7488

The center of the pixel is actually the latter, i.e. (0.005,0.995).

As Nicol Bolas mentioned in the comments, you can prevent opengl from interpolatingh across pixels by changing the filtering method to GL_NEAREST when you create the texture.

Upvotes: 0

Related Questions