-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
732 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
use miniquad::*; | ||
|
||
use glam::{vec3, Mat3, Mat4}; | ||
use nanoimage::png; | ||
|
||
struct Stage { | ||
display_pipeline: Pipeline, | ||
display_bind: Bindings, | ||
rx: f32, | ||
ry: f32, | ||
ctx: Box<dyn RenderingBackend>, | ||
} | ||
|
||
impl Stage { | ||
pub fn new() -> Stage { | ||
let mut ctx: Box<dyn RenderingBackend> = window::new_rendering_backend(); | ||
|
||
let texture0 = png::decode(include_bytes!("assets/skybox/skybox_px.png")).unwrap(); | ||
let texture1 = png::decode(include_bytes!("assets/skybox/skybox_nx.png")).unwrap(); | ||
let texture2 = png::decode(include_bytes!("assets/skybox/skybox_py.png")).unwrap(); | ||
let texture3 = png::decode(include_bytes!("assets/skybox/skybox_ny.png")).unwrap(); | ||
let texture4 = png::decode(include_bytes!("assets/skybox/skybox_pz.png")).unwrap(); | ||
let texture5 = png::decode(include_bytes!("assets/skybox/skybox_nz.png")).unwrap(); | ||
let color_img = ctx.new_texture( | ||
TextureAccess::Static, | ||
TextureSource::Array(&[ | ||
&[&texture0.data], | ||
&[&texture1.data], | ||
&[&texture2.data], | ||
&[&texture3.data], | ||
&[&texture4.data], | ||
&[&texture5.data], | ||
]), | ||
TextureParams { | ||
width: texture0.width as _, | ||
height: texture0.height as _, | ||
format: TextureFormat::RGBA8, | ||
..Default::default() | ||
}, | ||
); | ||
|
||
#[rustfmt::skip] | ||
let vertices: &[f32] = &[ | ||
/* pos color uvs */ | ||
-1.0, -1.0, -1.0, 1.0, 0.5, 0.5, 1.0, 0.0, 0.0, | ||
1.0, -1.0, -1.0, 1.0, 0.5, 0.5, 1.0, 1.0, 0.0, | ||
1.0, 1.0, -1.0, 1.0, 0.5, 0.5, 1.0, 1.0, 1.0, | ||
-1.0, 1.0, -1.0, 1.0, 0.5, 0.5, 1.0, 0.0, 1.0, | ||
|
||
-1.0, -1.0, 1.0, 0.5, 1.0, 0.5, 1.0, 0.0, 0.0, | ||
1.0, -1.0, 1.0, 0.5, 1.0, 0.5, 1.0, 1.0, 0.0, | ||
1.0, 1.0, 1.0, 0.5, 1.0, 0.5, 1.0, 1.0, 1.0, | ||
-1.0, 1.0, 1.0, 0.5, 1.0, 0.5, 1.0, 0.0, 1.0, | ||
|
||
-1.0, -1.0, -1.0, 0.5, 0.5, 1.0, 1.0, 0.0, 0.0, | ||
-1.0, 1.0, -1.0, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, | ||
-1.0, 1.0, 1.0, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, | ||
-1.0, -1.0, 1.0, 0.5, 0.5, 1.0, 1.0, 0.0, 1.0, | ||
|
||
1.0, -1.0, -1.0, 1.0, 0.5, 0.0, 1.0, 0.0, 0.0, | ||
1.0, 1.0, -1.0, 1.0, 0.5, 0.0, 1.0, 1.0, 0.0, | ||
1.0, 1.0, 1.0, 1.0, 0.5, 0.0, 1.0, 1.0, 1.0, | ||
1.0, -1.0, 1.0, 1.0, 0.5, 0.0, 1.0, 0.0, 1.0, | ||
|
||
-1.0, -1.0, -1.0, 0.0, 0.5, 1.0, 1.0, 0.0, 0.0, | ||
-1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 1.0, 1.0, 0.0, | ||
1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0, | ||
1.0, -1.0, -1.0, 0.0, 0.5, 1.0, 1.0, 0.0, 1.0, | ||
|
||
-1.0, 1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 0.0, 0.0, | ||
-1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 1.0, 1.0, 0.0, | ||
1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 1.0, 1.0, 1.0, | ||
1.0, 1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 0.0, 1.0 | ||
]; | ||
|
||
let vertex_buffer = ctx.new_buffer( | ||
BufferType::VertexBuffer, | ||
BufferUsage::Immutable, | ||
BufferSource::slice(&vertices), | ||
); | ||
|
||
#[rustfmt::skip] | ||
let indices: &[u16] = &[ | ||
0, 1, 2, 0, 2, 3, | ||
6, 5, 4, 7, 6, 4, | ||
8, 9, 10, 8, 10, 11, | ||
14, 13, 12, 15, 14, 12, | ||
16, 17, 18, 16, 18, 19, | ||
22, 21, 20, 23, 22, 20 | ||
]; | ||
|
||
let index_buffer = ctx.new_buffer( | ||
BufferType::IndexBuffer, | ||
BufferUsage::Immutable, | ||
BufferSource::slice(&indices), | ||
); | ||
|
||
let display_bind = Bindings { | ||
vertex_buffers: vec![vertex_buffer], | ||
index_buffer: index_buffer, | ||
images: vec![color_img], | ||
}; | ||
|
||
let default_shader = ctx | ||
.new_shader( | ||
ShaderSource { | ||
glsl_vertex: Some(display_shader::VERTEX), | ||
glsl_fragment: Some(display_shader::FRAGMENT), | ||
metal_shader: Some(display_shader::METAL), | ||
}, | ||
display_shader::meta(), | ||
) | ||
.unwrap(); | ||
|
||
let display_pipeline = ctx.new_pipeline_with_params( | ||
&[BufferLayout::default()], | ||
&[ | ||
VertexAttribute::new("in_pos", VertexFormat::Float3), | ||
VertexAttribute::new("in_color", VertexFormat::Float4), | ||
VertexAttribute::new("in_uv", VertexFormat::Float2), | ||
], | ||
default_shader, | ||
PipelineParams { | ||
depth_test: Comparison::LessOrEqual, | ||
depth_write: false, | ||
..Default::default() | ||
}, | ||
); | ||
|
||
Stage { | ||
display_pipeline, | ||
display_bind, | ||
rx: 0., | ||
ry: 0., | ||
ctx, | ||
} | ||
} | ||
} | ||
|
||
impl EventHandler for Stage { | ||
fn update(&mut self) {} | ||
|
||
fn draw(&mut self) { | ||
let (width, height) = window::screen_size(); | ||
let proj = Mat4::perspective_rh_gl(60.0f32.to_radians(), width / height, 0.01, 10.0); | ||
let view = Mat4::look_at_rh( | ||
vec3(10.0 * self.rx.sin(), 0.5, 10.0 * self.rx.cos()), | ||
vec3(0.0, 0.0, 0.0), | ||
vec3(0.0, 1.0, 0.0), | ||
); | ||
let view_proj = proj * Mat4::from_mat3(Mat3::from_mat4(view)); | ||
|
||
self.rx += 0.01; | ||
self.ry += 0.03; | ||
// let model = Mat4::from_rotation_ypr(self.rx, self.ry, 0.); | ||
|
||
let vs_params = display_shader::Uniforms { mvp: view_proj }; | ||
|
||
self.ctx | ||
.begin_default_pass(PassAction::clear_color(0.0, 0., 0.45, 1.)); | ||
self.ctx.apply_pipeline(&self.display_pipeline); | ||
self.ctx.apply_bindings(&self.display_bind); | ||
self.ctx.apply_uniforms(UniformsSource::table(&vs_params)); | ||
self.ctx.draw(0, 36, 1); | ||
self.ctx.end_render_pass(); | ||
|
||
self.ctx.commit_frame(); | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut conf = conf::Conf::default(); | ||
let metal = std::env::args().nth(1).as_deref() == Some("metal"); | ||
conf.platform.apple_gfx_api = if metal { | ||
conf::AppleGfxApi::Metal | ||
} else { | ||
conf::AppleGfxApi::OpenGl | ||
}; | ||
|
||
miniquad::start(conf, move || Box::new(Stage::new())); | ||
} | ||
|
||
mod display_shader { | ||
use miniquad::*; | ||
|
||
pub const VERTEX: &str = r#"#version 100 | ||
attribute vec3 in_pos; | ||
attribute vec4 in_color; | ||
attribute vec2 in_uv; | ||
varying lowp vec4 color; | ||
varying lowp vec3 uv; | ||
uniform mat4 mvp; | ||
void main() { | ||
vec4 pos = mvp * vec4(in_pos, 1.0); | ||
gl_Position = pos.xyww; | ||
color = in_color; | ||
uv = in_pos.xyz; | ||
} | ||
"#; | ||
|
||
pub const FRAGMENT: &str = r#"#version 100 | ||
varying lowp vec4 color; | ||
varying lowp vec3 uv; | ||
uniform samplerCube tex; | ||
void main() { | ||
gl_FragColor = textureCube(tex, uv); | ||
} | ||
"#; | ||
|
||
pub const METAL: &str = r#"#include <metal_stdlib> | ||
using namespace metal; | ||
struct Uniforms | ||
{ | ||
float4x4 mvp; | ||
}; | ||
struct Vertex | ||
{ | ||
float3 in_pos [[attribute(0)]]; | ||
float4 in_color [[attribute(1)]]; | ||
float2 in_uv [[attribute(2)]]; | ||
}; | ||
struct RasterizerData | ||
{ | ||
float4 position [[position]]; | ||
float4 color [[user(locn0)]]; | ||
float3 uv [[user(locn1)]]; | ||
}; | ||
vertex RasterizerData vertexShader(Vertex v [[stage_in]], constant Uniforms& uniforms [[buffer(0)]]) | ||
{ | ||
RasterizerData out; | ||
out.position = uniforms.mvp * float4(v.in_pos, 1.0); | ||
out.color = v.in_color; | ||
out.uv = v.in_pos; | ||
return out; | ||
} | ||
fragment float4 fragmentShader( | ||
RasterizerData in [[stage_in]], | ||
texturecube<float> tex [[texture(0)]], | ||
sampler texSmplr [[sampler(0)]]) | ||
{ | ||
return tex.sample(texSmplr, in.uv); | ||
}"#; | ||
|
||
pub fn meta() -> ShaderMeta { | ||
ShaderMeta { | ||
images: vec!["tex".to_string()], | ||
uniforms: UniformBlockLayout { | ||
uniforms: vec![UniformDesc::new("mvp", UniformType::Mat4)], | ||
}, | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
pub struct Uniforms { | ||
pub mvp: glam::Mat4, | ||
} | ||
} |
Oops, something went wrong.