Initial revision
[lsystem3d.git] / lsystem3d / src / model.cpp
diff --git a/lsystem3d/src/model.cpp b/lsystem3d/src/model.cpp
new file mode 100644 (file)
index 0000000..48308a0
--- /dev/null
@@ -0,0 +1,175 @@
+// Copyright (C) 2006 Erik Dahlberg
+// 
+// This file is part of LSystem3d.
+// 
+// LSystem3D is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+// 
+// LSystem3D is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with LSystem3D; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+
+
+
+#include "GL/gl.h"
+#include "GL/glu.h"
+
+#include "model.h"
+#include "vector.h"
+
+
+
+/**
+ * Constructor
+ */
+Model::Model()
+{
+    _displayList = glGenLists(1);
+}
+
+
+
+/**
+ * Destructor
+ */
+Model::~Model()
+{
+    glDeleteLists(_displayList, 1);
+}
+
+
+
+/**
+ * Connect two points
+ * @param diameter diameter of segment
+ * @param x1 x start point
+ * @param y1 y start point
+ * @param z1 z start point
+ * @param x2 x end point
+ * @param y2 y end point
+ * @param z2 z end point
+ */
+void Model::segment(double diameter, double x1, double y1, double z1, double x2, double y2, double z2)
+{
+    // wireframe
+    //
+    // glColor3f(1,1,1);
+    // glBegin(GL_LINES);
+    //     glVertex3d(x1, y1, z1);
+    //     glVertex3d(x2, y2, z2);
+    // glEnd();
+    
+    
+    
+    // solid
+    
+    // find rotation vector and angle of cylinder
+    
+    Vector initial(0.0,   1.0,   0.0);      // initial rotation of cylinder
+    Vector desired(x2-x1, y2-y1, z2-z1);    // desired rotation of cylinder
+    desired.normalize();
+    
+    // rotation angle 
+    double angle = initial.getAngle(desired);
+    
+    // rotation vector
+    Vector crossProduct = initial.getCrossProduct(desired);
+    crossProduct.normalize();
+    
+    
+    // render the cylinder
+    
+    GLUquadric *quadric = gluNewQuadric();
+    
+    glColor3f(1.0, 0.5, 0.0);
+        
+    glPushMatrix();
+        glTranslatef(x1, y1, z1);
+        glRotatef(angle, crossProduct.getX(), crossProduct.getY(), crossProduct.getZ());
+        glRotatef(-90, 1, 0, 0);
+        
+        gluCylinder(quadric, diameter, diameter, 1.0, 6, 6);
+    glPopMatrix();
+}
+
+
+
+/**
+ * Draw model to screen
+ */
+void Model::draw()
+{
+    glCallList(_displayList);
+}
+
+
+
+/**
+ * Clear the model
+ */
+void Model::clear()
+{
+    // TODO: not needed?
+}
+
+
+
+/**
+ * Start recording of drawing operations
+ */
+void Model::begin()
+{
+    glNewList(_displayList, GL_COMPILE);
+}
+
+
+
+/**
+ * Stop recording of drawing operations
+ */
+void Model::end()
+{
+    glEndList();
+}
+
+
+
+/**
+ * Begin creation of a filled surface
+ */
+void Model::fillBegin()
+{
+    glColor3f(0.0, 0.5, 0.0);
+    glBegin(GL_POLYGON);
+}
+
+
+
+/**
+ * End creation of a filled surface
+ */
+void Model::fillEnd()
+{
+    glEnd();
+}
+
+
+
+/**
+ * Create one vertex in the filled surface
+ * @param x x-coordinate
+ * @param y y-coordinate
+ * @param z z-coordinate
+ */
+void Model::point(double x, double y, double z)
+{
+    glVertex3d(x, y, z);
+}