-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
115 lines (100 loc) · 3.92 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use glfw::{Key, Modifiers, WindowEvent};
use gloam::{
app,
error::Result,
shader::{program::Linker, Shader, ShaderType},
texture::{TextureBuilder, TextureFilterParam, TextureWrapParam},
uniform::Uniform,
vertex::{Primitive, Usage, VOBInit, VertexObjectBuilder},
};
use std::path::PathBuf;
const POSITION_ATTR: [f32; 12] = [
0.5, 0.5, 0.0, // top right
0.5, -0.5, 0.0, // bottom right
-0.5, -0.5, 0.0, // bottom left
-0.5, 0.5, 0.0, // top left
];
const TEXTURE_ATTR: [f32; 8] = [
1.0, 1.0, // top right
1.0, 0.0, // bottom right
0.0, 0.0, // bottom left
0.0, 1.0, // bottom right
];
const COLOR_ATTR: [f32; 12] = [
1.0, 0.0, 0.0, // top right
0.0, 1.0, 0.0, // bottom right
0.0, 0.0, 1.0, // bottom left
1.0, 1.0, 0.0, // bottom right
];
const INDICES: [u32; 6] = [
0, 1, 3, // first triangle
1, 2, 3, // second triangle
];
fn main() -> Result<()> {
env_logger::init();
let (mut window, mut ctx) = app::init_default_opengl_3_3("HelloTextures")?;
window.set_key_polling(true);
window.set_framebuffer_size_polling(true);
let vertex_shader_src = PathBuf::new()
.join("examples")
.join("hello_textures")
.join("hello_textures_vertex.glsl");
let fragment_shader_src = PathBuf::new()
.join("examples")
.join("hello_textures")
.join("hello_textures_fragment.glsl");
let vertex_shader = Shader::new(vertex_shader_src, ShaderType::Vertex)?;
let fragment_shader = Shader::new(fragment_shader_src, ShaderType::Fragment)?;
let program = Linker::new()
.attach_shader(vertex_shader)
.attach_shader(fragment_shader)
.link(&mut ctx)?;
let texture_wood_src = PathBuf::new()
.join("examples")
.join("hello_textures")
.join("hello_textures.jpg");
let texture_wood = TextureBuilder::new_2d_rgba8(texture_wood_src)
.map(|b| b.s_wrap(TextureWrapParam::Repeat))
.map(|b| b.t_wrap(TextureWrapParam::Repeat))
.map(|b| b.min_filter(TextureFilterParam::LinearMipmapLinear))
.map(|b| b.mag_filter(TextureFilterParam::Linear))
.and_then(|b| b.build(&mut ctx))?;
let texture_wood_smiley = PathBuf::new()
.join("examples")
.join("hello_textures")
.join("hello_textures_smiley.png");
let texture_smiley = TextureBuilder::new_2d_rgba8(texture_wood_smiley)
.map(|b| b.s_wrap(TextureWrapParam::Repeat))
.map(|b| b.t_wrap(TextureWrapParam::Repeat))
.map(|b| b.min_filter(TextureFilterParam::LinearMipmapLinear))
.map(|b| b.mag_filter(TextureFilterParam::Linear))
.and_then(|b| b.build(&mut ctx))?;
let surface = VertexObjectBuilder::<VOBInit>::new(Primitive::Triangles, Usage::Static)
.attribute("apos", 3, &POSITION_ATTR)
.and_then(|b| b.attribute("acol", 3, &COLOR_ATTR))
.and_then(|b| b.attribute("atex", 2, &TEXTURE_ATTR))
.and_then(|b| b.indexes(&INDICES))
.and_then(|b| b.build(&mut ctx, program))?;
ctx.try_use_program(program)?;
ctx.try_bind_vertex_object(surface)?;
let texture_unit_wood = ctx.activate_texture(texture_wood, true)?;
let texture_unit_smiley = ctx.activate_texture(texture_smiley, true)?;
ctx.try_set_uniform(&Uniform::new_1i("texture1", texture_unit_wood))?;
ctx.try_set_uniform(&Uniform::new_1i("texture2", texture_unit_smiley))?;
window.run_event_loop(|win, event| {
match event {
None => (),
Some(win_event) => match win_event {
WindowEvent::Key(key, _, _, modifier) => match (modifier, key) {
(Modifiers::Super, Key::W) | (_, Key::Escape) => win.set_should_close(true),
_ => (),
},
WindowEvent::FramebufferSize(width, height) => ctx.viewport(0, 0, width, height),
_ => (),
},
}
ctx.try_render()?;
win.draw();
Ok(())
})
}