ccdemos/textures.cc
author Radek Brich <radek.brich@devl.cz>
Sat, 29 Dec 2007 13:53:33 +0100
branchpyrit
changeset 42 fbdeb3e04543
child 43 0b8b968b42d1
permissions -rw-r--r--
cleaned Texture interface new C++ demo: textures slightly adjusted SAH for kd-tree slightly optimized kd-tree building -- moved termination cond. so it's tested before recursion minor sphere intersection optimization
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#include "raytracer.h"
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
#include "octree.h"
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
#include "image.h"
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
#include "common_sdl.h"
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
Camera cam;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
Light light(Vector3(-2.0, 10.0, -2.0), Colour(0.9, 0.9, 0.9));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
Float lx, ly, lz, cf;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
/*class CloudTexture: public Texture
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
	Vector3 centre;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
public:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
	Colour evaluate(Vector3 point)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
	{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
		Float sum = 0.0;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
		for (int i = 1; i < 5; i++)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
			sum += fabsf(perlin(point.x*i, point.y*i, point.z*i))/i;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
		Float value = sinf(point.x + sum)/2 + 0.5;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
		return Colour(value, value*0.5, value*0.5);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
	};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
};*/
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
void texture2D(Float u, Float v, Float &r, Float &g, Float &b)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
	// checkers
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
	r = fabs((int)(u*4.4)%2 + (int)(v*4.4)%2 - 1);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
	g=r; b=r;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
class PlanarMapTexture: public Texture
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
	Vector3 centre;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
public:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
	PlanarMapTexture(Vector3 acentre): centre(acentre) {};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
	Colour evaluate(Vector3 point)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
	{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		point = point - centre;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
		Float u = 1000+point.x;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
		Float v = 1000+point.y;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
		Float r,g,b;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
		texture2D(u,v, r,g,b);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
		return Colour(r, g, b);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
	};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
class CubicMapTexture: public Texture
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	Vector3 centre;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
public:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
	CubicMapTexture(Vector3 acentre): centre(acentre) {};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
	Colour evaluate(Vector3 point)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
	{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
		point = point - centre;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
		Float u,v;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
		if (fabs(point.x) > fabs(point.y))
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
		{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
			if (fabs(point.x) > fabs(point.z))
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
			{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
				u = point.y;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
				v = point.z;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
			}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
			else
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
			{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
				u = point.x;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
				v = point.y;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
			}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
		}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
		else
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
		{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
			if (fabs(point.y) > fabs(point.z))
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
			{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
				u = point.x;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
				v = point.z;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
			}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
			else
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
			{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
				u = point.x;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
				v = point.y;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
			}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
		}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
		u += 1000;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
		v += 1000;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
		Float r,g,b;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
		texture2D(u,v, r,g,b);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
		return Colour(r, g, b);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
	};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
class CylinderMapTexture: public Texture
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
	Vector3 centre;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
public:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
	CylinderMapTexture(Vector3 acentre): centre(acentre) {};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
	Colour evaluate(Vector3 point)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
	{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
		point = point - centre;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
		Float u,v;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
		v = 1000+point.y;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
		u = M_PI + atan2(point.z, point.x);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
		Float r,g,b;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
		texture2D(u,v, r,g,b);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
		return Colour(r, g, b);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
	};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
class SphereMapTexture: public Texture
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
	Vector3 centre;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
public:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
	SphereMapTexture(Vector3 acentre): centre(acentre) {};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
	Colour evaluate(Vector3 point)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
	{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
		point = point - centre;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
		Float u,v;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
		v = acos(point.y / point.mag());
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
		u = M_PI + atan2(point.z, point.x);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
		Float r,g,b;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
		texture2D(u,v, r,g,b);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
		return Colour(r, g, b);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
	};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
};
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
void update_callback()
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
	if (lx != 0.0)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
		light.pos.x += lx;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
	if (ly != 0.0)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
		light.pos.y += ly;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   137
	if (lz != 0.0)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
		light.pos.z += lz;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   139
	if (cf != 0.0)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
		cam.f += cf;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
