Example #1
0
inline bool operator==(const Colour& a, const Colour& b)
{
    constexpr double localEpsilon = 0.05;
    return FastMath::IsNearly(a.R(), b.R(), localEpsilon)
           &&
           FastMath::IsNearly(a.G(), b.G(), localEpsilon)
           &&
           FastMath::IsNearly(a.B(), b.B(), localEpsilon);
}
Colour PhongMaterial::getColour(const Vector3D& normal, const Vector3D& viewDirection,
                                const std::list<Light*>& lights, const Colour& ambient, 
                                const Point3D& poi, const Primitive* primitive) const
{
  // Get diffuse coefficients
  Colour kd = getDiffuse(primitive, poi);

  // First add ambient light.
  Colour c = kd * ambient;

  for (std::list<Light*>::const_iterator it = lights.begin(); it != lights.end(); it++) {
    Light* light = (*it);

    Vector3D lightDirection = light->position - poi; // Note this needs to point towards the light.
    lightDirection.normalize();

    double dist = lightDirection.length();
    Vector3D r = -lightDirection + 2 * (lightDirection.dot(normal)) * normal;

    Colour contribution = (kd + m_ks * ( pow(r.dot(viewDirection), m_shininess) / normal.dot(lightDirection) )) *
                          light->colour * lightDirection.dot(normal) * 
                          (1 / (light->falloff[0] + light->falloff[1] * dist +
                                light->falloff[2] * dist * dist)); 

    // Ignore negative contributions
    if (contribution.R() >= 0 && contribution.G() >= 0 && contribution.B() >= 0) {
      c = c + contribution;
    }
  }

  return c;
}
Example #3
0
void SkyDome::setFog(char* filename){
  SDL_Surface* surface = SDL_LoadBMP(filename);
  m_fogColors = new Colour[surface->w];
  m_nFogColors = surface->w;
  for(int i=0; i<surface->w; i++){
    Colour color = getpixel(surface, i, surface->h-1);
    m_fogColors[i] = Colour(color.R()/255, color.G()/255, color.B()/255);
  }
}
Example #4
0
void TestScreen::Update(float dt)
{
	CSFMLTimer* timer = CEngine::GetInstance()->Timer();
	if (SFMLKeyboardHandler::Pressed(sf::Key::Escape))
	{
		exit(0);
	}
	if (SFMLKeyboardHandler::Pressed(sf::Key::Q) || SFMLMouseHandler::WheelDown())
	{
		timer->TimeScale(timer->TimeScale() - .1f);
	}
	if (SFMLKeyboardHandler::Pressed(sf::Key::E) || SFMLMouseHandler::WheelUp())
	{
		timer->TimeScale(timer->TimeScale() + .1f);
	}
	if (SFMLKeyboardHandler::Held(sf::Key::Space))
	{
		particleEmitter->Emit(1);
	}
	if (SFMLKeyboardHandler::Pressed(sf::Key::Z))
	{
		gameObject->RemoveComponent(GCIdType("VisualComponent"));
	}
	if (SFMLKeyboardHandler::Pressed(sf::Key::X))
	{
	}

	circleMod < M_PI * 2.f ? circleMod += 5.f * dt : circleMod -= (float)M_PI * 2.f;

	line2Start = SFMLMouseHandler::Position();

	Vector2f line1 = (line1End - line1Start).normalize();
	Vector2f line2 = (line2End - line2Start).normalize();

	float line1dotline2 = line1.dot(line2);
	float angle = acos(line1dotline2);
	angle *= 180.f/3.14159265358979323846f;
	angle = 180.f - angle;

	particleEmitter->Update(dt);

	Colour colour = Colour(.25f, .5f, 1.f, 1.f);
	float H, S, V;
	ColourUtils::RGB2HSV(colour, H, S, V);
	colour = ColourUtils::HSV2RGB(H, S, V);

	gameObject->Update(dt);
	
	testData = "TS: " + StringUtils::to_string(timer->TimeScale()) +
		" | UPS: " + StringUtils::to_string((int)(1.f/dt)) +
		" | DP: " + StringUtils::to_string(line1dotline2) +
		" | Angle: " + StringUtils::to_string(angle) +
		" | Particles: " + StringUtils::to_string(particleEmitter->Count()) +
		" | H: " + StringUtils::to_string(H) + " S: " + StringUtils::to_string(S) + " V: " + StringUtils::to_string(V) +
		" | R: " + StringUtils::to_string(colour.R()) + " G: " + StringUtils::to_string(colour.G()) + " B: " + StringUtils::to_string(colour.B());
}
Example #5
0
Hit Material::apply_normal_map(Hit h) {
  if (!m_has_normal_map) {
	return h;
  }
  int x = h.tex_coords[0] * m_normal_map.width();
  int y = (1 - h.tex_coords[1]) * m_normal_map.height();
  Colour c = Colour(m_normal_map(x, y, 0), m_normal_map(x, y, 1), m_normal_map(x, y, 2));

  // convert to surface normal;
  h.normal = Vector3D(2 * c.R() - 1.0, 2 * c.G() - 1.0, 2 * c.B() - 1.0);
  return h;
}
Example #6
0
void a4_render(// What to render
               SceneNode* root,
               // Where to output the image
               const std::string& filename,
               // Image size
               int width, int height,
               // Viewing parameters
               const Point3D& eye, const Vector3D& view,
               const Vector3D& up, double fov,
               // Lighting parameters
               const Colour& ambient,
               const std::list<Light*>& lights,
               double fogDist
               )
{
  // Fill in raytracing code here.

  std::cerr << "Stub: a4_render(" << root << ",\n     "
            << filename << ", " << width << ", " << height << ",\n     "
            << eye << ", " << view << ", " << up << ", " << fov << ",\n     "
            << ambient << ",\n     {";

  for (std::list<Light*>::const_iterator I = lights.begin(); I != lights.end(); ++I) {
    if (I != lights.begin()) std::cerr << ", ";
    std::cerr << **I;
  }
  std::cerr << "});" << std::endl;

  Vector3D viewVector = view;
  Vector3D upVector = up;
  Vector3D sideVector = viewVector.cross(upVector);
  viewVector.normalize();
  upVector.normalize();
  sideVector.normalize();
  
  Image img(width, height, 3);

  int progress = 0;
  int numPixels = width*height;
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      int newProgress = (int)((double)(y*width + x)/numPixels*100);
      if(newProgress >= progress+5){
        progress = newProgress;
        std::cerr << progress << std::endl;
      }

      double d = height/2.0/tan(toRadian(fov)/2);
      Vector3D dir = (x-(width/2.0))*sideVector + 
          ((height/2.0)-y)*upVector + d*viewVector;
      dir.normalize();
      Ray ray = Ray(eye, dir);

      bool fogOn = true;
      if(fogDist <= 0)
        fogOn = false;
      Colour* color = rayTrace(ambient, eye, ray, root, lights, 5, fogDist);
      Colour fog(1,1,1);
      if(color == NULL) {
        // sunset colours
        Colour horizon(0.94, 0.55, 0.05);
        Colour zenith(0.2, 0.27, 0.4);
        Colour bg = zenith*(1-(double)y/height) + horizon*((double)y/height);
        if(fogOn)
          color = new Colour(0.8,0.8,0.8);
        else
          color = new Colour(bg);
      }

      img(x, y, 0) = color->R();
      img(x, y, 1) = color->G();
      img(x, y, 2) = color->B();
    }
  }
  img.savePng(filename);
  
}
Example #7
0
inline Colour operator +(const Colour& a, const Colour& b)
{
  return Colour(a.R()+b.R(), a.G()+b.G(), a.B()+b.B());
}
Example #8
0
inline Colour operator *(double s, const Colour& a)
{
  return Colour(s*a.R(), s*a.G(), s*a.B());
}
Example #9
0
inline Colour operator +(const Colour& a, const Colour& b)
{
  return Colour(a.R()+b.R(), a.G()+b.G(), a.B()+b.B());
  //return Colour(1.0 - (1.0-a.R())*(1.0-b.R()), 1.0 - (1.0-a.G())*(1.0-b.G()), 
  // 1.0 - (1.0-a.B())*(1.0-b.B()));
}
Example #10
0
void SkyDome::draw(int time){
  
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, m_gradient);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

  float U = (float)time/(CYCLE);

  for(int i=0; i<m_nLat; i++){
    for(int j=0; j<m_nLong; j++){
      int a = i*(m_nLong+1) + j;
      int b = a + (m_nLong+1);


      float VTop = (float)(i+1)*2/(m_nLat+3);
      float VBot = (float)(i+2)*2/(m_nLat+3);

      glBegin(GL_TRIANGLES);
      glTexCoord2f(U,VTop);
      glVertex3d(m_vertices[a][0], m_vertices[a][1], m_vertices[a][2]);
      glTexCoord2f(U,VBot);
      glVertex3d(m_vertices[b][0], m_vertices[b][1], m_vertices[b][2]);
      glTexCoord2f(U,VTop);
      glVertex3d(m_vertices[a+1][0], m_vertices[a+1][1], m_vertices[a+1][2]);
      glEnd();

      glBegin(GL_TRIANGLES);
      glTexCoord2f(U,VBot);
      glVertex3d(m_vertices[b][0], m_vertices[b][1], m_vertices[b][2]);
      glVertex3d(m_vertices[b+1][0], m_vertices[b+1][1], m_vertices[b+1][2]);
      glTexCoord2f(U,VTop);
      glVertex3d(m_vertices[a+1][0], m_vertices[a+1][1], m_vertices[a+1][2]);
      glEnd();
    }
  }

  glDisable(GL_TEXTURE_2D);

  // calc sun position
  Point3D* sunPos = getSunPos(time);
  Point3D* moonPos = getMoonPos(time);

  //TODO calc sun color and size
  double sina = sin(getT(time, 0.1, 0.55)*M_PI);
  int sunSize = 2500 - sina*1200;
  Colour sunColor = Colour(1,0.85,0.7)*(1-sina) + Colour(1,1,1)*sina;


  // draw sun
  GLUquadricObj* qsphere = gluNewQuadric();

  gluQuadricDrawStyle(qsphere, GLU_FILL);
  gluQuadricOrientation(qsphere, GLU_OUTSIDE);
  gluQuadricTexture(qsphere, GL_FALSE);

  glColor3f(sunColor.R(),sunColor.G(),sunColor.B());
  glPushMatrix();
  glTranslatef((*sunPos)[0],(*sunPos)[1],(*sunPos)[2]);
  gluSphere(qsphere, sunSize, 20, 20);
  glPopMatrix();

  // draw moon
  GLUquadricObj* qsphere2 = gluNewQuadric();

  gluQuadricDrawStyle(qsphere2, GLU_FILL);
  gluQuadricOrientation(qsphere2, GLU_OUTSIDE);
  gluQuadricTexture(qsphere2, GL_FALSE);

  glColor3f(1,0.9,0.6);
  glPushMatrix();
  glTranslatef((*moonPos)[0],(*moonPos)[1],(*moonPos)[2]);
  gluSphere(qsphere2, 1000, 20, 20);
  glPopMatrix();

}
Example #11
0
inline Colour operator/(const Colour& a, const double& b)
{
    return Colour(a.R() / b, a.G() / b, a.B() / b);
}
inline Colour operator *(const Colour& a, const Colour& b)
{
  return Colour(a.R()*b.R(), a.G()*b.G(), a.B()*b.B(), a.A()*b.A());
}
inline Colour operator /(const Colour& a, double s)
{
  return Colour(a.R()/s, a.G()/s, a.B()/s, a.A()/s);
}