gl_PointCoord give always 0,0

621 views
Skip to first unread message

Cedric

unread,
Sep 3, 2010, 11:53:58 AM9/3/10
to WebGL Dev List
Hi,

I am trying to do some point sprite using a texture and sampling it
from
gl_PointCoord in the fragment shader.
But I always get a coord (0,0) so do I need to enable something to
have
uv between (0,0) (1,1) ?

vec4 c;
c = texture2D(TexUnit4, gl_PointCoord);


Cheers,
Cedric

--
Provide OpenGL, WebGL and OpenSceneGraph services
+33 659 598 614 Cedric Pinson mailto:cedric...@plopbyte.net
http://www.plopbyte.net

Giles Thomas

unread,
Sep 3, 2010, 12:18:13 PM9/3/10
to webgl-d...@googlegroups.com
Hi Cedric,

On 3 September 2010 16:53, Cedric <tri...@gmail.com> wrote:
I am trying to do some point sprite using a texture and sampling it
from
gl_PointCoord in the fragment shader.
But I always get a coord (0,0) so do I need to enable something to
have
uv between (0,0) (1,1) ?

vec4 c;
c = texture2D(TexUnit4, gl_PointCoord);

Sorry if it's a silly question, but are you setting gl_PointSize?


Cheers,

Giles
--
Giles Thomas
gi...@giles.net
http://www.gilesthomas.com/
http://projectdirigible.com/
http://learningwebgl.com/

Paul Brunt

unread,
Sep 6, 2010, 12:08:36 PM9/6/10
to webgl-d...@googlegroups.com
I had the same problem, in the end I hacked round the problem using gl_FragCoord to figure out what the gl_PointCoord should have been from the point size and position, more long winded and inefficient but worked just as well.

trigrou

unread,
Sep 6, 2010, 6:36:07 PM9/6/10
to webgl-d...@googlegroups.com
Hi
Would you have an example I could have look online ?

Cheers,
Cedric

Paul Brunt

unread,
Sep 7, 2010, 3:56:11 AM9/7/10
to webgl-d...@googlegroups.com
I don't have it available at the moment it's on another machine but of the top of my head it should be something like:

vertex shader:

attribute vec3 aPosition
attribute float aPointSize

uniform mat4 uViewMatrix
uniform mat4 uProjection

varying float vPointSize
varying vec2 vPosition

void main(){
vec4 position=uProjection * uViewMatrix * vec4(aPosition,1.0);
gl_PointSize=aPointSize;
gl_Position=position;
//send the point size though to the fragment shader
vPointSize=aPointSize;
//send the screen coord though to the fragment shader
vPosition=vec2(position.x/position.w,position.y/position.w);
}

fragment shader:

varying float vPointSize
varying vec2 vPosition

uniform vec2 uCanvasDims

void main(){
//get screen coord from frag coord
float screenX=(gl_FragCoord.x/uCanvasDims.x-0.5)*2;
float screenY=(gl_FragCoord.y/uCanvasDims.y-0.5)*2;
//find the difference between the fragments screen position and the points screen position then simply work out the point coord from the point size
vec2 pointCoord=vec2( (screenX-vPosition.x)/(vPointSize/uCanvasDims.x), (screenY-vPosition.y)/(vPointSize/uCanvasDims.x));
}

I've not tested so there maybe errors but hopefully you get the giest and it points you in a workable direction.

Paul
Reply all
Reply to author
Forward
0 new messages