Example #1
0
File: main.cpp Project: r4ccoon/CG
void initLights1()
{
	Light *l;
	l = new Light(Vec3d(4,3,2));
	l->Intensity(0.288675);
	rt->AddLight(l);
	l = new Light(Vec3d(1,-4,4));
	l->Intensity(0.288675);
	rt->AddLight(l);
	l = new Light(Vec3d(-3,1,5));
	l->Intensity(0.288675);
	rt->AddLight(l);
}
Example #2
0
SEXP R_igraph_getsphere(SEXP pos, SEXP radius, SEXP color, SEXP bgcolor,
			SEXP lightpos, SEXP lightcolor, SEXP width,
			SEXP height) {

  /* All error checking is done at the R level */

  int i;
  double *spos=REAL(pos);
  double *scolor=REAL(color);
  double *svgcolor=REAL(bgcolor);
  int no_lights=GET_LENGTH(lightpos); 
  RayTracer* p_ray_tracer;
  Sphere * sphere;
  int swidth=INTEGER(width)[0];
  int sheight=INTEGER(height)[0];
  int nopixels=swidth * sheight;
  SEXP result, dim;
  Image image;
  
  p_ray_tracer = new RayTracer();
  p_ray_tracer->EyePoint(Point(0,0,0));  
  
  for (i=0; i<no_lights; i++) {
    double *lpos=REAL(VECTOR_ELT(lightpos, i));
    double *lcol=REAL(VECTOR_ELT(lightcolor, i));
    Light *light = new Light(Point(lpos[0], lpos[1], lpos[2]));
    light->Intensity(1);
    light->LightColor(Color(lcol[0], lcol[1], lcol[2]));
    p_ray_tracer->AddLight(light);
  }
  
  sphere = new Sphere(Point(spos[0], spos[1], spos[2]), REAL(radius)[0]);
  sphere->ShapeColor(Color(scolor[0], scolor[1], scolor[2]));
  p_ray_tracer->AddShape(sphere);

  PROTECT(result=NEW_NUMERIC(nopixels * 4));
  PROTECT(dim=NEW_INTEGER(3));
  INTEGER(dim)[0]=swidth; INTEGER(dim)[1]=sheight; INTEGER(dim)[2]=4;
  SET_DIM(result, dim);
  
  image.width=swidth;
  image.height=sheight;
  image.red=REAL(result);
  image.green=image.red + nopixels;
  image.blue=image.green + nopixels;
  image.trans=image.blue + nopixels;

  p_ray_tracer->RayTrace(image);
  delete p_ray_tracer;
  
  UNPROTECT(2);
  return result;
}
void
TerrainPatch::Illuminate(Color ambient, List<Light>& lights)
{
	if (!model || model->NumVerts() < 1) return;
	Surface* s = model->GetSurfaces().first();
	if (!s) return;

	illuminating = true;

	// clear the solid lights to ambient:
	VertexSet*  vset   = s->GetVertexSet();
	int         nverts = vset->nverts;
	DWORD       aval   = ambient.Value();

	for (int i = 0; i < nverts; i++) {
		vset->diffuse[i]  = aval;
	}

	TerrainRegion* trgn     = terrain->GetRegion();
	bool           eclipsed = false;
	bool           first    = terrain->IsFirstPatch(this);

	if (trgn && !first) {
		eclipsed = trgn->IsEclipsed();
	}

	// for sun and back lights:
	ListIter<Light> iter = lights;
	while (++iter) {
		Light* light = iter.value();

		if (!light->IsDirectional())                                // only do sun and
		continue;                                                // back lights

		if (light->CastsShadow() && first) {
			eclipsed  = light->Location().y < -100 ||                // has sun set, or

			scene->IsLightObscured(vset->loc[0],         // is sun in eclipse
			light->Location(),    // by orbital body
			radius);              // such as a moon?
		}

		if (!light->CastsShadow() || !eclipsed) {
			Vec3 vl = light->Location();
			vl.Normalize();

			for (int i = 0; i < nverts; i++) {
				Vec3&  nrm = vset->nrm[i];
				double val = 0;
				double gain = vl * nrm;

				if (gain > 0) {
					val = light->Intensity() * (0.85 * gain);

					if (val > 1)
					val = 1;
				}

				if (val > 0.01)
				vset->diffuse[i] = ((light->GetColor().dim(val)) + vset->diffuse[i]).Value();
			}
		}
	}

	// combine blend weights:
	if (ndetail >= 2) {
		for (int i = 0; i < nverts; i++) {
			vset->diffuse[i] = vset->specular[i] | (vset->diffuse[i] & 0x00ffffff);
		}
	}

	if (trgn && first) {
		trgn->SetEclipsed(eclipsed);
	}

	InvalidateSurfaceData();
	illuminating = false;
}