Compare commits
2 Commits
0dcd45ddc7
...
91fb640d37
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91fb640d37 | ||
|
|
eeb3d016c8 |
@@ -1 +1,2 @@
|
||||
pub mod render_timer;
|
||||
pub mod shader;
|
||||
34
src/main.rs
34
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();
|
||||
@@ -121,7 +137,7 @@ fn main() {
|
||||
handle_window_event(&mut window, event);
|
||||
}
|
||||
|
||||
render(&mut render_timer, sp ,vao);
|
||||
render(&mut render_timer, sp, vao);
|
||||
|
||||
window.swap_buffers();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user