~zjm/Moon3D

0e81b821a0782674dbed435a58ad682e7aef0282 — Zack Michener 5 months ago 1021e6b
wrap rendering objects in a RenderingContext struct
5 files changed, 40 insertions(+), 29 deletions(-)

M src/3d/render.c
M src/3d/render.h
M src/scene.c
M src/scene.h
M src/sketch.c
M src/3d/render.c => src/3d/render.c +18 -9
@@ 4,27 4,36 @@
#include "device.h"
#include "abstract.h"

void DrawObject(Object *object, LightSource **lights, Viewport *viewport, RasterSettings settings)
RenderingContext MakeRenderingContext(Viewport *viewport, Camera *camera, RasterSettings settings)
{
	RenderingContext context;
	context.viewport = viewport;
	context.camera = camera;
	context.settings = settings;
	return context;
}

void DrawObject(Object *object, LightSource **lights, RenderingContext context)
{
	Transform t = ComposeTransform(Translation(object->position), object->transform);
	for (int i = 0; i < NumFacets(object->geometry); i++) {
		Polygon *modeled = FromPolygon(object->geometry->facets[i], t);
		Color shade = LightPolygon(modeled, object->color, object->glow, lights);
		DrawPolygon(modeled, shade, viewport, settings);
		DrawPolygon(modeled, shade, context);
		FreePolygon(modeled);
	}
}

void DrawPolygon(Polygon *polygon, Color color, Viewport *viewport, RasterSettings settings)
void DrawPolygon(Polygon *polygon, Color color, RenderingContext context)
{
	Polygon *image = ProjectPolygon(polygon);
	if (ClipPolygon(&image)) {
		if (!BackfaceCulled(image)) {
			HomogenizePolygon(image);
			ViewmapPolygon(image);
			RasterPolygon(image, color, viewport, settings);
			if (settings.showWireframes) {
				WireframePolygon(image, BLACK, viewport);
			RasterPolygon(image, color, context.viewport, context.settings);
			if (context.settings.showWireframes) {
				WireframePolygon(image, BLACK, context.viewport);
			}
		}
	}


@@ 41,13 50,13 @@ void WireframePolygon(Polygon *polygon, Color color, Viewport *viewport)
	}
}

void DrawPoint(Point *p, Viewport *viewport)
void DrawPoint(Point *p, RenderingContext context)
{
	Vector projected = ProjectPoint(p->position);
	if (ClipPoint(projected)) {
		Vector mapped = ViewmapPoint(Homogenize(projected));
		if (UpdateDepthBuffer(mapped.x, mapped.y, mapped.z, viewport)) {
			WritePixel(mapped.x, mapped.y, p->color, viewport);
		if (UpdateDepthBuffer(mapped.x, mapped.y, mapped.z, context.viewport)) {
			WritePixel(mapped.x, mapped.y, p->color, context.viewport);
		}
	}
}

M src/3d/render.h => src/3d/render.h +11 -3
@@ 4,11 4,19 @@
#include "abstract.h"
#include "world.h"
#include "device.h"
#include "image.h"

void DrawPolygon(Polygon *polygon, Color color, Viewport *viewport, RasterSettings settings);
void DrawObject(Object *object, LightSource **lights, Viewport *viewport, RasterSettings settings);
typedef struct RenderingContext {
	Viewport *viewport;
	Camera *camera;
	RasterSettings settings;
} RenderingContext;

RenderingContext MakeRenderingContext(Viewport *viewport, Camera *camera, RasterSettings settings);
void DrawObject(Object *object, LightSource **lights, RenderingContext context);
void DrawPolygon(Polygon *polygon, Color color, RenderingContext context);
void WireframePolygon(Polygon *polygon, Color color, Viewport *viewport);
void DrawPoint(Point *p, Viewport *viewport);
void DrawPoint(Point *p, RenderingContext context);
void DrawLine(Vertex a, Vertex b, Color color, Viewport *viewport);
void DrawAxes(Viewport *viewport);
bool BackfaceCulled(Polygon *polygon);

M src/scene.c => src/scene.c +3 -3
@@ 25,13 25,13 @@ void AddPoint(Scene *scene, Point *point)
	list_push(scene->points, point);
}

void RenderScene(Scene *scene, Viewport *viewport, RasterSettings settings)
void RenderScene(Scene *scene, RenderingContext context)
{
	for (int i = 0; i < list_count(scene->objects); i++) {
		DrawObject(scene->objects[i], scene->lights, viewport, settings);
		DrawObject(scene->objects[i], scene->lights, context);
	}

	for (int i = 0; i < list_count(scene->points); i++) {
		DrawPoint(scene->points[i], viewport);
		DrawPoint(scene->points[i], context);
	}
}

M src/scene.h => src/scene.h +1 -2
@@ 18,5 18,4 @@ Scene *NewScene(void);
void AddObject(Scene *scene, Object *obj);
void AddLight(Scene *scene, LightSource *light);
void AddPoint(Scene *scene, Point *point);

void RenderScene(Scene *scene, Viewport *viewport, RasterSettings settings);
void RenderScene(Scene *scene, RenderingContext context);

M src/sketch.c => src/sketch.c +7 -12
@@ 8,18 8,14 @@ bool dither = false;
Scene *scene;
static SpotLightSource *flashlight;
int now, lastTime;
Viewport *viewport;
RenderingContext context;

void sketch_setup(Pixel *pixels)
{
	// viewport = NewViewport(pixels, BLACK, 400, 240);
	viewport = NewViewport(pixels, BLACK, 400, 240);

	Viewport *viewport = NewViewport(pixels, BLACK, 400, 240);
	RasterSettings settings = DefaultRasterSettings();
	scene = CubeWorld();

	// ShowZBuffer();
	// ShowWireframes();
	// SetOrtho(Vol(-5, 5, -5, 5, -10, 0.1));
	context = MakeRenderingContext(viewport, &scene->player->camera, settings);

	flashlight = SpotLight(CameraPosition(), CameraDirection(), 0.1, 0);
	AddLight(scene, (LightSource *)flashlight);


@@ 29,14 25,13 @@ void sketch_draw(long ticks)
{
	int dt = ticks - lastTime;
	lastTime = ticks;
	ClearViewport(viewport);
	RasterSettings settings = DefaultRasterSettings();
	ClearViewport(context.viewport);

	(*(scene->onUpdate))(dt);
	RenderScene(scene, viewport, settings);
	RenderScene(scene, context);

	if (dither) {
		Dither(viewport);
		Dither(context.viewport);
	}
}