Starting to work on rendering
This commit is contained in:
parent
037b38c704
commit
80b397d3ec
|
@ -2,3 +2,5 @@
|
|||
/target/
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
|
||||
assets/models
|
||||
|
|
|
@ -1,22 +1,8 @@
|
|||
varying vec3 fNormal;
|
||||
varying vec4 fFrontColor;
|
||||
#version 140
|
||||
|
||||
vec3 ambientLight = vec3(0.2,0.2,0.2);
|
||||
vec3 directionnalLight = normalize(vec3(10,5,7));
|
||||
vec3 directionnalLightFactor = vec3(0.5,0.5,0.5);
|
||||
|
||||
uniform sampler2D tex;
|
||||
out vec4 color;
|
||||
|
||||
void main() {
|
||||
|
||||
vec3 ambientFactor = ambientLight;
|
||||
vec3 lambertFactor = max(vec3(0.0,0.0,0.0), dot(directionnalLight, fNormal) * directionnalLightFactor);
|
||||
vec4 noTexColor = vec4(ambientFactor + lambertFactor, 1.0);
|
||||
|
||||
vec4 color = texture2D(tex, gl_TexCoord[0].st);
|
||||
|
||||
vec4 fragColor = noTexColor * color;
|
||||
gl_FragColor = fragColor * fFrontColor;
|
||||
|
||||
color = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
varying vec3 fNormal;
|
||||
varying vec4 fTexCoord;
|
||||
varying vec4 fFrontColor;
|
||||
#version 140
|
||||
|
||||
in vec3 vertex;
|
||||
|
||||
void main() {
|
||||
|
||||
fNormal = gl_Normal;
|
||||
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
|
||||
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
|
||||
fFrontColor = gl_Color;
|
||||
|
||||
gl_Position = vec4(vertex, 1.0);
|
||||
}
|
||||
|
|
|
@ -31,6 +31,12 @@ macro_rules! make_vector {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Into<[T; $number]> for $name<T> {
|
||||
fn into(self) -> [T; $number] {
|
||||
self.data
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Clone> Into<($( $t ) ,* )> for $name<T> {
|
||||
fn into(self) -> ($( $t ) ,* ) {
|
||||
( $( self.data[$y] ), *)
|
||||
|
|
26
src/model.rs
26
src/model.rs
|
@ -8,6 +8,15 @@ use parser::{parse, ParserError};
|
|||
use math::vector::{Vector2, Vector3};
|
||||
use math::bounding_box::BoundingBox3;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub struct Vertex {
|
||||
vertex: [f64; 3],
|
||||
tex_coord: [f64; 2],
|
||||
normal: [f64; 3],
|
||||
}
|
||||
|
||||
implement_vertex!(Vertex, vertex, tex_coord, normal);
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
/// The indices needed for each vertex of a face.
|
||||
pub struct FaceVertex {
|
||||
|
@ -154,6 +163,23 @@ impl Part {
|
|||
f.material_name = self.material_name.clone();
|
||||
self.faces.push(f);
|
||||
}
|
||||
|
||||
pub fn build_shape(&self, vertices: &Vec<Vector3<f64>>, tex_coords: &Vec<Vector2<f64>>, normals: &Vec<Vector3<f64>>) -> Vec<Vertex> {
|
||||
|
||||
let mut shape = vec![];
|
||||
for face in &self.faces {
|
||||
for &&v in &[&face.a, &face.b, &face.c] {
|
||||
shape.push(Vertex {
|
||||
vertex: vertices[v.vertex].into(),
|
||||
tex_coord: tex_coords[v.texture_coordinate.unwrap()].into(),
|
||||
normal: normals[v.normal.unwrap()].into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
shape
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// A 3D model.
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
extern crate glium;
|
||||
extern crate model_converter;
|
||||
|
||||
use glium::Display;
|
||||
use glium::glutin;
|
||||
|
@ -12,6 +13,9 @@ use glium::glutin::Event;
|
|||
use glium::glutin::WindowEvent;
|
||||
use glium::glutin::VirtualKeyCode;
|
||||
|
||||
use model_converter::parser::parse;
|
||||
use model_converter::renderer::Renderer;
|
||||
|
||||
fn main() {
|
||||
|
||||
let mut events_loop = EventsLoop::new();
|
||||
|
@ -19,20 +23,16 @@ fn main() {
|
|||
let context = ContextBuilder::new();
|
||||
let display = Display::new(window, context, &events_loop).unwrap();
|
||||
|
||||
let program = glium::Program::from_source(
|
||||
&display,
|
||||
include_str!("../../assets/shaders/shader.vert"),
|
||||
include_str!("../../assets/shaders/shader.frag"),
|
||||
None
|
||||
).unwrap();
|
||||
|
||||
let mut closed = false;
|
||||
|
||||
while !closed {
|
||||
let mut target = display.draw();
|
||||
let model = parse("./assets/models/toonlink/link.obj").unwrap();
|
||||
let mut renderer = Renderer::new(display);
|
||||
renderer.add_model(&model);
|
||||
|
||||
use glium::Surface;
|
||||
target.clear_color(0.0, 0.0, 1.0, 1.0);
|
||||
while !closed {
|
||||
|
||||
let mut target = renderer.draw();
|
||||
renderer.render(&mut target);
|
||||
|
||||
target.finish().unwrap();
|
||||
|
||||
|
|
|
@ -1 +1,68 @@
|
|||
pub struct Renderer;
|
||||
use glium::index::{
|
||||
NoIndices,
|
||||
PrimitiveType
|
||||
};
|
||||
use glium::{
|
||||
Program,
|
||||
Display,
|
||||
VertexBuffer,
|
||||
Frame,
|
||||
};
|
||||
|
||||
use model::{Model, Vertex};
|
||||
|
||||
pub struct Renderer<'a> {
|
||||
display: Display,
|
||||
program: Program,
|
||||
models: Vec<(&'a Model, Vec<VertexBuffer<Vertex>>)>,
|
||||
}
|
||||
|
||||
impl<'a> Renderer<'a> {
|
||||
pub fn new(display: Display) -> Renderer<'a> {
|
||||
|
||||
let program = Program::from_source(
|
||||
&display,
|
||||
include_str!("../assets/shaders/shader.vert"),
|
||||
include_str!("../assets/shaders/shader.frag"),
|
||||
None
|
||||
).unwrap();
|
||||
|
||||
Renderer {
|
||||
display: display,
|
||||
program: program,
|
||||
models: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_model(&mut self, model: &'a Model) {
|
||||
let mut buffers = vec![];
|
||||
|
||||
for part in &model.parts {
|
||||
let shape = part.build_shape(&model.vertices, &model.texture_coordinates, &model.normals);
|
||||
buffers.push(VertexBuffer::new(&self.display, &shape).unwrap());
|
||||
}
|
||||
|
||||
self.models.push((model, buffers));
|
||||
}
|
||||
|
||||
pub fn draw(&self) -> Frame {
|
||||
self.display.draw()
|
||||
}
|
||||
|
||||
pub fn render(&self, target: &mut Frame) {
|
||||
use glium::Surface;
|
||||
target.clear_color(0.0, 0.0, 0.0, 1.0);
|
||||
|
||||
for &(_, ref buffers) in &self.models {
|
||||
for buffer in buffers {
|
||||
target.draw(
|
||||
buffer,
|
||||
NoIndices(PrimitiveType::TrianglesList),
|
||||
&self.program,
|
||||
&uniform!(),
|
||||
&Default::default(),
|
||||
).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue