0

I want to draw a point, to simulate the position of which the light originates, however the issue I am facing now is that it is always the same size, regardless of the distance:

I push literally one vertex to the GPU, my code:

point.vs.glsl

#version 440 core

layout(location = 0) in vec4 position;

layout(location = 0) uniform mat4 model_matrix;
layout(location = 1) uniform mat4 view_matrix;
layout(location = 2) uniform mat4 proj_matrix;

void main(void) {
    gl_PointSize = 100.0;
    gl_Position = proj_matrix * view_matrix * model_matrix * position;
}

point.fs.glsl

#version 440 core

out vec4 color;

void main(void) {
    //create round point
    vec2 p = gl_PointCoord * 2.0 - vec2(1.0);
    if (dot(p, p) > 1.0) {
        discard;
    }

    color = vec4(0.5, 0.0, 0.0, 1.0);
}

Also does the code for creating a round point actually work in 3D? I believe it came from a 2D tutorial if I'm not mistaken.

About the point size again, I know what I am doing wrong, however how would I correctly calcualte the size? I have the information available I think, with view_matrix and somehow I need to get the distance to the point, and scale depending on that.

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • Round points come automatically if you enable `GL_POINT_SMOOTH` or multisampling. But they have a maximum size on many implementations of ~63.5. `gl_PointSize` is defined in screen-space though, and this is why it has constant size no matter how far away the point is. – Andon M. Coleman Feb 01 '14 at 19:02
  • @AndonM.Coleman Thanks for the explanation so for, however how can I scale the points then respectively such that it looks as if they came from world size? – skiwi Feb 01 '14 at 19:05
  • Also `glEnable(GL_POINT_SMOOTH)` gives an invalid enum error, am I missing something there? – skiwi Feb 01 '14 at 19:09
  • 1
    No, `GL_POINT_SMOOTH` is deprecated, in a core profile that is to be expected. Do you want this point to roughly approximate the light's volume? Or just a simple point that gets smaller as it gets farther away? You could try something like `gl_PointSize = 100.0 * gl_Position.w` assuming you have a perspective projection. – Andon M. Coleman Feb 01 '14 at 19:11
  • @AndonM.Coleman I'm using a first person view, which translates to a frustum ultimately, so I think that still is a perspective projection? Also I'd recommend dividing by `gl.Position.w` – skiwi Feb 01 '14 at 19:14
  • Yeah, you're right. I was thinking that the 100.0 in this case was already in screen-space and the idea was to go back to world-space by multiplying by W. But `gl_PointSize` is in screen-space, and 100.0 is in world-space. Was that approach adequate for your purpose? To do this properly you will probably need the viewport dimensions. – Andon M. Coleman Feb 01 '14 at 19:32
  • @AndonM.Coleman It seems to be working actually perfectly. – skiwi Feb 01 '14 at 20:12

0 Answers0