Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GL_POINTS and gl_PointSize #334

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

h-isthebestletter
Copy link

Simple pull request to add support for drawing Points as a primitive as well as support for using gl_PointSize in the vertex shader. Sorry if I missed anything, I don't know much about OpenGL. However I've used it in my own toy project and it works (not sure how much of an assurance that is)

Now you should be able to make a pipeline that has gl_PointSize support by doing

let pipeline = gfx.create_pipeline()
    .from(&VERTEX, &FRAGMENT)
    .with_vertex_info(&vertex_info)
    .with_point_size_available(true) // note
    .build()
    .unwrap();

And you should be able to draw points by doing

renderer.set_primitive(DrawPrimitive::Points);

@Nazariglez
Copy link
Owner

Hello! Have you tried this in web and native builds? Never used this primitive so I am not sure if this is something that will work in the same way in different targets.

Thanks for the PR!

@h-isthebestletter
Copy link
Author

Hi, I'm currently away right now so I cannot compile any test code. But from what I can remember about Points, they have a hard limit as to how big they can get. I also encountered this limit in my test
project. (https://community.khronos.org/t/adjust-gl-points-size/67980)

Seems to be device dependent too, but that one I'm not too sure.

@h-isthebestletter
Copy link
Author

h-isthebestletter commented Jan 29, 2025

Hi, here to report back on stuff:

Here I attempt to draw 3 points. The first one (red) is drawn with point size 128.0, the second one (green) with point size 256.0 and the third one (blue) with point size 512.0. Notice how the green and blue points have the same size, that's because the maximum point size is 256 (at least that is what it looks like).

Here is the project running on native build (NixOS 24.11 with Gnome 47):
image

Here is the project running on Firefox 134.0.1 (compiled with the index.html provided in the README and trunk serve):
image

So it does seem like they have the same behaviour.

And here is the code (just slightly modified from examples/renderer_triangle.rs):

use notan::prelude::*;

//language=glsl
const VERT: ShaderSource = notan::vertex_shader! {
    r#"
    #version 450
    layout(location = 0) in vec2 a_pos;
    layout(location = 1) in vec3 a_color;
    layout(location = 2) in float a_size;

    layout(location = 0) out vec3 v_color;

    void main() {
        v_color = a_color;
        gl_Position = vec4(a_pos - 0.5, 0.0, 1.0);
        gl_PointSize = a_size;
    }
    "#
};

//language=glsl
const FRAG: ShaderSource = notan::fragment_shader! {
    r#"
    #version 450
    precision mediump float;

    layout(location = 0) in vec3 v_color;
    layout(location = 0) out vec4 color;

    void main() {
        color = vec4(v_color, 1.0);
    }
    "#
};

#[derive(AppState)]
struct State {
    clear_options: ClearOptions,
    pipeline: Pipeline,
    vbo: Buffer,
}

#[notan_main]
fn main() -> Result<(), String> {
    notan::init_with(setup).draw(draw).build()
}

fn setup(gfx: &mut Graphics) -> State {
    let clear_options = ClearOptions::color(Color::new(0.1, 0.2, 0.3, 1.0));

    let vertex_info = VertexInfo::new()
        .attr(0, VertexFormat::Float32x2)
        .attr(1, VertexFormat::Float32x3)
        .attr(2, VertexFormat::Float32);

    let pipeline = gfx
        .create_pipeline()
        .from(&VERT, &FRAG)
        .with_vertex_info(&vertex_info)
        .with_point_size_available(true)
        .build()
        .unwrap();

    #[rustfmt::skip]
    let vertices = [
        0.5, 1.0,   1.0, 0.2, 0.3,   128.0,
        0.0, 0.0,   0.1, 1.0, 0.3,   256.0,
        1.0, 0.0,   0.1, 0.2, 1.0,   512.0,
    ];

    let vbo = gfx
        .create_vertex_buffer()
        .with_info(&vertex_info)
        .with_data(&vertices)
        .build()
        .unwrap();

    State {
        clear_options,
        pipeline,
        vbo,
    }
}

fn draw(gfx: &mut Graphics, state: &mut State) {
    let mut renderer = gfx.create_renderer();

    renderer.begin(Some(state.clear_options));
    renderer.set_primitive(DrawPrimitive::Points);
    renderer.set_pipeline(&state.pipeline);
    renderer.bind_buffer(&state.vbo);
    renderer.draw(0, 3);
    renderer.end();

    gfx.render(&renderer);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants