PistonDevelopers / glfw-rs

GLFW3 bindings and idiomatic wrapper for Rust.
Apache License 2.0
645 stars 123 forks source link

problems with loading uniforms... #565

Open malkmusl opened 7 months ago

malkmusl commented 7 months ago

problems with loading uniforms... im trying to set a simple color but if i try to use the uniform it dosnt work if i use a simple shader with a solid color i got no problems and the color is applyed but if i try to use the uniform i get an OpenGL error 1282 for some reasons i also tryed to load the shader from a .frag file and added the plain string to the rust file and yes i use u8 to provide the data this is a thing of my data generator that compiles all files in assets to u8

here is the rust code

pub(crate) fn draw_sidebar(size: f32, alignment: Alignment) {
        let topbar_vertex_shader = compile_shader_from_u8(&shaders::topbar::vertex_shader::SHADER_DATA, gl::VERTEX_SHADER); // Use the same shader as the default vertex shader
        let topbar_fragment_shader = compile_shader_from_u8(&shaders::topbar::fragment_shader::SHADER_DATA, gl::FRAGMENT_SHADER); // Use the same shader as the default fragment shader
        let topbar_shader_program = shaders::create_program();

        shaders::attach_shader(topbar_shader_program, topbar_vertex_shader);
        shaders::attach_shader(topbar_shader_program, topbar_fragment_shader);
        shaders::link_program(topbar_shader_program);

        unsafe {
            let frag_color_location = gl::GetUniformLocation(topbar_shader_program, CString::new("vertexColor").unwrap().as_ptr());
            if frag_color_location == -1 {
                println!("Error: Failed to find uniform location for vertexColor");
            } else {
                let color: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; // Red color
                gl::Uniform4fv(frag_color_location, 1, color.as_ptr());
                let error = gl::GetError();
                if error != gl::NO_ERROR {
                    println!("OpenGL error: {}", error);
                }
            }
        }

        // Activate the shader program

        unsafe {
            shaders::use_program(topbar_shader_program);
            // Vertex data for a square

            let mut vertices: [f32; 8] = match alignment {
                Alignment::Left =>
                    [
                        -1.0 + (size / 10.0), 1.0,     // Top-left
                        -1.0, 1.0,                      // Top-right
                        -1.0, -1.0,                     // Bottom-right
                        -1.0 + (size / 10.0), -1.0,     // Bottom-left
                    ],
                Alignment::Right =>
                    [
                        1.0-(size/10.0), 1.0,   // Top-left
                        1.0, 1.0,    // Top-right
                        1.0, -1.0,    // Bottom-right
                        1.0-(size/10.0), -1.0,   // Bottom-left
                    ]
            };

            // Generate and bind VAO
            gl::GenVertexArrays(1, &mut VAO);
            gl::BindVertexArray(VAO);

            // Generate VBO and bind buffer
            gl::GenBuffers(1, &mut VBO);
            gl::BindBuffer(gl::ARRAY_BUFFER, VBO);

            // Define buffer data and attributes
            gl::BufferData(gl::ARRAY_BUFFER,
                           (vertices.len() * std::mem::size_of::<f32>()) as GLsizeiptr,
                           vertices.as_ptr() as *const GLvoid,
                           gl::STATIC_DRAW);

            // Specify vertex attributes
            gl::VertexAttribPointer(0, 2, gl::FLOAT, gl::FALSE,
                                    2 * std::mem::size_of::<f32>() as GLsizei,
                                    ptr::null());
            gl::EnableVertexAttribArray(0);
            gl::DrawArrays(gl::TRIANGLE_FAN, 0, 4);
        }
    }

and here the shader code

pub const SHADER_DATA: [u8; 174] = [
  0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x34, 0x36, 0x30,
  0x20, 0x63, 0x6f, 0x72, 0x65, 0x0a, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x76,
  0x65, 0x63, 0x34, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f,
  0x72, 0x3b, 0x0a, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20,
  0x76, 0x65, 0x63, 0x34, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x43,
  0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x20, 0x2f, 0x2f, 0x20, 0x55, 0x6e, 0x69,
  0x66, 0x6f, 0x72, 0x6d, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x0a, 0x0a,
  0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x20,
  0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f,
  0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78,
  0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x20, 0x2f, 0x2f, 0x20, 0x53, 0x65,
  0x74, 0x20, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x63,
  0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x68, 0x65, 0x20,
  0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x63, 0x6f,
  0x6c, 0x6f, 0x72, 0x0a, 0x7d, 0x0a
];

pub const SHADER_SOURCE: &str = r#"
#version 460 core

out vec4 FragColor;

uniform vec4 vertexColor; // Uniform color

void main() {
    FragColor = vertexColor; // Set fragment color to the specified color
}
"#;