Compare commits
3 Commits
0dcd45ddc7
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d97d7a01f | ||
|
|
91fb640d37 | ||
|
|
eeb3d016c8 |
@@ -1 +1,3 @@
|
||||
pub mod render_timer;
|
||||
pub mod shader;
|
||||
pub mod vao;
|
||||
32
src/main.rs
32
src/main.rs
@@ -88,7 +88,9 @@ fn main() {
|
||||
unsafe { gl::BindVertexArray(vao) };
|
||||
|
||||
// 创建VBO
|
||||
let a: [f32; _] = [-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0];
|
||||
let a: [f32; _] = [
|
||||
0.5, 0.5, 0.0, 0.5, -0.5, 0.0, -0.5, -0.5, 0.0, -0.5, 0.5, 0.0,
|
||||
];
|
||||
let mut vbo: u32 = 0;
|
||||
unsafe { gl::GenBuffers(1, &mut vbo) };
|
||||
unsafe { gl::BindBuffer(gl::ARRAY_BUFFER, vbo) };
|
||||
@@ -114,6 +116,20 @@ fn main() {
|
||||
};
|
||||
unsafe { gl::EnableVertexAttribArray(0) };
|
||||
|
||||
// 创建EBO
|
||||
let b: [u32; _] = [0, 1, 3, 1, 2, 3];
|
||||
let mut ebo = 0;
|
||||
unsafe { gl::GenBuffers(1, &mut ebo) };
|
||||
unsafe { gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ebo) };
|
||||
unsafe {
|
||||
gl::BufferData(
|
||||
gl::ELEMENT_ARRAY_BUFFER,
|
||||
size_of_val(&b) as _,
|
||||
b.as_ptr() as _,
|
||||
gl::STATIC_DRAW,
|
||||
)
|
||||
};
|
||||
|
||||
// 渲染循环
|
||||
while !window.should_close() {
|
||||
glfw.poll_events();
|
||||
@@ -135,11 +151,23 @@ fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) {
|
||||
}
|
||||
|
||||
fn render(render_timer: &mut RenderTimer, sp: u32, vao: u32) {
|
||||
// 设置背景颜色
|
||||
let t = render_timer.time_count() as f32 / 1000.0 as f32;
|
||||
unsafe { gl::ClearColor(t.sin() * 0.5 + 1.0, t.cos() * 0.5 + 1.0, 0.3, 1.0) };
|
||||
unsafe { gl::Clear(gl::COLOR_BUFFER_BIT) };
|
||||
|
||||
// 向着色器传入颜色
|
||||
let location = unsafe { gl::GetUniformLocation(sp, "inColor".as_ptr() as _) };
|
||||
unsafe { gl::Uniform4f(location, t.cos() * 0.5 + 1.0, t.sin() * 0.5 + 1.0, 0.7, 1.0) };
|
||||
|
||||
// 绘制图形
|
||||
unsafe { gl::UseProgram(sp) };
|
||||
unsafe { gl::BindVertexArray(vao) };
|
||||
unsafe { gl::DrawArrays(gl::TRIANGLES, 0, 3) };
|
||||
if (t as u32) % 2 == 1 {
|
||||
unsafe { gl::PolygonMode(gl::FRONT_AND_BACK, gl::LINE) };
|
||||
}
|
||||
else {
|
||||
unsafe { gl::PolygonMode(gl::FRONT_AND_BACK, gl::FILL) };
|
||||
}
|
||||
unsafe { gl::DrawElements(gl::TRIANGLES, 6, gl::UNSIGNED_INT, null()) };
|
||||
}
|
||||
|
||||
86
src/shader.rs
Normal file
86
src/shader.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
use std::{
|
||||
ptr::{null, null_mut},
|
||||
str::from_utf8,
|
||||
};
|
||||
|
||||
use gl::{CompileShader, ShaderSource};
|
||||
|
||||
|
||||
pub struct Shader {
|
||||
id: gl::types::GLuint,
|
||||
}
|
||||
|
||||
impl Shader {
|
||||
pub fn new(src: &str, type_: gl::types::GLenum) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
// 编译着色器
|
||||
let id = unsafe { gl::CreateShader(type_) };
|
||||
let c_src = std::ffi::CString::new(src)?;
|
||||
unsafe { ShaderSource(id, 1, &c_src.as_ptr() as _, null()) };
|
||||
unsafe { CompileShader(id) };
|
||||
|
||||
// 检查编译是否成功
|
||||
let mut success = 0;
|
||||
unsafe { gl::GetShaderiv(id, gl::COMPILE_STATUS, &mut success) };
|
||||
if success == 0 {
|
||||
let mut log = [0; 512];
|
||||
unsafe { gl::GetShaderInfoLog(id, 512, core::ptr::null_mut(), log.as_mut_ptr()) };
|
||||
let log_u8: [u8; 512] = unsafe { std::mem::transmute(log) };
|
||||
let e = from_utf8(&log_u8)?;
|
||||
return Err(e.into());
|
||||
}
|
||||
|
||||
Ok(Shader { id: id })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Shader {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
gl::DeleteShader(self.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Program {
|
||||
id: gl::types::GLuint,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
pub fn new() -> Self {
|
||||
let id = unsafe { gl::CreateProgram() };
|
||||
Program { id: id }
|
||||
}
|
||||
|
||||
pub fn attach_shader(&self, shader: &Shader) {
|
||||
unsafe { gl::AttachShader(self.id, shader.id) };
|
||||
}
|
||||
|
||||
pub fn link_program(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// 链接着色器
|
||||
unsafe { gl::LinkProgram(self.id) };
|
||||
|
||||
// 检查链接是否成功
|
||||
let mut success = 0;
|
||||
unsafe { gl::GetProgramiv(self.id, gl::LINK_STATUS, &mut success) };
|
||||
if success == 0 {
|
||||
let mut log = [0; 512];
|
||||
unsafe { gl::GetProgramInfoLog(self.id, 512, null_mut(), log.as_mut_ptr()) };
|
||||
let log_u8: [u8; 512] = unsafe { std::mem::transmute(log) };
|
||||
let e = from_utf8(&log_u8)?;
|
||||
return Err(e.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn use_program(&self) {
|
||||
unsafe { gl::UseProgram(self.id) };
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Program {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
gl::DeleteProgram(self.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec4 inColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(0.0, 0.8824, 1.0, 1.0);
|
||||
FragColor = inColor;
|
||||
}
|
||||
104
src/vao.rs
Normal file
104
src/vao.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use std::os::raw::c_void;
|
||||
|
||||
pub struct EBO {
|
||||
id: gl::types::GLuint,
|
||||
}
|
||||
|
||||
impl EBO {
|
||||
pub fn new(data: &[u32]) -> Self {
|
||||
let mut id = 0;
|
||||
unsafe { gl::GenBuffers(1, &mut id) };
|
||||
unsafe { gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, id) };
|
||||
unsafe {
|
||||
gl::BufferData(
|
||||
gl::ELEMENT_ARRAY_BUFFER,
|
||||
size_of_val(&data) as _,
|
||||
data.as_ptr() as _,
|
||||
gl::STATIC_DRAW,
|
||||
)
|
||||
};
|
||||
|
||||
EBO { id: id }
|
||||
}
|
||||
|
||||
pub fn bind(&self) {
|
||||
unsafe { gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, self.id) };
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VBO {
|
||||
id: gl::types::GLuint,
|
||||
}
|
||||
|
||||
impl VBO {
|
||||
pub fn new(data: &[u32]) -> Self {
|
||||
let mut id: u32 = 0;
|
||||
unsafe { gl::GenBuffers(1, &mut id) };
|
||||
unsafe { gl::BindBuffer(gl::ARRAY_BUFFER, id) };
|
||||
unsafe {
|
||||
gl::BufferData(
|
||||
gl::ARRAY_BUFFER,
|
||||
size_of_val(&data) as _,
|
||||
data.as_ptr() as _,
|
||||
gl::STATIC_DRAW,
|
||||
)
|
||||
};
|
||||
|
||||
VBO { id: id }
|
||||
}
|
||||
|
||||
pub fn bind(&self) {
|
||||
unsafe { gl::BindBuffer(gl::ARRAY_BUFFER, self.id) };
|
||||
}
|
||||
|
||||
pub fn add_attrib(// TODO: 迁移到其他地方
|
||||
&self,
|
||||
index: u32,
|
||||
size: i32,
|
||||
type_: u32,
|
||||
normalized: u8,
|
||||
stride: i32,
|
||||
pointer: *const c_void,
|
||||
) {
|
||||
self.bind();
|
||||
|
||||
unsafe { gl::VertexAttribPointer(index, size, type_, normalized, stride, pointer) };
|
||||
unsafe { gl::EnableVertexAttribArray(index) };
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VAO {
|
||||
id: gl::types::GLuint,
|
||||
vbo: Option<VBO>,
|
||||
ebo: Option<EBO>,
|
||||
}
|
||||
|
||||
impl VAO {
|
||||
pub fn new() -> Self {
|
||||
let mut id = 0;
|
||||
unsafe { gl::GenVertexArrays(1, &mut id) };
|
||||
unsafe { gl::BindVertexArray(id) };
|
||||
|
||||
VAO { id: id, vbo: None, ebo: None }
|
||||
}
|
||||
|
||||
pub fn bind(&self) {
|
||||
unsafe { gl::BindVertexArray(self.id) };
|
||||
}
|
||||
|
||||
pub fn new_vbo(&mut self, data:&[u32]) {
|
||||
self.bind();
|
||||
|
||||
self.vbo = Some(VBO::new(data));
|
||||
}
|
||||
|
||||
pub fn new_ebo(&mut self, data:&[u32]) {
|
||||
self.bind();
|
||||
|
||||
self.ebo = Some(EBO::new(data));
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VertexAttribute {
|
||||
// TODO: 定义
|
||||
}
|
||||
Reference in New Issue
Block a user