Draw a settings button, not functional yet
This commit is contained in:
parent
a81850a899
commit
0b662b0660
Binary file not shown.
After Width: | Height: | Size: 1.0 KiB |
|
@ -12,13 +12,13 @@
|
||||||
|
|
||||||
precision highp float;
|
precision highp float;
|
||||||
|
|
||||||
uniform sampler2D uFontTexture;
|
uniform sampler2D uTexture;
|
||||||
|
|
||||||
in vec2 vTexCoord;
|
in vec2 vTexCoord;
|
||||||
|
|
||||||
out vec4 oFragColor;
|
out vec4 oFragColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
float coverage = texture(uFontTexture, vTexCoord).r;
|
float alpha = texture(uTexture, vTexCoord).r;
|
||||||
oFragColor = vec4(coverage);
|
oFragColor = vec4(alpha);
|
||||||
}
|
}
|
|
@ -13,7 +13,7 @@
|
||||||
precision highp float;
|
precision highp float;
|
||||||
|
|
||||||
uniform vec2 uFramebufferSize;
|
uniform vec2 uFramebufferSize;
|
||||||
uniform vec2 uFontTextureSize;
|
uniform vec2 uTextureSize;
|
||||||
|
|
||||||
in vec2 aPosition;
|
in vec2 aPosition;
|
||||||
in vec2 aTexCoord;
|
in vec2 aTexCoord;
|
||||||
|
@ -21,7 +21,7 @@ in vec2 aTexCoord;
|
||||||
out vec2 vTexCoord;
|
out vec2 vTexCoord;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vTexCoord = aTexCoord / uFontTextureSize;
|
vTexCoord = aTexCoord / uTextureSize;
|
||||||
vec2 position = aPosition / uFramebufferSize * 2.0 - 1.0;
|
vec2 position = aPosition / uFramebufferSize * 2.0 - 1.0;
|
||||||
gl_Position = vec4(position.x, -position.y, 0.0, 1.0);
|
gl_Position = vec4(position.x, -position.y, 0.0, 1.0);
|
||||||
}
|
}
|
|
@ -43,6 +43,8 @@ const MAIN_FRAMEBUFFER_HEIGHT: u32 = 800;
|
||||||
const MOUSELOOK_ROTATION_SPEED: f32 = 0.007;
|
const MOUSELOOK_ROTATION_SPEED: f32 = 0.007;
|
||||||
const CAMERA_VELOCITY: f32 = 25.0;
|
const CAMERA_VELOCITY: f32 = 25.0;
|
||||||
|
|
||||||
|
const BACKGROUND_COLOR: f32 = 0.22;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let options = Options::get();
|
let options = Options::get();
|
||||||
|
|
||||||
|
@ -117,7 +119,7 @@ fn main() {
|
||||||
tile_time
|
tile_time
|
||||||
}) = scene_thread_proxy.receiver.recv() {
|
}) = scene_thread_proxy.receiver.recv() {
|
||||||
unsafe {
|
unsafe {
|
||||||
gl::ClearColor(0.7, 0.7, 0.7, 1.0);
|
gl::ClearColor(BACKGROUND_COLOR, BACKGROUND_COLOR, BACKGROUND_COLOR, 1.0);
|
||||||
gl::Clear(gl::COLOR_BUFFER_BIT);
|
gl::Clear(gl::COLOR_BUFFER_BIT);
|
||||||
renderer.render_scene(&built_scene);
|
renderer.render_scene(&built_scene);
|
||||||
|
|
||||||
|
|
149
gl/src/debug.rs
149
gl/src/debug.rs
|
@ -29,19 +29,24 @@ use std::io::BufReader;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
const DEBUG_FONT_VERTEX_SIZE: GLint = 8;
|
const DEBUG_TEXTURE_VERTEX_SIZE: GLint = 8;
|
||||||
const DEBUG_SOLID_VERTEX_SIZE: GLint = 4;
|
const DEBUG_SOLID_VERTEX_SIZE: GLint = 4;
|
||||||
|
|
||||||
const WINDOW_WIDTH: i32 = 400;
|
const WINDOW_WIDTH: i32 = 300;
|
||||||
const WINDOW_HEIGHT: i32 = LINE_HEIGHT * 2 + PADDING + 2;
|
const WINDOW_HEIGHT: i32 = LINE_HEIGHT * 2 + PADDING + 2;
|
||||||
const PADDING: i32 = 12;
|
const PADDING: i32 = 12;
|
||||||
const FONT_ASCENT: i32 = 28;
|
const FONT_ASCENT: i32 = 28;
|
||||||
const LINE_HEIGHT: i32 = 42;
|
const LINE_HEIGHT: i32 = 42;
|
||||||
|
const ICON_SIZE: i32 = 48;
|
||||||
|
const BUTTON_WIDTH: i32 = PADDING * 2 + ICON_SIZE;
|
||||||
|
const BUTTON_HEIGHT: i32 = PADDING * 2 + ICON_SIZE;
|
||||||
|
|
||||||
static WINDOW_COLOR: ColorU = ColorU { r: 30, g: 30, b: 30, a: 255 - 30 };
|
static WINDOW_COLOR: ColorU = ColorU { r: 30, g: 30, b: 30, a: 255 - 30 };
|
||||||
|
|
||||||
static JSON_PATH: &'static str = "resources/debug-font.json";
|
static JSON_PATH: &'static str = "resources/debug-font.json";
|
||||||
static PNG_NAME: &'static str = "debug-font";
|
|
||||||
|
static FONT_PNG_NAME: &'static str = "debug-font";
|
||||||
|
static SETTINGS_PNG_NAME: &'static str = "debug-settings";
|
||||||
|
|
||||||
static QUAD_INDICES: [u32; 6] = [0, 1, 3, 1, 2, 3];
|
static QUAD_INDICES: [u32; 6] = [0, 1, 3, 1, 2, 3];
|
||||||
|
|
||||||
|
@ -78,19 +83,19 @@ impl DebugFont {
|
||||||
|
|
||||||
pub struct DebugRenderer {
|
pub struct DebugRenderer {
|
||||||
framebuffer_size: Size2D<u32>,
|
framebuffer_size: Size2D<u32>,
|
||||||
font_program: DebugFontProgram,
|
texture_program: DebugTextureProgram,
|
||||||
font_vertex_array: DebugFontVertexArray,
|
texture_vertex_array: DebugTextureVertexArray,
|
||||||
font_texture: Texture,
|
|
||||||
font: DebugFont,
|
font: DebugFont,
|
||||||
solid_program: DebugSolidProgram,
|
solid_program: DebugSolidProgram,
|
||||||
solid_vertex_array: DebugSolidVertexArray,
|
solid_vertex_array: DebugSolidVertexArray,
|
||||||
|
font_texture: Texture,
|
||||||
|
settings_texture: Texture,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DebugRenderer {
|
impl DebugRenderer {
|
||||||
pub fn new(framebuffer_size: &Size2D<u32>) -> DebugRenderer {
|
pub fn new(framebuffer_size: &Size2D<u32>) -> DebugRenderer {
|
||||||
let font_program = DebugFontProgram::new();
|
let texture_program = DebugTextureProgram::new();
|
||||||
let font_vertex_array = DebugFontVertexArray::new(&font_program);
|
let texture_vertex_array = DebugTextureVertexArray::new(&texture_program);
|
||||||
let font_texture = Texture::from_png(PNG_NAME);
|
|
||||||
let font = DebugFont::load();
|
let font = DebugFont::load();
|
||||||
|
|
||||||
let solid_program = DebugSolidProgram::new();
|
let solid_program = DebugSolidProgram::new();
|
||||||
|
@ -99,14 +104,18 @@ impl DebugRenderer {
|
||||||
BufferTarget::Index,
|
BufferTarget::Index,
|
||||||
BufferUploadMode::Static);
|
BufferUploadMode::Static);
|
||||||
|
|
||||||
|
let font_texture = Texture::from_png(FONT_PNG_NAME);
|
||||||
|
let settings_texture = Texture::from_png(SETTINGS_PNG_NAME);
|
||||||
|
|
||||||
DebugRenderer {
|
DebugRenderer {
|
||||||
framebuffer_size: *framebuffer_size,
|
framebuffer_size: *framebuffer_size,
|
||||||
font_program,
|
texture_program,
|
||||||
font_vertex_array,
|
texture_vertex_array,
|
||||||
font_texture,
|
|
||||||
font,
|
font,
|
||||||
solid_program,
|
solid_program,
|
||||||
solid_vertex_array,
|
solid_vertex_array,
|
||||||
|
font_texture,
|
||||||
|
settings_texture,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,20 +124,29 @@ impl DebugRenderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&self, tile_time: Duration, rendering_time: Option<Duration>) {
|
pub fn draw(&self, tile_time: Duration, rendering_time: Option<Duration>) {
|
||||||
|
// Draw performance window.
|
||||||
|
let bottom = self.framebuffer_size.height as i32 - PADDING;
|
||||||
let window_rect = RectI32::new(
|
let window_rect = RectI32::new(
|
||||||
Point2DI32::new(self.framebuffer_size.width as i32 - PADDING - WINDOW_WIDTH,
|
Point2DI32::new(self.framebuffer_size.width as i32 - PADDING - WINDOW_WIDTH,
|
||||||
self.framebuffer_size.height as i32 - PADDING - WINDOW_HEIGHT),
|
bottom - WINDOW_HEIGHT),
|
||||||
Point2DI32::new(WINDOW_WIDTH, WINDOW_HEIGHT));
|
Point2DI32::new(WINDOW_WIDTH, WINDOW_HEIGHT));
|
||||||
self.draw_solid_rect(window_rect, WINDOW_COLOR);
|
self.draw_solid_rect(window_rect, WINDOW_COLOR);
|
||||||
self.draw_text(&format!("Tiling: {:.3} ms", duration_ms(tile_time)),
|
self.draw_text(&format!("CPU: {:.3} ms", duration_ms(tile_time)),
|
||||||
Point2DI32::new(window_rect.min_x() + PADDING,
|
Point2DI32::new(window_rect.min_x() + PADDING,
|
||||||
window_rect.min_y() + PADDING + FONT_ASCENT));
|
window_rect.min_y() + PADDING + FONT_ASCENT));
|
||||||
if let Some(rendering_time) = rendering_time {
|
if let Some(rendering_time) = rendering_time {
|
||||||
self.draw_text(&format!("Rendering: {:.3} ms", duration_ms(rendering_time)),
|
self.draw_text(&format!("GPU: {:.3} ms", duration_ms(rendering_time)),
|
||||||
Point2DI32::new(
|
Point2DI32::new(
|
||||||
window_rect.min_x() + PADDING,
|
window_rect.min_x() + PADDING,
|
||||||
window_rect.min_y() + PADDING + FONT_ASCENT + LINE_HEIGHT));
|
window_rect.min_y() + PADDING + FONT_ASCENT + LINE_HEIGHT));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw settings button.
|
||||||
|
self.draw_solid_rect(RectI32::new(Point2DI32::new(PADDING, bottom - BUTTON_HEIGHT),
|
||||||
|
Point2DI32::new(BUTTON_WIDTH, BUTTON_HEIGHT)),
|
||||||
|
WINDOW_COLOR);
|
||||||
|
self.draw_texture(Point2DI32::new(PADDING + PADDING, bottom - BUTTON_HEIGHT + PADDING),
|
||||||
|
&self.settings_texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_solid_rect(&self, rect: RectI32, color: ColorU) {
|
fn draw_solid_rect(&self, rect: RectI32, color: ColorU) {
|
||||||
|
@ -179,10 +197,10 @@ impl DebugRenderer {
|
||||||
Point2DI32::new(info.width, info.height));
|
Point2DI32::new(info.width, info.height));
|
||||||
let first_vertex_index = vertex_data.len();
|
let first_vertex_index = vertex_data.len();
|
||||||
vertex_data.extend_from_slice(&[
|
vertex_data.extend_from_slice(&[
|
||||||
DebugFontVertex::new(position_rect.origin(), tex_coord_rect.origin()),
|
DebugTextureVertex::new(position_rect.origin(), tex_coord_rect.origin()),
|
||||||
DebugFontVertex::new(position_rect.upper_right(), tex_coord_rect.upper_right()),
|
DebugTextureVertex::new(position_rect.upper_right(), tex_coord_rect.upper_right()),
|
||||||
DebugFontVertex::new(position_rect.lower_right(), tex_coord_rect.lower_right()),
|
DebugTextureVertex::new(position_rect.lower_right(), tex_coord_rect.lower_right()),
|
||||||
DebugFontVertex::new(position_rect.lower_left(), tex_coord_rect.lower_left()),
|
DebugTextureVertex::new(position_rect.lower_left(), tex_coord_rect.lower_left()),
|
||||||
]);
|
]);
|
||||||
index_data.extend(QUAD_INDICES.iter().map(|&i| i + first_vertex_index as u32));
|
index_data.extend(QUAD_INDICES.iter().map(|&i| i + first_vertex_index as u32));
|
||||||
|
|
||||||
|
@ -190,24 +208,45 @@ impl DebugRenderer {
|
||||||
next.set_x(next_x);
|
next.set_x(next_x);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.font_vertex_array
|
self.draw_texture_with_vertex_data(&vertex_data, &index_data, &self.font_texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_texture(&self, origin: Point2DI32, texture: &Texture) {
|
||||||
|
let size = Point2DI32::new(texture.size.width as i32, texture.size.height as i32);
|
||||||
|
let position_rect = RectI32::new(origin, size);
|
||||||
|
let tex_coord_rect = RectI32::new(Point2DI32::default(), size);
|
||||||
|
let vertex_data = [
|
||||||
|
DebugTextureVertex::new(position_rect.origin(), tex_coord_rect.origin()),
|
||||||
|
DebugTextureVertex::new(position_rect.upper_right(), tex_coord_rect.upper_right()),
|
||||||
|
DebugTextureVertex::new(position_rect.lower_right(), tex_coord_rect.lower_right()),
|
||||||
|
DebugTextureVertex::new(position_rect.lower_left(), tex_coord_rect.lower_left()),
|
||||||
|
];
|
||||||
|
|
||||||
|
self.draw_texture_with_vertex_data(&vertex_data, &QUAD_INDICES, texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_texture_with_vertex_data(&self,
|
||||||
|
vertex_data: &[DebugTextureVertex],
|
||||||
|
index_data: &[u32],
|
||||||
|
texture: &Texture) {
|
||||||
|
self.texture_vertex_array
|
||||||
.vertex_buffer
|
.vertex_buffer
|
||||||
.upload(&vertex_data, BufferTarget::Vertex, BufferUploadMode::Dynamic);
|
.upload(&vertex_data, BufferTarget::Vertex, BufferUploadMode::Dynamic);
|
||||||
self.font_vertex_array
|
self.texture_vertex_array
|
||||||
.index_buffer
|
.index_buffer
|
||||||
.upload(&index_data, BufferTarget::Index, BufferUploadMode::Dynamic);
|
.upload(&index_data, BufferTarget::Index, BufferUploadMode::Dynamic);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
gl::BindVertexArray(self.font_vertex_array.gl_vertex_array);
|
gl::BindVertexArray(self.texture_vertex_array.gl_vertex_array);
|
||||||
gl::UseProgram(self.font_program.program.gl_program);
|
gl::UseProgram(self.texture_program.program.gl_program);
|
||||||
gl::Uniform2f(self.font_program.framebuffer_size_uniform.location,
|
gl::Uniform2f(self.texture_program.framebuffer_size_uniform.location,
|
||||||
self.framebuffer_size.width as GLfloat,
|
self.framebuffer_size.width as GLfloat,
|
||||||
self.framebuffer_size.height as GLfloat);
|
self.framebuffer_size.height as GLfloat);
|
||||||
gl::Uniform2f(self.font_program.font_texture_size_uniform.location,
|
gl::Uniform2f(self.texture_program.texture_size_uniform.location,
|
||||||
self.font_texture.size.width as GLfloat,
|
texture.size.width as GLfloat,
|
||||||
self.font_texture.size.height as GLfloat);
|
texture.size.height as GLfloat);
|
||||||
self.font_texture.bind(0);
|
texture.bind(0);
|
||||||
gl::Uniform1i(self.font_program.font_texture_uniform.location, 0);
|
gl::Uniform1i(self.texture_program.texture_uniform.location, 0);
|
||||||
gl::BlendEquation(gl::FUNC_ADD);
|
gl::BlendEquation(gl::FUNC_ADD);
|
||||||
gl::BlendFunc(gl::ONE, gl::ONE_MINUS_SRC_ALPHA);
|
gl::BlendFunc(gl::ONE, gl::ONE_MINUS_SRC_ALPHA);
|
||||||
gl::Enable(gl::BLEND);
|
gl::Enable(gl::BLEND);
|
||||||
|
@ -220,45 +259,45 @@ impl DebugRenderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DebugFontVertexArray {
|
struct DebugTextureVertexArray {
|
||||||
gl_vertex_array: GLuint,
|
gl_vertex_array: GLuint,
|
||||||
vertex_buffer: Buffer,
|
vertex_buffer: Buffer,
|
||||||
index_buffer: Buffer,
|
index_buffer: Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DebugFontVertexArray {
|
impl DebugTextureVertexArray {
|
||||||
fn new(debug_font_program: &DebugFontProgram) -> DebugFontVertexArray {
|
fn new(debug_texture_program: &DebugTextureProgram) -> DebugTextureVertexArray {
|
||||||
let vertex_buffer = Buffer::new();
|
let vertex_buffer = Buffer::new();
|
||||||
let index_buffer = Buffer::new();
|
let index_buffer = Buffer::new();
|
||||||
let mut gl_vertex_array = 0;
|
let mut gl_vertex_array = 0;
|
||||||
unsafe {
|
unsafe {
|
||||||
let position_attr = VertexAttr::new(&debug_font_program.program, "Position");
|
let position_attr = VertexAttr::new(&debug_texture_program.program, "Position");
|
||||||
let tex_coord_attr = VertexAttr::new(&debug_font_program.program, "TexCoord");
|
let tex_coord_attr = VertexAttr::new(&debug_texture_program.program, "TexCoord");
|
||||||
|
|
||||||
gl::GenVertexArrays(1, &mut gl_vertex_array);
|
gl::GenVertexArrays(1, &mut gl_vertex_array);
|
||||||
gl::BindVertexArray(gl_vertex_array);
|
gl::BindVertexArray(gl_vertex_array);
|
||||||
gl::UseProgram(debug_font_program.program.gl_program);
|
gl::UseProgram(debug_texture_program.program.gl_program);
|
||||||
gl::BindBuffer(gl::ARRAY_BUFFER, vertex_buffer.gl_buffer);
|
gl::BindBuffer(gl::ARRAY_BUFFER, vertex_buffer.gl_buffer);
|
||||||
position_attr.configure_float(2,
|
position_attr.configure_float(2,
|
||||||
gl::UNSIGNED_SHORT,
|
gl::UNSIGNED_SHORT,
|
||||||
false,
|
false,
|
||||||
DEBUG_FONT_VERTEX_SIZE,
|
DEBUG_TEXTURE_VERTEX_SIZE,
|
||||||
0,
|
0,
|
||||||
0);
|
0);
|
||||||
tex_coord_attr.configure_float(2,
|
tex_coord_attr.configure_float(2,
|
||||||
gl::UNSIGNED_SHORT,
|
gl::UNSIGNED_SHORT,
|
||||||
false,
|
false,
|
||||||
DEBUG_FONT_VERTEX_SIZE,
|
DEBUG_TEXTURE_VERTEX_SIZE,
|
||||||
4,
|
4,
|
||||||
0);
|
0);
|
||||||
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, index_buffer.gl_buffer);
|
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, index_buffer.gl_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
DebugFontVertexArray { gl_vertex_array, vertex_buffer, index_buffer }
|
DebugTextureVertexArray { gl_vertex_array, vertex_buffer, index_buffer }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for DebugFontVertexArray {
|
impl Drop for DebugTextureVertexArray {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -307,24 +346,24 @@ impl Drop for DebugSolidVertexArray {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DebugFontProgram {
|
struct DebugTextureProgram {
|
||||||
program: Program,
|
program: Program,
|
||||||
framebuffer_size_uniform: Uniform,
|
framebuffer_size_uniform: Uniform,
|
||||||
font_texture_size_uniform: Uniform,
|
texture_size_uniform: Uniform,
|
||||||
font_texture_uniform: Uniform,
|
texture_uniform: Uniform,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DebugFontProgram {
|
impl DebugTextureProgram {
|
||||||
fn new() -> DebugFontProgram {
|
fn new() -> DebugTextureProgram {
|
||||||
let program = Program::new("debug_font");
|
let program = Program::new("debug_texture");
|
||||||
let framebuffer_size_uniform = Uniform::new(&program, "FramebufferSize");
|
let framebuffer_size_uniform = Uniform::new(&program, "FramebufferSize");
|
||||||
let font_texture_size_uniform = Uniform::new(&program, "FontTextureSize");
|
let texture_size_uniform = Uniform::new(&program, "TextureSize");
|
||||||
let font_texture_uniform = Uniform::new(&program, "FontTexture");
|
let texture_uniform = Uniform::new(&program, "Texture");
|
||||||
DebugFontProgram {
|
DebugTextureProgram {
|
||||||
program,
|
program,
|
||||||
framebuffer_size_uniform,
|
framebuffer_size_uniform,
|
||||||
font_texture_size_uniform,
|
texture_size_uniform,
|
||||||
font_texture_uniform,
|
texture_uniform,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -346,16 +385,16 @@ impl DebugSolidProgram {
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
struct DebugFontVertex {
|
struct DebugTextureVertex {
|
||||||
position_x: i16,
|
position_x: i16,
|
||||||
position_y: i16,
|
position_y: i16,
|
||||||
tex_coord_x: u16,
|
tex_coord_x: u16,
|
||||||
tex_coord_y: u16,
|
tex_coord_y: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DebugFontVertex {
|
impl DebugTextureVertex {
|
||||||
fn new(position: Point2DI32, tex_coord: Point2DI32) -> DebugFontVertex {
|
fn new(position: Point2DI32, tex_coord: Point2DI32) -> DebugTextureVertex {
|
||||||
DebugFontVertex {
|
DebugTextureVertex {
|
||||||
position_x: position.x() as i16,
|
position_x: position.x() as i16,
|
||||||
position_y: position.y() as i16,
|
position_y: position.y() as i16,
|
||||||
tex_coord_x: tex_coord.x() as u16,
|
tex_coord_x: tex_coord.x() as u16,
|
||||||
|
|
Loading…
Reference in New Issue