clray

changeset 20:63a6b46f58a0

fixed
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Aug 2010 12:55:40 +0100
parents 8baea9b66b50
children bd6c2b25f6e7
files rt.cl src/clray.cc src/mesh.cc src/rt.cc src/rt.h
diffstat 5 files changed, 36 insertions(+), 88 deletions(-) [+]
line diff
     1.1 --- a/rt.cl	Mon Aug 09 06:45:57 2010 +0100
     1.2 +++ b/rt.cl	Mon Aug 09 12:55:40 2010 +0100
     1.3 @@ -5,7 +5,6 @@
     1.4  	int num_faces, num_lights;
     1.5  	int max_iter;
     1.6  	float4 ambient;
     1.7 -	int dbg;
     1.8  };
     1.9  
    1.10  struct Vertex {
    1.11 @@ -56,7 +55,6 @@
    1.12  #define MIN_ENERGY	0.001
    1.13  #define EPSILON		1e-6
    1.14  
    1.15 -//float4 trace(struct Ray ray, struct Scene *scn);
    1.16  float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp);
    1.17  bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *sp);
    1.18  bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp);
    1.19 @@ -74,17 +72,10 @@
    1.20  		global const struct Light *lights,
    1.21  		global const struct Ray *primrays,
    1.22  		global const float *xform,
    1.23 -		global const float *invtrans,
    1.24 -		global struct Face *outfaces)
    1.25 +		global const float *invtrans)
    1.26  {
    1.27  	int idx = get_global_id(0);
    1.28  
    1.29 -	if(!idx) {
    1.30 -		for(int i=0; i<rinf->num_faces; i++) {
    1.31 -			outfaces[i] = faces[i];
    1.32 -		}
    1.33 -	}
    1.34 -
    1.35  	struct Scene scn;
    1.36  	scn.ambient = rinf->ambient;
    1.37  	scn.faces = faces;
    1.38 @@ -96,8 +87,6 @@
    1.39  	struct Ray ray = primrays[idx];
    1.40  	transform_ray(&ray, xform, invtrans);
    1.41  
    1.42 -	//fb[idx] = trace(ray, &scn);
    1.43 -
    1.44  	float4 pixel = (float4)(0, 0, 0, 0);
    1.45  	float4 energy = (float4)(1.0, 1.0, 1.0, 1.0);
    1.46  	int iter = 0;
    1.47 @@ -121,19 +110,6 @@
    1.48  	fb[idx] = pixel;
    1.49  }
    1.50  
    1.51 -/*float4 trace(struct Ray ray, struct Scene *scn)
    1.52 -{
    1.53 -	float4 color;
    1.54 -	struct SurfPoint sp;
    1.55 -
    1.56 -	if(find_intersection(ray, scn, &sp)) {
    1.57 -		color = shade(ray, scn, &sp);
    1.58 -	} else {
    1.59 -		color = (float4)(0, 0, 0, 0);
    1.60 -	}
    1.61 -	return color;
    1.62 -}*/
    1.63 -
    1.64  float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp)
    1.65  {
    1.66  	float4 norm = sp->norm;
    1.67 @@ -162,8 +138,8 @@
    1.68  			float diff = fmax(dot(ldir, norm), 0.0f);
    1.69  			dcol += sp->mat.kd * diff * scn->lights[i].color;
    1.70  
    1.71 -			//float spec = powr(fmax(dot(ldir, vref), 0.0f), mat.spow);
    1.72 -			//scol += sp->mat.ks * spec * scn->lights[i].color;
    1.73 +			float spec = powr(fmax(dot(ldir, vref), 0.0f), sp->mat.spow);
    1.74 +			scol += sp->mat.ks * spec * scn->lights[i].color;
    1.75  		}
    1.76  	}
    1.77  
    1.78 @@ -221,14 +197,14 @@
    1.79  	float4 bc = calc_bary(pt, face, norm);
    1.80  	float bc_sum = bc.x + bc.y + bc.z;
    1.81  
    1.82 -	if(bc_sum < 0.0 || bc_sum > 1.0 + EPSILON) {
    1.83 +	if(bc_sum < 1.0 - EPSILON || bc_sum > 1.0 + EPSILON) {
    1.84  		return false;
    1.85  		bc *= 1.2;
    1.86  	}
    1.87  
    1.88  	sp->t = t;
    1.89  	sp->pos = pt;
    1.90 -	sp->norm = norm;
    1.91 +	sp->norm = -normalize(face->v[0].normal * bc.x + face->v[1].normal * bc.y + face->v[2].normal * bc.z);
    1.92  	sp->obj = face;
    1.93  	sp->dbg = bc;
    1.94  	return true;
     2.1 --- a/src/clray.cc	Mon Aug 09 06:45:57 2010 +0100
     2.2 +++ b/src/clray.cc	Mon Aug 09 12:55:40 2010 +0100
     2.3 @@ -26,7 +26,6 @@
     2.4  static float cam_dist = 10.0;
     2.5  
     2.6  static bool dbg_glrender = true;
     2.7 -static int dbg_dbg = 8;
     2.8  
     2.9  static Scene scn;
    2.10  
    2.11 @@ -173,18 +172,6 @@
    2.12  		glutPostRedisplay();
    2.13  		break;
    2.14  
    2.15 -	case '=':
    2.16 -		dbg_set_dbg(++dbg_dbg);
    2.17 -		need_update = true;
    2.18 -		glutPostRedisplay();
    2.19 -		break;
    2.20 -
    2.21 -	case '-':
    2.22 -		dbg_set_dbg(--dbg_dbg);
    2.23 -		need_update = true;
    2.24 -		glutPostRedisplay();
    2.25 -		break;
    2.26 -
    2.27  	default:
    2.28  		break;
    2.29  	}
     3.1 --- a/src/mesh.cc	Mon Aug 09 06:45:57 2010 +0100
     3.2 +++ b/src/mesh.cc	Mon Aug 09 12:55:40 2010 +0100
     3.3 @@ -59,6 +59,8 @@
     3.4  
     3.5  	Vector3() { x = y = z = 0.0; }
     3.6  	Vector3(float a, float b, float c) { x = a; y = b; z = c; }
     3.7 +
     3.8 +	void normalize() { float len = sqrt(x * x + y * y + z * z); x /= len; y /= len; z /= len; }
     3.9  };
    3.10  
    3.11  struct Vector2 {
    3.12 @@ -117,6 +119,9 @@
    3.13  static bool find_file(char *res, int sz, const char *fname, const char *path = ".", const char *mode = "rb");
    3.14  static const char *dirname(const char *str);
    3.15  
    3.16 +static Vector3 operator -(const Vector3 &a, const Vector3 &b);
    3.17 +static Vector3 cross(const Vector3 &a, const Vector3 &b);
    3.18 +
    3.19  static map<string, int> matnames;
    3.20  
    3.21  
    3.22 @@ -406,13 +411,14 @@
    3.23  
    3.24  	for(size_t i=0; i<obj->f.size(); i++) {
    3.25  		Face face;
    3.26 +		Vector3 v[3];
    3.27  
    3.28  		for(int j=0; j<3; j++) {
    3.29  			obj_face *f = &obj->f[i];
    3.30  
    3.31 -			face.v[j].pos[0] = obj->v[f->v[j]].x;
    3.32 -			face.v[j].pos[1] = obj->v[f->v[j]].y;
    3.33 -			face.v[j].pos[2] = obj->v[f->v[j]].z;
    3.34 +			face.v[j].pos[0] = v[j].x = obj->v[f->v[j]].x;
    3.35 +			face.v[j].pos[1] = v[j].y = obj->v[f->v[j]].y;
    3.36 +			face.v[j].pos[2] = v[j].z = obj->v[f->v[j]].z;
    3.37  			face.v[j].pos[3] = 0.0;
    3.38  
    3.39  			int nidx = f->n[j] < 0 ? 0 : f->n[j];
    3.40 @@ -426,9 +432,14 @@
    3.41  			face.v[j].tex[1] = obj->vt[tidx].y;
    3.42  		}
    3.43  
    3.44 -		face.normal[0] = face.v[0].normal[0];
    3.45 -		face.normal[1] = face.v[1].normal[1];
    3.46 -		face.normal[2] = face.v[2].normal[2];
    3.47 +		Vector3 a = v[1] - v[0];
    3.48 +		Vector3 b = v[2] - v[0];
    3.49 +		Vector3 n = cross(a, b);
    3.50 +		n.normalize();
    3.51 +
    3.52 +		face.normal[0] = n.x;
    3.53 +		face.normal[1] = n.y;
    3.54 +		face.normal[2] = n.z;
    3.55  		face.normal[3] = 0.0;
    3.56  
    3.57  		mesh->faces.push_back(face);
    3.58 @@ -729,3 +740,17 @@
    3.59  	}
    3.60  	return buf;
    3.61  }
    3.62 +
    3.63 +static Vector3 operator -(const Vector3 &a, const Vector3 &b)
    3.64 +{
    3.65 +	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    3.66 +}
    3.67 +
    3.68 +static Vector3 cross(const Vector3 &a, const Vector3 &b)
    3.69 +{
    3.70 +	Vector3 res;
    3.71 +	res.x = a.y * b.z - a.z * b.y;
    3.72 +	res.y = a.z * b.x - a.x * b.z;
    3.73 +	res.z = a.x * b.y - a.y * b.x;
    3.74 +	return res;
    3.75 +}
     4.1 --- a/src/rt.cc	Mon Aug 09 06:45:57 2010 +0100
     4.2 +++ b/src/rt.cc	Mon Aug 09 12:55:40 2010 +0100
     4.3 @@ -16,7 +16,6 @@
     4.4  	KARG_PRIM_RAYS,
     4.5  	KARG_XFORM,
     4.6  	KARG_INVTRANS_XFORM,
     4.7 -	KARG_OUTFACES,	/* DBG */
     4.8  
     4.9  	NUM_KERNEL_ARGS
    4.10  };
    4.11 @@ -26,7 +25,6 @@
    4.12  	int num_faces, num_lights;
    4.13  	int max_iter;
    4.14  	float ambient[4];
    4.15 -	int dbg;
    4.16  };
    4.17  
    4.18  struct Ray {
    4.19 @@ -64,7 +62,6 @@
    4.20  	rinf.num_faces = scn->get_num_faces();
    4.21  	rinf.num_lights = sizeof lightlist / sizeof *lightlist;
    4.22  	rinf.max_iter = 6;
    4.23 -	rinf.dbg = 8;
    4.24  
    4.25  	/* calculate primary rays */
    4.26  	prim_rays = new Ray[xsz * ysz];
    4.27 @@ -96,7 +93,6 @@
    4.28  	prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
    4.29  	prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
    4.30  	prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
    4.31 -	prog->set_arg_buffer(KARG_OUTFACES, ARG_WR, rinf.num_faces * sizeof(Face));
    4.32  
    4.33  	if(prog->get_num_args() < NUM_KERNEL_ARGS) {
    4.34  		return false;
    4.35 @@ -126,19 +122,6 @@
    4.36  	}
    4.37  	printf("done\n");
    4.38  
    4.39 -	/* DEBUG */
    4.40 -	CLMemBuffer *dbgbuf = prog->get_arg_buffer(KARG_OUTFACES);
    4.41 -	Face *outfaces = (Face*)map_mem_buffer(dbgbuf, MAP_RD);
    4.42 -	for(int i=0; i<rinf.num_faces; i++) {
    4.43 -		if(!(faces[i] == outfaces[i])) {
    4.44 -			fprintf(stderr, "SKATA %d\n", i);
    4.45 -			return false;
    4.46 -		}
    4.47 -		faces[i] = outfaces[i];
    4.48 -	}
    4.49 -	printf("equality test passed\n");
    4.50 -	unmap_mem_buffer(dbgbuf);
    4.51 -
    4.52  
    4.53  	CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_FRAMEBUFFER);
    4.54  	void *fb = map_mem_buffer(mbuf, MAP_RD);
    4.55 @@ -152,16 +135,6 @@
    4.56  	return true;
    4.57  }
    4.58  
    4.59 -void dbg_set_dbg(int dbg)
    4.60 -{
    4.61 -	printf("setting dbg: %d\n", dbg);
    4.62 -
    4.63 -	CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_RENDER_INFO);
    4.64 -	RendInfo *rinf = (RendInfo*)map_mem_buffer(mbuf, MAP_WR);
    4.65 -	rinf->dbg = dbg;
    4.66 -	unmap_mem_buffer(mbuf);
    4.67 -}
    4.68 -
    4.69  void dbg_render_gl(Scene *scn)
    4.70  {
    4.71  	float lpos[] = {-1, 1, 10, 0};
    4.72 @@ -199,18 +172,6 @@
    4.73  			glVertex3fv(pos);
    4.74  		}
    4.75  	}
    4.76 -
    4.77 -	/*for(size_t i=0; i<scn->meshes.size(); i++) {
    4.78 -		Material *mat = &scn->matlib[scn->meshes[i]->matid];
    4.79 -
    4.80 -		glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
    4.81 -		for(size_t j=0; j<scn->meshes[i]->faces.size(); j++) {
    4.82 -			for(int k=0; k<3; k++) {
    4.83 -				float *pos = scn->meshes[i]->faces[j].v[k].pos;
    4.84 -				glVertex3f(pos[0], pos[1], pos[2]);
    4.85 -			}
    4.86 -		}
    4.87 -	}*/
    4.88  	glEnd();
    4.89  
    4.90  	glPopMatrix();
     5.1 --- a/src/rt.h	Mon Aug 09 06:45:57 2010 +0100
     5.2 +++ b/src/rt.h	Mon Aug 09 12:55:40 2010 +0100
     5.3 @@ -8,7 +8,6 @@
     5.4  bool render();
     5.5  void set_xform(float *matrix, float *invtrans);
     5.6  
     5.7 -void dbg_set_dbg(int dbg);
     5.8  void dbg_render_gl(Scene *scn);
     5.9  
    5.10  #endif	/* RT_H_ */