Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
[package]
name = "game_engine"
name = "render_engine"
version = "0.1.0-alpha.2"
edition = "2021"
description = "A Metal-based 3D rendering engine for macOS"
license = "MIT"
repository = "https://github.com/gituser12981u2/3d-engine.git"
documentation = "https://docs.rs/render_engine"
readme = "README.md"
keywords = ["graphics", "rendering", "metal", "3d"]
categories = ["graphics", "rendering", "game-engines"]
build = "build.rs"

[lib]
path = "src/lib.rs"
crate-type = ["rlib"]

[dependencies]
cocoa = "0.26.0"
core-graphics = "0.23.2"
Expand Down
39 changes: 6 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,15 @@ This project is not a typical game engine, but an ambitious attempt to create a

## Installation

1. Clone the repository:
Add this to the Cargo.toml:

```bash
git clone https://github.com/gituser12981u2/3d-engine.git
cd 3D-engine
```

2. Build the project:

```bash
cargo build
```

## Usage

Here is a basic example of how to use the engine:

```rust
use 3D_engine::{Renderer, Color, RenderVector3};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut renderer = Renderer::new(800, 600, "My 3D Scene")?;
```bash
cargo add render_engine
```

renderer.set_render_callback(|r| {
r.draw_triangle(
RenderVector3::new(-0.5, -0.5, 0.0),
RenderVector3::new(0.5, -0.5, 0.0),
RenderVector3::new(0.0, 0.5, 0.0),
Color::new(1.0, 0.0, 0.0, 1.0)
)?;
Ok(())
});
## API Documentation

renderer.run()
}
```
Full documentation is available at [docs.rs/render_engine](https://docs.rs/render_engine)

## Contributing

Expand Down
47 changes: 47 additions & 0 deletions examples/multiple_shapes_scene.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use glam::{Mat4, Quat, Vec3};
use render_engine::{Color, RendererSystem, ShapeBuilder};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut renderer_system = RendererSystem::new(800, 600, "Metal Renderer")?;

renderer_system.set_render_callback(move |r| {
// 1. Create a triangle
r.create_triangle(
Vec3::new(-1.5, 0.5, 0.0),
Vec3::new(-2.0, -0.5, 0.0),
Vec3::new(-1.0, -0.5, 0.0),
Color::new(1.0, 0.0, 0.0, 1.0),
)
.as_primitive()
.with_transform(Mat4::from_translation(Vec3::new(0.0, 0.0, 0.0)))
.draw(r);

// 2. Custom shape using the create_shape method
let vertices = vec![
// Create a square (position, color)
(Vec3::new(0.5, 0.5, 0.0), Color::new(0.0, 1.0, 0.0, 1.0)),
(Vec3::new(1.5, 0.5, 0.0), Color::new(0.0, 1.0, 0.0, 1.0)),
(Vec3::new(1.5, -0.5, 0.0), Color::new(0.0, 1.0, 0.0, 1.0)),
(Vec3::new(0.5, -0.5, 0.0), Color::new(0.0, 1.0, 0.0, 1.0)),
];

let indices = vec![
0, 1, 2, // First triangle
0, 2, 3, // Second triangle
];

r.create_shape(vertices)
.as_mesh()
.with_indices(indices)
.with_transform(Mat4::from_rotation_translation(
Quat::from_rotation_y(0.5),
Vec3::new(0.0, 0.0, 0.0),
))
.draw(r);

r.render()
});

renderer_system.run()?;
Ok(())
}
22 changes: 1 addition & 21 deletions src/main.rs → examples/simple_scene.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
use env_logger::Builder;
use glam::{Mat4, Quat, Vec3};
use log::LevelFilter;
use renderer::{shape_builders::shape_builder::ShapeBuilder, Color, RendererSystem};
use render_engine::{Color, RendererSystem, ShapeBuilder};
use std::time::Instant;

mod physics;
mod renderer;

