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>
|
|
|
|
#include "IL/il.h"
|
|
|
|
#include "IL/ilu.h"
|
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
#include "shared/types.h"
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "array.h"
|
|
|
|
#include "color.h"
|
2012-01-24 13:16:20 +00:00
|
|
|
#include "system.h"
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
struct RenderArea
|
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
RenderParams params;
|
2012-01-29 21:45:58 +00:00
|
|
|
int pixel_count;
|
|
|
|
Array* pixels;
|
|
|
|
RenderFragment* scanline_up;
|
|
|
|
RenderFragment* scanline_down;
|
|
|
|
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-04-27 20:05:33 +00:00
|
|
|
#define RENDER_INVERSE 1
|
2011-12-10 13:25:22 +00:00
|
|
|
#define RENDER_WIREFRAME 1
|
|
|
|
|
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) {}
|
|
|
|
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;
|
|
|
|
|
|
|
|
result = malloc(sizeof(RenderArea));
|
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;
|
|
|
|
result->pixels = malloc(sizeof(Array));
|
|
|
|
arrayCreate(result->pixels, sizeof(RenderFragment));
|
|
|
|
result->scanline_up = malloc(sizeof(RenderFragment));
|
|
|
|
result->scanline_down = malloc(sizeof(RenderFragment));
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2012-02-12 16:57:29 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
for (x = 0; x < area->params.width * area->params.antialias; x++)
|
2012-02-12 16:57:29 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
for (y = 0; y < area->params.height * area->params.antialias; y++)
|
2012-02-12 16:57:29 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
arrayDelete(area->pixels + (y * area->params.width * area->params.antialias + x));
|
2012-02-12 16:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 x, y;
|
|
|
|
int width, height;
|
|
|
|
|
|
|
|
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
|
|
|
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-13 15:38:11 +00:00
|
|
|
arrayDelete(area->pixels + (y * area->params.width * area->params.antialias + x));
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
area->params = params;
|
2012-01-29 21:45:58 +00:00
|
|
|
area->pixels = realloc(area->pixels, sizeof(Array) * width * height);
|
|
|
|
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;
|
|
|
|
area->scanline_up = realloc(area->scanline_up, sizeof(RenderFragment) * width);
|
|
|
|
area->scanline_down = realloc(area->scanline_down, sizeof(RenderFragment) * 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
|
|
|
|
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
arrayCreate(area->pixels + (y * width + x), sizeof(RenderFragment));
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-29 21:45:58 +00:00
|
|
|
void renderClear(RenderArea* area)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
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-13 15:38:11 +00:00
|
|
|
arrayClear(area->pixels + (y * area->params.width * area->params.antialias + x));
|
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;
|
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-01-29 21:45:58 +00:00
|
|
|
/*static int _sortRenderFragment(void const* a, void const* b)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
double za, zb;
|
|
|
|
za = ((RenderFragment*)a)->z;
|
|
|
|
zb = ((RenderFragment*)b)->z;
|
|
|
|
if (za > zb)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (za < zb)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-29 21:45:58 +00:00
|
|
|
}*/
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
static Color _getPixelColor(Color base, Array* pixel_data)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
RenderFragment* fragment;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (pixel_data->length > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < pixel_data->length; i++)
|
|
|
|
{
|
|
|
|
fragment = ((RenderFragment*)pixel_data->data) + i;
|
2012-01-29 21:45:58 +00:00
|
|
|
colorMask(&base, &(fragment->vertex.color));
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
return base;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
static inline void _setDirtyPixel(RenderArea* area, Array* pixel_data, int x, int y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
pixel_data->dirty = 1;
|
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-01-29 21:45:58 +00:00
|
|
|
static void _processDirtyPixels(RenderArea* area)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Color col;
|
|
|
|
Array* pixel_data;
|
|
|
|
int x, y;
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
for (y = area->dirty_down; y <= area->dirty_up; y++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
for (x = area->dirty_left; x <= area->dirty_right; x++)
|
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
|
|
|
if (pixel_data->dirty)
|
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
col = _getPixelColor(area->background_color, pixel_data);
|
|
|
|
area->callback_draw(x, y, col);
|
2011-12-10 13:25:22 +00:00
|
|
|
pixel_data->dirty = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-29 21:45:58 +00:00
|
|
|
static void _setAllDirty(RenderArea* area)
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
|
|
|
int x, y;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_left = 0;
|
2012-06-13 15:38:11 +00:00
|
|
|
area->dirty_right = area->params.width - 1;
|
2012-01-29 21:45:58 +00:00
|
|
|
area->dirty_down = 0;
|
2012-06-13 15:38:11 +00:00
|
|
|
area->dirty_up = area->params.height - 1;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
for (y = area->dirty_down; y <= area->dirty_up; y++)
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
for (x = area->dirty_left; x <= area->dirty_right; x++)
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
(area->pixels + y * area->params.width * area->params.antialias + x)->dirty = 1;
|
2012-01-18 16:20:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void renderAddFragment(RenderArea* area, RenderFragment* fragment)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Array* pixel_data;
|
|
|
|
int x = fragment->x;
|
|
|
|
int y = fragment->y;
|
|
|
|
double z = fragment->z;
|
|
|
|
|
|
|
|
int i, dirty;
|
|
|
|
int fragments_count;
|
|
|
|
RenderFragment* fragments;
|
|
|
|
|
|
|
|
dirty = 0;
|
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
|
|
|
fragments = (RenderFragment*)pixel_data->data;
|
|
|
|
fragments_count = pixel_data->length;
|
|
|
|
|
|
|
|
if (fragments_count == 0)
|
|
|
|
{
|
|
|
|
arrayAppend(pixel_data, fragment);
|
|
|
|
dirty = 1;
|
|
|
|
}
|
|
|
|
else if (fragments[0].z > z)
|
|
|
|
{
|
|
|
|
if (fragments[0].vertex.color.a < 1.0)
|
|
|
|
{
|
|
|
|
arrayInsert(pixel_data, fragment, 0);
|
|
|
|
dirty = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 1; i <= fragments_count; i++)
|
|
|
|
{
|
|
|
|
if ((i == fragments_count) || (fragments[i].z > z))
|
|
|
|
{
|
|
|
|
if (fragment->vertex.color.a > 0.999999)
|
|
|
|
{
|
|
|
|
if (i > 1)
|
|
|
|
{
|
|
|
|
arrayLStrip(pixel_data, i - 1);
|
|
|
|
}
|
|
|
|
arrayReplace(pixel_data, fragment, 0);
|
|
|
|
}
|
|
|
|
else if (i == fragments_count)
|
|
|
|
{
|
|
|
|
arrayAppend(pixel_data, fragment);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arrayInsert(pixel_data, fragment, i);
|
|
|
|
}
|
|
|
|
dirty = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirty)
|
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
_setDirtyPixel(area, pixel_data, x, y);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void renderPushFragment(RenderArea* area, int x, int y, double z, Vertex* vertex)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
RenderFragment fragment;
|
|
|
|
|
|
|
|
fragment.x = x;
|
|
|
|
fragment.y = y;
|
|
|
|
fragment.z = z;
|
|
|
|
fragment.vertex = *vertex;
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
renderAddFragment(area, &fragment);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __vertexGetDiff(Vertex* v1, Vertex* v2, Vertex* result)
|
|
|
|
{
|
|
|
|
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->normal.x = v2->normal.x - v1->normal.x;
|
|
|
|
result->normal.y = v2->normal.y - v1->normal.y;
|
|
|
|
result->normal.z = v2->normal.z - v1->normal.z;
|
|
|
|
result->color.r = v2->color.r - v1->color.r;
|
|
|
|
result->color.g = v2->color.g - v1->color.g;
|
|
|
|
result->color.b = v2->color.b - v1->color.b;
|
|
|
|
result->color.a = v2->color.a - v1->color.a;
|
|
|
|
result->callback = v1->callback;
|
2012-01-24 13:16:20 +00:00
|
|
|
result->callback_data = v1->callback_data;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __vertexInterpolate(Vertex* v1, Vertex* diff, double value, Vertex* result)
|
|
|
|
{
|
|
|
|
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->normal.x = v1->normal.x + diff->normal.x * value;
|
|
|
|
result->normal.y = v1->normal.y + diff->normal.y * value;
|
|
|
|
result->normal.z = v1->normal.z + diff->normal.z * value;
|
|
|
|
result->color.r = v1->color.r + diff->color.r * value;
|
|
|
|
result->color.g = v1->color.g + diff->color.g * value;
|
|
|
|
result->color.b = v1->color.b + diff->color.b * value;
|
|
|
|
result->color.a = v1->color.a + diff->color.a * value;
|
|
|
|
result->callback = v1->callback;
|
2012-01-24 13:16:20 +00:00
|
|
|
result->callback_data = v1->callback_data;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
static void __pushScanLinePoint(RenderArea* area, RenderFragment point)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
if (point.x < 0 || point.x >= area->params.width * area->params.antialias)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
if (point.x > area->scanline_right)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +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-01-29 21:45:58 +00:00
|
|
|
area->scanline_left = point.x;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-29 21:45:58 +00:00
|
|
|
else if (point.x < area->scanline_left)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +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-01-29 21:45:58 +00:00
|
|
|
if (point.y > area->scanline_up[point.x].y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
area->scanline_up[point.x] = point;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-01-29 21:45:58 +00:00
|
|
|
if (point.y < area->scanline_down[point.x].y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
area->scanline_down[point.x] = point;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
static void __pushScanLineEdge(RenderArea* area, Vector3 v1, Vector3 v2, Vertex* vertex1, Vertex* vertex2)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
double dx, dy, dz, fx;
|
|
|
|
Vertex diff;
|
|
|
|
int startx = lround(v1.x);
|
|
|
|
int endx = lround(v2.x);
|
|
|
|
int curx;
|
|
|
|
RenderFragment fragment;
|
|
|
|
|
|
|
|
if (endx < startx)
|
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
__pushScanLineEdge(area, v2, v1, vertex2, vertex1);
|
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)
|
|
|
|
{
|
|
|
|
fragment.x = startx;
|
|
|
|
fragment.y = lround(v1.y);
|
|
|
|
fragment.z = v1.z;
|
|
|
|
fragment.vertex = *vertex1;
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
__pushScanLinePoint(area, fragment);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
fragment.x = endx;
|
|
|
|
fragment.y = lround(v2.y);
|
|
|
|
fragment.z = v2.z;
|
|
|
|
fragment.vertex = *vertex2;
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
__pushScanLinePoint(area, fragment);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
dx = v2.x - v1.x;
|
|
|
|
dy = v2.y - v1.y;
|
|
|
|
dz = v2.z - v1.z;
|
|
|
|
__vertexGetDiff(vertex1, vertex2, &diff);
|
|
|
|
for (curx = startx; curx <= endx; curx++)
|
|
|
|
{
|
|
|
|
fx = (double)curx + 0.5;
|
|
|
|
if (fx < v1.x)
|
|
|
|
{
|
|
|
|
fx = v1.x;
|
|
|
|
}
|
|
|
|
else if (fx > v2.x)
|
|
|
|
{
|
|
|
|
fx = v2.x;
|
|
|
|
}
|
|
|
|
fx = fx - v1.x;
|
|
|
|
fragment.x = curx;
|
|
|
|
fragment.y = lround(v1.y + dy * fx / dx);
|
|
|
|
fragment.z = v1.z + dz * fx / dx;
|
|
|
|
__vertexInterpolate(vertex1, &diff, fx / dx, &(fragment.vertex));
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
__pushScanLinePoint(area, fragment);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
static void __clearScanLines(RenderArea* area)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int x;
|
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
|
|
|
area->scanline_up[x].y = -1;
|
2012-06-13 15:38:11 +00:00
|
|
|
area->scanline_down[x].y = area->params.height * area->params.antialias;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-06-13 15:38:11 +00:00
|
|
|
area->scanline_left = area->params.width * area->params.antialias;
|
2012-01-29 21:45:58 +00:00
|
|
|
area->scanline_right = -1;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
static void __renderScanLines(RenderArea* area)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
int x, starty, endy, cury;
|
|
|
|
Vertex diff;
|
|
|
|
double dy, dz, fy;
|
|
|
|
RenderFragment up, down, current;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
dy = (double)(up.y - down.y);
|
|
|
|
dz = up.z - down.z;
|
|
|
|
__vertexGetDiff(&down.vertex, &up.vertex, &diff);
|
|
|
|
|
|
|
|
current.x = x;
|
|
|
|
for (cury = starty; cury <= endy; cury++)
|
|
|
|
{
|
|
|
|
fy = (double)cury - down.y;
|
|
|
|
|
|
|
|
current.y = cury;
|
|
|
|
current.z = down.z + dz * fy / dy;
|
|
|
|
__vertexInterpolate(&down.vertex, &diff, fy / dy, ¤t.vertex);
|
|
|
|
|
|
|
|
#ifdef RENDER_WIREFRAME
|
|
|
|
if (cury == starty || cury == endy)
|
|
|
|
{
|
|
|
|
current.vertex.color = COLOR_RED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
renderAddFragment(area, ¤t);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void renderPushTriangle(RenderArea* area, Vertex* v1, Vertex* v2, Vertex* v3, Vector3 p1, Vector3 p2, Vector3 p3)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
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 */
|
|
|
|
if (p1.z < 1.0 || p2.z < 1.0 || p3.z < 1.0 || (p1.x < 0.0 && p2.x < 0.0 && p3.x < 0.0) || (p1.y < 0.0 && p2.y < 0.0 && p3.y < 0.0) || (p1.x > limit_width && p2.x > limit_width && p3.x > limit_width) || (p1.y > limit_height && p2.y > limit_height && p3.y > limit_height))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
__clearScanLines(area);
|
|
|
|
__pushScanLineEdge(area, p1, p2, v1, v2);
|
|
|
|
__pushScanLineEdge(area, p2, p3, v2, v3);
|
|
|
|
__pushScanLineEdge(area, p3, p1, v3, v1);
|
|
|
|
mutexAcquire(area->lock);
|
|
|
|
__renderScanLines(area);
|
|
|
|
mutexRelease(area->lock);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int startx;
|
|
|
|
int endx;
|
|
|
|
int starty;
|
|
|
|
int endy;
|
|
|
|
int finished;
|
|
|
|
int interrupt;
|
|
|
|
Thread* thread;
|
2012-01-29 21:45:58 +00:00
|
|
|
RenderArea* area;
|
2012-01-24 13:16:20 +00:00
|
|
|
Renderer* renderer;
|
2011-12-10 13:25:22 +00:00
|
|
|
} RenderChunk;
|
|
|
|
|
|
|
|
void* _renderPostProcessChunk(void* data)
|
|
|
|
{
|
|
|
|
int x, y, i;
|
|
|
|
int dirty;
|
|
|
|
Array* pixel_data;
|
|
|
|
RenderFragment* fragments;
|
|
|
|
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-13 15:38:11 +00:00
|
|
|
pixel_data = chunk->area->pixels + (y * chunk->area->params.width * chunk->area->params.antialias + x);
|
2011-12-10 13:25:22 +00:00
|
|
|
fragments = (RenderFragment*)pixel_data->data;
|
|
|
|
dirty = 0;
|
|
|
|
for (i = 0; i < pixel_data->length; i++)
|
|
|
|
{
|
|
|
|
if (fragments[i].vertex.callback)
|
|
|
|
{
|
2012-01-24 13:16:20 +00:00
|
|
|
if (fragments[i].vertex.callback(fragments + i, chunk->renderer, fragments[i].vertex.callback_data))
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
colorNormalize(&fragments[i].vertex.color);
|
|
|
|
dirty = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dirty)
|
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
mutexAcquire(chunk->area->lock);
|
|
|
|
_setDirtyPixel(chunk->area, pixel_data, x, y);
|
|
|
|
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);
|
|
|
|
/*_progress = (double)_progress_pixels / (double)_pixel_count;*/
|
|
|
|
_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);
|
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
|
|
|
{
|
|
|
|
ILuint image_id;
|
|
|
|
ilGenImages(1, &image_id);
|
|
|
|
ilBindImage(image_id);
|
|
|
|
Color result;
|
|
|
|
ILuint x, y;
|
|
|
|
ILuint rgba;
|
2012-06-13 15:38:11 +00:00
|
|
|
ILuint data[area->params.height * area->params.width];
|
2011-12-10 13:25:22 +00:00
|
|
|
ILenum error;
|
|
|
|
Array* pixel_data;
|
2012-05-06 10:13:34 +00:00
|
|
|
int error_count;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
for (y = 0; y < area->params.height; y++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
for (x = 0; x < area->params.width; x++)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-13 15:38:11 +00:00
|
|
|
/* TODO Anti-alias */
|
|
|
|
pixel_data = area->pixels + (y * area->params.width + x);
|
2012-01-29 21:45:58 +00:00
|
|
|
result = _getPixelColor(area->background_color, pixel_data);
|
2011-12-10 13:25:22 +00:00
|
|
|
rgba = colorTo32BitRGBA(&result);
|
2012-06-13 15:38:11 +00:00
|
|
|
data[y * area->params.width + x] = rgba;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
ilTexImage((ILuint)area->params.width, (ILuint)area->params.height, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, data);
|
2012-05-06 10:13:34 +00:00
|
|
|
remove(path);
|
2011-12-10 13:25:22 +00:00
|
|
|
ilSaveImage(path);
|
|
|
|
|
|
|
|
ilDeleteImages(1, &image_id);
|
|
|
|
|
2012-05-06 10:13:34 +00:00
|
|
|
error_count = 0;
|
2011-12-10 13:25:22 +00:00
|
|
|
while ((error=ilGetError()) != IL_NO_ERROR)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "IL ERROR : %s\n", iluErrorString(error));
|
2012-05-06 10:13:34 +00:00
|
|
|
error_count++;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
2012-05-06 10:13:34 +00:00
|
|
|
return !error_count;
|
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
|
|
|
}
|