void normal_extension::update()
{
	int i = 0;

	m_normals.clear();
	
	for (ldraw::model::const_iterator it = m_model->elements().begin(); it != m_model->elements().end(); ++it) {
		ldraw::vector v1, v2, v3;
		ldraw::vector nvec;

		switch ((*it)->get_type()) {
			case ldraw::type_triangle:
			{
				const ldraw::element_triangle *t = CAST_AS_CONST_TRIANGLE(*it);
				nvec = calculate_normal(t->pos1(), t->pos2(), t->pos3());
				break;
			}
			case ldraw::type_quadrilateral:
			{
				const ldraw::element_quadrilateral *t = CAST_AS_CONST_QUADRILATERAL(*it);
				nvec = calculate_normal(t->pos1(), t->pos2(), t->pos3());
				break;
			}
			default:
				++i;
				continue;
		}

		m_normals[i++] = nvec;
	}
}
Exemple #2
0
double Face::area() {
    if (mem_area == 0.0) {
        calculate_normal();
    }

    return mem_area;
}
Exemple #3
0
/*======== double calculate_dot() ==========
  Inputs:   struct matrix *points
            int i  
  Returns: The dot product of a surface normal and
           a view vector
  
  calculates the dot product of the surface normal to
  triangle points[i], points[i+1], points[i+2] and a 
  view vector (use <0, 0, -1> to start.

  04/17/12 16:38:34
  jonalf
  ====================*/
double calculate_dot( struct matrix *points, int i ) {

  double ax, ay, az, bx, by, bz;
  double *normal;
  double vx, vy, vz;
  double dot, mag;

  //get as and bs to calculate the normal
  ax = points->m[0][i + 1] - points->m[0][ i ];
  ay = points->m[1][i + 1] - points->m[1][ i ];
  az = points->m[2][i + 1] - points->m[2][ i ];

  bx = points->m[0][ i ] - points->m[0][ i + 2 ];
  by = points->m[1][ i ] - points->m[1][ i + 2 ];
  bz = points->m[2][ i ] - points->m[2][ i + 2 ];

  normal = calculate_normal( ax, ay, az, bx, by, bz );

  //set up the view vector values
  vx = 0; //m[0][i]
  vy = 0; //m[1][i]
  vz = -1;//m[2][i]

  //calculate the dot product
  dot = normal[0] * vx + normal[1] * vy + normal[2] * vz;

  free( normal );
  
  return dot;
}
Exemple #4
0
void calculate_vertex_normals()
{
    vertex_normals = vector< vector<float> >(no_of_points);
    vector< vector<int> > in_faces(no_of_polygons);
    vector< vector<float> > face_normals(no_of_polygons);
    // Face normals
    for (int i = 0; i < no_of_polygons; i++) {
        vector<float> normal(3);
        calculate_normal(polygons[i], &normal[0]);
        for (int j = 0; j < polygons[i].size(); j++) {
            in_faces[polygons[i][j]].push_back(i);
        }
        face_normals[i] = normal;
    }

    // Vertex normals
    for (int i = 0; i < no_of_points; i++) {
        vector<int> &faces = in_faces[i];
        int faces_count = 0;
        vector<float> normal(3);
        for (vector<int>::iterator it = faces.begin(); it != faces.end(); ++it){
            normal[0] += face_normals[*it][0];
            normal[1] += face_normals[*it][1];
            normal[2] += face_normals[*it][2];
            faces_count++;
        }
        normalise(&normal[0]);

        vertex_normals[i] = normal;
    }
}
Exemple #5
0
/*======== double calculate_dot() ==========
  Inputs:   struct matrix *points
            int i
  Returns: The dot product of a surface normal and
           a view vector

  calculates the dot product of the surface normal to
  triangle points[i], points[i+1], points[i+2] and a
  view vector (use <0, 0, -1> to start.

  04/17/12 16:38:34
  jonalf
  ====================*/
double calculate_dot( struct matrix *points, int i ) {

    double *view;
    double *norm;
    double dot;

    view = (double *)malloc(3 * sizeof(double));
    view[0] = 0;
    view[1] = 0;
    view[2] = -1;

    double Ax = points->m[0][i+1] - points->m[0][i];
    double Ay = points->m[1][i+1] - points->m[1][i];
    double Az = points->m[2][i+1] - points->m[2][i];

    double Bx = points->m[0][i+1] - points->m[0][i+2];
    double By = points->m[1][i+1] - points->m[1][i+2];
    double Bz = points->m[2][i+1] - points->m[2][i+2];

    norm = (double *)malloc(3 * sizeof(double));
    norm = calculate_normal(Ax, Ay, Az, Bx, By, Bz);

    dot = norm[0] * view[0] + norm[+1] * view[1] + norm[2] * view[2];

    free(view);
    return dot;
}
Exemple #6
0
/*======== double calculate_dot() ==========
  Inputs:   struct matrix *points
            int i
  Returns: The dot product of a surface normal and
           a view vector

  calculates the dot product of the surface normal to
  triangle points[i], points[i+1], points[i+2] and a
  view vector (use <0, 0, -1> to start.

  04/17/12 16:38:34
  jonalf
  ====================*/
double calculate_dot( struct matrix *points, int i ) {

  double ax, ay, az, bx, by, bz;
  double *normal;
  double vx, vy, vz;
  double dot;

  //calculate A and B vectors
  ax = points->m[0][i+1] - points->m[0][i];
  ay = points->m[1][i+1] - points->m[1][i];
  az = points->m[2][i+1] - points->m[2][i];

  bx = points->m[0][i+2] - points->m[0][i];
  by = points->m[1][i+2] - points->m[1][i];
  bz = points->m[2][i+2] - points->m[2][i];

  //get the surface normal
  normal = calculate_normal( ax, ay, az, bx, by, bz );

  //set up view vector
  vx = 0;
  vy = 0;
  vz = -1;

  //calculate dot product
  dot = normal[0] * vx + normal[1] * vy + normal[2] * vz;

  free(normal);
  return dot;
}
Exemple #7
0
Vector Face::normal() {
    if (mem_normal == Vector()) {
        calculate_normal();
    }

    return mem_normal;
}
Exemple #8
0
/*======== color calculate_color() ==========
  Inputs:  struct matrix * polygons
  int i
  Returns: color given by lighting equation 
  (ambient + diffuse + specular)

  ====================*/
color calculate_color(struct matrix * polygons, int i)
{
  color c;
  struct light_node * lights = master_list;
  double ax, ay, az, bx, by, bz;
  double * normal;
  double ndotl, specular;

  //CALCULATE NORMAL (VECTOR N)
  ax = polygons->m[0][i+1] - polygons->m[0][i];
  ay = polygons->m[1][i+1] - polygons->m[1][i];
  az = polygons->m[2][i+1] - polygons->m[2][i];
  bx = polygons->m[0][i] - polygons->m[0][i+2];
  by = polygons->m[1][i] - polygons->m[1][i+2];
  bz = polygons->m[2][i] - polygons->m[2][i+2];
  normal = calculate_normal(ax, ay, az, bx, by, bz);

  //AMBIENT LIGHT
  c.red = cons[0][0] * ambient.red;
  c.green = cons[1][0] * ambient.green;
  c.blue = cons[2][0] * ambient.blue;

  while (lights != NULL) {
    //DIFFUSE REFLECTION
    //N dot L
    ndotl = normal[0] * lights->dir[0] 
      + normal[1] * lights->dir[1] 
      + normal[2] * lights->dir[2];
    //ndotl *= ndotl / 2;
    if(ndotl < 0)
      ndotl = 0;

    c.red = cons[0][1] * ndotl * lights->c.red;
    c.green = cons[1][1] * ndotl * lights->c.green;
    c.blue = cons[2][1] * ndotl * lights->c.blue;

    //SPECULAR REFLECTION
    //(2 * N * (N dot L)) dot V 
    specular = -1.0 * (2*normal[2]*ndotl - lights->dir[2]);
    specular = (fabs(specular) + specular) / 2;
    c.red += cons[0][2] * pow(specular, 5) * lights->c.red;
    c.green += cons[1][2] * pow(specular, 5) * lights->c.green;
    c.blue += cons[2][2] * pow(specular, 5) * lights->c.blue;
    
    lights = lights->next;
  }
  
  //IN CASE THERE IS A CALCULATION ERROR
  if(c.red > 255)
    c.red = 255;
  if(c.green > 255)
    c.green = 255;
  if(c.blue > 255)
    c.blue = 255;

  return c;

}
Exemple #9
0
void triangle(point a, point b, point c)
{
	calculate_normal(a, b, c);
	glBegin(GL_POLYGON);
		glNormal3fv(normal);
		glVertex3fv(a);
		glVertex3fv(b);
		glVertex3fv(c);
	glEnd();
}
Exemple #10
0
color flat(double x1, double y1, double z1, double x2, double y2, double z2, color c){
  double * normal = calculate_normal(x1, y1, z1, x2, y2, z2);
  double ka = 0.3;
  double kd = 0.37;
  double ks = 0.13;
  int red = c.red; int blue = c.blue; int green = c.green;
  c.red = red * ka; c.blue = blue * ka; c.green = green * ka;
  int dot = calculate_dot(x1, y1, z1, x2, y2, z2);
  c.red = c.red + (red * dot); c.blue = c.blue + (blue *dot); c. green = c.green + (green * dot);
  return c;
}
Exemple #11
0
Model::Model(const char *filename)
  : m_filename(filename)
{
  std::string err = tinyobj::LoadObj(m_shapes, filename);
  ASSERT_MSG(err.empty(), "%s", err.c_str());

  for ( size_t i=0; i < m_shapes.size(); i++ ) {
    if ( m_shapes[i].mesh.normals.empty() )
      calculate_normal(i);
  }
}
Exemple #12
0
/*======== double calculate_dot() ==========
  Inputs:   struct matrix *points
            int i
  Returns: The dot product of a surface normal and
           a view vector

  calculates the dot product of the surface normal to
  triangle points[i], points[i+1], points[i+2] and a
  view vector (use <0, 0, -1> to start.

  04/17/12 16:38:34
  jonalf
  ====================*/
double calculate_dot( struct matrix *points, int i ) {
    double * s = (double *)malloc(3 * sizeof(double));
    double x1, y1, z1, x2, y2, z2;
    x1 = points->m[0][i+1] - points->m[0][i];
    x2 = points->m[0][i+2] - points->m[0][i];
    y1 = points->m[1][i+1] - points->m[1][i];
    y2 = points->m[1][i+2] - points->m[1][i];
    z1 = points->m[2][i+1] - points->m[2][i];
    z2 = points->m[2][i+2] - points->m[2][i];
    s = calculate_normal(x1,y1,z1,x2,y2,z2);
    double dot = 0 * s[0] + 0 * s[1] + -1 * s[2];
    return dot;
}
static void calculate_normals(void)
{
	int f, i, j;

	printf("calculating normals\n");
	for (f = 0; f < 6; f++) {
		for (i = 0; i < DIM; i++) {
			for (j = 0; j < DIM; j++) {
				calculate_normal(f, i, j);
			}
		}
	}
}
Exemple #14
0
/*======== double calculate_dot() ==========
  Inputs:   struct matrix *points
            int i  
  Returns: The dot product of a surface normal and
           a view vector
  
  calculates the dot product of the surface normal to
  triangle points[i], points[i+1], points[i+2] and a 
  view vector (use <0, 0, -1> to start.

  04/17/12 16:38:34
  jonalf
  ====================*/
