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

Implement boolean uniforms. #1186

Merged
merged 5 commits into from Aug 26, 2015
Merged

Implement boolean uniforms. #1186

merged 5 commits into from Aug 26, 2015

Conversation

nicokoch
Copy link
Collaborator

fixes #1135

Since this is my first "real" PR for this lib, I am not sure if this is the right way to implement this, so you should review carefully.

You can use this modified triangle example to test:

#[macro_use]
extern crate glium;

mod support;

use glium::Surface;
use glium::glutin;
use glium::index::PrimitiveType;

fn main() {
    use glium::DisplayBuild;

    // building the display, ie. the main object
    let display = glutin::WindowBuilder::new()
        .build_glium()
        .unwrap();

    // building the vertex buffer, which contains all the vertices that we will draw
    let vertex_buffer = {
        #[derive(Copy, Clone)]
        struct Vertex {
            position: [f32; 2],
            color: [f32; 3],
        }

        implement_vertex!(Vertex, position, color);

        glium::VertexBuffer::new(&display,
            &[
                Vertex { position: [-0.5, -0.5], color: [0.0, 1.0, 0.0] },
                Vertex { position: [ 0.0,  0.5], color: [0.0, 0.0, 1.0] },
                Vertex { position: [ 0.5, -0.5], color: [1.0, 0.0, 0.0] },
            ]
        ).unwrap()
    };

    // building the index buffer
    let index_buffer = glium::IndexBuffer::new(&display, PrimitiveType::TrianglesList,
                                               &[0u16, 1, 2]).unwrap();

    let vertex_shader_src = "
        #version 140

        uniform mat4 matrix;

        in vec2 position;
        in vec3 color;

        out vec3 vColor;

        void main() {
            gl_Position = vec4(position, 0.0, 1.0) * matrix;
            vColor = color;
        }
    ";

    let fragment_shader_src = "
        #version 140

        uniform bool mybool;

        in vec3 vColor;
        out vec4 f_color;

        void main() {
            if(mybool)
                f_color = vec4(0.0, 1.0, 0.0, 1.0); //green
            else
                f_color = vec4(1.0, 1.0, 1.0, 1.0); //white
        }
    ";

    let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();
    let be_green = &mut true;
    // the main loop
    support::start_loop(|| {
        // building the uniforms
        let uniforms = uniform! {
            mybool: *be_green,
            matrix: [
                [1.0, 0.0, 0.0, 0.0],
                [0.0, 1.0, 0.0, 0.0],
                [0.0, 0.0, 1.0, 0.0],
                [0.0, 0.0, 0.0, 1.0f32]
            ],
        };

        // drawing a frame
        let mut target = display.draw();
        target.clear_color(0.0, 0.0, 0.0, 0.0);
        target.draw(&vertex_buffer, &index_buffer, &program, &uniforms, &Default::default()).unwrap();
        target.finish().unwrap();
        *be_green = !*be_green;

        // polling and handling the events received by the window
        for event in display.poll_events() {
            match event {
                glutin::Event::Closed => return support::Action::Stop,
                _ => ()
            }
        }

        support::Action::Continue
    });
}

@tomaka
Copy link
Member

tomaka commented Aug 25, 2015

Thanks!

Could you add a line about this in CHANGELOG.md?

@nicokoch
Copy link
Collaborator Author

Done!
Should BoolVec[2|3|4] also be implemented?

@tomaka
Copy link
Member

tomaka commented Aug 25, 2015

Oh right, that would be nice as well.

@nicokoch
Copy link
Collaborator Author

Rebased

@msiglreith
Copy link
Contributor

The UniformBlock trait should probably be implemented for bool also to be used in uniform blocks.

@tomaka
Copy link
Member

tomaka commented Aug 26, 2015

Thanks

tomaka added a commit that referenced this pull request Aug 26, 2015
Implement boolean uniforms.
@tomaka tomaka merged commit 626d4a8 into glium:master Aug 26, 2015
@coveralls
Copy link

coveralls commented May 27, 2017

Coverage Status

Coverage increased (+23.2%) to 76.692% when pulling 5c60600 on nicokoch:fix-1135 into ff20c8d on tomaka:master.

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.

Add support for boolean uniforms
4 participants