Starting to work on rendering
This commit is contained in:
parent
037b38c704
commit
80b397d3ec
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,3 +2,5 @@
|
|||||||
/target/
|
/target/
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
|
||||||
|
assets/models
|
||||||
|
@ -1,22 +1,8 @@
|
|||||||
varying vec3 fNormal;
|
#version 140
|
||||||
varying vec4 fFrontColor;
|
|
||||||
|
|
||||||
vec3 ambientLight = vec3(0.2,0.2,0.2);
|
out vec4 color;
|
||||||
vec3 directionnalLight = normalize(vec3(10,5,7));
|
|
||||||
vec3 directionnalLightFactor = vec3(0.5,0.5,0.5);
|
|
||||||
|
|
||||||
uniform sampler2D tex;
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
color = vec4(1.0, 0.0, 0.0, 1.0);
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,7 @@
|
|||||||
varying vec3 fNormal;
|
#version 140
|
||||||
varying vec4 fTexCoord;
|
|
||||||
varying vec4 fFrontColor;
|
in vec3 vertex;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
gl_Position = vec4(vertex, 1.0);
|
||||||
fNormal = gl_Normal;
|
|
||||||
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
|
|
||||||
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
|
|
||||||
fFrontColor = gl_Color;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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> {
|
impl<T: Copy + Clone> Into<($( $t ) ,* )> for $name<T> {
|
||||||
fn into(self) -> ($( $t ) ,* ) {
|
fn into(self) -> ($( $t ) ,* ) {
|
||||||
( $( self.data[$y] ), *)
|
( $( 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::vector::{Vector2, Vector3};
|
||||||
use math::bounding_box::BoundingBox3;
|
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)]
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
/// The indices needed for each vertex of a face.
|
/// The indices needed for each vertex of a face.
|
||||||
pub struct FaceVertex {
|
pub struct FaceVertex {
|
||||||
@ -154,6 +163,23 @@ impl Part {
|
|||||||
f.material_name = self.material_name.clone();
|
f.material_name = self.material_name.clone();
|
||||||
self.faces.push(f);
|
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.
|
/// A 3D model.
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
extern crate glium;
|
extern crate glium;
|
||||||
|
extern crate model_converter;
|
||||||
|
|
||||||
use glium::Display;
|
use glium::Display;
|
||||||
use glium::glutin;
|
use glium::glutin;
|
||||||
@ -12,6 +13,9 @@ use glium::glutin::Event;
|
|||||||
use glium::glutin::WindowEvent;
|
use glium::glutin::WindowEvent;
|
||||||
use glium::glutin::VirtualKeyCode;
|
use glium::glutin::VirtualKeyCode;
|
||||||
|
|
||||||
|
use model_converter::parser::parse;
|
||||||
|
use model_converter::renderer::Renderer;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
let mut events_loop = EventsLoop::new();
|
let mut events_loop = EventsLoop::new();
|
||||||
@ -19,20 +23,16 @@ fn main() {
|
|||||||
let context = ContextBuilder::new();
|
let context = ContextBuilder::new();
|
||||||
let display = Display::new(window, context, &events_loop).unwrap();
|
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;
|
let mut closed = false;
|
||||||
|
|
||||||
while !closed {
|
let model = parse("./assets/models/toonlink/link.obj").unwrap();
|
||||||
let mut target = display.draw();
|
let mut renderer = Renderer::new(display);
|
||||||
|
renderer.add_model(&model);
|
||||||
|
|
||||||
use glium::Surface;
|
while !closed {
|
||||||
target.clear_color(0.0, 0.0, 1.0, 1.0);
|
|
||||||
|
let mut target = renderer.draw();
|
||||||
|
renderer.render(&mut target);
|
||||||
|
|
||||||
target.finish().unwrap();
|
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…
x
Reference in New Issue
Block a user