#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug_trace {
($($arg:tt)*) => ( log::trace!($($arg)*) );
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debug_trace {
($($arg:tt)*) => {};
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
Builder::new().filter_level(LevelFilter::Debug).init();

let mut renderer_system = RendererSystem::new(800, 600, "Metal Renderer")?;
let start_time = Instant::now();

// Create the infinite ground
// let ground_size = 1000.0;
// let ground_divisions = 100;
// let ground_vertices = create_infinite_ground(ground_size, ground_divisions);

renderer_system.set_render_callback(move |r| {
let elapsed = start_time.elapsed().as_secs_f32();

Expand Down
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! A Metal-based 3D rendering library with a focus on performance
//!
//! This library provides a comprehensive rendering system including camera controls,
//! primitive shape construction, mesh management, and an efficient rendering pipeline.

#[cfg(debug_assertions)]
#[macro_export]
macro_rules! debug_trace {
($($arg:tt)*) => ( log::trace!($($arg)*) );
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! debug_trace {
($($arg:tt)*) => {};
}

// Core components
pub use crate::renderer::{
// Basic types
Camera,
Color,
// Error handling
RendererError,
// Core renderer system
RendererSystem,
};

// Shape builders
pub use crate::renderer::shape_builders::{
shape_builder::{vec3_color_to_vertex, MeshBuilder, PrimitiveBuilder, ShapeBuilder, ShapeData},
triangle_builder::TriangleBuilder,
};

pub mod renderer;
68 changes: 0 additions & 68 deletions src/renderer/shape_builders/shape_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,12 @@ impl ShapeData {
}

/// Adds indices to the primitive.
///
/// # Example
///
/// ```
/// .with_indices(vec![0, 1, 2])
/// ```
///
fn with_indices(mut self, indices: Vec<u32>) -> Self {
self.indices = Some(indices);
self
}

/// Applies a transformation to the primitive.
///
/// # Example
///
/// ```
/// .with_transform(Mat4::from_translation(Vec3::new(1.5, 0.0, 0.0)))
/// ```
fn with_transform(mut self, transform: Mat4) -> Self {
self.transform = transform;
self
Expand All @@ -86,21 +73,6 @@ impl ShapeBuilder for ShapeData {
}

/// Builder for creating and customizing primitive shapes.
///
/// # Example
///
/// ```
/// renderer.create_triangle(
/// Vec3::new(0.0, 0.5, 0.0),
/// Vec3::new(-0.5, -0.5, 0.0),
/// Vec3::new(0.5, -0.5, 0.0),
/// Color::new(1.0, 0.0, 0.0, 1.0)
/// )
/// .as_primitive()
/// .with_indices(vec![0, 1, 2])
/// .with_transform(Mat4::from_translation(Vec3::new(1.5, 0.0, 0.0)))
/// .draw(renderer);
/// ```
pub struct PrimitiveBuilder {
data: ShapeData,
}
Expand All @@ -114,26 +86,13 @@ impl PrimitiveBuilder {
}

/// Adds indices to the primitive.
///
/// # Example
///
/// ```
/// .with_indices(vec![0, 1, 2])
/// ```
///
#[allow(dead_code)]
pub fn with_indices(mut self, indices: Vec<u32>) -> Self {
self.data = self.data.with_indices(indices);
self
}

/// Applies a transformation to the primitive.
///
/// # Example
///
/// ```
/// .with_transform(Mat4::from_translation(Vec3::new(1.5, 0.0, 0.0)))
/// ```
#[allow(dead_code)]
pub fn with_transform(mut self, transform: Mat4) -> Self {
self.data = self.data.with_transform(transform);
Expand Down Expand Up @@ -166,21 +125,6 @@ impl PrimitiveBuilder {
}

/// Builder for creating and customizing mesh shapes.
///
/// # Example
///
/// ```
/// renderer.create_triangle(
/// Vec3::new(0.0, 0.5, 0.0),
/// Vec3::new(-0.5, -0.5, 0.0),
/// Vec3::new(0.5, -0.5, 0.0),
/// Color::new(1.0, 0.0, 0.0, 1.0)
/// )
/// .as_mesh()
/// .with_indices(vec![0, 1, 2])
/// .with_transform(Mat4::from_translation(Vec3::new(1.5, 0.0, 0.0)))
/// .draw(renderer);
/// ```
#[derive(Clone)]
pub struct MeshBuilder {
pub data: ShapeData,
Expand All @@ -195,25 +139,13 @@ impl MeshBuilder {
}

/// Adds indices to the primitive.
///
/// # Example
///
/// ```
/// .with_indices(vec![0, 1, 2])
/// ```
#[allow(dead_code)]
pub fn with_indices(mut self, indices: Vec<u32>) -> Self {
self.data = self.data.with_indices(indices);
self
}

/// Applies a transformation to the primitive.
///
/// # Example
///
/// ```
/// .with_transform(Mat4::from_translation(Vec3::new(1.5, 0.0, 0.0)))
/// ```
#[allow(dead_code)]
pub fn with_transform(mut self, transform: Mat4) -> Self {
self.data = self.data.with_transform(transform);
Expand Down
12 changes: 0 additions & 12 deletions src/renderer/shape_builders/triangle_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ use crate::renderer::{
use glam::Vec3;

/// Builder for creating a simple triangle primitive
///
/// # Example
///
/// ```
/// let triangle = renderer.create_triangle(
/// Vec3::new(0.0, 0.5, 0.0), // Top vertex
/// Vec3::new(-0.5, -0.5, 0.0), // Bottom-left vertex
/// Vec3::new(0.5, -0.5, 0.0), // Bottom-right vertex
/// Color::new(1.0, 0.0, 0.0, 1.0) // Red color
/// );
/// ```
pub struct TriangleBuilder {
vertices: [Vertex; 3],
}
Expand Down Expand Up @@ -72,6 +61,5 @@ mod tests {
assert_eq!(triangle.vertices[0].position, [0.0, 0.5, 0.0]);
assert_eq!(triangle.vertices[1].position, [-0.5, -0.5, 0.0]);
assert_eq!(triangle.vertices[2].position, [0.5, -0.5, 0.0]);
// assert_eq!(triangle.vertices[0].color, color.as_array());
}
}
Loading