obj2png

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

README

obj2png

Go Report Card

A Go module for rendering Wavefront OBJ files (with MTL material support) to PNG images using software rasterization.

created by tigwen

Install

go get github.com/mlctrez/obj2png

Requires Go 1.21 or later.

Dependencies

Usage

Basic render with defaults
package main

import (
	"log"

	"github.com/mlctrez/obj2png"
)

func main() {
	options := obj2png.DefaultRenderOptions()
	if err := obj2png.Render("model.obj", "model.png", options); err != nil {
		log.Fatal(err)
	}
}
Render with a specific view angle
options := obj2png.DefaultRenderOptions()
err := obj2png.Render("model.obj", "front_left.png", options, obj2png.ViewFrontLeft)
Available view angles
Constant Description
ViewFrontRight Front-right elevated (default)
ViewFrontLeft Front-left elevated
ViewFront Front center elevated
ViewRight Right side elevated
ViewLeft Left side elevated
ViewBackRight Back-right elevated
ViewBackLeft Back-left elevated
ViewBack Back center elevated
ViewTop Directly above
Custom render options
options := obj2png.RenderOptions{
	Width:  1024,
	Height: 1024,
	Scale:  2,
	Fovy:   30,
	Near:   1,
	Far:    10,
	Eye:    fauxgl.V(3, 3, 3),
	Center: fauxgl.V(0, 0, 0),
	Up:     fauxgl.V(0, 1, 0),
	Light:  fauxgl.V(1.5, 2.7, 1.5).Normalize(),
}
err := obj2png.Render("model.obj", "model_hires.png", options)

Note: when a ViewAngle is provided, it overrides the Eye field in options.

CLI

A command-line tool is included in cmd/main.go for batch rendering directories of OBJ files with index page generation.

# single file
go run ./cmd model.obj output.png

# directory of OBJ files
go run ./cmd ./models ./output

# skip specific directories
go run ./cmd -skip=archive,temp ./models ./output

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(inputFile, outputFile string, options RenderOptions, viewAngle ...ViewAngle) error

Render loads an OBJ file and renders it to a PNG file. An optional ViewAngle can be provided to control the camera angle. If not provided, the default view angle from options.Eye is used. The light position is automatically placed relative to the camera to ensure consistent illumination across all view angles.

Types

type Material

type Material struct {
	Name    string
	Diffuse fauxgl.Color
	Texture fauxgl.Texture
}

Material represents an MTL material

type RenderOptions

type RenderOptions struct {
	Width  int
	Height int
	Scale  int
	Fovy   float64
	Near   float64
	Far    float64
	Eye    fauxgl.Vector
	Center fauxgl.Vector
	Up     fauxgl.Vector
	Light  fauxgl.Vector
}

RenderOptions defines the options for the Render function

func DefaultRenderOptions

func DefaultRenderOptions() RenderOptions

DefaultRenderOptions returns the default options for rendering

type ViewAngle

type ViewAngle int

ViewAngle represents a named camera angle for rendering.

const (
	// ViewFrontRight is the default view angle, looking from the front-right at elevation.
	// This matches the original camera position of (3, 3, 3).
	ViewFrontRight ViewAngle = iota
	// ViewFrontLeft looks from the front-left at elevation.
	ViewFrontLeft
	// ViewFront looks from directly in front at elevation.
	ViewFront
	// ViewRight looks from the right side at elevation.
	ViewRight
	// ViewLeft looks from the left side at elevation.
	ViewLeft
	// ViewBackRight looks from the back-right at elevation.
	ViewBackRight
	// ViewBackLeft looks from the back-left at elevation.
	ViewBackLeft
	// ViewBack looks from directly behind at elevation.
	ViewBack
	// ViewTop looks from directly above.
	ViewTop
)

func (ViewAngle) EyePosition

func (v ViewAngle) EyePosition() fauxgl.Vector

EyePosition returns the camera eye position for this view angle.

func (ViewAngle) String

func (v ViewAngle) String() string

String returns the name of the view angle.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址