6
\$\begingroup\$

I am trying to draw point sprites in OpenGL with a shader but gl_PointCoord is always zero.

Here is my code

Setup:

//Shader creation..(includes glBindAttribLocation(program, ATTRIB_P, "p");)
glEnableVertexAttribArray(ATTRIB_P);

In the rendering loop:

glUseProgram(shader_particles);
float vertices[]={0.0f,0.0f,0.0f};
glEnable(GL_TEXTURE_2D);
glEnable(GL_POINT_SPRITE);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
//glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);(tried with this on/off, doesn't work)
glVertexAttribPointer(ATTRIB_P, 3, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_POINTS, 0, 1);

Vertex Shader:

attribute highp vec4 p;
void main() {
  gl_PointSize = 40.0f;
  gl_Position = p;
}

Fragment Shader:

void main() {
 gl_FragColor = vec4(gl_PointCoord.st, 0, 1);//if the coords range from 0-1, this should draw a square with black,red,green,yellow corners
}

But this only draws a black square with a size of 40. What am I doing wrong?

Edit: Point sprites work when i use the fixed function, but I need to use shaders because in the end the code will be for opengl es 2.0

glUseProgram(0);
glEnable(GL_TEXTURE_2D);
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
glPointSize(40);
glBegin(GL_POINTS);
glVertex3f(0.0f,0.0f,0.0f);
glEnd();

Is anyone able to get point sprites working with shader? If so, please share some code.

\$\endgroup\$
7
  • \$\begingroup\$ Can you add more of your code? For example Attribute initialization and setting. \$\endgroup\$
    – zacharmarz
    Apr 14 '11 at 16:35
  • \$\begingroup\$ I added/changed the code a little \$\endgroup\$
    – Jonathan
    Apr 14 '11 at 21:24
  • \$\begingroup\$ You pass only one vertex with coordinates 0,0,0, so it have to draw just one point with these coordinates and also with black color. I don't think, it could give you any other color. Do I understand it wrong? \$\endgroup\$
    – zacharmarz
    Apr 15 '11 at 8:23
  • \$\begingroup\$ if gl_PointCoord ranges from 0-1(as it should), the point's red value should range form 0-1 horizontally, and the green value from 0-1 vertically because of this line in the fragment shader: gl_FragColor = vec4(gl_PointCoord.st, 0, 1);, but I only get black. \$\endgroup\$
    – Jonathan
    Apr 15 '11 at 9:40
  • \$\begingroup\$ When I look at your coordinates: float vertices[]={0.0f,0.0f,0.0f}; You pass just one point, and also draw just one point (glDrawArrays(GL_POINTS, 0, 1);). When you pass your coordinates (0,0,0) in VS, you draw just one pointSprite on screen and it's black colored (the same as your coordinates). I don't know, if pointCoords are somehow transfered over the whole pointSprite. I think, it's only passed from VS. \$\endgroup\$
    – zacharmarz
    Apr 15 '11 at 10:29
1
\$\begingroup\$

Curious. It's working exactly as it should for me. The only real difference is that I'm using an OpenGL 3.2 Core Profile.

Vertex shader:

#version 150

uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ModelMatrix;

in vec3 Position;

void main(void)
{
   gl_PointSize = 40.0;
   gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vec4(Position, 1);
}

Fragment shader:

#version 150

out vec4 FragmentColour;

void main(void)
{
   FragmentColour = vec4(gl_PointCoord.st, 0, 1);
}
\$\endgroup\$
1
  • \$\begingroup\$ this ended up being a driver bug \$\endgroup\$
    – Jonathan
    Jul 8 '11 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .