#sound #opengl #glfw #graphics #gamedev

已删除 heavyli

一个图形库(同时支持声音功能)

0.0.6 2022年7月21日
0.0.5 2022年7月21日
0.0.4 2022年7月20日

#25 in #glfw

自定义许可证

50KB
964

HeavylI

HeavylI 是一个图形库/ crate(同时支持声音功能),使用 OpenGL-GLFW 创建。

使用方法

此项目目前旨在创建 2D 游戏,支持 2D 纹理的实现(检查 heavyli::rendering::texture_2d)、不同形状类型(检查 heavyli::rendering::shape::shape_vertices)和声音播放(检查 heavyli::sound::sound_player)。

代码示例

在编写代码之前,您应该在程序的主目录中放置这两个着色器文件: shaders/basic_fragment.glsl

#version 330 core

out vec4 FragColor;

in vec3 ourColor;

void main()
{
	FragColor = vec4(ourColor, 1.0f);
}

shaders/basic_vertex.glsl:

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;

out vec3 ourColor;

uniform mat4 translation;

void main()
{
	gl_Position = translation * vec4(aPos, 1.0);
	ourColor = aColor;
}

现在,屏幕上渲染矩形的代码示例: src/main.rs

extern crate glfw;
extern crate heavyli;
extern crate nalgebra_glm as glm;
use heavyli::opengl_modules::init_glfw;
use heavyli::rendering::shader::{self, ShaderValue};
use heavyli::rendering::shape::{
    render,
    buffers::{self, Buffers},
    shape_vertices::ShapeVertices,
    vertices::{DrawType, Vertex},
};
use heavyli::transform::{projection_type::ProjectionType, transform, view};
use heavyli::rendering::window::{ClearColor, Window};

// Function to initialize the VBO and VAO with the new vertices.
//
// @param buffers a reference to the VAO and VBO.
// @param vertices the vertices of the new shape (including position, color and texture information).
fn init_buffers(buffers: &Buffers, vertices: &mut Vec<Vertex>) {
    unsafe {
        buffers::bind_buffers(buffers); // Every changes to VAO or VBO should start with binding.

        // texture_2d::attach(vertices);  // Attach the 2D texture coordinates to the vertices.

        buffers::bind_data(buffers.vao, vertices); // Bind the vertices into the vertex array object.

        buffers::update_vertex_attributes(DrawType::Triangles); // With that determine the shader's layouts.

        buffers::unbind();
    }
}

// Reserves camera information.
struct Camera {
    position: glm::Vec3,
    rotation: glm::Vec2,
}

// Screen width and height:
const SCR_WIDTH: u32 = 800;
const SCR_HEIGHT: u32 = 600;

fn main() {
    let mut glfw = init_glfw();  // Initialize GLFW handler.
    // Create a new window:
    let mut window = Window::new(&glfw, "Hello Rectangle", SCR_WIDTH, SCR_HEIGHT);

    // Window Configurations:
    window.make_current();
    window.set_key_polling(true);
    window.set_framebuffer_size_polling(true);
    window.load_function_pointers();

    let mut buffers = unsafe { buffers::generate_buffers() };  // Buffers used for VBO and VAO.
    let shader_id = shader::compile("shaders/basic_vertex.glsl", "shaders/basic_fragment.glsl");  // Initialize shader.
    // Vertices of Rectangle (default color white):
    let mut vertices = ShapeVertices::Rectangle(glm::vec2(1.0, 1.0), glm::vec2(0.0, 0.0)).value();

    // Set different colors:
    vertices[0].color = glm::vec3(0.5, 0.3, 1.0);
    vertices[1].color = glm::vec3(0.5, 0.3, 1.0);
    vertices[2].color = glm::vec3(1.0, 0.3, 1.0);
    vertices[3].color = glm::vec3(0.5, 0.7, 1.0);
    vertices[4].color = glm::vec3(0.5, 0.0, 1.0);
    vertices[5].color = glm::vec3(1.0, 0.7, 1.0);

    // Initialize the vertices into the vertex buffers:
    init_buffers(&buffers, &mut vertices);

    // Create a new transform to move the object (in our case the rectangle):
    let transform = transform::create_transform_by_position(glm::vec3(0.0, 0.0, 5.0));

    // Initialize Camera:
    let camera = Camera {
        position: glm::vec3(0.0, 0.0, -2.0),
        rotation: glm::vec2(0.0, 90.0),
    };

    while window.is_open() {  // Runs until the window closes.
        // Process input events:
        window.process_events();

        // Clear window to change background color and render next frame:
        unsafe {
            Window::clear(ClearColor {
                red: 0.0,
                green: 0.3,
                blue: 0.8,
                alpha: 1.0,
            });
        }

        // Camrea Perspective Configurations:
        const Z_NEAR: f32 = 0.1;
        const Z_FAR: f32 = 100.0;
        const FOV: f32 = 45.0;

        unsafe {
            // Set values in shader:
            shader::use_program(shader_id);
            shader::set_value(
                shader_id,
                "translation",
                ShaderValue::Mat4(transform::translate(
                    &transform,
                    Z_NEAR,
                    Z_FAR,
                    FOV,
                    view::get_view_matrix(&camera.position, camera.rotation.x, camera.rotation.y),
                    glm::vec2(SCR_WIDTH as f32, SCR_HEIGHT as f32),
                    0.0,
                    ProjectionType::Perspective,
                )),
            );

            // Draw all rectangle's data:
            render::draw_data(
                shader_id,
                buffers.vao,
                DrawType::Triangles,
                vertices.len() as i32,
                true,
            );
        }

        // Put at the end of the frame:
        window.swap_buffers();
        glfw.poll_events();
    }

    // Must be called to free memory:
    unsafe {
        buffers::delete_buffers(&mut buffers);
    }
}

特性

  • 形状处理(VAO、VBO、顶点等...)
  • 变换(Transform 结构,3D 世界视图)
  • 2D 纹理处理
  • 声音播放(使用 rodio crate)

要求

  • cmake
  • make

Windows 用户 - 使用 MSYS & MinGW 编译

请确保已安装 MSYS 工具。然后按照以下步骤操作

  1. 使用以下命令更新 MSYS
pacman -Syuu
  1. 安装工具链:a) 64 位
pacman -S mingw-w64-x86_64-toolchain

b) 32 位

pacman -S mingw-w64-i686-toolchain

更多信息,请查看 此链接

  1. 下载 GLFW 预编译二进制文件

  2. 将满足您计算机位架构的二进制文件放入

path/to/msys/mingw-your-version/lib

依赖项

~9–14MB
~228K SLoC