void key_callback(int key, int down)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
	switch (key)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
	{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   147
		case SDLK_r:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
			lx = -0.1 * down;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
			break;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
		case SDLK_t:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   151
			lx = +0.1 * down;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
			break;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
		case SDLK_f:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
			ly = -0.1 * down;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
			break;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
		case SDLK_g:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
			ly = +0.1 * down;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
			break;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
		case SDLK_v:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
			lz = -0.1 * down;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
			break;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
		case SDLK_b:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   163
			lz = +0.1 * down;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
			break;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
		case SDLK_z:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   167
			cf = -0.02 * down;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
			break;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   169
		case SDLK_x:
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   170
			cf = +0.02 * down;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   171
			break;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   172
	}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   173
}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   174
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
int main(int argc, char **argv)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   176
{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
	Raytracer rt;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   178
	rt.setOversample(0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
	rt.setSubsample(8);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
	Octree top;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   182
	rt.setTop(&top);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   183
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   184
	rt.addlight(&light);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   185
	light.castShadows(false);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   186
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   187
	Material mat0a(Colour(0.7, 0.7, 0.7));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   188
	mat0a. setReflectivity(0.0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   189
	Box box(Vector3(-12.0, -1.2, -20.0), Vector3(12.0, -1.0, 0.0), &mat0a);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   190
	rt.addshape(&box);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
	Material mat0b(Colour(0.1, 0.7, 0.8));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   193
	mat0b.setReflectivity(0.7);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194
	Box box2(Vector3(-12.0, -1.2, -10.0), Vector3(12.0, 10.0, -10.2), &mat0b);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   195
	rt.addshape(&box2);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   196
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   197
	// spheres
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   198
	Material mat1(Colour(1.0, 1.0, 1.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   199
	mat1.texture = new PlanarMapTexture(Vector3(4.0, 2.0, -7.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   200
	Sphere sphere1(Vector3(4.0, 2.0, -7.0), 1.0, &mat1);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   201
	rt.addshape(&sphere1);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   202
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   203
	Material mat2(Colour(1.0, 1.0, 1.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   204
	mat2.texture = new CubicMapTexture(Vector3(1.0, 2.0, -7.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   205
	Sphere sphere2(Vector3(1.0, 2.0, -7.0), 1.0, &mat2);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   206
	rt.addshape(&sphere2);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   207
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   208
	Material mat3(Colour(1.0, 1.0, 1.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   209
	mat3.texture = new CylinderMapTexture(Vector3(-2.0, 2.0, -7.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   210
	Sphere sphere3(Vector3(-2.0, 2.0, -7.0), 1.0, &mat3);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   211
	rt.addshape(&sphere3);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   212
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   213
	Material mat4(Colour(1.0, 1.0, 1.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   214
	mat4.texture = new SphereMapTexture(Vector3(-5.0, 2.0, -7.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   215
	Sphere sphere4(Vector3(-5.0, 2.0, -7.0), 1.0, &mat4);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   216
	rt.addshape(&sphere4);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   217
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   218
	// cubes
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   219
	Material mat5(Colour(1.0, 1.0, 1.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   220
	mat5.texture = new PlanarMapTexture(Vector3(4.0, 0.0, -7.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   221
	Box cube1(Vector3(4.0, 0.0, -7.0)-1.0, Vector3(4.0, 0.0, -7.0)+1.0, &mat5);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   222
	rt.addshape(&cube1);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   223
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   224
	Material mat6(Colour(1.0, 1.0, 1.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   225
	mat6.texture = new CubicMapTexture(Vector3(1.0, 0.0, -7.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   226
	Box cube2(Vector3(1.0, 0.0, -7.0)-1.0, Vector3(1.0, 0.0, -7.0)+1.0, &mat6);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   227
	rt.addshape(&cube2);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   228
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   229
	Material mat7(Colour(1.0, 1.0, 1.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   230
	mat7.texture = new CylinderMapTexture(Vector3(-2.0, 0.0, -7.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   231
	Box cube3(Vector3(-2.0, 0.0, -7.0)-1.0, Vector3(-2.0, 0.0, -7.0)+1.0, &mat7);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   232
	rt.addshape(&cube3);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   233
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   234
	Material mat8(Colour(1.0, 1.0, 1.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   235
	mat8.texture = new SphereMapTexture(Vector3(-5.0, 0.0, -7.0));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   236
	Box cube4(Vector3(-5.0, 0.0, -7.0)-1.0, Vector3(-5.0, 0.0, -7.0)+1.0, &mat8);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   237
	rt.addshape(&cube4);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   238
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   239
	mat1.setReflectivity(0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   240
	mat2.setReflectivity(0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   241
	mat3.setReflectivity(0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   242
	mat4.setReflectivity(0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   243
	mat5.setReflectivity(0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   244
	mat6.setReflectivity(0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   245
	mat7.setReflectivity(0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   246
	mat8.setReflectivity(0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   247
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   248
	top.optimize();
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   249
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   250
	cam.setEye(Vector3(-0.530505, 11.0964, 11.2208));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   251
	cam.p = Vector3(-4.18144e-08, -0.461779, -0.886995);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   252
	cam.u = Vector3(-1, 0, 6.3393e-11);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   253
	cam.v = Vector3(3.19387e-08, 0.886995, -0.461779);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   254
	rt.setCamera(&cam);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   255
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   256
	w = 1024;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   257
	h = 600;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   258
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   259
	/* run interactive mode */
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   260
	loop_sdl(rt, cam, update_callback, key_callback);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   261
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   262
	/* render image */
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   263
	if (argc == 2 && !strcmp(argv[1], "-r"))
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   264
	{
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   265
		pyrit_verbosity = 2;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   266
		Float *fdata = (Float *) malloc(w*h*3*sizeof(Float));
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   267
		rt.setOversample(2);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   268
		rt.setSubsample(1);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   269
		rt.render(w, h, fdata);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   270
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   271
		struct image *img;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   272
		new_image(&img, w, h, 3);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   273
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   274
		Float *fd = fdata;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   275
		for (char *cd = img->data; cd != img->data + w*h*3; cd++, fd++) {
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   276
			if (*fd > 1.0)
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   277
				*cd = 255;
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   278
			else
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   279
				*cd = (unsigned char)(*fd * 255.0);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   280
		}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   281
		free(fdata);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   282
		save_png("textures.png", img);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   283
		destroy_image(&img);
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   284
	}
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   285
}