double calculate_dot(struct matrix *points, int i) {

    double ax, ay, az, bx, by, bz;
    ax = points->m[0][i] - points->m[0][i + 1];
    ay = points->m[1][i] - points->m[1][i + 1];
    az = points->m[2][i] - points->m[2][i + 1];
    bx = points->m[0][i + 2] - points->m[0][i + 1];
    by = points->m[1][i + 2] - points->m[1][i + 1];
    bz = points->m[2][i + 2] - points->m[2][i + 1];
    double * normal = calculate_normal(ax, ay, az, bx, by, bz);
    double dot = -1 * normal[2];
    return dot;
}
Exemple #15
0
/*======== void calculate_surface_normal() ==========
  Inputs: Inputs: struct matrix *points
	int i
	vector normal

	Calculates the surface normal of polygon by taking the cross product
	of two vectors based on triangle points[i], points[i+1], points[i+2]

  ====================*/
void calculate_surface_normal(struct matrix *points, int i, vector normal){
	double ax, ay, az, bx, by, bz;

  //calculate A and B vectors
  ax = points->m[X][i+1] - points->m[X][i];
  ay = points->m[Y][i+1] - points->m[Y][i];
  az = points->m[Z][i+1] - points->m[Z][i];

  bx = points->m[X][i+2] - points->m[X][i];
  by = points->m[Y][i+2] - points->m[Y][i];
  bz = points->m[Z][i+2] - points->m[Z][i];

  calculate_normal(normal, ax, ay, az, bx, by, bz );
}
Exemple #16
0
/*======== double calculate_dot() ==========
Inputs:   struct matrix *points
int i  
Returns: The dot product of a surface normal and
a view vector

calculates the dot product of the surface normal to
triangle points[i], points[i+1], points[i+2] and a 
view vector (use <0, 0, -1> to start.

04/17/12 16:38:34
jonalf
====================*/
double calculate_dot( struct matrix *points, int i ) {
    double a[3], b[3];
    a[0] = points->m[0][i+1] - points->m[0][i];
    a[1] = points->m[1][i+1] - points->m[1][i];
    a[2] = points->m[2][i+1] - points->m[2][i];
    b[0] = points->m[0][i+2] - points->m[0][i];
    b[1] = points->m[1][i+2] - points->m[1][i];
    b[2] = points->m[2][i+2] - points->m[2][i];  
    double *normal = calculate_normal(a[0], a[1], a[2], b[0], b[1], b[2]);

    double dot = -normal[2];
    free(normal);
    return dot;
}
Exemple #17
0
/*======== double calculate_dot() ==========
Inputs: struct matrix *points
int i
Returns: The dot product of a surface normal and
a view vector
calculates the dot product of the surface normal to
triangle points[i], points[i+1], points[i+2] and a
view vector (use <0, 0, -1> to start)

04/17/12 16:38:34
jonalf
====================*/
double calculate_dot(struct matrix * points, int i) {
  double dot, x0, y0, z0, x1, y1, z1;
  double * normal;
  normal = (double *) malloc(3 * sizeof(double));
  x0 = points->m[0][i+1] - points->m[0][i];
  y0 = points->m[1][i+1] - points->m[1][i];
  z0 = points->m[2][i+1] - points->m[2][i];
  x1 = points->m[0][i+2] - points->m[0][i];
  y1 = points->m[1][i+2] - points->m[1][i];
  z1 = points->m[2][i+2] - points->m[2][i];
  normal = calculate_normal(x0, y0, z0, x1, y1, z1);
  // difficulties below
  dot = 0 * normal[0] + 0 * normal[1] + (-1 * normal[2]);
  return dot;
}
Exemple #18
0
/*======== double calculate_dot() ==========
  Inputs:   struct matrix *points
            int i  
  Returns: The dot product of a surface normal and
           a view vector
  
  calculates the dot product of the surface normal to
  triangle points[i], points[i+1], points[i+2] and a 
  view vector (use <0, 0, -1> to start.

  04/17/12 16:38:34
  jonalf
  ====================*/
double calculate_dot( struct matrix *points, int i ) {
  double dot = -1;
  double a, b, c, a1, b1, c1;
  a = points->m[0][i] - points->m[0][i+1];
  a1 = points->m[1][i] - points->m[1][i+1];
  b = points->m[2][i] - m[2][i+1];
  b1 = points->m[0][i+ 2] - points->m[0][i+1];
  c = points->m[1][i+2] - points->m[1][i+1];
  c1 = points->m[2][i+2] - points->m[2][i+1];
  double * temp;
  temp = (double *)malloc(3 * sizeof(double));
  temp = calculate_normal(a,b,c,a1,b1,c1);
  double dot = temp[2]; 
  return dot;
}
Exemple #19
0
/*======== double calculate_dot() ==========
  Inputs:   struct matrix *points
            int i  
  Returns: The dot product of a surface normal and
           a view vector
  
  calculates the dot product of the surface normal to
  triangle points[i], points[i+1], points[i+2] and a 
  view vector (use <0, 0, -1> to start.

  04/17/12 16:38:34
  jonalf
  ====================*/
double calculate_dot( struct matrix *points, int i ) {
  double dot;
  double ax, ay, az, bx, by, bz;
  double *normal;
  normal = (double *)malloc(3 * sizeof(double));
  ax = points->m[0][i+1] - points->m[0][i];
  ay = points->m[1][i+1] - points->m[1][i];
  az = points->m[2][i+1] - points->m[2][i];
  bx = points->m[0][i+2] - points->m[0][i];
  by = points->m[1][i+2] - points->m[1][i];
  bz = points->m[2][i+2] - points->m[2][i];
  normal = calculate_normal(ax, ay, az, bx, by, bz);
  //view vector is 0, 0, -1
  dot = normal[2] * -1;
  return dot;
}
Exemple #20
0
void calculate_surface_normal(struct matrix *polygons, int i){
  double ax,ay,az,bx,by,bz,mag;
  double *normal;
  ax = polygons->m[0][i+1] - polygons->m[0][i];
  bx = polygons->m[0][i+2] - polygons->m[0][i];
  ay = polygons->m[1][i+1] - polygons->m[1][i];
  by = polygons->m[1][i+2] - polygons->m[1][i];
  az = polygons->m[2][i+1] - polygons->m[2][i];
  bz = polygons->m[2][i+2] - polygons->m[2][i];
  
  normal = calculate_normal(ax,ay,az,bx,by,bz);

  mag = sqrt((normal[0]*normal[0])+(normal[1]*normal[1])+(normal[2]*normal[2]));

  surface_normal[0] = normal[0]/mag;
  surface_normal[1] = normal[1]/mag;
  surface_normal[2] = normal[2]/mag;
}
Exemple #21
0
/*======== double calculate_dot() ==========
  Inputs:   struct matrix *points
            int i  
  Returns: The dot product of a surface normal and
           a view vector
  
  calculates the dot product of the surface normal to
  triangle points[i], points[i+1], points[i+2] and a 
  view vector (use <0, 0, -1> to start.

  04/17/12 16:38:34
  jonalf
  ====================*/
double calculate_dot( struct matrix *points, int i ) {

  double *g;
  g = (double *)malloc(3 *sizeof(double));
  double x, y, z;
  double x1, y1, z1;
  //double dot = -1;
  double dot;
  x = points->m[0][i+1] - points->m[0][i];
  x1 = points->m[0][i+2] - points->m[0][i];
  y = points->m[1][i+1] - points->m[1][i];
  y1 = points->m[1][i+2] - points->m[1][i];
  z = points->m[2][i+1] - points->m[2][i];
  z1 = points->m[2][i+2] - points->m[2][i];
  g = calculate_normal(x,y,z,x1,y1,z1);
  dot = 0 * g[1] + 0 * g[1] + (-1 * g[2]);
  return dot;
}
Exemple #22
0
/*======== double calculate_dot() ==========
  Inputs:   struct matrix *points
            int i  
  Returns: The dot product of a surface normal and
           a view vector
  
  calculates the dot product of the surface normal to
  triangle points[i], points[i+1], points[i+2] and a 
  view vector (use <0, 0, -1> to start.

  04/17/12 16:38:34
  jonalf
  ====================*/
double calculate_dot( struct matrix *points, int i ) {

  double dot;
  double *surface = (double *)malloc(3 * sizeof(double));
  double ax, ay, az, bx, by, bz;
  
  ax = points->m[0][i+1] - points->m[0][i];
  ay = points->m[1][i+1] - points->m[1][i];
  az = points->m[2][i+1] - points->m[2][i];
  bx = points->m[0][i+2] - points->m[0][i];
  by = points->m[1][i+2] - points->m[1][i];
  bz = points->m[2][i+2] - points->m[2][i];

  surface = calculate_normal(ax, ay, az, bx, by, bz);

  dot = surface[2] * -1;
  
  return dot;
}
Exemple #23
0
double * vertex_normal(struct matrix * polygons, int j){
double x, y, z;
int i;
x = polygons->m[0][j];
y = polygons->m[1][j];
z = polygons->m[2][j];
int l = 0;
double *vnormal = (double *)malloc(3 * sizeof(double));
vnormal[0] = 0;
vnormal[1] = 0;
vnormal[2] = 0;
  double ax, ay, az, bx, by, bz;

for(i = 0; i < polygons->lastcol; i+=3){
if ((polygons->m[0][i] == x && polygons->m[1][i] == y && polygons->m[2][i] == z) || (polygons->m[0][i+1] == x && polygons->m[1][i+1] == y && polygons->m[2][i+1] == z) || (polygons->m[0][i+2] == x && polygons->m[1][i+2] == y && polygons->m[2][i+2] == z)){

  ax = polygons->m[0][i + 1] - polygons->m[0][ i ];
  ay = polygons->m[1][i + 1] - polygons->m[1][ i ];
  az = polygons->m[2][i + 1] - polygons->m[2][ i ];

  bx = polygons->m[0][ i ] - polygons->m[0][ i + 2 ];
  by = polygons->m[1][ i ] - polygons->m[1][ i + 2 ];
  bz = polygons->m[2][ i ] - polygons->m[2][ i + 2 ];

  double * normal = calculate_normal(ax,ay,az,bx,by,bx);
vnormal[0] += normal[0];
vnormal[1] += normal[1];
vnormal[2] += normal[2];
free(normal);
l++;
}
}

if (l > 0){
vnormal[0] *= 1/l;
vnormal[1] *= 1/l;
vnormal[2] *= 1/l;
}
else{
printf("None found? - l = %d\n", l);
}
return (double *)(vnormal);
}
Exemple #24
0
double *surface_normal(struct matrix *points, int i){
	double ax, ay, az, bx, by, bz;
  double *normal;
  double vx, vy, vz;
  double dot;

  //calculate A and B vectors
  ax = points->m[0][i+1] - points->m[0][i];
  ay = points->m[1][i+1] - points->m[1][i];
  az = points->m[2][i+1] - points->m[2][i];

  bx = points->m[0][i+2] - points->m[0][i];
  by = points->m[1][i+2] - points->m[1][i];
  bz = points->m[2][i+2] - points->m[2][i];

  //get the surface normal
  normal = calculate_normal( ax, ay, az, bx, by, bz );
	return normal;
}
/* calculate the angle and distance between the two groups */
static void calc_angle(int ePBC,matrix box,rvec x[], atom_id index1[], 
		       atom_id index2[], int gnx1, int gnx2,
		       real *angle,      real *distance, 
		       real *distance1,  real *distance2)

