GithubHelp home page GithubHelp logo

oaguinagalde / tinyrenderer Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 16.82 MB

exploration of software rendering, audio, windowing and other stuff written in zig for `win32` and `wasm`

Zig 95.09% CMake 0.01% Makefile 0.02% C 0.51% C++ 2.64% Lua 1.63% Batchfile 0.01% Shell 0.01% Objective-C 0.01% Objective-C++ 0.08% GLSL 0.01% Kotlin 0.01% HTML 0.01% GDB 0.01% AMPL 0.01% JavaScript 0.01%

tinyrenderer's Introduction

Software Renderer

This is a software renderer which tries to emulate the "mental model" of opengl, although the API is completely different. Originally a fork of ssloy/tinyrenderer, a fantastic and brief computer graphics/rendering course. I followed the course until I added a win32 platform layer, deviated from the original path and slowly became something else. Now its a software renderer written in zig with no dependencies!

Quickstart

> zig version
0.12.0-dev.1261+bb0419599
> zig build -Dwin32 run

You should now see a 3D world with different things being rendered on screen where you can move around.

The Source

If you are curious, the way the application works is:

  1. You define a rendering pipeline with comptine known information. Here is a rendering pipeline which renders a 3D object using gouraud shading.
const gouraud_renderer = struct {
    
    const Context = struct {
        texture: Buffer2D(RGB),
        texture_width: usize,
        texture_height: usize,
        view_model_matrix: M44,
        projection_matrix: M44,
        light_position_camera_space: Vector3f,
    };
    
    const Invariant = struct {
        texture_uv: Vector2f,
        light_intensity: f32,
    };
    
    const Vertex = struct {
        pos: Vector3f,
        uv: Vector2f,
        normal: Vector3f,
    };
    
    const pipeline_configuration = GraphicsPipelineConfiguration {
        .blend_with_background = false,
        .use_index_buffer = false,
        .do_triangle_clipping = false,
        .do_depth_testing = true,
        .do_perspective_correct_interpolation = true,
        .do_scissoring = false,
        .use_triangle_2 = use_triangle_2,
    };

    const Pipeline = GraphicsPipeline(
        win32.RGBA, Context, Invariant, Vertex, pipeline_configuration,
        struct {
            fn vertex_shader(context: Context, vertex: Vertex, out_invariant: *Invariant) Vector4f {
                const position_camera_space = context.view_model_matrix.apply_to_vec3(vertex.pos);
                const light_direction = context.light_position_camera_space.substract(position_camera_space.discard_w()).normalized();
                out_invariant.light_intensity = std.math.clamp(vertex.normal.normalized().dot(light_direction), 0, 1);
                out_invariant.texture_uv = vertex.uv;
                return context.projection_matrix.apply_to_vec4(position_camera_space);
            }
        }.vertex_shader,
        struct {
            fn fragment_shader(context: Context, invariants: Invariant) win32.RGBA {
                const sample =
                    if (bilinear) texture_bilinear_sample(RGB, true, context.texture.data, context.texture_width, context.texture_height, invariants.texture_uv)
                    else texture_point_sample(RGB, true, context.texture.data, context.texture_width, context.texture_height, invariants.texture_uv);
                const rgba = sample.scale(invariants.light_intensity);
                return win32.rgba(rgba.r, rgba.g, rgba.b, 255);
            }
        }.fragment_shader,
    );

};
  1. Then you execute the pipeline with some input.
const render_requirements: gouraud_renderer.pipeline_configuration.Requirements() = .{
    .depth_buffer = state.depth_buffer,
    .viewport_matrix = state.viewport_matrix,
};
gouraud_renderer.Pipeline.render(state.pixel_buffer, render_context, vertex_buffer.items, @divExact(vertex_buffer.items.len, 3), render_requirements);

This is all very much a learning project and is not particularly fast, but it's probably alright for simple 2D games that dont aim to have 1080p resolutions, which is neat.

tinyrenderer's People

Contributors

oaguinagalde avatar ssloy avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.