Friday, September 16, 2016

opengl - gl_PointCoord always zero


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.



Answer



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);
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...