Fragment position

Hey guys. I started openGL ES a month ago, because it was a project requirement and I must say I really love it, so far.

I had a multi-texturing issue regarding finding a fragment position inside a primitive (rectangle). I needed a fragment position as x = [0.0 - 1.0], y = [0.0 - 1.0]. I found out about gl_FragCoord and gl_PointCoord.

The first one would give me a fragment coordinate in the window coordinate system which didn’t do much for me. At first glance it worked fine, but it was just because the primitive was taking the whole screen, in other words I had no projection.

gl_PointCoord was somehow always 0. The thing that was confusing about it was what the reference said about “gl_PointCoord is a fragment language input variable that contains the two-dimensional coordinates indicating where within a point primitive the current fragment is located. If the current primitive is not a point, then values read from gl_PointCoord are undefined.”, as I’m not exactly sure what a point primitive is. I ended up passing 0.0 - 1.0 coordinates for each vertex and getting a varying value inside the fragment shader, which worked just fine, but I’m sure there’s another (better) way to do it.

Thanks in advance

I’m not exactly sure what a point primitive is

It’s a point. That enumerator you use when you draw stuff? GL_TRIANGLES, GL_TRIANGLE_STRIP? That’s your primitive. You can also draw GL_LINES and GL_POINTS. Points are basically screen-aligned squares. gl_PointCoord is the location within the point of that fragment.

I’m sure there’s another (better) way to do it

There isn’t. You need gl_PointCoord because points are generated from a single vertex and a size. Since there is only one vertex, the rasterizer has to provide some location within the expanded point primitive.

When you’re dealing with lines and triangles, this isn’t necessary (and would be a lot more difficult to define in the general case). It’s better to leave it to the user, so that they can pass whatever info they want. After all, OpenGL doesn’t know you’re rendering quads; all it sees is a single triangle at a time. The best it can give you is barycentric coordinates, which doesn’t really help.

I’m sorry but I don’t seem to understand this.

From the GLSL 4.3 spec:

The values in gl_PointCoord are two-dimensional coordinates indicating where within a point primitive
the current fragment is located, when point sprites are enabled. They range from 0.0 to 1.0 across the
point. If the current primitive is not a point, or if point sprites are not enabled, then the values read from
gl_PointCoord are undefined.

Think of point primitives as quads where you only have to pass down a single vertex for the center of the quad. And think of gl_PointCoord like a (0,0)…(1,1) texcoord you can use to lookup the texture value for the current fragment (candidate pixel) in that quad).

Right, got it now. Definitely have to improve my English, not to ask question twice.

Thanks a lot

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.