PDA

View Full Version : GL_POINTS problem with drawing circles with outline



karkarotten1337
10-21-2017, 02:13 PM
I got a problem with drawing circles with the GL_POINTS operation.

I'm was using Vispy's gloo module, but because of Vispy not supporting geometry shaders i changed to raw PyOpenGL.
I copied the fragment shader from vispy's examples (https://github.com/vispy/vispy/blob/master/examples/demo/gloo/cloud.py) and it worked under vispy but now it won't work in pure PyOpenGL.
When just only setting the gl_FragColor with "gl_FragColor = v_bg_color;" inside the fragment shader i see the squares so the vertex shader and everything else works.
Am i missing some glEnable() stuff or what is going on? Do i need more blend functions or even any at all? And how do i know what i need for my shaders in the future?

Here is the vertex shader:

#version 330

layout(location = 0) in vec3 a_position;
layout(location = 1) in vec4 a_fg_color;
layout(location = 2) in vec4 a_bg_color;
layout(location = 3) in float a_size;

uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;
uniform float u_linewidth;
uniform float u_antialias;
uniform float u_size;

varying vec4 v_fg_color;
varying vec4 v_bg_color;
varying float v_size;
varying float v_linewidth;
varying float v_antialias;

void main (void) {
v_size = a_size * u_size;
v_linewidth = u_linewidth;
v_antialias = u_antialias;
v_fg_color = a_fg_color;
v_bg_color = a_bg_color;
gl_Position = projection * view * model * vec4(a_position,1.0);
gl_PointSize = (v_size + 2) * (v_linewidth + 1.5 * v_antialias) / gl_Position.w;
}

Here is the fragment shader:

#version 330

varying vec4 v_fg_color;
varying vec4 v_bg_color;
varying float v_size;
varying float v_linewidth;
varying float v_antialias;

float disc(vec2 P, float size)
{
float r = length((P.xy - vec2(0.5,0.5))*size);
r -= v_size/2;
return r;
}

void main()
{
float size = v_size +2*(v_linewidth + 1.5*v_antialias);
float t = v_linewidth/2.0-v_antialias;

float r = disc(gl_PointCoord, size);

float d = abs(r) - t;
if( r > (v_linewidth/2.0+v_antialias))
{
discard;
}
else if( d < 0.0 )
{
gl_FragColor = v_fg_color;
}
else
{
float alpha = d/v_antialias;
alpha = exp(-alpha*alpha);
if (r > 0)
gl_FragColor = vec4(v_fg_color.rgb, alpha*v_fg_color.a);
else
gl_FragColor = mix(v_bg_color, v_fg_color, alpha);
}
}

Here is the paintGL function:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glUseProgram(self.program)

glEnable(GL_PROGRAM_POINT_SIZE)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

glUniformMatrix4fv(self.UNIFORM_LOCATIONS['model'], 1, GL_FALSE, self.model)
glUniformMatrix4fv(self.UNIFORM_LOCATIONS['projection'], 1, GL_FALSE, self.projection)
glUniformMatrix4fv(self.UNIFORM_LOCATIONS['view'], 1, GL_FALSE, self.view)
glUniform1f(self.UNIFORM_LOCATIONS['u_linewidth'], self.u_linewidth)
glUniform1f(self.UNIFORM_LOCATIONS['u_antialias'], self.u_antialias)
glUniform1f(self.UNIFORM_LOCATIONS['u_size'], self.u_size)

self.vbo.bind()

glEnableVertexAttribArray(self.ATTRIBUTE_LOCATIONS['a_position'])
glEnableVertexAttribArray(self.ATTRIBUTE_LOCATIONS['a_fg_color'])
glEnableVertexAttribArray(self.ATTRIBUTE_LOCATIONS['a_bg_color'])
glEnableVertexAttribArray(self.ATTRIBUTE_LOCATIONS['a_size'])

stride = 48

glVertexAttribPointer(
self.ATTRIBUTE_LOCATIONS['a_position'],
3, GL_FLOAT, False, stride, self.vbo
)
glVertexAttribPointer(
self.ATTRIBUTE_LOCATIONS['a_fg_color'],
4, GL_FLOAT, False, stride, self.vbo + 12
)
glVertexAttribPointer(
self.ATTRIBUTE_LOCATIONS['a_bg_color'],
4, GL_FLOAT, False, stride, self.vbo + 28
)
glVertexAttribPointer(
self.ATTRIBUTE_LOCATIONS['a_size'],
1, GL_FLOAT, False, stride, self.vbo + 44
)

glDrawArrays(GL_POINTS, 0, 100000)

self.vbo.unbind()

glDisableVertexAttribArray(self.ATTRIBUTE_LOCATION S['a_position'])
glDisableVertexAttribArray(self.ATTRIBUTE_LOCATION S['a_fg_color'])
glDisableVertexAttribArray(self.ATTRIBUTE_LOCATION S['a_bg_color'])
glDisableVertexAttribArray(self.ATTRIBUTE_LOCATION S['a_size'])

glDisable(GL_PROGRAM_POINT_SIZE)
glDisable(GL_BLEND)

glUseProgram(0)

Thanks if anyone cares to help :)

karkarotten1337
10-28-2017, 07:10 AM
I got it now, I needed to activate "GL_POINT_SPRITE" because the gl_PointCoord was always zero (https://www.opengl.org/discussion_boards/showthread.php/182248-understanding-gl_PointCoord-%28always-0%29)...

OceanJeff40
10-28-2017, 04:39 PM
Where's the like button? :biggrin-new:

Jeff