Skip to content

Commit

Permalink
WIP mipmaps, to be rebased later
Browse files Browse the repository at this point in the history
  • Loading branch information
not-fl3 committed Aug 26, 2023
1 parent f9a03ab commit d2fe1f0
Show file tree
Hide file tree
Showing 15 changed files with 559 additions and 100 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = """
Cross-platform window context and rendering library.
"""
readme="README.md"
exclude = ["examples/"]
keywords = ["graphics", "3D", "opengl", "gamedev", "windowing"]
categories = ["rendering::graphics-api"]

Expand All @@ -33,6 +34,6 @@ ndk-sys = "0.2"
objc = "0.2"

[dev-dependencies]
glam = {version = "0.14", features = ["scalar-math"] }
glam = {version = "0.24", features = ["scalar-math"] }
quad-rand = "0.1"

nanoimage = "0.1"
Binary file added examples/assets/skybox/skybox_nx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/assets/skybox/skybox_ny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/assets/skybox/skybox_nz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/assets/skybox/skybox_px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/assets/skybox/skybox_py.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/assets/skybox/skybox_pz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
266 changes: 266 additions & 0 deletions examples/cubemap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
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 vec4 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 * in_pos;
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) + vec4(0.1, 0, 0, 0);
}
"#;

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)]];
float2 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_uv;
return out;
}
fragment float4 fragmentShader(RasterizerData in [[stage_in]], texture2d<float> tex [[texture(0)]], sampler texSmplr [[sampler(0)]])
{
return in.color * 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,
}
}
Loading

0 comments on commit d2fe1f0

Please sign in to comment.