projects
/
csgray
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
added cylinder primitive
[csgray]
/
src
/
csgray.c
diff --git
a/src/csgray.c
b/src/csgray.c
index
150f5fb
..
ae0f8ad
100644
(file)
--- a/
src/csgray.c
+++ b/
src/csgray.c
@@
-44,12
+44,30
@@
void csg_destroy(void)
void csg_view(float x, float y, float z, float tx, float ty, float tz)
{
void csg_view(float x, float y, float z, float tx, float ty, float tz)
{
+ float dir[3];
+ float len;
+
cam.x = x;
cam.y = y;
cam.z = z;
cam.tx = tx;
cam.ty = ty;
cam.tz = tz;
cam.x = x;
cam.y = y;
cam.z = z;
cam.tx = tx;
cam.ty = ty;
cam.tz = tz;
+
+ dir[0] = tx - x;
+ dir[1] = ty - y;
+ dir[2] = tz - z;
+ len = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
+
+ if(1.0f - fabs(ty - y) / len < 1e-6f) {
+ cam.ux = cam.uy = 0.0f;
+ cam.uz = -1.0f;
+ } else {
+ cam.ux = cam.uz = 0.0f;
+ cam.uy = 1.0f;
+ }
+
+ mat4_lookat(cam.xform, x, y, z, tx, ty, tz, cam.ux, cam.uy, cam.uz);
}
void csg_fov(float fov)
}
void csg_fov(float fov)
@@
-135,10
+153,12
@@
int csg_remove_object(csg_object *o)
void csg_free_object(csg_object *o)
{
void csg_free_object(csg_object *o)
{
- if(o->ob.destroy) {
- o->ob.destroy(o);
+ if(o) {
+ if(o->ob.destroy) {
+ o->ob.destroy(o);
+ }
+ free(o);
}
}
- free(o);
}
static union csg_object *alloc_object(int type)
}
static union csg_object *alloc_object(int type)
@@
-169,6
+189,7
@@
csg_object *csg_null(float x, float y, float z)
}
mat4_translation(o->ob.xform, x, y, z);
}
mat4_translation(o->ob.xform, x, y, z);
+ mat4_translation(o->ob.inv_xform, -x, -y, -z);
return o;
}
return o;
}
@@
-182,25
+203,27
@@
csg_object *csg_sphere(float x, float y, float z, float r)
o->sph.rad = r;
mat4_translation(o->ob.xform, x, y, z);
o->sph.rad = r;
mat4_translation(o->ob.xform, x, y, z);
- mat4_copy(o->ob.inv_xform, o->ob.xform);
- mat4_inverse(o->ob.inv_xform);
+ mat4_translation(o->ob.inv_xform, -x, -y, -z);
return o;
}
csg_object *csg_cylinder(float x0, float y0, float z0, float x1, float y1, float z1, float r)
{
csg_object *o;
return o;
}
csg_object *csg_cylinder(float x0, float y0, float z0, float x1, float y1, float z1, float r)
{
csg_object *o;
- float dx, dy, dz;
+ float x, y, z, dx, dy, dz;
int major;
if(!(o = alloc_object(OB_CYLINDER))) {
return 0;
}
int major;
if(!(o = alloc_object(OB_CYLINDER))) {
return 0;
}
+ o->cyl.rad = r;
dx = x1 - x0;
dy = y1 - y0;
dz = z1 - z0;
dx = x1 - x0;
dy = y1 - y0;
dz = z1 - z0;
+ o->cyl.height = sqrt(dx * dx + dy * dy + dz * dz);
+
if(fabs(dx) > fabs(dy) && fabs(dx) > fabs(dz)) {
major = 0;
} else if(fabs(dy) > fabs(dz)) {
if(fabs(dx) > fabs(dy) && fabs(dx) > fabs(dz)) {
major = 0;
} else if(fabs(dy) > fabs(dz)) {
@@
-209,8
+232,11
@@
csg_object *csg_cylinder(float x0, float y0, float z0, float x1, float y1, float
major = 2;
}
major = 2;
}
- o->cyl.rad = r;
- mat4_lookat(o->ob.xform, x0, y0, z0, x1, y1, z1, 0, major == 2 ? 1 : 0, major == 2 ? 0 : 1);
+ x = (x0 + x1) / 2.0f;
+ y = (y0 + y1) / 2.0f;
+ z = (z0 + z1) / 2.0f;
+
+ mat4_lookat(o->ob.xform, x, y, z, dx, dz, -dy, 0, major == 2 ? 0 : 1, major == 2 ? 1 : 0);
mat4_copy(o->ob.inv_xform, o->ob.xform);
mat4_inverse(o->ob.inv_xform);
return o;
mat4_copy(o->ob.inv_xform, o->ob.xform);
mat4_inverse(o->ob.inv_xform);
return o;
@@
-236,13
+262,28
@@
csg_object *csg_plane(float x, float y, float z, float nx, float ny, float nz)
o->plane.nx = nx;
o->plane.ny = ny;
o->plane.nz = nz;
o->plane.nx = nx;
o->plane.ny = ny;
o->plane.nz = nz;
- o->plane.d = x * nx + y * ny + z * nz;
+ o->plane.d = 0.0f;
+
+ mat4_translation(o->ob.xform, x, y, z);
+ mat4_translation(o->ob.inv_xform, -x, -y, -z);
return o;
}
csg_object *csg_box(float x, float y, float z, float xsz, float ysz, float zsz)
{
return o;
}
csg_object *csg_box(float x, float y, float z, float xsz, float ysz, float zsz)
{
- return 0;
+ csg_object *o;
+
+ if(!(o = alloc_object(OB_BOX))) {
+ return 0;
+ }
+
+ o->box.xsz = xsz;
+ o->box.ysz = ysz;
+ o->box.zsz = zsz;
+
+ mat4_translation(o->ob.xform, x, y, z);
+ mat4_translation(o->ob.inv_xform, -x, -y, -z);
+ return o;
}
csg_object *csg_union(csg_object *a, csg_object *b)
}
csg_object *csg_union(csg_object *a, csg_object *b)
@@
-351,6
+392,8
@@
void csg_render_pixel(int x, int y, int width, int height, float aspect, float *
{
struct ray ray;
{
struct ray ray;
+ csg_dbg_pixel = (x == 400 && y == 186);
+
calc_primary_ray(&ray, x, y, width, height, aspect);
ray_trace(&ray, color);
}
calc_primary_ray(&ray, x, y, width, height, aspect);
ray_trace(&ray, color);
}
@@
-370,14
+413,15
@@
void csg_render_image(float *pixels, int width, int height)
static void calc_primary_ray(struct ray *ray, int x, int y, int w, int h, float aspect)
{
static void calc_primary_ray(struct ray *ray, int x, int y, int w, int h, float aspect)
{
- /* TODO */
ray->dx = aspect * ((float)x / (float)w * 2.0f - 1.0f);
ray->dy = 1.0f - (float)y / (float)h * 2.0f;
ray->dz = -1.0f / tan(cam.fov * 0.5f);
ray->dx = aspect * ((float)x / (float)w * 2.0f - 1.0f);
ray->dy = 1.0f - (float)y / (float)h * 2.0f;
ray->dz = -1.0f / tan(cam.fov * 0.5f);
- ray->x = cam.x;
- ray->y = cam.y;
- ray->z = cam.z;
+ ray->x = 0;
+ ray->y = 0;
+ ray->z = 0;
+
+ xform_ray(ray, cam.xform);
}
static int ray_trace(struct ray *ray, float *col)
}
static int ray_trace(struct ray *ray, float *col)
@@
-423,7
+467,7
@@
static void shade(float *col, struct ray *ray, struct hit *hit)
sray.dy = ldir[1];
sray.dz = ldir[2];
sray.dy = ldir[1];
sray.dz = ldir[2];
- if(!find_intersection(&sray, &tmphit) || tmphit.t > 1.0f) {
+ if(!find_intersection(&sray, &tmphit) || tmphit.t < 0.000001 || tmphit.t > 1.0f) {
if((len = sqrt(ldir[0] * ldir[0] + ldir[1] * ldir[1] + ldir[2] * ldir[2])) != 0.0f) {
float s = 1.0f / len;
ldir[0] *= s;
if((len = sqrt(ldir[0] * ldir[0] + ldir[1] * ldir[1] + ldir[2] * ldir[2])) != 0.0f) {
float s = 1.0f / len;
ldir[0] *= s;
@@
-540,6
+584,13
@@
static csg_object *load_object(struct ts_node *node)
goto err;
}
goto err;
}
+ } else if(strcmp(node->name, "cylinder") == 0) {
+ float rad = ts_get_attr_num(node, "radius", 1.0f);
+ float height = ts_get_attr_num(node, "height", 1.0f);
+ if(!(o = csg_cylinder(0, -height/2.0f, 0, 0, height/2.0f, 0, rad))) {
+ goto err;
+ }
+
} else if(strcmp(node->name, "plane") == 0) {
static float def_norm[] = {0, 1, 0};
float *norm = ts_get_attr_vec(node, "normal", def_norm);
} else if(strcmp(node->name, "plane") == 0) {
static float def_norm[] = {0, 1, 0};
float *norm = ts_get_attr_vec(node, "normal", def_norm);