/* distance is distance between centers, distance 1 between center of plane
   and atom one of vector, distance 2 same for atom two
*/

{
  rvec 
    normal1,normal2,  	/* normals on planes of interest */
    center1,center2,  	/* center of triangle of points given to define plane,*/
                      	/* or center of vector if a vector is given */
    h1,h2,h3,h4,h5;  	/* temp. vectors */
  t_pbc pbc;

  set_pbc(&pbc,ePBC,box);

  switch(gnx1)
    {
    case 3:           /* group 1 defines plane */
      calculate_normal(index1,x,normal1,center1);
      break;
    case 2:           /* group 1 defines vector */
      rvec_sub(x[index1[0]],x[index1[1]],normal1);
      rvec_add(x[index1[0]],x[index1[1]],h1);
      svmul(0.5,h1,center1);  /* center is geometric mean */
      break;
    default:          /* group 1 does none of the above */
      gmx_fatal(FARGS,"Something wrong with contents of index file.\n");
    }

  switch(gnx2)
    {
    case 3:          /* group 2 defines plane */
      calculate_normal(index2,x,normal2,center2);
      break;
    case 2:          /* group 2 defines vector */
      rvec_sub(x[index2[0]],x[index2[1]],normal2);
      rvec_add(x[index2[0]],x[index2[1]],h2);
      svmul(0.5,h2,center2);  /* center is geometric mean */
      break;
    case 0:
      normal2[XX] = 0;
      normal2[YY] = 0;
      normal2[ZZ] = 1;
      center2[XX] = 0;
      center2[YY] = 0;
      center2[ZZ] = 0;
      break;
    default:         /* group 2 does none of the above */
      gmx_fatal(FARGS,"Something wrong with contents of index file.\n");
    }
  
  *angle = cos_angle(normal1,normal2);

  if (box)
    pbc_dx(&pbc,center1,center2,h3);
  else
    rvec_sub(center1,center2,h3); 
  *distance = norm(h3);

  if (gnx1 == 3 && gnx2 == 2) {
    if (box) {
      pbc_dx(&pbc,center1,x[index2[0]],h4);
      pbc_dx(&pbc,center1,x[index2[1]],h5);
    } else {
      rvec_sub(center1,x[index2[0]],h4);
      rvec_sub(center1,x[index2[1]],h5);
    }
    *distance1 = norm(h4);
    *distance2 = norm(h5);
  }
  else if (gnx1 == 2 && gnx2 ==3) {
    rvec_sub(center1,x[index1[0]],h4);
    rvec_sub(center1,x[index1[1]],h5);
    *distance1 = norm(h4);
    *distance2 = norm(h5);
  }
  else {
    *distance1 = 0; *distance2 = 0;
  } 
}
Exemple #26
0
void scanline_conversion( struct matrix *polygons, screen s, color c, 
			  struct light* light, struct constants* constants, double vx, double vy, double vz) {
  int i;
  int bot,mid,top,temp;
  double ax,ay,az,bx,by,bz,centerx,centery,centerz;
  double* normal;
  double BT,BM,X1,X2,Y;
  double botX,botY,midX,midY,topX,topY;
  struct cur_light;
  color shaded,temp_l;
  struct light* cur_light;
  for(i = 0; i < polygons->lastcol-2; i+=3){
    if ( calculate_dot( polygons, i ,vx, vy, vz) < 0 ) {
	ax = polygons->m[0][i+1] - polygons->m[0][i];
	ay = polygons->m[1][i+1] - polygons->m[1][i];
	az = polygons->m[2][i+1] - polygons->m[2][i];
	bx = polygons->m[0][i+2] - polygons->m[0][i];
	by = polygons->m[1][i+2] - polygons->m[1][i];
	bz = polygons->m[2][i+2] - polygons->m[2][i];
	centerx=(polygons->m[0][i]+polygons->m[0][i+1]+polygons->m[0][i+2])/3;
	centery=(polygons->m[1][i]+polygons->m[1][i+1]+polygons->m[1][i+2])/3;
	centerz=(polygons->m[2][i]+polygons->m[2][i+1]+polygons->m[2][i+2])/3;
	normal = calculate_normal(ax,ay,az,bx,by,bz);
	shaded.red=0;
	shaded.blue=0;
	shaded.green=0;

	for (cur_light=light;cur_light!=NULL;cur_light=cur_light->next){
	  temp_l=calculate_diffuse(cur_light,c,constants,
				   centerx,centery,centerz,
				   normal[0],normal[1],normal[2]);
	  shaded.red+=temp_l.red;
	  shaded.blue+=temp_l.blue;
	  shaded.green+=temp_l.green;
	}


	shaded.red+=c.red * constants->red;
	shaded.green+=c.green * constants->green;
	shaded.blue+=c.blue * constants->blue;
	
	shaded.red = shaded.red<255? shaded.red : 255;
	shaded.red = shaded.red>0? shaded.red : 0;
	shaded.green = shaded.green<255? shaded.green : 255;
	shaded.green = shaded.green>0? shaded.green : 0;
	shaded.blue = shaded.blue<255? shaded.blue : 255;
	shaded.blue = shaded.blue>0? shaded.blue : 0;
	
      bot=0;
      for (temp=0;temp<3;temp++){
	if (polygons->m[1][i+temp]<polygons->m[1][i+bot]){
	  bot = temp;
	}
      }
      top=0;
      for (temp=0;temp<3;temp++){
	if (polygons->m[1][i+temp]>polygons->m[1][i+top]){
	  top = temp;
	}
      }
      for (temp=0;temp<3;temp++){
	if (temp!=top && temp!=bot){
	  mid=temp;
	}
      }
      //polygons->m[0][i+bot],polygons->m[1][i+bot] is bottom point
      //polygons->m[0][i+mid],polygons->m[1][i+mid] is middle point
      //polygons->m[0][i+top],polygons->m[1][i+top] is top point
      botX = polygons->m[0][i+bot];botY = polygons->m[1][i+bot];
      midX = polygons->m[0][i+mid];midY = polygons->m[1][i+mid];
      topX = polygons->m[0][i+top];topY = polygons->m[1][i+top];
      //initial slopes BT = bot to top BM = bot to Mid
      BT = (topX - botX)/(topY - botY);
      BM = (midX - botX)/(midY - botY);
      //Cooridinates that we'll be working with
      X1 = botX;
      X2 = botY<midY?botX:midX;
      Y = (int)botY;
      while (midY > Y){
	draw_line(X1,Y,X2,Y,s,shaded);
	Y += 1;
	X1 += BT;
	X2 += BM;
	if (fabs(X2-botX)>fabs(midX-botX)){
	  X2=midX;
	}
      }
      BM = (topX - midX)/(topY - midY);
      while (topY > Y){
	draw_line(X1,Y,X2,Y,s,shaded);
	Y += 1;
	X1 += BT;
	X2 += BM;
      }
    }
  }
}
Exemple #27
0
color allLight(struct matrix *polygons, int a) {
    color c;
    double *normal;
    double ux, uy, uz, vx, vy, vz;
    struct ill_node *lumos = master;
    double dotProd, specDot;

    ux = polygons->m[0][a+1] - polygons->m[0][a];
    uy = polygons->m[1][a+1] - polygons->m[1][a];
    uz = polygons->m[2][a+1] - polygons->m[2][a];

    vx = polygons->m[0][a] - polygons->m[0][a+2];
    vy = polygons->m[1][a] - polygons->m[1][a+2];
    vz = polygons->m[2][a] - polygons->m[2][a+2];

    normal = calculate_normal(ux, uy, uz, vx, vy, vz);

    //ambi.red = ambiRed;
    //ambi.green = ambiGreen;
    //ambi.blue = ambiBlue;
    c.red = pointColor[0][0] * ambi.red;
    c.green = pointColor[0][1] * ambi.green;
    c.blue = pointColor[0][2] * ambi.blue;

    while(lumos != NULL) {
        dotProd = normal[0] * lumos->vect[0] +
                  normal[1] * lumos->vect[1] +
                  normal[2] * lumos->vect[2];
        if(dotProd < 0) {
            dotProd = 0;
        }

        // Diffuse Reflection
        c.red += pointColor[1][0] * dotProd * lumos->d.red;
        c.green += pointColor[1][1] * dotProd * lumos->d.green;
        c.blue += pointColor[1][2] * dotProd * lumos->d.blue;

        normal[0] = (dotProd * normal[0] * 2) - lumos->vect[0];
        normal[1] = (dotProd * normal[1] * 2) - lumos->vect[1];
        normal[2] = (dotProd * normal[2] * 2) - lumos->vect[2];
        // Assuming view vector is < 0, 0, -1 >

        specDot = -1 * normal[2];

        specDot = pow(specDot, 5);

        // Specular Reflection
        c.red += pointColor[2][0] * specDot * lumos->d.red;
        c.green += pointColor[2][1] * specDot * lumos->d.green;
        c.blue += pointColor[2][2] * specDot * lumos->d.blue;

        lumos = lumos->next;
    }


    if(c.red > 255) {
        c.red = 255;
    }
    if(c.green > 255) {
        c.green = 255;
    }
    if(c.blue > 255) {
        c.blue = 255;
    }

    return c;
}
Exemple #28
0
void setup_terrain(void) {

   float normalx,normaly,normalz;

       /* Grass */
   terrain[GRASS_TERRAIN]=glGenLists(1);
   glNewList(terrain[GRASS_TERRAIN],GL_COMPILE);
   
   glEnable(GL_TEXTURE_2D);
   glBindTexture(GL_TEXTURE_2D,textures[GRASS_TEXTURE]);
   
   glBegin(GL_QUADS);
      glNormal3f(0.0,0.0,1.0);
      glTexCoord2f(0.0,0.0);
      glVertex3f(-2.0,-2.0,0.0);
   
      glTexCoord2f(1.0,0.0);
      glVertex3f(2.0,-2.0,0.0);
   
      glTexCoord2f(1.0,1.0);
      glVertex3f(2.0,2.0,0.0);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(-2.0,2.0,0.0);
   glEnd();
   glDisable(GL_TEXTURE_2D);
   
   glEndList();
   
          /* Ocean City */
   terrain[OCEAN_CITY_TERRAIN]=glGenLists(1);
   glNewList(terrain[OCEAN_CITY_TERRAIN],GL_COMPILE);

   
   glEnable(GL_TEXTURE_2D);
   glBindTexture(GL_TEXTURE_2D,textures[GRASS_TEXTURE]);
   
   glBegin(GL_QUADS);
      glNormal3f(0.0,0.0,1.0);
      glTexCoord2f(0.0,0.0);
      glVertex3f(-2.0,-2.0,0.0);
   
      glTexCoord2f(1.0,0.0);
      glVertex3f(2.0,-2.0,0.0);
   
      glTexCoord2f(1.0,1.0);
      glVertex3f(2.0,2.0,0.0);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(-2.0,2.0,0.0);
   glEnd();
   
   glBindTexture(GL_TEXTURE_2D,textures[TILE_DOOR_TEXTURE]);
   glBegin(GL_QUADS);
      glNormal3f(0.0,-1.0,0.0);
      glTexCoord2f(1.0,1.0);
      glVertex3f(-2,-1,0);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(-1,-1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(-1,-1,1);
   
      glTexCoord2f(1.0,0.0);
      glVertex3f(-2,-1,1);
   
          /* */
   
      glTexCoord2f(1.0,1.0);
      glVertex3f(1,-1,0);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(2,-1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(2,-1,1);
   
      glTexCoord2f(1.0,0.0);
      glVertex3f(1,-1,1);
  
   glEnd();
   
   glBindTexture(GL_TEXTURE_2D,textures[TILE_WINDOW_TEXTURE]);
   glBegin(GL_QUADS);
      glNormal3f(0.0,-1.0,0.0);
      glTexCoord2f(1.0,2.0);
      glVertex3f(-1,-1,0);
   
      glTexCoord2f(0.0,2.0);
      glVertex3f(0,-1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(0,-1,2);
   
      glTexCoord2f(1.0,0.0);
      glVertex3f(-1,-1,2);
       /* back */
      glNormal3f(0.0,1.0,0.0);
      glTexCoord2f(1.0,1.0);
      glVertex3f(-1,1,0);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(-2,1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(-2,1,1);
   
      glTexCoord2f(1.0,0.0);
      glVertex3f(-1,1,1);   
          /* back2 */
      glNormal3f(0.0,1.0,0.0);
      glTexCoord2f(1.0,2.0);
      glVertex3f(0,1,0);
   
      glTexCoord2f(0.0,2.0);
      glVertex3f(-1,1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(-1,1,2);
   
      glTexCoord2f(1.0,0.0);
      glVertex3f(0,1,2);   
          /* back3*/
      glNormal3f(0.0,1.0,0.0);
      glTexCoord2f(1.0,1.0);
      glVertex3f(2,1,0);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(1,1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(1,1,1);
   
      glTexCoord2f(1.0,0.0);
      glVertex3f(2,1,1);   
   
          /* side1 */
      glNormal3f(-1.0,0.0,0.0);
      glTexCoord2f(2.0,1.0);
      glVertex3f(-2,1,0);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(-2,-1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(-2,-1,1);
   
      glTexCoord2f(2.0,0.0);
      glVertex3f(-2,1,1);   
   
             /* side2 */
      glNormal3f(-1.0,0.0,0.0);
      glTexCoord2f(2.0,1.0);
      glVertex3f(-1,1,1);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(-1,-1,1);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(-1,-1,2);
   
      glTexCoord2f(2.0,0.0);
      glVertex3f(-1,1,2);   
   
             /* side3 */
      glNormal3f(-1.0,0.0,0.0);
      glTexCoord2f(2.0,1.0);
      glVertex3f(1,1,0);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(1,-1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(1,-1,1);
   
      glTexCoord2f(2.0,0.0);
      glVertex3f(1,1,1);   
   
             /* -side1 */
      glNormal3f(1.0,0.0,0.0);
      glTexCoord2f(2.0,2.0);
      glVertex3f(0,1,0);
   
      glTexCoord2f(0.0,2.0);
      glVertex3f(0,-1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(0,-1,2);
   
      glTexCoord2f(2.0,0.0);
      glVertex3f(0,1,2);   
   
                /* -side2 */
      glNormal3f(1.0,0.0,0.0);
      glTexCoord2f(2.0,1.0);
      glVertex3f(2,1,0);
   
      glTexCoord2f(0.0,1.0);
      glVertex3f(2,-1,0);
   
      glTexCoord2f(0.0,0.0);
      glVertex3f(2,-1,1);
   
      glTexCoord2f(2.0,0.0);
      glVertex3f(2,1,1);   
   
   glEnd();
   glBindTexture(GL_TEXTURE_2D,textures[TILE_BLANK_TEXTURE]);  
   glBegin(GL_TRIANGLES);
   
          /* tri 1 */
      glNormal3f(-1.0,0,0);
      glTexCoord2f(0,0);
      glVertex3f(-2,1,1);
   
      glTexCoord2f(1,0);
      glVertex3f(-2,-1,1);
   
      glTexCoord2f(0.5,1);
      glVertex3f(-2,0,1.5);

          /* tri 2 */
      glNormal3f(-1.0,0,0);
      glTexCoord2f(0,0);
      glVertex3f(-1,1,2);
   
      glTexCoord2f(1,0);
      glVertex3f(-1,-1,2);
   
      glTexCoord2f(0.5,1);
      glVertex3f(-1,0,2.5);
   
       /* tri 3 */
      glNormal3f(1.0,0,0);
      glTexCoord2f(0,0);
      glVertex3f(0,1,2);
   
      glTexCoord2f(1,0);
      glVertex3f(0,-1,2);
   
      glTexCoord2f(0.5,1);
      glVertex3f(0,0,2.5);
   
          /* tri 4 */
      glNormal3f(-1.0,0,0);
      glTexCoord2f(0,0);
      glVertex3f(1,1,1);
   
      glTexCoord2f(1,0);
      glVertex3f(1,-1,1);
   
      glTexCoord2f(0.5,1);
      glVertex3f(1,0,1.5);
     
          /* tri 5 */
      glNormal3f(1.0,0,0);
      glTexCoord2f(0,0);
      glVertex3f(2,1,1);
   
      glTexCoord2f(1,0);
      glVertex3f(2,-1,1);
   
      glTexCoord2f(0.5,1);
      glVertex3f(2,0,1.5);
   
   glEnd();
   glBindTexture(GL_TEXTURE_2D,textures[TILE_ROOF_TEXTURE]);    
   glBegin(GL_QUADS);
   
        /* roof front 1 */
      glNormal3f(0.0,-0.447,0.89442);
      glTexCoord2f(0,0);
      glVertex3f(-2.1,-1.2,0.9);       
   
      glTexCoord2f(1,0);
      glVertex3f(-1,-1.2,0.9);       
   
      glTexCoord2f(1,1);
      glVertex3f(-1,0,1.5);       
   
      glTexCoord2f(0,1);
      glVertex3f(-2.1,0,1.5);       
   
           /* roof front 2 */
      glNormal3f(0.0,-0.447,0.89442);
      glTexCoord2f(0,0);
      glVertex3f(-1.1,-1.2,1.9);       
   
      glTexCoord2f(1,0);
      glVertex3f(0.1,-1.2,1.9);       
   
      glTexCoord2f(1,1);
      glVertex3f(0.1,0,2.5);       
   
      glTexCoord2f(0,1);
      glVertex3f(-1.1,0,2.5);       
   
           /* roof front 3 */
      glNormal3f(0.0,-0.447,0.89442);
      glTexCoord2f(0,0);
      glVertex3f(0.9,-1.2,0.9);       
   
      glTexCoord2f(1,0);
      glVertex3f(2.1,-1.2,0.9);       
   
      glTexCoord2f(1,1);
      glVertex3f(2.1,0,1.5);       
   
      glTexCoord2f(0,1);
      glVertex3f(0.9,0,1.5);       
   
           /* roof back 1 */
      glNormal3f(0.0,0.447,0.89442);
      glTexCoord2f(0,0);
      glVertex3f(-2.1,1.2,0.9);       
   
      glTexCoord2f(1,0);
      glVertex3f(-1,1.2,0.9);       
   
      glTexCoord2f(1,1);
      glVertex3f(-1,0,1.5);       
   
      glTexCoord2f(0,1);
      glVertex3f(-2.1,0,1.5);       
   
           /* roof back 2 */
      glNormal3f(0.0,0.447,0.89442);
      glTexCoord2f(0,0);
      glVertex3f(-1.1,1.2,1.9);       
   
      glTexCoord2f(1,0);
      glVertex3f(0.1,1.2,1.9);       
   
      glTexCoord2f(1,1);
      glVertex3f(0.1,0,2.5);       
   
      glTexCoord2f(0,1);
      glVertex3f(-1.1,0,2.5);       
   
           /* roof back 3 */
      glNormal3f(0.0,0.447,0.89442);
      glTexCoord2f(0,0);
      glVertex3f(0.9,1.2,0.9);       
   
      glTexCoord2f(1,0);
      glVertex3f(2.1,1.2,0.9);       
   
      glTexCoord2f(1,1);
      glVertex3f(2.1,0,1.5);       
   
      glTexCoord2f(0,1);
      glVertex3f(0.9,0,1.5);       
   
   glEnd();
   
   glDisable(GL_TEXTURE_2D);
   
   glEndList();
   terrain[JOPPATOWNE_TERRAIN]=terrain[OCEAN_CITY_TERRAIN];
/*******************************************************
 * OCEAN
 ********************************************************/
    terrain[OCEAN_TERRAIN]=glGenLists(1);
    glNewList(terrain[OCEAN_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures[OCEAN_TEXTURE]);
   
    glBegin(GL_QUADS);
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,-0.25);
    glEnd();
    glDisable(GL_TEXTURE_2D);
   
    glEndList();

/*************************************************************
 * TUNDRA
 * ************************************************************/
    terrain[TUNDRA_TERRAIN]=glGenLists(1);
    glNewList(terrain[TUNDRA_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures[TUNDRA_TEXTURE]);
   
    glBegin(GL_QUADS);
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,0);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,0);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,0);
    glEnd();
    glDisable(GL_TEXTURE_2D);
   
    glEndList();
   
 /*************************************************************
 * ARCTIC BASE
 * ************************************************************/
    terrain[ARCTIC_BASE_TERRAIN]=glGenLists(1);
    glNewList(terrain[ARCTIC_BASE_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures[TUNDRA_TEXTURE]);
   
    glBegin(GL_QUADS);
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,0);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,0);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,0);
    glEnd();

       /* roof */
    glBindTexture(GL_TEXTURE_2D,textures[ARCTIC_ROOF_TEXTURE]);
    glBegin(GL_QUADS);
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0,0);
       glVertex3f(-1,-1,1);
   
       glTexCoord2f(1,0);
       glVertex3f(1,-1,1);
   
       glTexCoord2f(1,1);
       glVertex3f(1,1,1);
   
       glTexCoord2f(0,1);
       glVertex3f(-1,1,1);
   
    glEnd();
   
       /* door */
    glBindTexture(GL_TEXTURE_2D,textures[ARCTIC_DOOR_TEXTURE]);
    glBegin(GL_QUADS);
       glNormal3f(0.89442,0.0,0.447);
       glTexCoord2f(1,1);
       glVertex3f(1.5,-1.5,0);
   
       glTexCoord2f(0,1);
       glVertex3f(1.5,1.5,0);
   
       glTexCoord2f(0,0);
       glVertex3f(1,1,1);
   
       glTexCoord2f(1,0);
       glVertex3f(1,-1,1);
   
    glEnd();
   
          /* North window1 */
    glBindTexture(GL_TEXTURE_2D,textures[ARCTIC_WINDOW_TEXTURE]);
    glBegin(GL_QUADS);
       glNormal3f(0.0,0.89442,0.447);
       glTexCoord2f(1,1);
       glVertex3f(1.5,1.5,0);
   
       glTexCoord2f(0,1);
       glVertex3f(-1.5,1.5,0);
   
       glTexCoord2f(0,0);
       glVertex3f(-1,1,1);
   
       glTexCoord2f(1,0);
       glVertex3f(1,1,1);
   
          /* West window */

       glNormal3f(-0.89442,0.0,0.447);
       glTexCoord2f(1,1);
       glVertex3f(-1.5,1.5,0);
   
       glTexCoord2f(0,1);
       glVertex3f(-1.5,-1.5,0);
   
       glTexCoord2f(0,0);
       glVertex3f(-1,-1,1);
   
       glTexCoord2f(1,0);
       glVertex3f(-1,1,1);
      
          /* South Window */
   
       glNormal3f(0.0,-0.89442,0.447);
       glTexCoord2f(1,1);
       glVertex3f(-1.5,-1.5,0);
   
       glTexCoord2f(0,1);
       glVertex3f(1.5,-1.5,0);
   
       glTexCoord2f(0,0);
       glVertex3f(1,-1,1);
   
       glTexCoord2f(1,0);
       glVertex3f(-1,-1,1);
   
    glEnd();
   
    glDisable(GL_TEXTURE_2D);
   
    glEndList();
/********************************************************
 * CLIFF
 * *******************************************************/
    
    terrain[CLIFF_TERRAIN]=glGenLists(1);
    glNewList(terrain[CLIFF_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures[OCEAN_TEXTURE]);
   
    glBegin(GL_QUADS);
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,-0.25);
    glEnd();
   
    glBindTexture(GL_TEXTURE_2D,textures[CLIFF_TEXTURE]);
   
    glBegin(GL_QUADS);
   
       glNormal3f(-1.0,0.0,0.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(2.0,2.0,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,-2.0,0);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(2.0,2.0,0);
    glEnd();
   
    glDisable(GL_TEXTURE_2D);
   
    glEndList();
   
   
/********************************************************
 * CLIFFCORNER
 * *******************************************************/
    
    terrain[CLIFFCORNER_TERRAIN]=glGenLists(1);
    glNewList(terrain[CLIFFCORNER_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures[OCEAN_TEXTURE]);
   
    glBegin(GL_TRIANGLES);
   
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,-0.25);

    glEnd();
   
    glBindTexture(GL_TEXTURE_2D,textures[CLIFF_TEXTURE]);

    
   
    glBegin(GL_QUADS);
   
       glNormal3f(-0.70710678,0.70710678,0.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(2.0,2.0,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(-2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(-2.0,-2.0,0);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(2.0,2.0,0);
    glEnd();
   
    glBegin(GL_TRIANGLES);
   
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(2.0,2.0,0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(-2.0,-2.0,0);
   
       glTexCoord2f(0.5,0.5);
       glVertex3f(2.0,-2.0,0);

    glEnd();

    glDisable(GL_TEXTURE_2D);
   
    glEndList();
   
/********************************************************
 * BEACH
 * *******************************************************/
    
    terrain[BEACH_TERRAIN]=glGenLists(1);
    glNewList(terrain[BEACH_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures[SHALLOW_TEXTURE]);
   
    glBegin(GL_QUADS);
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(0.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(0,2.0,-0.25);
    glEnd();
       
    glBindTexture(GL_TEXTURE_2D,textures[SAND_TEXTURE]);
   
    glBegin(GL_QUADS);
   
       glNormal3f(0.124,0.0,0.992278);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(0,-2,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(0,2.0,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,0);
    glEnd();
   
    glBindTexture(GL_TEXTURE_2D,textures[CLIFF_TEXTURE]);
   
    glBegin(GL_TRIANGLES);
    
       glNormal3f(0,-1,0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2,-2,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(0,-2,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2,-2,0);

       glNormal3f(0,1,0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2,2,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(0,2,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2,2,0);

       
   
    glEnd();
   
    glDisable(GL_TEXTURE_2D);
   
    glEndList();
   
/********************************************************
 * BEACH CORNER
 * *******************************************************/
    
    terrain[BEACHCORNER_TERRAIN]=glGenLists(1);
    glNewList(terrain[BEACHCORNER_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures[SHALLOW_TEXTURE]);
   
    glBegin(GL_QUADS);
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(0.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,0.0,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(0,0,-0.25);
    glEnd();
       
    glBindTexture(GL_TEXTURE_2D,textures[SAND_TEXTURE]);
   
    glBegin(GL_QUADS);
   
       glNormal3f(0.124,0.0,0.992278);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(0,-2,-0.25);
   
       glTexCoord2f(1.0,0.5);
       glVertex3f(0,0.0,-0.25);
   
       glTexCoord2f(0.0,0.5);
       glVertex3f(-2.0,0.0,0);
    glEnd();
   
    glBegin(GL_TRIANGLES);

       glNormal3f(0.124,0.0,0.992278);
       
       glTexCoord2f(0.0,0.5);
       glVertex3f(-2.0,0.0,0);
   
       glTexCoord2f(1.0,0.5);
       glVertex3f(0.0,0.0,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,0);
   
       glNormal3f(0.0,-0.124,0.992278);
       
       glTexCoord2f(0.5,0.0);
       glVertex3f(0.0,0.0,-0.25);
   
       glTexCoord2f(0.5,1.0);
       glVertex3f(0.0,2.0,0.0);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,0);
   
    glEnd();
   
   
    glBegin(GL_QUADS);

       glNormal3f(0.0,-0.124,0.992278);
       
       glTexCoord2f(0.5,0.0);
       glVertex3f(0.0,0.0,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,0.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,0.0);
   
       glTexCoord2f(0.5,1.0);
       glVertex3f(0.0,2.0,0.0);
   
    glEnd();
   
    glBindTexture(GL_TEXTURE_2D,textures[CLIFF_TEXTURE]);
   
    glBegin(GL_TRIANGLES);
    
       glNormal3f(0,-1,0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2,-2,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(0,-2,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2,-2,0);

   
       glNormal3f(1,0,0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(2,0,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2,2,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(2,2,0);

    glEnd();
   
    glDisable(GL_TEXTURE_2D);
   
    glEndList();
   
/********************************************************
 * BEACH ENDS
 * *******************************************************/
    
    terrain[BEACHEND_TERRAIN]=glGenLists(1);
    glNewList(terrain[BEACHEND_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
   
    glBindTexture(GL_TEXTURE_2D,textures[OCEAN_TEXTURE]);
   
    glBegin(GL_QUADS);
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.5,0.0);
       glVertex3f(0.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,-0.25);
   
       glTexCoord2f(1.0,0.5);
       glVertex3f(2.0,0.0,-0.25);
   
       glTexCoord2f(0.5,0.5);
       glVertex3f(0.0,0.0,-0.25);
   
    glEnd();
   
    glBegin(GL_TRIANGLES);
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,-0.25);
   
       glTexCoord2f(0.5,0.0);
       glVertex3f(0.0,-2.0,-0.25);
   
       glTexCoord2f(0.5,0.5);
       glVertex3f(0.0,0.0,-0.25);

          /* 2 */
       glTexCoord2f(0.5,0.5);
       glVertex3f(0,0,-0.25);
   
       glTexCoord2f(1.0,0.5);
       glVertex3f(2.0,0.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,-0.25);
   
    glEnd();
   
    glBindTexture(GL_TEXTURE_2D,textures[SHALLOW_TEXTURE]);
   
    glBegin(GL_TRIANGLES);
   
       glNormal3f(0.0,0.0,1.0);
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,-0.25);
   
       glTexCoord2f(0.5,0.5);
       glVertex3f(0.0,0.0,-0.25);
   
       glTexCoord2f(0.0,0.5);
       glVertex3f(-2.0,0.0,-0.25);
          /* 2 */
       glTexCoord2f(0.0,0.5);
       glVertex3f(-2.0,0.0,-0.25);
   
       glTexCoord2f(0.5,0.5);
       glVertex3f(0.0,0.0,-0.25);
   
       glTexCoord2f(0.5,1.0);
       glVertex3f(0.0,2.0,-0.25);
          /* 3 */
       glTexCoord2f(0.5,0.5);
       glVertex3f(0.0,0.0,-0.25);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,-0.25);
   
       glTexCoord2f(0.5,1.0);
       glVertex3f(0.0,2.0,-0.25);
   
    glEnd();
       
    glBindTexture(GL_TEXTURE_2D,textures[SAND_TEXTURE]);
   
    glBegin(GL_TRIANGLES);
      
       glNormal3f(0.123091,-0.123091,0.984732);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,0.0,-0.25);
   
       glTexCoord2f(0.5,1.0);
       glVertex3f(0,2,-0.25);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2,2.0,0.0);
   
    glEnd();
   
    glDisable(GL_TEXTURE_2D);
   
    glEndList();

/*********************************************************
 * MOUNTAIN
 * *******************************************************/
   
    terrain[MOUNTAIN_TERRAIN]=glGenLists(1);
    glNewList(terrain[MOUNTAIN_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures[MOUNTAIN_TEXTURE]);
   
    glBegin(GL_TRIANGLES);
   
          /* Face 1 */

       calculate_normal(-2.0,2.0,0.0,
			-2.0,-2.0,0.0,
			0.0,0.0,4.0,
			&normalx,&normaly,&normalz);
       glNormal3f(normalx,normaly,normalz);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,2.0,0.0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(-2.0,-2.0,0.0);
   
       glTexCoord2f(0.5,1.0);
       glVertex3f(0.0,0.0,4.0);
   
          /* Face 2 */
   
       calculate_normal(-2.0,-2.0,0.0,
			2.0,-2.0,0.0,
			0.0,0.0,4.0,
			&normalx,&normaly,&normalz);
       glNormal3f(normalx,normaly,normalz);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,0.0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,0.0);
   
       glTexCoord2f(0.5,1.0);
       glVertex3f(0.0,0.0,4.0);
   
          /* Face 3 */
   
      calculate_normal(2.0,-2.0,0.0,
			2.0,2.0,0.0,
			0.0,0.0,4.0,
			&normalx,&normaly,&normalz);
       glNormal3f(normalx,normaly,normalz);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(2.0,-2.0,0.0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,2.0,0.0);
   
       glTexCoord2f(0.5,1.0);
       glVertex3f(0.0,0.0,4.0);
   
          /* Face 4 */
   
          calculate_normal(2.0,2.0,0.0,
			-2.0,2.0,0.0,
			0.0,0.0,4.0,
			&normalx,&normaly,&normalz);
       glNormal3f(normalx,normaly,normalz);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(2.0,2.0,0.0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(-2.0,2.0,0.0);
   
       glTexCoord2f(0.5,1.0);
       glVertex3f(0.0,0.0,4.0);
   
   
    glEnd();
    glDisable(GL_TEXTURE_2D);
   
    glEndList();
   
   
       /* Forest */
    terrain[FOREST_TERRAIN]=glGenLists(1);
    glNewList(terrain[FOREST_TERRAIN],GL_COMPILE);
   
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,textures[FOREST_TEXTURE]);
   
    glBegin(GL_QUADS);
   
          /* Face 1 */

       glNormal3f(0,-1,0);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,0.0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,0.0);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,-2.0,0.5);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,-2.0,0.5);
   
             /* Face 2 */

       glNormal3f(1,0,0);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(2.0,-2.0,0.0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,2.0,0.0);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,0.5);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(2.0,-2.0,0.5);
   
             /* Face 3 */

       glNormal3f(0,1,0);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(2.0,2.0,0.0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(-2.0,2.0,0.0);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(-2.0,2.0,0.5);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(2.0,2.0,0.5);
   
             /* Face 4 */

       glNormal3f(-1,0,0);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,2.0,0.0);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(-2.0,-2.0,0.0);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(-2.0,-2.0,0.5);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,0.5);
   
             /* Top */

       glNormal3f(0,0,1);
   
       glTexCoord2f(0.0,0.0);
       glVertex3f(-2.0,-2.0,0.5);
   
       glTexCoord2f(1.0,0.0);
       glVertex3f(2.0,-2.0,0.5);
   
       glTexCoord2f(1.0,1.0);
       glVertex3f(2.0,2.0,0.5);
   
       glTexCoord2f(0.0,1.0);
       glVertex3f(-2.0,2.0,0.5);

    glEnd();
    glDisable(GL_TEXTURE_2D);
   
    glEndList();

}
void march_cube(size_t x, size_t y, size_t z, float scale, float threshold, scan_data *data, vector_buffer *vertices, vector_buffer *normals) {

    int corner, vertex, vertex_test, edge, triangle, flag_index, edge_flags;
    float offset;
    float cube_values[8];

    vec3 edge_vertex[12];
    vec3 edge_norm[12];
    vec3 points;
    vec3 normal;

    for (vertex = 0; vertex < 8; vertex++) {
        cube_values[vertex] = sample(x + cube_vertices[vertex][0],
                                     y + cube_vertices[vertex][1],
                                     z + cube_vertices[vertex][2],
                                     data);
    }

    flag_index = 0;
    for (vertex_test = 0; vertex_test < 8; vertex_test++) {
        if (cube_values[vertex_test] <= threshold)
            flag_index |= 1 << vertex_test;
    }


    edge_flags = cube_edge_flags[flag_index];


    if (edge_flags == 0) {
        return;
    }

    for (edge = 0; edge < 12; edge++) {
        if (edge_flags & (1 << edge)) {
            offset = get_intersection_offset(cube_values[cube_edge_connections[edge][0]],
                                             cube_values[cube_edge_connections[edge][1]], threshold);

            edge_vertex[edge][0] = (x + cube_vertices[cube_edge_connections[edge][0]][0] + offset * cube_edge_directions[edge][0]) * scale;
            edge_vertex[edge][1] = (y + cube_vertices[cube_edge_connections[edge][0]][1] + offset * cube_edge_directions[edge][1]) * scale;
            edge_vertex[edge][2] = (z + cube_vertices[cube_edge_connections[edge][0]][2] + offset * cube_edge_directions[edge][2]) * scale;


            calculate_normal((vec3_t) &edge_norm[edge], edge, x, y, z, offset, data);
        }
    }

    for (triangle = 0; triangle < 5; triangle++) {
        if (triangle_connection_table[flag_index][3 * triangle] < 0)
            break;

        for (corner = 0; corner < 3; corner++) {
            vertex = triangle_connection_table[flag_index][3 * triangle + corner];

            vec3_set((vec3_t) &edge_vertex[vertex], (vec3_t) &points);
            vec3_set((vec3_t) &edge_norm[vertex], (vec3_t) &normal);

            vector_append(vertices, (vec3_t) &points);
            vector_append(normals, (vec3_t) &normal);
        }
    }
}
Exemple #30
0
/*======== void draw_polygons() ==========
Inputs:   struct matrix *polygons
         screen s
         color c  
Returns: 


04/16/13 13:13:27
jdyrlandweaver
====================*/
void draw_polygons( struct matrix *polygons, screen s, color c2 ) {
  int i = 0;

double kar, kag, kab, kdr,kdg,kdb,ksb,ksr,ksg;
lcons* curcons= lclist->next;
if(curcons != NULL){
//printf("Not null.\n");
//kar = curcons->kar;
//kag = curcons->kag;
//kab = curcons->kab;
//printf("Object %d ambient constants -> %lf, %lf, %lf\n",i, kar,kag,kab);
while (i < obj){
if (curcons->next != NULL){
curcons = curcons->next;
//printf("shift\n");
}
i++;
}
}
if (curcons != NULL){
kar = curcons->kar;
kag = curcons->kag;
kab = curcons->kab;
kdr = curcons->kdr;
kdg = curcons->kdg;
kdb = curcons->kdb;
ksr = curcons->ksr;
ksg = curcons->ksg;
ksb = curcons->ksb;
}


//printf("Object %d ambient constants -> %lf, %lf, %lf\n",obj, kar,kag,kab);
//printf("Object %d diffuse constants -> %lf, %lf, %lf\n",obj, kdr,kdg,kdb);
//printf("Object %d specular constants -> %lf, %lf, %lf\n\n",obj, ksr,ksg,ksb);


color c;
int j = 0;

//c.red = c2.red * kar;
//c.green = c2.green * kag;
//c.blue = c2.blue * kab;


  if ( polygons->lastcol < 3 ) {
    printf("Need at least 3 points to draw a polygon!\n");
    return;
  }
  //printf("\n\n");
  for( i=0; i < polygons->lastcol-2; i+=3 ) {


//  c.red = (c.red + 17)%255;
//  c.green =(c.green + 3)%255;
//  c.blue = (c.blue + 5)%255;


    if ( calculate_dot( polygons, i ) >= 0 ) {
    //      printf("drawing polygon %d\n", i/3);   


double b, m, t;
double uo, ut, ur;
int oo, ot, or,ob, ott, om;
int bbb, mmm, ttt;
oo = 0;
ot = 0;
or = 0;
ob = 0;
om = 0;
ott = 0;
uo = polygons->m[1][i];
ut = polygons->m[1][i+1];
ur = polygons->m[1][i+2];
double x1, x2, x3;
double z1, z2, z3;
//
x1 = polygons->m[0][i];
x2 = polygons->m[0][i+1];
x3 = polygons->m[0][i+2];
z1 = polygons->m[2][i];
z2 = polygons->m[2][i+1];
z3 = polygons->m[2][i+2];
//printf("points->(%lf,%lf,%lf);(%lf,%lf,%lf);(%lf,%lf,%lf)\n",x1,uo,z1,x2,ut,z2,x3,ur,z3);
//
if(ott == 0 && uo >= ut && uo>= ur){
t = uo;
oo++;
ott++;
x3 = polygons->m[0][i];
z3 = polygons->m[2][i];
ttt = 0;
}
if(ott == 0 && ut >= uo && ut>= ur){
t = ut;
ot++;
ott++;
x3 = polygons->m[0][i+1];
z3 = polygons->m[2][i+1];
ttt = 1;
}
if(ott == 0 && ur >= ut && ur>= uo){
t = ur;
or++;
ott++;
x3 = polygons->m[0][i+2];
z3 = polygons->m[2][i+2];
ttt = 2;
}

if(ob == 0 && oo == 0 && uo <= ut && uo<= ur){
b = uo;
oo++;
ob++;
x1 = polygons->m[0][i];
z1 = polygons->m[2][i];
bbb = 0;
}
if(ob == 0 && ot == 0 && ut <= uo && ut<= ur){
b = ut;
ot++;
ob++;
x1 = polygons->m[0][i+1];
z1 = polygons->m[2][i+1];
bbb = 1;
}
if(ob == 0 && or == 0 && ur <= ut && ur<= uo){
b = ur;
or++;
ob++;
x1 = polygons->m[0][i+2];
z1 = polygons->m[2][i+2];
bbb = 2;
}

if (om == 0 && oo == 0){
m = uo;
om++;
x2 = polygons->m[0][i];
z2 = polygons->m[2][i];
mmm = 0;
}
if (om == 0 && ot == 0){
m = ut;
om++;
x2 = polygons->m[0][i+1];
z2 = polygons->m[2][i+1];
mmm = 1;
}
if (om == 0 && or == 0){
m = ur;
om++;
x2 = polygons->m[0][i+2];
z2 = polygons->m[2][i+2];
mmm = 2;
}
//printf("bmt -> (%lf,%lf,%lf);(%lf,%lf,%lf);(%lf,%lf,%lf)\n\n",x1,b,z1,x2,m,z2,x3,t,z3);
double bt, bm, mt;
if (t == b){
bt = DBL_MAX;//9999999;//DBL_MAX;//
if (x3 < x1){
bt = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
bt = 1.0 * (x3 - x1)/(t - b);
}
if (m == b){
bm = DBL_MAX;//9999999;//DBL_MAX;//
if (x2 < x1){
bm = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
bm = 1.0 * (x2 - x1)/(m - b);
}
if (t == m){
mt = DBL_MAX;//9999999;//DBL_MAX;//
if (x3 < x2){
mt = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
mt = 1.0 * (x3 - x2)/(t - m);
}
//////zzzzzzzz
double btz, bmz, mtz;
if (t == b){
btz = DBL_MAX;//9999999;//DBL_MAX;//
if (z3 < z1){
btz = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
btz = 1.0 * (z3 - z1)/(t - b);
}
if (m == b){
bmz = DBL_MAX;//9999999;//DBL_MAX;//
if (z2 < z1){
bmz = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
bmz = 1.0 * (z2 - z1)/(m - b);
}
if (t == m){
mtz = DBL_MAX;//9999999;//DBL_MAX;//
if (z3 < z2){
mtz = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
mtz = 1.0 * (z3 - z2)/(t - m);
}
//printf("inv slopes %lf %lf %lf\n",bt,bm,mt);
//printf("inv z slopes %lf %lf %lf\n",btz,bmz,mtz);
double m2 = bm;
double mz2 = bmz;
double drx, drx2, drx3;
double drz, drz2, drz3;
drx2 = x1;
drx = x1;
drx3 = x2;
drz2 = z1;
drz = z1;
drz3 = z2;
int abcdef = 0;





c.red = c2.red * kar;
c.green = c2.green * kag;
c.blue = c2.blue * kab; 
///*//////////////////////////////////////FLAT
double ax, ay, az, bx, by, bz, cx, cy, cz;
  double *normal;
  double vx, vy, vz;
  double dot, mag;

for (j = 0; j < ptl; j++){

  //get as and bs to calculate the normal
  ax = polygons->m[0][i + 1] - polygons->m[0][ i ];
  ay = polygons->m[1][i + 1] - polygons->m[1][ i ];
  az = polygons->m[2][i + 1] - polygons->m[2][ i ];

  bx = polygons->m[0][ i ] - polygons->m[0][ i + 2 ];
  by = polygons->m[1][ i ] - polygons->m[1][ i + 2 ];
  bz = polygons->m[2][ i ] - polygons->m[2][ i + 2 ];

  cx = (polygons->m[0][ i ] + polygons->m[0][i + 1] + polygons->m[0][ i + 2 ])/3;
  cy = (polygons->m[1][ i ] + polygons->m[1][i + 1] +  polygons->m[1][ i + 2 ])/3;
  cz = (polygons->m[2][ i ] + polygons->m[2][i + 1] +  polygons->m[2][ i + 2 ])/3;

  normal = calculate_normal( ax, ay, az, bx, by, bz );

  //normalize the surface normal
  mag = sqrt( (normal[0] * normal[0]) + 
	      (normal[1] * normal[1]) + 
	      (normal[2] * normal[2]) );

  normal[0] = normal[0] / mag;
  normal[1] = normal[1] / mag;
  normal[2] = normal[2] / mag;

  //set up the view vector values
  vx = ptlights[j][0] - cx;
  vy = ptlights[j][1] - cy;
  vz = ptlights[j][2] - cz;

  mag = sqrt( (vx * vx) + 
	      (vy * vy) + 
	      (vz * vz) );

  vx = vx / mag;
  vy = vy / mag;
  vz = vz / mag;

  //calculate the dot product
  dot = normal[0] * vx + normal[1] * vy + normal[2] * vz;

/////////////////////diffuse

if (c.red + ptlights[j][3] * kdr * dot >= 255){
c.red = 255;
}
else{
if (ptlights[j][3] * kdr * dot >= 0){
c.red+= ptlights[j][3] * kdr * dot;
}
}
if (c.green + ptlights[j][4] * kdg * dot >= 255){
c.green = 255;
}
else{
if (ptlights[j][4] * kdg * dot >= 0){
c.green+= ptlights[j][4] * kdg * dot;
}
}
if (c.blue + ptlights[j][5] * kdb * dot >= 255){
c.blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot >= 0){
c.blue+= ptlights[j][5] * kdb * dot;
}
}
/////////////////////specular
normal[0]*= 2 * dot; 
normal[1]*= 2 * dot; 
normal[2]*= 2 * dot; 
	
normal[0]-= vx; 
normal[1]-= vy; 
normal[2]-= vz;

dot = -1 * normal[2];

if (c.red + ptlights[j][3] * kdr * dot >= 255){
c.red = 255;
}
else{
if (ptlights[j][3] * kdr * dot >= 0){
c.red+= ptlights[j][3] * kdr * dot;
}
}
if (c.green + ptlights[j][4] * kdg * dot >= 255){
c.green = 255;
}
else{
if (ptlights[j][4] * kdg * dot >= 0){
c.green+= ptlights[j][4] * kdg * dot;
}
}
if (c.blue + ptlights[j][5] * kdb * dot >= 255){
c.blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot >= 0){
c.blue+= ptlights[j][5] * kdb * dot;
}
}

  free( normal );
}
ib.red = c.red;
ib.green = c.green;
ib.blue = c.blue;
im.red = c.red;
im.green = c.green;
im.blue = c.blue;
it.red = c.red;
it.green = c.green;
it.blue = c.blue;
//*///////////////////////////////////////\FLAT


/*//////////////////////////////////////GOROUD
setc = 1;

ib.red = c.red;
ib.green = c.green;
ib.blue = c.blue;
im.red = c.red;
im.green = c.green;
im.blue = c.blue;
it.red = c.red;
it.green = c.green;
it.blue = c.blue;

  double *normal;
  double *normal2;
  double *normal3;
  double vx, vy, vz;
  double vx1, vy1, vz1;
  double vx2, vy2, vz2;
  double dot, mag, dot2, dot3;

for (j = 0; j < ptl; j++){


normal = vertex_normal(polygons,i);
normal2 = vertex_normal(polygons,i+1);
normal3 = vertex_normal(polygons,i+2);

  //normalize the surface normal
  mag = sqrt( (normal[0] * normal[0]) + 
	      (normal[1] * normal[1]) + 
	      (normal[2] * normal[2]) );

  normal[0] = normal[0] / mag;
  normal[1] = normal[1] / mag;
  normal[2] = normal[2] / mag;
mag = sqrt( (normal2[0] * normal2[0]) + 
	      (normal2[1] * normal2[1]) + 
	      (normal2[2] * normal2[2]) );

  normal2[0] = normal2[0] / mag;
  normal2[1] = normal2[1] / mag;
  normal2[2] = normal2[2] / mag;
mag = sqrt( (normal3[0] * normal3[0]) + 
	      (normal3[1] * normal3[1]) + 
	      (normal3[2] * normal3[2]) );

  normal3[0] = normal3[0] / mag;
  normal3[1] = normal3[1] / mag;
  normal3[2] = normal3[2] / mag;

  //set up the view vector values
  vx = -ptlights[j][0] + polygons->m[0][ i ];
  vy = -ptlights[j][1] + polygons->m[1][ i ];
  vz = -ptlights[j][2] + polygons->m[2][ i ];
  vx1 = -ptlights[j][0] + polygons->m[0][ i+1];
  vy1 = -ptlights[j][1] + polygons->m[1][ i+1 ];
  vz1 = -ptlights[j][2] + polygons->m[2][ i+1 ];
  vx2 = -ptlights[j][0] + polygons->m[0][ i+2 ];
  vy2 = -ptlights[j][1] + polygons->m[1][ i+2 ];
  vz2 = -ptlights[j][2] + polygons->m[2][ i+2 ];

  mag = sqrt( (vx * vx) + 
	      (vy * vy) + 
	      (vz * vz) );

  vx = vx / mag;
  vy = vy / mag;
  vz = vz / mag;

  mag = sqrt( (vx1 * vx1) + 
	      (vy1 * vy1) + 
	      (vz1 * vz1) );

  vx1 = vx1 / mag;
  vy1 = vy1 / mag;
  vz1 = vz1 / mag;

  mag = sqrt( (vx2 * vx2) + 
	      (vy2 * vy2) + 
	      (vz2 * vz2) );

  vx2 = vx2 / mag;
  vy2 = vy2 / mag;
  vz2 = vz2 / mag;

  //calculate the dot product
  dot = normal[0] * vx + normal[1] * vy + normal[2] * vz;
dot2 = normal2[0] * vx1 + normal2[1] * vy1 + normal2[2] * vz1;
dot3 = normal3[0] * vx2 + normal3[1] * vy2 + normal3[2] * vz2;
///////////////////////////////////
int ooo, www, rrr;
if (bbb == 0){
ooo = 0;
}
if (bbb == 1){
www = 0;
}
if (bbb == 2){
rrr = 0;
}
if (mmm == 0){
ooo = 1;
}
if (mmm == 1){
www = 1;
}
if (mmm == 2){
rrr = 1;
}
if (ttt == 0){
ooo = 2;
}
if (ttt == 1){
www = 2;
}
if (ttt == 2){
rrr = 2;
}


/////////////////////diffuse

if (cbmt[ooo].red + ptlights[j][3] * kdr * dot >= 255){
cbmt[ooo].red = 255;
}
else{
if (ptlights[j][3] * kdr * dot >= 0){
cbmt[ooo].red += ptlights[j][3] * kdr * dot;
}
}
if (cbmt[ooo].green + ptlights[j][4] * kdg * dot >= 255){
cbmt[ooo].green = 255;
}
else{
if (ptlights[j][4] * kdg * dot >= 0){
cbmt[ooo].green += ptlights[j][4] * kdg * dot;
}
}
if (cbmt[ooo].blue + ptlights[j][5] * kdb * dot >= 255){
cbmt[ooo].blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot >= 0){
cbmt[ooo].blue += ptlights[j][5] * kdb * dot;
}
}

if (cbmt[www].red + ptlights[j][3] * kdr * dot2 >= 255){
cbmt[www].red = 255;
}
else{
if (ptlights[j][3] * kdr * dot2 >= 0){
cbmt[www].red += ptlights[j][3] * kdr * dot2;
}
}
if (cbmt[www].green + ptlights[j][4] * kdg * dot2 >= 255){
cbmt[www].green = 255;
}
else{
if (ptlights[j][4] * kdg * dot2 >= 0){
cbmt[www].green += ptlights[j][4] * kdg * dot2;
}
}
if (cbmt[www].blue + ptlights[j][5] * kdb * dot2 >= 255){
cbmt[www].blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot2 >= 0){
cbmt[www].blue += ptlights[j][5] * kdb * dot2;
}
}


if (cbmt[rrr].red + ptlights[j][3] * kdr * dot3 >= 255){
cbmt[rrr].red = 255;
}
else{
if (ptlights[j][3] * kdr * dot3 >= 0){
cbmt[rrr].red += ptlights[j][3] * kdr * dot3;
}
}
if (cbmt[rrr].green + ptlights[j][4] * kdg * dot3 >= 255){
cbmt[rrr].green = 255;
}
else{
if (ptlights[j][4] * kdg * dot3 >= 0){
cbmt[rrr].green += ptlights[j][4] * kdg * dot3;
}
}
if (cbmt[rrr].blue + ptlights[j][5] * kdb * dot3 >= 255){
cbmt[rrr].blue = 255;
}
else{
if (ptlights[j][5] * kdb * dot3 >= 0){
cbmt[rrr].blue += ptlights[j][5] * kdb * dot3;
}
}
ib.red = cbmt[0].red;
ib.green = cbmt[0].green;
ib.blue = cbmt[0].blue;
im.red = cbmt[1].red;
im.green = cbmt[1].green;
im.blue = cbmt[1].blue;
it.red = cbmt[2].red;
it.green = cbmt[2].green;
it.blue = cbmt[2].blue;
/////////////////////specular
normal[0]*= 2 * dot; 
normal[1]*= 2 * dot; 
normal[2]*= 2 * dot; 
	
normal[0]-= vx; 
normal[1]-= vy; 
normal[2]-= vz;

normal2[0]*= 2 * dot2; 
normal2[1]*= 2 * dot2; 
normal2[2]*= 2 * dot2; 
	
normal2[0]-= vx1; 
normal2[1]-= vy1; 
normal2[2]-= vz1;

normal3[0]*= 2 * dot3; 
normal3[1]*= 2 * dot3; 
normal3[2]*= 2 * dot3; 
	
normal3[0]-= vx2; 
normal3[1]-= vy2; 
normal3[2]-= vz2;

dot = -1 * normal[2];
dot2 = -1 * normal2[2];
dot3 = -1 * normal3[2];

if (cbmt[ooo].red + ptlights[j][3] * ksr * dot >= 255){
cbmt[ooo].red = 255;
}
else{
if (ptlights[j][3] * ksr * dot >= 0){
cbmt[ooo].red += ptlights[j][3] * ksr * dot;
}
}
if (cbmt[ooo].green + ptlights[j][4] * ksg * dot >= 255){
cbmt[ooo].green = 255;
}
else{
if (ptlights[j][4] * ksg * dot >= 0){
cbmt[ooo].green += ptlights[j][4] * ksg * dot;
}
}
if (cbmt[ooo].blue + ptlights[j][5] * ksb * dot >= 255){
cbmt[ooo].blue = 255;
}
else{
if (ptlights[j][5] * ksb * dot >= 0){
cbmt[ooo].blue += ptlights[j][5] * ksb * dot;
}
}

if (cbmt[www].red + ptlights[j][3] * ksr * dot2 >= 255){
cbmt[www].red = 255;
}
else{
if (ptlights[j][3] * ksr * dot2 >= 0){
cbmt[www].red += ptlights[j][3] * ksr * dot2;
}
}
if (cbmt[www].green + ptlights[j][4] * ksg * dot2 >= 255){
cbmt[www].green = 255;
}
else{
if (ptlights[j][4] * ksg * dot2 >= 0){
cbmt[www].green += ptlights[j][4] * ksg * dot2;
}
}
if (cbmt[www].blue + ptlights[j][5] * ksb * dot2 >= 255){
cbmt[www].blue = 255;
}
else{
if (ptlights[j][5] * ksb * dot2 >= 0){
cbmt[www].blue += ptlights[j][5] * ksb * dot2;
}
}


if (cbmt[rrr].red + ptlights[j][3] * ksr * dot3 >= 255){
cbmt[rrr].red = 255;
}
else{
if (ptlights[j][3] * ksr * dot3 >= 0){
cbmt[rrr].red += ptlights[j][3] * ksr * dot3;
}
}
if (cbmt[rrr].green + ptlights[j][4] * ksg * dot3 >= 255){
cbmt[rrr].green = 255;
}
else{
if (ptlights[j][4] * ksg * dot3 >= 0){
cbmt[rrr].green += ptlights[j][4] * ksg * dot3;
}
}
if (cbmt[rrr].blue + ptlights[j][5] * ksb * dot3 >= 255){
cbmt[rrr].blue = 255;
}
else{
if (ptlights[j][5] * ksb * dot3 >= 0){
cbmt[rrr].blue += ptlights[j][5] * ksb * dot3;
}
}
ib.red = cbmt[0].red;
ib.green = cbmt[0].green;
ib.blue = cbmt[0].blue;
im.red = cbmt[1].red;
im.green = cbmt[1].green;
im.blue = cbmt[1].blue;
it.red = cbmt[2].red;
it.green = cbmt[2].green;
it.blue = cbmt[2].blue;
  free( normal );
  free( normal2 );
  free( normal3 );
}
//*////////////////////////////////////////////////\\GOROUD(End of excludable portion)

double btcr, bmcr, mtcr;
double btcg, bmcg, mtcg;
double btcb, bmcb, mtcb;
if (t == b){
btcr = DBL_MAX;//9999999;//DBL_MAX;//
btcg = DBL_MAX;//9999999;//DBL_MAX;//
btcb = DBL_MAX;//9999999;//DBL_MAX;//
if (it.red < ib.red){
btcr = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (it.green < ib.green){
btcg = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (it.blue < ib.blue){
btcb = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
btcg = 1.0 * (it.green - ib.green)/(t - b);
btcr = 1.0 * (it.red - ib.red)/(t - b);
btcb = 1.0 * (it.blue - ib.blue)/(t - b);
}
if (m == b){
bmcr = DBL_MAX;//9999999;//DBL_MAX;//
bmcg = DBL_MAX;//9999999;//DBL_MAX;//
bmcb = DBL_MAX;//9999999;//DBL_MAX;//
if (im.red < ib.red){
bmcr = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (im.green < ib.green){
bmcg = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (im.blue < ib.blue){
bmcb = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
bmcr = 1.0 * (im.red - ib.red)/(m - b);
bmcg = 1.0 * (im.green - ib.green)/(m - b);
bmcb = 1.0 * (im.blue - ib.blue)/(m - b);
}
if (t == m){
mtcr = DBL_MAX;//9999999;//DBL_MAX;//
mtcg = DBL_MAX;//9999999;//DBL_MAX;//
mtcb = DBL_MAX;//9999999;//DBL_MAX;//
if (it.red < im.red){
mtcr = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (it.green < im.green){
mtcg = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
if (it.blue < im.blue){
mtcb = -DBL_MAX;//-9999999;//-DBL_MAX;//
}
}
else{
mtcr = 1.0 * (it.red - im.red)/(t - m);
mtcg = 1.0 * (it.green - im.green)/(t - m);
mtcb = 1.0 * (it.blue - im.blue)/(t - m);
}
double mr2 = bmcr;
double mg2 = bmcg;
double mb2 = bmcb;
double drr, drr2, drr3;
double drg, drg2, drg3;
double drb, drb2, drb3;
drr2 = ib.red + 0;
drr = ib.red + 0;
drr3 = im.red + 0;
drg2 = ib.green + 0;
drg = ib.green + 0;
drg3 = im.green + 0;
drb2 = ib.blue + 0;
drb = ib.blue + 0;
drb3 = im.blue + 0;

//*///////////////////////////////////////\GOROUD


///*//////////////////////////////////////PHONG


//*///////////////////////////////////////PHONG
      draw_line( polygons->m[0][i],
		 polygons->m[1][i],
		 polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 s, c,
		polygons->m[2][i],
		 polygons->m[2][i+1]);
      draw_line( polygons->m[0][i+1],
		 polygons->m[1][i+1],
		 polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 s, c,
		polygons->m[2][i+1],
		 polygons->m[2][i+2]);
      draw_line( polygons->m[0][i+2],
		 polygons->m[1][i+2],
		 polygons->m[0][i],
		 polygons->m[1][i],
		 s, c,
		polygons->m[2][i+2],
		 polygons->m[2][i]);

while(b < t){
if (abcdef == 0 && b >= m){
m2 = mt;
mr2 = mtcr;
mg2 = mtcg;
mb2 = mtcb;
mz2 = mtz;
drx2 = x2;
drr2 = im.red + 0;
drg2 = im.green + 0;
drb2 = im.blue + 0;
drx3 = x3;
drr2 = it.red + 0;
drg2 = it.green + 0;
drb2 = it.blue + 0;
drz2 = z2;
drz3 = z3;
abcdef++;
}
drx += bt;
drx2 += m2;
drr += btcr;
drg += btcg;
drb += btcb;
drr2 += mr2;
drg2 += mg2;
drb2 += mb2;
drz += btz;
drz2 += mz2;
if ((bt < 0 && drx < x3) || (bt >= 0 && drx > x3)){
drx = x3;
}
if ((m2 < 0 && drx2 < drx3) || (m2 >= 0 && drx2 > drx3)){
drx2 = drx3;
}///
if ((btcr < 0 && drr < it.red) || (btcr >= 0 && drr > it.red)){
drr = it.red + 0;
}
if ((mr2 < 0 && drr2 < drr3) || (mr2 >= 0 && drr2 > drr3)){
drr2 = drr3 + 0;
}//////
if ((btcg < 0 && drg < it.green) || (btcg >= 0 && drg > it.green)){
drg = it.green + 0;
}
if ((mg2 < 0 && drg2 < drg3) || (mg2 >= 0 && drg2 > drg3)){
drg2 = drg3 + 0;
}//////
if ((btcb < 0 && drb < it.blue) || (btcb >= 0 && drb > it.blue)){
drb = it.blue + 0;
}
if ((mb2 < 0 && drb2 < drb3) || (mb2 >= 0 && drb2 > drb3)){
drb2 = drb3 + 0;
}///
if ((btz < 0 && drz < z3) || (btz >= 0 && drz > z3)){
drz = z3 + 0;
}
if ((mz2 < 0 && drz2 < drz3) || (mz2 >= 0 && drz2 > drz3)){
drz2 = drz3 + 0;
}
//printf("drawing (%lf,%lf,%lf) to (%lf,%lf,%lf)\n",drx,b,drz,drx2,b,drz2);
//printf("\tbmt -> (%lf,%lf,%lf);(%lf,%lf,%lf);(%lf,%lf,%lf)\n",x1,b,z1,x2,m,z2,x3,t,z3);
cl.red = drr;
cl.green = drg;
cl.blue = drb;
cr.red = drr2;
cr.green = drg2;
cr.blue = drb2;
draw_line(drx,b,drx2,b,s,c, drz, drz2);
b++;
}
//printf("polygon colors:b%d,%d,%d;;        m%d,%d,%d;;             t%d,%d,%d;;\n\tl%d,%d,%d;;        r%d,%d,%d;;\n",ib.red,ib.green,ib.blue,im.red,im.green,im.blue,it.red,it.green,it.blue,cl.red,cl.green,cl.blue,cr.red,cr.green,cr.blue);
//printf("line colors:l%d,%d,%d;;        r%d,%d,%d;;\n",cl.red,cl.green,cl.blue,cr.red,cr.green,cr.blue);

    }
  }
setc = 0;
//printf("polygon colors:b%d,%d,%d;;        m%d,%d,%d;;             t%d,%d,%d;;\n\tl%d,%d,%d;;        r%d,%d,%d;;\n",ib.red,ib.green,ib.blue,im.red,im.green,im.blue,it.red,it.green,it.blue,cl.red,cl.green,cl.blue,cr.red,cr.green,cr.blue);
}