// Center of tree void centerBranch(int depth) { int terminatingBranch = 0; if (depth == 0) { translateBy(0, 5 * (1.0 / scaleFactor), 0); terminatingLeaf(); return; } // if if (depth == 1) { terminatingBranch = TERM; } // if translateBy(0, 6 * (depth * 1.0 / scaleFactor), 0); drawBranch(CENTER, depth); // Left branch pushMatrix(); turnLeft(); translateBy(0, 6 * (depth * 1.0 / scaleFactor), 0); drawBranch(LEFT | terminatingBranch, depth); leftBranch(depth - 1); popMatrix(); // 3D left branch pushMatrix(); rotateBy(90, 0, 1, 0); rotateBy(15, 0, 0, 1); translateBy(1, 0, 0); turnLeft(); translateBy(0, 6 * (depth * 1.0 / scaleFactor), 0); drawBranch(LEFT | terminatingBranch, depth); leftBranch(depth - 1); popMatrix(); translateBy(0, 6 * (depth * 1.0 / scaleFactor), 0); drawBranch(CENTER, depth); // Right branch pushMatrix(); turnRight(); translateBy(0, 6 * (depth * 1.0 / scaleFactor), 0); drawBranch(RIGHT | terminatingBranch, depth); rightBranch(depth - 1); popMatrix(); // 3D right branch pushMatrix(); rotateBy(90, 0, 1, 0); rotateBy(-15, 0, 0, 1); translateBy(-1, 0, 0); turnRight(); translateBy(0, 6 * (depth * 1.0 / scaleFactor), 0); drawBranch(RIGHT | terminatingBranch, depth); rightBranch(depth - 1); popMatrix(); translateBy(0, 6 * (depth * 1.0 / scaleFactor), 0); drawBranch(CENTER | TERM, depth); centerBranch(depth - 1); } // centerBranch
/** * Before calling the recursive L-system, draws the tree trunk to the screen. * It's supposed to resemble a hexagonal cylinder (which is more realistic * than a box!) */ void initialize(void) { // Draw the trunk GLfloat vertices[] = { 1.0, 0.0, -2.0, 2.0, 0.0, 0.0, 1.0, 0.0, 2.0, -1.0, 0.0, 2.0, -2.0, 0.0, 0.0, -1.0, 0.0, -2.0, // top vertices 1.0, 18.0, -1.0, 1.0, 18.0, 0.0, 1.0, 18.0, 1.0, -1.0, 18.0, 1.0, -1.0, 18.0, 0.0, -1.0, 18.0, -1.0 }; GLuint indices[] = { 0, 1, 2, 3, 4, 5, // top 6, 7, 8, 9, 10, 11,// bottom 1, 7, 6, 0, 2, 8, 7, 1, 3, 9, 8, 2, 4, 10, 9, 3, 5, 11, 10, 4, 0, 6, 11, 5 }; translateBy(0, -39, 0); int i; int v1, v2, v3; glColor3f(treeRed, treeGreen, treeBlue); glBegin(GL_QUADS); for (i = 0; i < 36; i++) { v1 = indices[i] * 3; v2 = indices[i] * 3 + 1; v3 = indices[i] * 3 + 2; glVertex3f(vertices[v1], vertices[v2], vertices[v3]); } // for glEnd(); translateBy(0, 12, 0); } // initialize
void CC3Matrix::populateToLookAt( const CC3Vector& targetLocation, const CC3Vector& eyeLocation, const CC3Vector& upDirection ) { CC3Vector fwdDir = targetLocation.difference( eyeLocation ); populateToPointTowards( fwdDir, upDirection ); transpose(); translateBy( eyeLocation.negate() ); m_isIdentity = false; m_isRigid = true; }
void ModelShow::mouseMoveEvent(QMouseEvent *event) { int dx=event->x()-lastPos.x(); int dy=event->y()-lastPos.y(); if(event->buttons() & Qt::LeftButton){ rotateBy(-8*dy,8*dx,0); } else if(event->buttons() & Qt::RightButton){ zoomBy(-0.01*dx); } else if(event->buttons() & Qt::MiddleButton){ translateBy(0.01*dx,0.01*dy,0); } lastPos=event->pos(); }
// Draw an apple! An apple's a fruit! void drawFruit(void) { translateBy(0, -2.5, 0); glColor3f(1.0, 0.2, 0.04); glutSolidSphere(1.5, 30, 30); } // drawFruit