Display two textured triangles in 3D

This commit is contained in:
2022-10-16 22:57:32 +02:00
parent 238052c8a8
commit ea07e78b18
3 changed files with 26 additions and 10 deletions
+1
View File
@@ -5,4 +5,5 @@ edition = "2021"
[dependencies] [dependencies]
glium = "0.32.1" glium = "0.32.1"
image = "0.24.4"
glutin = "0.29.1" glutin = "0.29.1"
+17 -2
View File
@@ -1,6 +1,8 @@
#[macro_use] #[macro_use]
extern crate glium; extern crate glium;
use std::io::Cursor;
#[allow(unused_imports)] #[allow(unused_imports)]
use glium::{glutin, Surface}; use glium::{glutin, Surface};
@@ -16,6 +18,12 @@ fn main() {
// building the vertex and index buffers // building the vertex and index buffers
let vertex_buffer = support::load_ground(&display); let vertex_buffer = support::load_ground(&display);
let image = image::load(Cursor::new(&include_bytes!("../opengl.png")),
image::ImageFormat::Png).unwrap().to_rgba8();
let image_dimensions = image.dimensions();
let image = glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);
let texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap();
// the program // the program
let program = program!(&display, let program = program!(&display,
140 => { 140 => {
@@ -27,12 +35,16 @@ fn main() {
in vec3 position; in vec3 position;
in vec3 normal; in vec3 normal;
in vec2 tex_coords;
out vec3 v_position; out vec3 v_position;
out vec3 v_normal; out vec3 v_normal;
out vec2 v_tex_coords;
void main() { void main() {
v_position = position; v_position = position;
v_normal = normal; v_normal = normal;
v_tex_coords = tex_coords;
gl_Position = persp_matrix * view_matrix * vec4(v_position * 0.005, 1.0); gl_Position = persp_matrix * view_matrix * vec4(v_position * 0.005, 1.0);
} }
", ",
@@ -41,14 +53,16 @@ fn main() {
#version 140 #version 140
in vec3 v_normal; in vec3 v_normal;
in vec2 v_tex_coords;
out vec4 f_color; out vec4 f_color;
uniform sampler2D tex;
const vec3 LIGHT = vec3(0.0, -1.0, 0.0); const vec3 LIGHT = vec3(0.0, -1.0, 0.0);
void main() { void main() {
float lum = max(dot(normalize(v_normal), normalize(LIGHT)), 0.0); float lum = max(dot(normalize(v_normal), normalize(LIGHT)), 0.0);
vec3 color = (0.3 + 0.7 * lum) * vec3(1.0, 1.0, 1.0); f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex, v_tex_coords);
f_color = vec4(color, 1.0);
} }
" "
}, },
@@ -65,6 +79,7 @@ fn main() {
let uniforms = uniform! { let uniforms = uniform! {
persp_matrix: camera.get_perspective(), persp_matrix: camera.get_perspective(),
view_matrix: camera.get_view(), view_matrix: camera.get_view(),
tex: &texture,
}; };
// draw parameters // draw parameters
+8 -8
View File
@@ -60,47 +60,47 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
struct Vertex { struct Vertex {
position: [f32; 3], position: [f32; 3],
normal: [f32; 3], normal: [f32; 3],
texture: [f32; 2], tex_coords: [f32; 2],
} }
implement_vertex!(Vertex, position, normal, texture); implement_vertex!(Vertex, position, normal, tex_coords);
let mut vertex_data = Vec::new(); let mut vertex_data = Vec::new();
vertex_data.push(Vertex { vertex_data.push(Vertex {
position: [0.0, 0.0, 0.0], position: [0.0, 0.0, 0.0],
normal: [0.0, 0.0, 1.0], normal: [0.0, 0.0, 1.0],
texture: [0.0, 0.0], tex_coords: [0.0, 0.0],
}); });
vertex_data.push(Vertex { vertex_data.push(Vertex {
position: [1.0, 0.0, 0.0], position: [1.0, 0.0, 0.0],
normal: [0.0, 0.0, 1.0], normal: [0.0, 0.0, 1.0],
texture: [0.0, 0.0], tex_coords: [1.0, 0.0],
}); });
vertex_data.push(Vertex { vertex_data.push(Vertex {
position: [0.0, 0.0, 1.0], position: [0.0, 0.0, 1.0],
normal: [0.0, 0.0, 1.0], normal: [0.0, 0.0, 1.0],
texture: [0.0, 0.0], tex_coords: [0.0, 1.0],
}); });
vertex_data.push(Vertex { vertex_data.push(Vertex {
position: [1.0, 0.0, 0.0], position: [1.0, 0.0, 0.0],
normal: [0.0, 0.0, 1.0], normal: [0.0, 0.0, 1.0],
texture: [0.0, 0.0], tex_coords: [1.0, 0.0],
}); });
vertex_data.push(Vertex { vertex_data.push(Vertex {
position: [0.0, 0.0, 1.0], position: [0.0, 0.0, 1.0],
normal: [0.0, 0.0, 1.0], normal: [0.0, 0.0, 1.0],
texture: [0.0, 0.0], tex_coords: [0.0, 1.0],
}); });
vertex_data.push(Vertex { vertex_data.push(Vertex {
position: [1.0, 0.0, 1.0], position: [1.0, 0.0, 1.0],
normal: [0.0, 0.0, 1.0], normal: [0.0, 0.0, 1.0],
texture: [0.0, 0.0], tex_coords: [1.0, 1.0],
}); });
/*for object in data.objects.iter() { /*for object in data.objects.iter() {