summaryrefslogtreecommitdiff
path: root/engine/src/object.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/object.rs')
-rw-r--r--engine/src/object.rs43
1 files changed, 37 insertions, 6 deletions
diff --git a/engine/src/object.rs b/engine/src/object.rs
index f2591f2..a8cd076 100644
--- a/engine/src/object.rs
+++ b/engine/src/object.rs
@@ -5,16 +5,21 @@ use crate::opengl::shader::{
Shader,
};
use crate::renderer::Renderable;
+use crate::texture::Texture;
use crate::transform::Transform;
use crate::vector::Vec3;
use crate::vertex::Vertex;
+const FRAG_SHADER_COLOR: &str = include_str!("../fragment-color.glsl");
+const FRAG_SHADER_TEXTURE: &str = include_str!("../fragment-texture.glsl");
+
#[derive(Debug)]
pub struct Object
{
id: Id,
renderable: Renderable,
transform: Transform,
+ texture: Option<Texture>,
}
impl Object
@@ -42,6 +47,12 @@ impl Object
self.transform.set_scaling(scaling);
}
+ #[must_use]
+ pub fn texture(&self) -> Option<&Texture>
+ {
+ self.texture.as_ref()
+ }
+
pub(crate) fn renderable(&self) -> &Renderable
{
&self.renderable
@@ -54,11 +65,12 @@ impl Object
}
/// Object builder.
-#[derive(Debug, Clone, Default)]
+#[derive(Debug, Default)]
pub struct Builder
{
vertices: Vec<Vertex>,
indices: Option<Vec<u32>>,
+ texture: Option<Texture>,
}
impl Builder
@@ -86,21 +98,35 @@ impl Builder
self
}
+ #[must_use]
+ pub fn texture(mut self, texture: Texture) -> Self
+ {
+ self.texture = Some(texture);
+
+ self
+ }
+
/// Builds a new [`Object`].
///
/// # Errors
/// Will return `Err` if:
/// - Creating shaders fails
/// - Linking the shader program fails
- pub fn build(&self, id: Id) -> Result<Object, Error>
+ pub fn build(self, id: Id) -> Result<Object, Error>
{
let vertex_shader =
Self::create_shader(ShaderKind::Vertex, include_str!("../vertex.glsl"))
.map_err(Error::CreateVertexShaderFailed)?;
- let fragment_shader =
- Self::create_shader(ShaderKind::Fragment, include_str!("../fragment.glsl"))
- .map_err(Error::CreateFragmentShaderFailed)?;
+ let fragment_shader = Self::create_shader(
+ ShaderKind::Fragment,
+ if self.texture.is_some() {
+ FRAG_SHADER_TEXTURE
+ } else {
+ FRAG_SHADER_COLOR
+ },
+ )
+ .map_err(Error::CreateFragmentShaderFailed)?;
let shader_program = ShaderProgram::new();
@@ -116,7 +142,12 @@ impl Builder
let transform = Transform::new();
- Ok(Object { id, renderable, transform })
+ Ok(Object {
+ id,
+ renderable,
+ transform,
+ texture: self.texture,
+ })
}
fn create_shader(kind: ShaderKind, source: &str) -> Result<Shader, ShaderError>