Newer
Older
cg / sources / light.cpp
@glproj03 glproj03 on 2 Feb 2006 2 KB es werde licht
#include "light.h"

#include <GL/gl.h>
#include <GL/glu.h>

Light::Light() {
  //TODO: this class should be abstract
  printf("light not initialized");
}

void Light::enable() {
  enabled = true;
  glEnable(num);
}

void Light::disable() {
  enabled = false;
  glDisable(num);
}

void Light::flip() {
  if (enabled)
	disable();
  else
	enable();
}


DiffuseLight::DiffuseLight(Vec& _pos, Color& _color, GLenum _lightnum) {  
  pos = _pos.clone();
  col = _color.clone();
  num = _lightnum;

  glLightfv(num, GL_DIFFUSE, col->v());
  GLfloat LightPosition[]= { pos->x(), pos->y(), pos->z(), 1.0f }; 
  glLightfv(num, GL_POSITION, LightPosition);
  enable();
}



SpecularLight::SpecularLight(Vec& _pos, Color& _color, GLenum _lightnum) {  
  pos = _pos.clone();
  col = _color.clone();
  num = _lightnum;

  glLightfv(num, GL_SPECULAR, col->v());		
  GLfloat LightPosition[]= { pos->x(), pos->y(), pos->z(), 1.0f }; 
  glLightfv(num, GL_POSITION,LightPosition);	
  enable();
}



SpotLight::SpotLight(Vec& _pos, Color& _color, GLenum _lightnum, float _angle) {  
  pos = _pos.clone();
  col = _color.clone();
  num = _lightnum;
  angle = _angle;

  glLightfv(num, GL_DIFFUSE, col->v());
  //glLightfv(num, GL_AMBIENT, col->v());
  //glTranslate(pos->x(), pos->y(), pos->z(), 1);
  //float p[] = {3,3,3, 1.0f};
  //glLightfv(num, GL_POSITION,p);

  //GLfloat dir[] = {2,-1,-2};



  //glLightf(num,GL_QUADRATIC_ATTENUATION,.00001f);
  repr = gluNewQuadric();

  enable();
  delta=0;
}

void SpotLight::draw() {
  //save camera position
  glPushMatrix();
  delta += 1;

  float p[] = {0.0f, 0.0f, 0.0f, 1.0f};
  GLfloat dir[] = {0.0f, -1.0f, 0.0f, 1.0f};

  glTranslatef(pos->x(), pos->y(), pos->z());

  glLightf(num, GL_SPOT_CUTOFF, angle);	
  glLightfv(num, GL_POSITION, p);
  glLightfv(num, GL_SPOT_DIRECTION, dir);
 

  //sphere
  glPushMatrix();
  glTranslatef(0,-120,0);
  gluSphere(gluNewQuadric(), 100, 300, 300);		
  glPopMatrix();

  //line
  glBegin(GL_LINES);
  float mat[] = {0,1,1};
  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat);
  glVertex3f(p[0], p[1], p[2]);
  glVertex3fv(dir);
  glEnd();

  //cone
  glPushMatrix();
  glTranslatef(0,5.5,0);
  glRotatef(90, 1, 0, 0);
  gluCylinder (repr, 0, 1, 5, 10, 20);
  glPopMatrix();
  //gluCylinder (repr, 0, 20, 20, 10, 20);

  glPopMatrix();
}



AmbientLight::AmbientLight(Color& _color, GLenum _lightnum) {
  col = _color.clone();
  num = _lightnum;
  glLightfv(num, GL_AMBIENT, col->v());
  enable();
}