// Called when the mouse moves void GLDvrWidget::mouseMoveEvent(QMouseEvent *event) { std::cout << "GLDvrWidget::mouseMoveEvent()" << std::endl; // TODO (CA2) int y = event->y(); int x = event->x(); int dy = y - m_prevMouseY; int dx = x - m_prevMouseX; if(event->buttons() & Qt::LeftButton) { Matrix4d rotX; rotX.identity(); rotX.rotate(0.5 * dy, Vector3d(1., 0., 0.)); // Update the stored transformation with the one just computed m_matRotX *= rotX; Matrix4d rotY; rotY.identity(); rotY.rotate(0.5*dx,Vector3d(0.,1.,0.)); m_matRotY *= rotY; } m_prevMouseX = x; m_prevMouseY = y; this->updateContent(); }
void Camera::computeMatrix(){ Vector3d z = e - d; z.normalize(); Vector3d x = up.cross(z); x.normalize(); Vector3d y = z.cross(x); y.normalize(); Matrix4d r; r.identity(); r.set(0, 0, x[0]); r.set(1, 0, x[1]); r.set(2, 0, x[2]); r.set(0, 1, y[0]); r.set(1, 1, y[1]); r.set(2, 1, y[2]); r.set(0, 2, z[0]); r.set(1, 2, z[1]); r.set(2, 2, z[2]); Matrix4d t; t.identity(); t.makeTranslate(-e[0], -e[1], -e[2]); m = r * t; }
void Camera::setCamera(double eyeX, double eyeY, double eyeZ, double centerX, double centerY, double centerZ, double upX, double upY, double upZ){ double f[3]; f[0] = centerX - eyeX; f[1] = centerY - eyeY; f[2] = centerZ - eyeZ; double m_f = sqrt(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]); f[0] /= m_f; f[1] /= m_f; f[2] /= m_f; double up[3]; up[0] = upX; up[1] = upY; up[2] = upZ; double m_u = sqrt(up[0] * up[0] + up[1] * up[1] + up[2] * up[2]); up[0] /= m_u; up[1] /= m_u; up[2] /= m_u; double s[3]; s[0] = f[1] * up[2] - f[2] * up[1]; s[1] = f[2] * up[0] - f[0] * up[2]; s[2] = f[0] * up[1] - f[1] * up[0]; double u[3]; u[0] = s[1] * f[2] - s[2] * f[1]; u[1] = s[2] * f[0] - s[0] * f[2]; u[2] = s[0] * f[1] - s[1] * f[0]; Matrix4d r; // inverse rotation r.identity(); r.set(0, 0, s[0]); r.set(1, 0, s[1]); r.set(2, 0, s[2]); r.set(0, 1, u[0]); r.set(1, 1, u[1]); r.set(2, 1, u[2]); r.set(0, 2, -f[0]); r.set(1, 2, -f[1]); r.set(2, 2, -f[2]); Matrix4d t; // inverse translation t.identity(); t.makeTranslate(-eyeX, -eyeY, -eyeZ); m = r * t; // inverse camera }
void BuildingConstructor::construct() { GLdouble currentMaxH = maxH; GLdouble currentMinH = minH; GLdouble xTranslation = ((double) rand() * 0.5) / (double) RAND_MAX; GLdouble zTranslation = ((double) rand() * 0.5) / (double) RAND_MAX; GLdouble xScaling = 2.0; GLdouble zScaling = 2.0; GLdouble rotationAngle = 90.0; GLdouble lotBias = 0.02; Matrix4d translation; Matrix4d scaling; Matrix4d rotation; Matrix4d rotation90; Building* building = NULL; GLdouble scaleMax = 0.90; GLdouble scaleMin = 0.5; double colorH = 1.0; double colorL = 0.90; double r = (double) rand() * (colorH + 1.0 - colorL) / (double) RAND_MAX + colorL; double g = (double) rand() * (colorH + 1.0 - colorL) / (double) RAND_MAX + colorL; double b = (double) rand() * (colorH + 1.0 - colorL) / (double) RAND_MAX + colorL; GLuint buildingChoice = rand() * (3 + 1) / RAND_MAX; GLuint roofChoice = rand() * (5 - 4 + 1) / RAND_MAX + 4; std::cerr << "buildingChoice: " << buildingChoice << " roofChoice: " << roofChoice << std::endl; rotation.identity(); rotation90.makeRotateY(rotationAngle); switch (choice) { // medium buildings case 0: while (buildingChoice == 1) { buildingChoice = rand() * (3 + 1) / RAND_MAX; } std::cerr << "hhhh" << std::endl; xScaling = (double) rand() * (3.0 + 1.0 - 2.0) / (double) RAND_MAX + 2.0; zScaling = (double) rand() * (3.0 + 1.0 - 2.0) / (double) RAND_MAX + 2.0; translation.makeTranslate(xTranslation, 0.0, zTranslation); scaling.makeScale(xScaling, currentMaxH, zScaling); building = new Building(buildingChoice, roofChoice, r, g, b, texture, xScaling, currentMaxH, zScaling, translation * scaling); root->addChild(building->getRoot()); break; // mid towards out buildings case 1: buildingChoice = rand() * (3 + 1 - 1) / RAND_MAX + 1; xScaling = zScaling = (double) rand() * (1.0 + 1.0 - 0.5) / (double) RAND_MAX + 0.5; for (int count = 0; count < layerCount - 1; ++count) { currentMaxH -= ((double) rand() * (scaleMax - scaleMin)) / (double) RAND_MAX + scaleMin;; xScaling += 0.3; zScaling += 0.3; if (xScaling > 3.0 || zScaling > 3.0) { break; } // std::cerr << "zScaling: " << zScaling << std::endl; scaling.makeScale(xScaling, currentMaxH, zScaling); building = new Building(buildingChoice, roofChoice, r, g, b, texture, xScaling, currentMaxH, zScaling, scaling); root->addChild(building->getRoot()); } break; // tall buildings with rotation case 2: scaling.makeScale(3.0, 1.0, 3.0); buildingChoice = rand() * (3 + 1 - 1) / RAND_MAX + 1; building = new Building(buildingChoice, roofChoice, r, g, b, texture, 3.0, 1.0, 3.0, scaling); root->addChild(building->getRoot()); translation.makeTranslate(xTranslation, 0.0, zTranslation); scaling.makeScale(xScaling, currentMaxH, zScaling); building = new Building(buildingChoice, roofChoice, r, g, b, texture, xScaling, currentMaxH, zScaling, translation * scaling); root->addChild(building->getRoot()); //t s m xTranslation = 0.5 - lotBias; zTranslation = 0.0; zScaling = ((double) rand() * (currentMaxH - currentMinH)) / (double) RAND_MAX + currentMinH; for (int count = 0; count < layerCount - 1; ++count) { currentMaxH -= ((double) rand() * (scaleMax - scaleMin)) / (double) RAND_MAX + scaleMin;; rotation = rotation90 * rotation; zScaling = ((double) rand() * (scaleMax - scaleMin)) / (double) RAND_MAX + scaleMin; // std::cerr << "zScaling: " << zScaling << std::endl; scaling.makeScale(xScaling, currentMaxH, zScaling); translation.makeTranslate(xTranslation, 0.0, 0.0); building = new Building(buildingChoice, roofChoice, r, g, b, texture, xScaling, currentMaxH, zScaling, rotation * translation * scaling); root->addChild(building->getRoot()); } break; default: break; } }