2012-01-24 13:16:20 +00:00
|
|
|
#include "render.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2012-05-06 10:13:34 +00:00
|
|
|
#include <stdio.h>
|
2011-12-10 13:25:22 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2013-01-14 19:07:56 +00:00
|
|
|
#include "renderer.h"
|
2012-01-24 13:16:20 +00:00
|
|
|
#include "system.h"
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
unsigned char dirty : 1;
|
|
|
|
unsigned char edge : 1;
|
|
|
|
unsigned char callback : 6;
|
|
|
|
} flags;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
Vector3 location;
|
|
|
|
struct {
|
|
|
|
double r;
|
|
|
|
double g;
|
|
|
|
double b;
|
|
|
|
} color;
|
|
|
|
} data;
|
|
|
|
double z;
|
|
|
|
} RenderFragment;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
Vector3 pixel;
|
|
|
|
Vector3 location;
|
|
|
|
int callback;
|
|
|
|
} ScanPoint;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
f_RenderFragmentCallback function;
|
|
|
|
void* data;
|
|
|
|
} FragmentCallback;
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
struct RenderArea
|
|
|
|
{
|
2013-02-05 21:25:30 +00:00
|
|
|
ColorProfile* hdr_mapping;
|
2012-06-13 15:38:11 +00:00
|
|
|
RenderParams params;
|
2012-01-29 21:45:58 +00:00
|
|
|
int pixel_count;
|
2012-06-15 19:45:41 +00:00
|
|
|
RenderFragment* pixels;
|
2012-06-17 09:40:40 +00:00
|
|
|
ScanPoint* scanline_up;
|
|
|
|
ScanPoint* scanline_down;
|
|
|
|
int fragment_callbacks_count;
|
|
|
|
FragmentCallback fragment_callbacks[64];
|
2012-01-29 21:45:58 +00:00
|
|
|
int scanline_left;
|
|
|
|
int scanline_right;
|
|
|
|
Color background_color;
|
|
|
|
volatile int dirty_left;
|
|
|
|
volatile int dirty_right;
|
|
|
|
volatile int dirty_up;
|
|
|
|
volatile int dirty_down;
|
|
|
|
volatile int dirty_count;
|
|
|
|
Mutex* lock;
|
|
|
|
RenderCallbackStart callback_start;
|
|
|
|
RenderCallbackDraw callback_draw;
|
|
|
|
RenderCallbackUpdate callback_update;
|
|
|
|
};
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
typedef struct {
|
|
|
|
int startx;
|
|
|
|
int endx;
|
|
|
|
int starty;
|
|
|
|
int endy;
|
|
|
|
int finished;
|
|
|
|
int interrupt;
|
|
|
|
Thread* thread;
|
|
|
|
RenderArea* area;
|
|
|
|
Renderer* renderer;
|
|
|
|
} RenderChunk;
|
|
|
|
|
2012-04-27 20:05:33 +00:00
|
|
|
#define RENDER_INVERSE 1
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
static void _callbackStart(int width, int height, Color background) {}
|
|
|
|
static void _callbackDraw(int x, int y, Color col) {}
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _callbackUpdate(double progress) {}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void renderInit()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-12 16:57:29 +00:00
|
|
|
void renderQuit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
RenderArea* renderCreateArea()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
RenderArea* result;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
result = malloc(sizeof(RenderArea));
|
2013-02-05 21:25:30 +00:00
|
|
|
result->hdr_mapping = colorProfileCreate();
|
2012-06-13 15:38:11 +00:00
|
|
|
result->params.width = 1;
|
|
|
|
result->params.height = 1;
|
|
|
|
result->params.antialias = 1;
|
|
|
|
result->params.quality = 5;
|
2012-01-29 21:45:58 +00:00
|
|
|
result->pixel_count = 1;
|
2012-06-15 19:45:41 +00:00
|
|
|
result->pixels = malloc(sizeof(RenderFragment));
|
2012-06-17 09:40:40 +00:00
|
|
|
result->fragment_callbacks_count = 0;
|
|
|
|
result->scanline_up = malloc(sizeof(ScanPoint));
|
|
|
|
result->scanline_down = malloc(sizeof(ScanPoint));
|
2012-01-29 21:45:58 +00:00
|
|
|
result->scanline_left = 0;
|
|
|
|
result->scanline_right = 0;
|
|
|
|
result->background_color = COLOR_TRANSPARENT;
|
|
|
|
result->dirty_left = 1;
|
|
|
|
result->dirty_right = -1;
|
|
|
|
result->dirty_down = 1;
|
|
|
|
result->dirty_up = -1;
|
|
|
|
result->dirty_count = 0;
|
|
|
|
result->lock = mutexCreate();
|
|
|
|
result->callback_start = _callbackStart;
|
|
|
|
result->callback_draw = _callbackDraw;
|
|
|
|
result->callback_update = _callbackUpdate;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
return result;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void renderDeleteArea(RenderArea* area)
|
2012-01-11 16:11:34 +00:00
|
|
|
{
|
2013-02-05 21:25:30 +00:00
|
|
|
colorProfileDelete(area->hdr_mapping);
|
2012-01-29 21:45:58 +00:00
|
|
|
mutexDestroy(area->lock);
|
2012-02-12 16:57:29 +00:00
|
|
|
free(area->pixels);
|
2012-01-29 21:45:58 +00:00
|
|
|
free(area->scanline_up);
|
|
|
|
free(area->scanline_down);
|
|
|
|
free(area);
|
2012-01-11 16:11:34 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 21:25:30 +00:00
|
|
|
static void _setAllDirty(RenderArea* area)
|
|
|
|
{
|
|
|
|
area->dirty_left = 0;
|
|
|
|
area->dirty_right = area->params.width * area->params.antialias - 1;
|
|
|
|
area->dirty_down = 0;
|
|
|
|
area->dirty_up = area->params.height * area->params.antialias - 1;
|
|
|
|
}
|
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
void renderSetParams(RenderArea* area, RenderParams params)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
int width, height;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
width = params.width * params.antialias;
|
|
|
|
height = params.height * params.antialias;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
area->params = params;
|
2012-06-15 19:45:41 +00:00
|
|
|
area->pixels = realloc(area->pixels, sizeof(RenderFragment) * width * height);
|
2012-01-29 21:45:58 +00:00
|
|
|
area->pixel_count = width * height;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
area->scanline_left = 0;
|
|
|
|
area->scanline_right = width - 1;
|
2012-06-17 09:40:40 +00:00
|
|
|
area->scanline_up = realloc(area->scanline_up, sizeof(ScanPoint) * width);
|
|
|
|
area->scanline_down = realloc(area->scanline_down, sizeof(ScanPoint) * width);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_left = width;
|
|
|
|
area->dirty_right = -1;
|
|
|
|
area->dirty_down = height;
|
|
|
|
area->dirty_up = -1;
|
|
|
|
area->dirty_count = 0;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-15 19:45:41 +00:00
|
|
|
renderClear(area);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 21:25:30 +00:00
|
|
|
void renderSetToneMapping(RenderArea* area, ToneMappingOperator tonemapper, double exposure)
|
|
|
|
{
|
|
|
|
colorProfileSetToneMapping(area->hdr_mapping, tonemapper, exposure);
|
|
|
|
_setAllDirty(area);
|
|
|
|
renderUpdate(area);
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void renderSetBackgroundColor(RenderArea* area, Color* col)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
area->background_color = *col;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _clearScanLines(RenderArea* area)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
for (x = area->scanline_left; x <= area->scanline_right; x++)
|
|
|
|
{
|
|
|
|
area->scanline_up[x].y = -1;
|
|
|
|
area->scanline_down[x].y = area->params.height * area->params.antialias;
|
|
|
|
}
|
|
|
|
area->scanline_left = area->params.width * area->params.antialias;
|
|
|
|
area->scanline_right = -1;
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void renderClear(RenderArea* area)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-15 19:45:41 +00:00
|
|
|
RenderFragment* pixel;
|
2011-12-10 13:25:22 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
area->fragment_callbacks_count = 1;
|
|
|
|
area->fragment_callbacks[0].function = NULL;
|
|
|
|
area->fragment_callbacks[0].data = NULL;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
for (x = 0; x < area->params.width * area->params.antialias; x++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
for (y = 0; y < area->params.height * area->params.antialias; y++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-15 19:45:41 +00:00
|
|
|
pixel = area->pixels + (y * area->params.width * area->params.antialias + x);
|
|
|
|
pixel->z = -100000000.0;
|
2012-06-17 09:40:40 +00:00
|
|
|
pixel->flags.dirty = 0;
|
|
|
|
pixel->flags.callback = 0;
|
|
|
|
pixel->data.color.r = area->background_color.r;
|
|
|
|
pixel->data.color.g = area->background_color.g;
|
|
|
|
pixel->data.color.b = area->background_color.b;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
area->scanline_left = 0;
|
2012-06-13 15:38:11 +00:00
|
|
|
area->scanline_right = area->params.width * area->params.antialias - 1;
|
2012-06-17 09:40:40 +00:00
|
|
|
_clearScanLines(area);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
area->callback_start(area->params.width, area->params.height, area->background_color);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
area->dirty_left = area->params.width * area->params.antialias;
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_right = -1;
|
2012-06-13 15:38:11 +00:00
|
|
|
area->dirty_down = area->params.height * area->params.antialias;
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_up = -1;
|
|
|
|
area->dirty_count = 0;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static inline void _setDirtyPixel(RenderArea* area, int x, int y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
if (x < area->dirty_left)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_left = x;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-01-29 21:45:58 +00:00
|
|
|
if (x > area->dirty_right)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_right = x;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-01-29 21:45:58 +00:00
|
|
|
if (y < area->dirty_down)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_down = y;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-01-29 21:45:58 +00:00
|
|
|
if (y > area->dirty_up)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_up = y;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_count++;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-13 20:38:01 +00:00
|
|
|
static inline Color _getFinalPixel(RenderArea* area, int x, int y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 20:38:01 +00:00
|
|
|
Color result, col;
|
|
|
|
int sx, sy;
|
2012-06-15 19:45:41 +00:00
|
|
|
RenderFragment* pixel_data;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-13 20:38:01 +00:00
|
|
|
result.r = result.g = result.b = 0.0;
|
|
|
|
result.a = 1.0;
|
|
|
|
for (sx = 0; sx < area->params.antialias; sx++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 20:38:01 +00:00
|
|
|
for (sy = 0; sy < area->params.antialias; sy++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 20:38:01 +00:00
|
|
|
pixel_data = area->pixels + (y * area->params.antialias + sy) * area->params.width * area->params.antialias + (x * area->params.antialias + sx);
|
2012-06-17 09:40:40 +00:00
|
|
|
if (pixel_data->flags.dirty)
|
|
|
|
{
|
|
|
|
if (pixel_data->flags.edge)
|
|
|
|
{
|
2012-06-17 15:59:20 +00:00
|
|
|
col = COLOR_GREY;
|
2012-06-17 09:40:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-17 15:59:20 +00:00
|
|
|
col = COLOR_WHITE;
|
2012-06-17 09:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
col.r = pixel_data->data.color.r;
|
|
|
|
col.g = pixel_data->data.color.g;
|
|
|
|
col.b = pixel_data->data.color.b;
|
|
|
|
}
|
|
|
|
result.r += col.r / (double)(area->params.antialias * area->params.antialias);
|
|
|
|
result.g += col.g / (double)(area->params.antialias * area->params.antialias);
|
|
|
|
result.b += col.b / (double)(area->params.antialias * area->params.antialias);
|
2013-02-05 21:25:30 +00:00
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2013-03-03 17:05:30 +00:00
|
|
|
return colorProfileApply(area->hdr_mapping, result);
|
2012-06-13 20:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _processDirtyPixels(RenderArea* area)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
int down, up, left, right;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-13 20:38:01 +00:00
|
|
|
down = area->dirty_down / area->params.antialias;
|
|
|
|
up = area->dirty_up / area->params.antialias;
|
|
|
|
left = area->dirty_left / area->params.antialias;
|
|
|
|
right = area->dirty_right / area->params.antialias;
|
|
|
|
|
|
|
|
for (y = down; y <= up; y++)
|
|
|
|
{
|
|
|
|
for (x = left; x <= right; x++)
|
|
|
|
{
|
|
|
|
area->callback_draw(x, y, _getFinalPixel(area, x, y));
|
|
|
|
}
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
area->callback_update(0.0);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
area->dirty_left = area->params.width * area->params.antialias;
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_right = -1;
|
2012-06-13 15:38:11 +00:00
|
|
|
area->dirty_down = area->params.height * area->params.antialias;
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_up = -1;
|
|
|
|
area->dirty_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderUpdate(RenderArea* area)
|
|
|
|
{
|
|
|
|
mutexAcquire(area->lock);
|
|
|
|
_processDirtyPixels(area);
|
|
|
|
mutexRelease(area->lock);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static inline unsigned int _pushCallback(RenderArea* area, FragmentCallback callback)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < area->fragment_callbacks_count; i++)
|
|
|
|
{
|
|
|
|
if (area->fragment_callbacks[i].function == callback.function && area->fragment_callbacks[i].data == callback.data)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
if (area->fragment_callbacks_count >= 64)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
area->fragment_callbacks[area->fragment_callbacks_count].function = callback.function;
|
|
|
|
area->fragment_callbacks[area->fragment_callbacks_count].data = callback.data;
|
|
|
|
return area->fragment_callbacks_count++;
|
|
|
|
}
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _pushFragment(RenderArea* area, int x, int y, double z, int edge, Vector3 location, int callback)
|
|
|
|
{
|
|
|
|
RenderFragment* pixel_data;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
if (x >= 0 && x < area->params.width * area->params.antialias && y >= 0 && y < area->params.height * area->params.antialias && z > 1.0)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
pixel_data = area->pixels + (y * area->params.width * area->params.antialias + x);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-15 19:45:41 +00:00
|
|
|
if (z > pixel_data->z)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
pixel_data->flags.dirty = (unsigned char)1;
|
|
|
|
pixel_data->flags.edge = (unsigned char)edge;
|
|
|
|
pixel_data->flags.callback = (unsigned char)callback;
|
|
|
|
pixel_data->data.location = location;
|
|
|
|
pixel_data->z = z;
|
|
|
|
_setDirtyPixel(area, x, y);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _scanGetDiff(ScanPoint* v1, ScanPoint* v2, ScanPoint* result)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
result->pixel.x = v2->pixel.x - v1->pixel.x;
|
|
|
|
result->pixel.y = v2->pixel.y - v1->pixel.y;
|
|
|
|
result->pixel.z = v2->pixel.z - v1->pixel.z;
|
2011-12-10 13:25:22 +00:00
|
|
|
result->location.x = v2->location.x - v1->location.x;
|
|
|
|
result->location.y = v2->location.y - v1->location.y;
|
|
|
|
result->location.z = v2->location.z - v1->location.z;
|
|
|
|
result->callback = v1->callback;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _scanInterpolate(ScanPoint* v1, ScanPoint* diff, double value, ScanPoint* result)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
result->pixel.x = v1->pixel.x + diff->pixel.x * value;
|
|
|
|
result->pixel.y = v1->pixel.y + diff->pixel.y * value;
|
|
|
|
result->pixel.z = v1->pixel.z + diff->pixel.z * value;
|
2011-12-10 13:25:22 +00:00
|
|
|
result->location.x = v1->location.x + diff->location.x * value;
|
|
|
|
result->location.y = v1->location.y + diff->location.y * value;
|
|
|
|
result->location.z = v1->location.z + diff->location.z * value;
|
|
|
|
result->callback = v1->callback;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _pushScanPoint(RenderArea* area, ScanPoint* point)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
point->x = (int)floor(point->pixel.x);
|
|
|
|
point->y = (int)floor(point->pixel.y);
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
if (point->x < 0 || point->x >= area->params.width * area->params.antialias)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
if (point->x > area->scanline_right)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
area->scanline_right = point->x;
|
|
|
|
area->scanline_up[area->scanline_right] = *point;
|
|
|
|
area->scanline_down[area->scanline_right] = *point;
|
|
|
|
if (point->x < area->scanline_left)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
area->scanline_left = point->x;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-17 09:40:40 +00:00
|
|
|
else if (point->x < area->scanline_left)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
area->scanline_left = point->x;
|
|
|
|
area->scanline_up[area->scanline_left] = *point;
|
|
|
|
area->scanline_down[area->scanline_left] = *point;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
if (point->y > area->scanline_up[point->x].y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
area->scanline_up[point->x] = *point;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-06-17 09:40:40 +00:00
|
|
|
if (point->y < area->scanline_down[point->x].y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
area->scanline_down[point->x] = *point;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _pushScanLineEdge(RenderArea* area, ScanPoint* point1, ScanPoint* point2)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double dx, fx;
|
|
|
|
ScanPoint diff, point;
|
|
|
|
int startx = lround(point1->pixel.x);
|
|
|
|
int endx = lround(point2->pixel.x);
|
2011-12-10 13:25:22 +00:00
|
|
|
int curx;
|
|
|
|
|
|
|
|
if (endx < startx)
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
_pushScanLineEdge(area, point2, point1);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-06-13 15:38:11 +00:00
|
|
|
else if (endx < 0 || startx >= area->params.width * area->params.antialias)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (startx == endx)
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
_pushScanPoint(area, point1);
|
|
|
|
_pushScanPoint(area, point2);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (startx < 0)
|
|
|
|
{
|
|
|
|
startx = 0;
|
|
|
|
}
|
2012-06-13 15:38:11 +00:00
|
|
|
if (endx >= area->params.width * area->params.antialias)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
endx = area->params.width * area->params.antialias - 1;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
dx = point2->pixel.x - point1->pixel.x;
|
|
|
|
_scanGetDiff(point1, point2, &diff);
|
2011-12-10 13:25:22 +00:00
|
|
|
for (curx = startx; curx <= endx; curx++)
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
fx = (double)curx + 0.5;
|
|
|
|
if (fx < point1->pixel.x)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
fx = point1->pixel.x;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-06-17 09:40:40 +00:00
|
|
|
else if (fx > point2->pixel.x)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
fx = point2->pixel.x;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-06-17 09:40:40 +00:00
|
|
|
fx = fx - point1->pixel.x;
|
|
|
|
_scanInterpolate(point1, &diff, fx / dx, &point);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
/*point.pixel.x = (double)curx;*/
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
_pushScanPoint(area, &point);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _renderScanLines(RenderArea* area)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int x, starty, endy, cury;
|
2012-06-17 09:40:40 +00:00
|
|
|
ScanPoint diff;
|
|
|
|
double dy, fy;
|
|
|
|
ScanPoint up, down, current;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
if (area->scanline_right > 0)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
for (x = area->scanline_left; x <= area->scanline_right; x++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
up = area->scanline_up[x];
|
|
|
|
down = area->scanline_down[x];
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
starty = down.y;
|
|
|
|
endy = up.y;
|
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
if (endy < 0 || starty >= area->params.height * area->params.antialias)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (starty < 0)
|
|
|
|
{
|
|
|
|
starty = 0;
|
|
|
|
}
|
2012-06-13 15:38:11 +00:00
|
|
|
if (endy >= area->params.height * area->params.antialias)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
endy = area->params.height * area->params.antialias - 1;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
dy = up.pixel.y - down.pixel.y;
|
|
|
|
_scanGetDiff(&down, &up, &diff);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
current.x = x;
|
|
|
|
for (cury = starty; cury <= endy; cury++)
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
fy = (double)cury + 0.5;
|
|
|
|
if (fy < down.pixel.y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
fy = down.pixel.y;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-06-17 09:40:40 +00:00
|
|
|
else if (fy > up.pixel.y)
|
|
|
|
{
|
|
|
|
fy = up.pixel.y;
|
|
|
|
}
|
|
|
|
fy = fy - down.pixel.y;
|
|
|
|
|
|
|
|
current.y = cury;
|
|
|
|
_scanInterpolate(&down, &diff, fy / dy, ¤t);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
_pushFragment(area, current.x, current.y, current.pixel.z, (cury == starty || cury == endy), current.location, current.callback);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
void renderPushTriangle(RenderArea* area, Vector3 pixel1, Vector3 pixel2, Vector3 pixel3, Vector3 location1, Vector3 location2, Vector3 location3, f_RenderFragmentCallback callback, void* callback_data)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
FragmentCallback fragment_callback = {callback, callback_data};
|
|
|
|
ScanPoint point1, point2, point3;
|
|
|
|
double limit_width = (double)(area->params.width * area->params.antialias - 1);
|
|
|
|
double limit_height = (double)(area->params.height * area->params.antialias - 1);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
/* Filter if outside screen */
|
2012-06-17 09:40:40 +00:00
|
|
|
if (pixel1.z < 1.0 || pixel2.z < 1.0 || pixel3.z < 1.0 || (pixel1.x < 0.0 && pixel2.x < 0.0 && pixel3.x < 0.0) || (pixel1.y < 0.0 && pixel2.y < 0.0 && pixel3.y < 0.0) || (pixel1.x > limit_width && pixel2.x > limit_width && pixel3.x > limit_width) || (pixel1.y > limit_height && pixel2.y > limit_height && pixel3.y > limit_height))
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
point1.pixel = pixel1;
|
|
|
|
point1.location = location1;
|
|
|
|
point1.callback = _pushCallback(area, fragment_callback);
|
|
|
|
|
|
|
|
point2.pixel = pixel2;
|
|
|
|
point2.location = location2;
|
|
|
|
point2.callback = _pushCallback(area, fragment_callback);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
point3.pixel = pixel3;
|
|
|
|
point3.location = location3;
|
|
|
|
point3.callback = _pushCallback(area, fragment_callback);
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
_clearScanLines(area);
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
_pushScanLineEdge(area, &point1, &point2);
|
|
|
|
_pushScanLineEdge(area, &point2, &point3);
|
|
|
|
_pushScanLineEdge(area, &point3, &point1);
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
mutexAcquire(area->lock);
|
2012-06-17 09:40:40 +00:00
|
|
|
_renderScanLines(area);
|
2012-01-29 21:45:58 +00:00
|
|
|
mutexRelease(area->lock);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* _renderPostProcessChunk(void* data)
|
|
|
|
{
|
2012-06-15 19:45:41 +00:00
|
|
|
int x, y;
|
|
|
|
RenderFragment* fragment;
|
2011-12-10 13:25:22 +00:00
|
|
|
RenderChunk* chunk = (RenderChunk*)data;
|
|
|
|
|
|
|
|
#ifdef RENDER_INVERSE
|
2012-06-13 15:38:11 +00:00
|
|
|
for (y = chunk->area->params.height * chunk->area->params.antialias - 1 - chunk->starty; y >= chunk->area->params.height * chunk->area->params.antialias - 1 - chunk->endy; y--)
|
2011-12-10 13:25:22 +00:00
|
|
|
#else
|
|
|
|
for (y = chunk->starty; y <= chunk->endy; y++)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
for (x = chunk->startx; x <= chunk->endx; x++)
|
|
|
|
{
|
2012-06-15 19:45:41 +00:00
|
|
|
fragment = chunk->area->pixels + (y * chunk->area->params.width * chunk->area->params.antialias + x);
|
2012-06-17 09:40:40 +00:00
|
|
|
if (fragment->flags.dirty)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
FragmentCallback callback;
|
|
|
|
Color col;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
callback = chunk->area->fragment_callbacks[fragment->flags.callback];
|
|
|
|
if (callback.function)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
col = callback.function(chunk->renderer, fragment->data.location, callback.data);
|
2013-01-14 19:07:56 +00:00
|
|
|
/*colorNormalize(&col);*/
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-06-17 09:40:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
col = COLOR_BLACK;
|
|
|
|
}
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
fragment->data.color.r = col.r;
|
|
|
|
fragment->data.color.g = col.g;
|
|
|
|
fragment->data.color.b = col.b;
|
|
|
|
|
2012-06-17 15:59:20 +00:00
|
|
|
mutexAcquire(chunk->area->lock);
|
2012-06-17 09:40:40 +00:00
|
|
|
fragment->flags.dirty = 0;
|
|
|
|
_setDirtyPixel(chunk->area, x, y);
|
2012-06-17 15:59:20 +00:00
|
|
|
mutexRelease(chunk->area->lock);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-01-29 21:45:58 +00:00
|
|
|
/* chunk->area->progress_pixels++; */
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
if (chunk->interrupt)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk->finished = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_CHUNKS 8
|
2012-01-29 21:45:58 +00:00
|
|
|
void renderPostProcess(RenderArea* area, Renderer* renderer, int nbchunks)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
volatile RenderChunk chunks[MAX_CHUNKS];
|
|
|
|
int i;
|
|
|
|
int x, y, dx, dy, nx, ny;
|
|
|
|
int loops, running;
|
|
|
|
|
|
|
|
if (nbchunks > MAX_CHUNKS)
|
|
|
|
{
|
|
|
|
nbchunks = MAX_CHUNKS;
|
|
|
|
}
|
|
|
|
if (nbchunks < 1)
|
|
|
|
{
|
|
|
|
nbchunks = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nx = 10;
|
|
|
|
ny = 10;
|
2012-06-13 15:38:11 +00:00
|
|
|
dx = area->params.width * area->params.antialias / nx;
|
|
|
|
dy = area->params.height * area->params.antialias / ny;
|
2011-12-10 13:25:22 +00:00
|
|
|
x = 0;
|
|
|
|
y = 0;
|
2012-01-29 21:45:58 +00:00
|
|
|
/*_progress_pixels = 0;*/
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nbchunks; i++)
|
|
|
|
{
|
|
|
|
chunks[i].thread = NULL;
|
2012-01-29 21:45:58 +00:00
|
|
|
chunks[i].area = area;
|
2012-01-24 13:16:20 +00:00
|
|
|
chunks[i].renderer = renderer;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
running = 0;
|
|
|
|
loops = 0;
|
2012-01-30 13:10:16 +00:00
|
|
|
while ((y < ny && !renderer->render_interrupt) || running > 0)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
timeSleepMs(100);
|
|
|
|
|
|
|
|
for (i = 0; i < nbchunks; i++)
|
|
|
|
{
|
|
|
|
if (chunks[i].thread)
|
|
|
|
{
|
|
|
|
if (chunks[i].finished)
|
|
|
|
{
|
|
|
|
threadJoin(chunks[i].thread);
|
|
|
|
chunks[i].thread = NULL;
|
|
|
|
running--;
|
|
|
|
}
|
2012-01-30 13:10:16 +00:00
|
|
|
else if (renderer->render_interrupt)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
chunks[i].interrupt = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-18 21:59:33 +00:00
|
|
|
|
2012-01-30 13:10:16 +00:00
|
|
|
if (y < ny && !chunks[i].thread && !renderer->render_interrupt)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
chunks[i].finished = 0;
|
|
|
|
chunks[i].interrupt = 0;
|
|
|
|
chunks[i].startx = x * dx;
|
2012-04-23 11:46:35 +00:00
|
|
|
if (x == nx - 1)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
chunks[i].endx = area->params.width * area->params.antialias - 1;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chunks[i].endx = (x + 1) * dx - 1;
|
|
|
|
}
|
|
|
|
chunks[i].starty = y * dy;
|
2012-04-23 11:46:35 +00:00
|
|
|
if (y == ny - 1)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
chunks[i].endy = area->params.height * area->params.antialias - 1;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chunks[i].endy = (y + 1) * dy - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
chunks[i].thread = threadCreate(_renderPostProcessChunk, (void*)(chunks + i));
|
|
|
|
running++;
|
|
|
|
|
|
|
|
if (++x >= nx)
|
|
|
|
{
|
|
|
|
y++;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++loops >= 10)
|
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
mutexAcquire(area->lock);
|
2012-06-17 09:40:40 +00:00
|
|
|
/*_progress = (double)_progress_pixels / (double)_pixel_count;*/
|
2012-01-29 21:45:58 +00:00
|
|
|
_processDirtyPixels(area);
|
|
|
|
mutexRelease(area->lock);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
loops = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
/*_progress = 1.0;*/
|
|
|
|
_processDirtyPixels(area);
|
2012-06-15 09:31:11 +00:00
|
|
|
area->callback_update(1.0);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-05-06 10:13:34 +00:00
|
|
|
int renderSaveToFile(RenderArea* area, const char* path)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-07-19 15:04:27 +00:00
|
|
|
return systemSavePictureFile(path, (PictureCallbackSavePixel)_getFinalPixel, area, area->params.width, area->params.height);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void renderSetPreviewCallbacks(RenderArea* area, RenderCallbackStart start, RenderCallbackDraw draw, RenderCallbackUpdate update)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
area->callback_start = start ? start : _callbackStart;
|
|
|
|
area->callback_draw = draw ? draw : _callbackDraw;
|
|
|
|
area->callback_update = update ? update : _callbackUpdate;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
area->callback_start(area->params.width, area->params.height, area->background_color);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
_setAllDirty(area);
|
|
|
|
_processDirtyPixels(area);
|
2012-01-18 16:20:14 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
area->callback_update(0.0);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|