Merge branch 'master' of goat:git/antikythera
[antikythera] / src / gear.cc
index 697610d..6a34815 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <GL/glew.h>
 #include "gear.h"
 #include "meshgen.h"
@@ -8,17 +9,21 @@ Gear::Gear()
 {
        pdist = 0;
        angle = 0;
-       nteeth = 42;
-       radius = 80;
        teeth_length = 5;
        thickness = 5;
        bevel = 1.5;
        init_angle = 0;
        xform_valid = false;
 
+       set_teeth(42, 10);
+
        supergear = 0;
 
        mesh = 0;
+
+       color = Vec3(0.6, 0.6, 0.6);
+       roughness = 1.0;
+       metallic = false;
 }
 
 Gear::~Gear()
@@ -74,6 +79,11 @@ float Gear::get_angular_offset() const
 
 void Gear::set_teeth(int nt, float tooth_pitch)
 {
+       if(tooth_pitch <= 0) {
+               tooth_pitch = this->tooth_pitch;
+       } else {
+               this->tooth_pitch = tooth_pitch;
+       }
        float circ = tooth_pitch * nt;
        radius = circ / (2.0 * M_PI);
        nteeth = nt;
@@ -94,12 +104,16 @@ void Gear::set_position(const Vec3 &pos)
 {
        if(!supergear) {
                this->pos = pos;
-               xform_valid = false;
        } else {
-               if(fabs(this->pos.z - pos.z) > 1e-5) {
-                       this->pos.z = pos.z;
-                       xform_valid = false;
-               }
+               this->pos = supergear->pos;
+               this->pos.z = pos.z;
+       }
+       xform_valid = false;
+
+       for(int i=0; i<(int)subgears.size(); i++) {
+               Vec3 subpos = this->pos;
+               subpos.z = subgears[i]->pos.z;
+               subgears[i]->set_position(subpos);
        }
 }
 
@@ -164,7 +178,7 @@ void Gear::draw() const
                calc_matrix();
        }
 
-       glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
+       glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT | GL_LIGHTING_BIT);
 
        glPushMatrix();
        glMultMatrixf(xform[0]);
@@ -174,6 +188,15 @@ void Gear::draw() const
                glEnable(GL_POLYGON_OFFSET_FILL);
        }
 
+       Vec3 diffuse = metallic ? Vec3(0, 0, 0) : color;
+       float col[] = {diffuse.x, diffuse.y, diffuse.z, 1.0};
+       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
+
+       Vec3 specular = (metallic ? color : Vec3(1, 1, 1)) * (1.0 - roughness);
+       float scol[] = {specular.x, specular.y, specular.z, 1.0};
+       glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
+       glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 1.0 + (1.0 - roughness) * 60.0);
+
        mesh->draw();
 
        glDisable(GL_LIGHTING);
@@ -183,13 +206,6 @@ void Gear::draw() const
                mesh->draw_wire();
        }
 
-       glLineWidth(2.0);
-       glBegin(GL_LINES);
-       glColor3f(0, 0, 1);
-       glVertex3f(0, 0, -10);
-       glVertex3f(0, 0, 10);
-       glEnd();
-
        glPopMatrix();
        glPopAttrib();
 }