void Material::fresnelDielectric(const QVector3D &d, const QVector3D &normal, float ni, float nt, QVector<QPair<float, QVector3D> > &rays) { QVector3D n = normal; float ndd = QVector3D::dotProduct(n, d); if (ndd > -Config::Epsilon && ndd < Config::Epsilon) { return ; } if (ndd > 0) { n *= -1; ndd *= -1; } QVector3D r = d - 2.0f * ndd * n; r.normalize(); QVector3D z = (d - ndd * n) * ni / nt; float z2 = z.lengthSquared(); if (z2 >= 1.0f) { rays.append(QPair<float, QVector3D>(1.0f, r)); return ; } QVector3D t = z - qSqrt(1.0f - z2) * n; t.normalize(); float ndt = QVector3D::dotProduct(n, t); float rPar = (nt * ndd - ni * ndt) / (nt * ndd + ni * ndt); float rPerp = (ni * ndd - nt * ndt) / (ni * ndd + nt * ndt); float fr = 0.5f * (rPar * rPar + rPerp * rPerp); rays.append(QPair<float, QVector3D>(fr, r)); rays.append(QPair<float, QVector3D>(1.0f - fr, t)); }
void Window::mouseMoveEvent(QMouseEvent *event) { if (!m_mouseDown) { m_terrain->pick(QPointF(event->localPos().x() / 1024, event->localPos().y() / 768), m_projection, m_view); return; } QPointF delta = event->localPos() - m_mousePos; QMatrix4x4 mat; mat.rotate(m_camera.orientation); QVector3D pitchAxis = QVector3D(mat(0, 0), mat(0, 1), mat(0, 2)); pitchAxis.normalize(); m_camera.orientation *= QQuaternion::fromAxisAndAngle(pitchAxis, perSecond(delta.y() / 200)); m_camera.orientation.normalize(); QVector3D yawAxis = QVector3D(mat(1, 0), mat(1, 1), mat(1, 2)); yawAxis.normalize(); m_camera.orientation *= QQuaternion::fromAxisAndAngle(yawAxis, perSecond(delta.x() / 200)); m_camera.orientation.normalize(); m_needsUpdate = true; m_mousePos = event->localPos(); }
void QAudioEngine::updateListenerOrientation() { QVector3D dir = m_listenerDirection; QVector3D up = m_listenerUp; dir.normalize(); up.normalize(); QVector3D u = up - dir * QVector3D::dotProduct(dir, up); u.normalize(); d->setListenerOrientation(dir, u); }
void Camera::moveXY(QPoint p) { QVector3D v = step_; v.setZ(0.0); v.normalize(); pos_ += v*(p.ry()*0.003f); v[0] = step_[1]; v[1] = -step_[0]; v.normalize(); pos_ -= v*(p.rx()*0.003f); }
void Quiddiards::keyPressEvent(QKeyEvent *event){ switch (event->key()) { case Qt::Key_F2: actStart(); break; case Qt::Key_P: actPause(); break; case Qt::Key_Escape: case Qt::Key_Q: close(); break; case Qt::Key_Space: case Qt::Key_Enter: case Qt::Key_Return: break; case Qt::Key_W:{ /* forward cueball */ QVector3D n = -eye; n.setZ(0); n.normalize(); n += cueball.getVelocity(); if (n.length() > cueball.getSpeed()){ n = cueball.getSpeed()*n.normalized(); } cueball.setVelocity(n); break; } case Qt::Key_A: phi += 10; break; case Qt::Key_S:{ QVector3D n = eye; n.setZ(0); n.normalize(); n += cueball.getVelocity(); if (n.length() > cueball.getSpeed()){ n = cueball.getSpeed()*n.normalized(); } cueball.setVelocity(n); break; } case Qt::Key_D: phi -= 10; break; case Qt::Key_Tab: //camera = CAMERA((camera + 1) % 2); break; default: return; } update(); }
void MyGLWidget::keyPressEvent(QKeyEvent *event) { GLfloat cameraSpeed = 1.0f ; QVector3D cross ; bool changedFront = false ; switch ( event->key()) { case Qt::Key_W : cameraPos += cameraSpeed * cameraFront ; break ; case Qt:: Key_S : cameraPos -= cameraSpeed * cameraFront ; break ; case Qt::Key_A : cross = cross.crossProduct(cameraFront , cameraUp) ; cross.normalize(); cameraPos -= cross * cameraSpeed ; break ; case Qt::Key_D : cross = cross.crossProduct(cameraFront , cameraUp) ; cross.normalize(); cameraPos += cross * cameraSpeed ; break ; case Qt::Key_Up : pitch += 1 ; changedFront = true ; break ; case Qt::Key_Down : pitch -= 1 ; changedFront = true ; break ; case Qt::Key_Left : yaw -= 1 ; changedFront = true ; break ; case Qt::Key_Right : yaw += 1 ; changedFront = true ; break ; case Qt::Key_P : paused = !paused ; break ; case Qt::Key_R : cameraPos = QVector3D(0.0f, 0.0f, 3.0f); cameraFront = QVector3D(0.0f, 0.0f, -1.0f); cameraUp = QVector3D(0.0f, 1.0f, 0.0f); break ; default : QGLWidget::keyPressEvent(event) ; } if (changedFront) { QVector3D front; front.setX ( cos(pitch*(M_PI/180)) * cos(yaw*(M_PI/180)) ); front.setY ( sin(pitch*(M_PI/180) ) ); front.setZ ( cos(pitch*(M_PI/180)) * sin(yaw*(M_PI/180)) ); front.normalize(); cameraFront = front ; } }
void AwesomeCamera::rotateView(float z_angle,float x_angle){ // rotM.setToIdentity(); double cosPhi = cos(mouse_sens*(-z_angle)/180*M_PI); double sinPhi = sin(mouse_sens*(-z_angle)/180*M_PI); direction = QVector3D(cosPhi*direction.x()+sinPhi*direction.z(),direction.y(), cosPhi*direction.z()-sinPhi*direction.x()); QMatrix4x4 rotMat; rotMat.setToIdentity(); rotMat.rotate(mouse_sens*(-x_angle),QVector3D::crossProduct(direction,QVector3D(0,1,0))); QVector3D tmpVec = (rotMat*QVector4D(direction)).toVector3D(); tmpVec.normalize(); double angleTheta = QVector3D::dotProduct(tmpVec,QVector3D(0,1,0)); if(qAbs(angleTheta) < 0.9){ rotMat.setToIdentity(); rotMat.rotate(mouse_sens*(-x_angle)*(1-qAbs(angleTheta)),QVector3D::crossProduct(direction,QVector3D(0,1,0))); QVector3D tmpVec = (rotMat*QVector4D(direction)).toVector3D(); tmpVec.normalize(); direction = tmpVec; } side_direction = QVector3D(cosPhi*side_direction.x()+sinPhi*side_direction.z(),0, cosPhi*side_direction.z()-sinPhi*side_direction.x()); updown_direction = QVector3D::crossProduct(direction,side_direction); /* rot_angles[0] += mouse_sens*(z_angle);//przesuniecie X rot_angles[1] -= mouse_sens*(x_angle);//przesuniecie Y if(rot_angles[1] > 90) rot_angles[1] = 90; if(rot_angles[1] <-90) rot_angles[1] = -90; // przesuniecie do przodu direction = QVector3D(-sin(rot_angles[0]/180*M_PI),sin(rot_angles[1]/180*M_PI),cos(rot_angles[0]/180*M_PI)); // przesuniece na boki side_direction = QVector3D(sin((rot_angles[0]+90)/180*M_PI),0,-cos((rot_angles[0]+90)/180*M_PI)); // przesuwanie gora dol updown_direction = QVector3D::crossProduct(direction,side_direction); */ direction.normalize(); side_direction.normalize(); updown_direction.normalize(); }
cv::Vec3f BlinnMaterial::brdf(const HitRecord &hit, QVector3D direction) const { QVector3D normal = hit.getSurfaceNormal(); normal.normalize(); QVector3D wo = -hit.getRay().getDirection().normalized(); QVector3D wi = -direction.normalized(); if(signum(QVector3D::dotProduct(normal, wo)) != signum(QVector3D::dotProduct(normal, wi))) { return cv::Vec3f(); } if(QVector3D::dotProduct(normal, wo) < 0) normal *= -1; QVector3D wh = wi + wo; wh.normalize(); return kd * (1 / M_PI) + ks * D(wh, normal) * G(wi, wo, normal) / (4 * QVector3D::dotProduct(wo, normal) * QVector3D::dotProduct(wi, normal)); }
void LightRendererFlat::meshChanged(const QVector<MeshLoader::Face> &faces){ storedFaces.clear(); for (MeshLoader::Face f : faces){ Face newFace; for (int i = 0; i < 3; i++){ newFace[i].position.setX(f[i].x); newFace[i].position.setY(f[i].y); newFace[i].position.setZ(f[i].z); newFace[i].position.setW(1.0); newFace[i].color.setX(f[i].r); newFace[i].color.setY(f[i].g); newFace[i].color.setZ(f[i].b); } QVector3D a = newFace[0].position.toVector3D(); QVector3D b= newFace[1].position.toVector3D(); QVector3D c = newFace[2].position.toVector3D(); QVector3D normal = QVector3D::crossProduct(b - a, c - a); normal.normalize(); newFace[0].normal = normal; newFace[1].normal = normal; newFace[2].normal = normal; storedFaces.append(newFace); } }
void SimpleRoadGraph::_generateMeshVertices(ucore::TextureManager* textureManager) { ucore::RenderableQuadList* renderable = new ucore::RenderableQuadList(textureManager->get("data/textures/street.jpg")); roadGraphEdgeIter ei, eend; for (boost::tie(ei, eend) = boost::edges(myRoadGraph); ei != eend; ++ei) { SimpleRoadGraphEdge* edge = &myRoadGraph[*ei]; QVector3D pt1 = myRoadGraph[boost::source(*ei, myRoadGraph)].getPt(); QVector3D pt2 = myRoadGraph[boost::target(*ei, myRoadGraph)].getPt(); QVector3D vec = pt2 - pt1; vec = QVector3D(-vec.y(), vec.x(), 0.0f); vec.normalize(); QVector3D p0 = pt1 + vec * edge->getWidth() / 2.0f + QVector3D(0, 0, heightAboveGround); QVector3D p1 = pt1 - vec * edge->getWidth() / 2.0f + QVector3D(0, 0, heightAboveGround); QVector3D p2 = pt2 - vec * edge->getWidth() / 2.0f + QVector3D(0, 0, heightAboveGround); QVector3D p3 = pt2 + vec * edge->getWidth() / 2.0f + QVector3D(0, 0, heightAboveGround); QVector3D normal = ucore::Util::calculateNormal(p0, p1, p2); //renderable2->addQuad(p0, p1, p2, p3, normal, color); renderable->addQuad(p0, p1, p2, p3, normal, 0, 1, 0, 1); } renderables.push_back(renderable); }
/*! \since 5.5 Returns the shortest arc quaternion to rotate from the direction described by the vector \a from to the direction described by the vector \a to. \sa fromDirection() */ QQuaternion QQuaternion::rotationTo(const QVector3D &from, const QVector3D &to) { // Based on Stan Melax's article in Game Programming Gems const QVector3D v0(from.normalized()); const QVector3D v1(to.normalized()); float d = QVector3D::dotProduct(v0, v1) + 1.0f; // if dest vector is close to the inverse of source vector, ANY axis of rotation is valid if (qFuzzyIsNull(d)) { QVector3D axis = QVector3D::crossProduct(QVector3D(1.0f, 0.0f, 0.0f), v0); if (qFuzzyIsNull(axis.lengthSquared())) axis = QVector3D::crossProduct(QVector3D(0.0f, 1.0f, 0.0f), v0); axis.normalize(); // same as QQuaternion::fromAxisAndAngle(axis, 180.0f) return QQuaternion(0.0f, axis.x(), axis.y(), axis.z()); } d = std::sqrt(2.0f * d); const QVector3D axis(QVector3D::crossProduct(v0, v1) / d); return QQuaternion(d * 0.5f, axis).normalized(); }
void utils::CalcNormals(const unsigned int* pIndices, unsigned int IndexCount, VertexTex *pVertices, unsigned int VertexCount) { unsigned int Index0; unsigned int Index1; unsigned int Index2; // Accumulate each triangle normal into each of the triangle vertices for (unsigned int i = 0; i < IndexCount; i += 3) { Index0 = pIndices[i]; Index1 = pIndices[i+1]; Index2 = pIndices[i+2]; QVector3D v1 = pVertices[Index1].getPos() - pVertices[Index0].getPos(); QVector3D v2 = pVertices[Index2].getPos() - pVertices[Index0].getPos(); QVector3D Normal = QVector3D::crossProduct(v1, v2); Normal.normalize(); pVertices[Index0].setNormal(pVertices[Index0].getNormal()+Normal); pVertices[Index1].setNormal(pVertices[Index1].getNormal()+Normal); pVertices[Index2].setNormal(pVertices[Index2].getNormal()+Normal); } // Normalize all the vertex normals for (unsigned int i = 0; i < VertexCount; i++) { pVertices[i].setNormal(pVertices[i].getNormal().normalized()); qDebug() << "normal " << pVertices[i].getNormal(); } }
vector<QVector3D> LayeredHairMesh::internalForces(const vector<QVector3D> &state) const { vector<QVector3D> ifs(state.size(), QVector3D()); float ks(5), kd(2), ko(1); int compSize = state.size() / 2; float lenSeg = compSize / seg; for (int i = 4; i < compSize; i ++) { // Spring restriction QVector3D dir = state[i - 4] - state[i]; float deltaLen = dir.length() - restLen[i]; dir.normalize();// *deltaLen; QVector3D fs = ks * dir * deltaLen; QVector3D fd = kd * QVector3D::dotProduct( state[i - 4 + compSize] - state[i + compSize], dir) * dir; // Constrant to original mesh float segU = (1 - (float)(i / seg) / lenSeg); QVector3D distDir = (points[i] - state[i]) * ko; ifs[i] = state[i + compSize]; ifs[i + compSize] += fs + fd - state[i + compSize] * 0.1 + distDir; ifs[i - 4 + compSize] -= fs + fd; } for (int i = 0; i < 4; i++) { ifs[i] = QVector3D(); ifs[i + compSize] = QVector3D(); } return ifs; }
void BulletComponent::update(float delta) { EnemyComponent *enemy = target->getComponent<EnemyComponent>(); if(enemy != nullptr) { destination = target->getPosition(); } QVector3D dir = -(this->getEntity()->getPosition() - destination); if(dir.length() < 10) { if(enemy != nullptr) { enemy->takeDamage(damage); QVector3D v = getEntity()->getPosition(); v.setX(v.x() / 768); v.setY(v.y() / 624); v.setZ(0); FMODManager::getInstance()->setCurrentEvent("event:/hit"); FMODManager::getInstance()->setEventInstancePosition(v); FMODManager::getInstance()->setEventInstanceVolume(0.4); FMODManager::getInstance()->setParameterValue("pitch", 0.3 + (qrand() % 200) * 0.001); FMODManager::getInstance()->startEventInstance(); } this->getEntity()->release(); } else { dir.normalize(); this->getEntity()->setPosition(this->getEntity()->getPosition() + dir * speed * delta); } }
QVector3D Bird::align(QVector<Bird> birds) { float neighbordist = 50; QVector3D sum = QVector3D(0,0,0); int count = 0; QVector<Bird>::iterator bird; for(bird = birds.begin(); bird != birds.end(); bird++) { float d = (position - (*bird).position).length(); if ((d > 0) && (d < neighbordist)) { sum+= (*bird).velocity; count++; } } if (count > 0) { sum /= (float)count; sum.normalize(); sum *= maxvelocity; QVector3D steer = sum-velocity; limit(steer,maxforce); return steer; } else { return QVector3D(0,0,0); } }
void SphericalCalculator::addFace(std::vector<Face> &faces,const Point &a,const Point &b,const Point &c) { Face f; f.x[0][0]=a.x[0]; f.x[0][1]=a.x[1]; f.x[0][2]=a.x[2]; f.x[1][0]=b.x[0]; f.x[1][1]=b.x[1]; f.x[1][2]=b.x[2]; f.x[2][0]=c.x[0]; f.x[2][1]=c.x[1]; f.x[2][2]=c.x[2]; QVector3D aa(f.x[0][0],f.x[0][1],f.x[0][2]); QVector3D bb(f.x[1][0],f.x[1][1],f.x[1][2]); QVector3D cc(f.x[2][0],f.x[2][1],f.x[2][2]); QVector3D nv; nv=-QVector3D::crossProduct(bb-aa,cc-aa); nv.normalize(); f.n[0]=nv.x(); f.n[1]=nv.y(); f.n[2]=nv.z(); faces.push_back(f); }
void TrackBall::move(const QPointF& p) { if (!m_pressed) return; QVector3D lastPos3D = QVector3D(m_lastPos.x(), m_lastPos.y(), 0.0f); float sqrZ = 1 - QVector3D::dotProduct(lastPos3D, lastPos3D); if (sqrZ > 0) lastPos3D.setZ(sqrt(sqrZ)); else lastPos3D.normalize(); QVector3D currentPos3D = QVector3D(p.x(), p.y(), 0.0f); sqrZ = 1 - QVector3D::dotProduct(currentPos3D, currentPos3D); if (sqrZ > 0) currentPos3D.setZ(sqrt(sqrZ)); else currentPos3D.normalize(); QVector3D axis = QVector3D::crossProduct(lastPos3D, currentPos3D); float angle = 360 / PI * asin(sqrt(QVector3D::dotProduct(axis, axis))); axis.normalize(); m_rotation = QQuaternion::fromAxisAndAngle(axis, angle) * m_rotation; m_lastPos = p; }
float G(const QVector3D& wi, const QVector3D& wo, const QVector3D& normal) { using std::min; QVector3D wh = wi + wo; wh.normalize(); return min(1.f, 2 * QVector3D::dotProduct(normal, wh) * min(QVector3D::dotProduct(normal, wo), QVector3D::dotProduct(normal, wi)) / QVector3D::dotProduct(wo, wh)); }
QVector3D MathUtil::Slerp(QVector3D p1, QVector3D p2, float i) { p1.normalize(); p2.normalize(); QVector3D c = QVector3D::crossProduct(p1, p2); if (c.length() < 0.001f) c = QVector3D(0, 1, 0); c.normalize(); float angle = acosf(QVector3D::dotProduct(p1, p2)); return GetRotatedAxis(i * angle, p1, c); }
void SatHorizon::render(QMatrix4x4 projection, float distance, QQuaternion quat, QVector3D posnorm, float alt, QColor rendercolor) { float radius = sqrt( alt * alt - 1 ) / alt; float theta = acos(QVector3D::dotProduct(QVector3D(0,0,1), posnorm)); QVector3D vecnorm = QVector3D::crossProduct(QVector3D(0,0,1), posnorm); vecnorm.normalize(); createCircleBuffer(radius, SEGMENTS); QMatrix4x4 modelview; modelview.translate(0.0, 0.0, distance); modelview.rotate(quat); modelview.translate(posnorm * (1/alt) * (alt > 1.5 ? 1.0015 : 1.0001)); modelview.rotate(theta * 180.0f/ PI, vecnorm ); posBuf.bind(); posBuf.write(0, vertexData, SEGMENTS * sizeof(QVector3D)); posBuf.release(); program->bind(); program->setUniformValue("MVP", projection * modelview); QMatrix3x3 norm = modelview.normalMatrix(); program->setUniformValue("NormalMatrix", norm); program->setUniformValue("outcolor", QVector4D(rendercolor.redF(), rendercolor.greenF(), rendercolor.blueF(), 1.0f)); QOpenGLVertexArrayObject::Binder vaoBinder(&vao); glDrawArrays(GL_LINE_LOOP, 0, SEGMENTS); }
void PlanetElement::setAtmoUniforms(ShaderProgram * program) { updateWaveLength(program); QVector3D lightPosition = SceneData::Instance().getLight("sunlight")->position; QVector3D lightDirection = lightPosition - planet->position; lightDirection.normalize(); float rayleigh = 0.0025f; // Rayleigh scattering constant float rayleigh4Pi = rayleigh * 4.0f * M_PI; float mie = 0.0010f; // Mie scattering constant float mie4Pi = mie * 4.0f * M_PI; float sun = 20.0f; // Sun brightness constant float g = -0.990f; // The Mie phase asymmetry factor // float mieScaleDepth = 0.1f; program->use(); program->setUniform("lightPosition", lightDirection); program->setUniform("rayleighSun", rayleigh * sun); program->setUniform("mieSun", mie * sun); program->setUniform("rayleigh4Pi", rayleigh4Pi); program->setUniform("mie4Pi", mie4Pi); program->setUniform("g", g); program->setUniform("g2", g * g); program->setUniform("useRayleigh", planet->useRayleigh); program->setUniform("useMie", planet->useMie); program->setUniform("attenuation", planet->useAttenuation); }
void ReferenceWidget::draw () { QPainter p (this); const int MOTOR_RADIUS = 20 / 2 * 3; const int MOTOR_DISTANCE = 17.5 * 3; p.fillRect (rect (), Qt::white); p.setPen (QPen (QColor (0, 0, 0, 255))); p.drawLine (20, 20, 180, 20); //p.setPen (QPen (QColor (0, 0, 0, 255))); //p.drawEllipse (QPoint (width () / 2 + px * MOTOR_DISTANCE, height () / 2 - py * MOTOR_DISTANCE), 3, 3); QVector3D quadroMagn (-my, mx, -mz); quadroMagn.normalize (); QVector3D quadroAccel (ax, ay, az); quadroAccel.normalize (); p.setPen (QPen (QColor (0, 0, 255, 255), 2)); p.drawLine (100, 20, 50 + quadroMagn.y (), 20 + quadroMagn.z ()); /*p.setPen (QPen (QColor (0, 0, 0, 255))); p.setBrush (QBrush (QColor (255, 0, 0, m1))); p.drawEllipse (QPoint (width () / 2 - MOTOR_DISTANCE, height () / 2), MOTOR_RADIUS, MOTOR_RADIUS); p.setBrush (QBrush (QColor (255, 0, 0, m3))); p.drawEllipse (QPoint (width () / 2 + MOTOR_DISTANCE, height () / 2), MOTOR_RADIUS, MOTOR_RADIUS); p.setBrush (QBrush (QColor (255, 0, 0, m2))); p.drawEllipse (QPoint (width () / 2, height () / 2 - MOTOR_DISTANCE), MOTOR_RADIUS, MOTOR_RADIUS); p.setBrush (QBrush (QColor (255, 0, 0, m4))); p.drawEllipse (QPoint (width () / 2, height () / 2 + MOTOR_DISTANCE), MOTOR_RADIUS, MOTOR_RADIUS); p.setBrush (QBrush (QColor (255, 0, 0, 255))); p.setPen (QPen (QColor (0, 0, 0, 255))); p.drawEllipse (QPoint (width () / 2, height () / 2), 3, 3); p.setBrush (QBrush (QColor (255, 0, 0, 255))); p.setPen (QPen (QColor (0, 0, 0, 255))); p.drawEllipse (QPoint (width () / 2 + px * MOTOR_DISTANCE, height () / 2 - py * MOTOR_DISTANCE), 3, 3); p.setBrush (QBrush (QColor (0, 255, 0, 255))); p.setPen (QPen (QColor (0, 0, 0, 255))); p.drawEllipse (QPoint (width () / 2 + px2 * MOTOR_DISTANCE, height () / 2 - py2 * MOTOR_DISTANCE), 3, 3);*/ }
QVector3D ModelRenderer::createPolygonNormal( QVector3D a, QVector3D b, QVector3D c ) { QVector3D ab( b - a ); QVector3D bc( c - b ); QVector3D normal = QVector3D::crossProduct( ab, bc ); //AB BCの外積 normal.normalize();//単位ベクトルにする return normal; }
void myCam::aktivatePlaymode(QVector3D kugelWhite, int abstand) { this->activePlaymode = true; this->distanz = abstand; this->kugelWhite = kugelWhite; QVector3D position; position.setX(kugelWhite.x()); position.setY(kugelWhite.y()); position.setZ(kugelWhite.z()); if(position.x()==0 && position.y()==0 && position.z() == 0) { position.setZ(-1); } position.normalize(); position.setX(position.x()*abstand + kugelWhite.x()); position.setY(8); position.setZ(position.z()*abstand + kugelWhite.z()); //this->viewMatrix.setToIdentity(); //this->viewMatrix.lookAt(position,kugelWhite,QVector3D(0,1,0)); this->queueAnimation(position,kugelWhite,100); position = position-kugelWhite; if(position.x()>0 && position.z()>0) { this->angleY = atan(position.z()/position.x())*180/3.1415926-90; } else if(position.x()<0 && position.z()>0) { this->angleY = atan(-position.x()/position.z())*180/3.1415926; } else if(position.x()<0 && position.z()<0) { this->angleY = atan(position.z()/position.x())*180/3.1415926+90; } else if(position.x()>0 && position.z()<0) { this->angleY = atan(position.x()/-position.z())*180/3.1415926+180; } else if(position.x() == 0) { if(position.z()>0) this->angleY = 0; else this->angleY = 180; } else if(position.z() == 0) { if(position.x()>0) this->angleY = 270; else this->angleY = 90; } this->angleX = 15; this->camRotate(0,0); this->freeCameramode = false; }
QVector3D cube::getCameraVector(PointDDD& polyPoint) { PointDDD cameraPos; cameraPos.setX(SCREENDIST * sin(m_omega) * cos(m_phi)); cameraPos.setY(SCREENDIST * sin(m_omega) * sin(m_phi)); cameraPos.setZ(SCREENDIST * cos(m_omega)); QVector3D res = createVectorByPoint(polyPoint,cameraPos); res.normalize(); return res; }
void GLWidget::checkClick(QMouseEvent *event) { QMatrix4x4 view = this->camera->getMatrix(); QMatrix4x4 perspective = this->camera->projection; GLfloat zNear = 0.0f, zFar = 50.0f; qreal w = this->_screenWidth, h = this->_screenHeight, X = (event->x()-w/2)/(w/2), Y = -(event->y()-h/2)/(h/2); QVector3D rayVector; QVector4D nearPoint, farPoint; QMatrix4x4 invPV = (view*perspective).inverted(); QVector4D out = QVector4D(X, Y, zNear, 1.0); nearPoint = invPV * out; out = QVector4D(X, Y, zFar, 1.0); farPoint = invPV * out; rayVector = farPoint.toVector3D() - nearPoint.toVector3D(); rayVector.normalize(); qreal minZ = 100.0f, z; for (int i = 0, length = this->_objects.size(); i < length; i++) { QLObject* object = this->_objects.at(i); z = object->isHit(rayVector, minZ); if (z < minZ) { qDebug() << z << i; minZ = z; this->selectedIndex = i; } } if (minZ < 100.0f &&event->buttons() & Qt::LeftButton) { this->_objects.at(this->selectedIndex)->clicked(); } else { qDebug() << "NO HIT :/!!" << nearPoint; } return; }
/* multLookAt -- Create a matrix to make an object, such as a camera, "look at" another object or location, from a specified position. Algorithm: The desired transformation is obtained with this 4x4 matrix: | [xaxis] 0 | | [up] 0 | | [-at] 0 | | [eye] 1 | Where 'xaxis', 'up' and 'at' are the new X, Y, and Z axes of the transforned object, respectively, and 'eye' is the input new location of the transformed object. Assumptions: The camera geometry is defined to be facing the negative Z axis. Usage: multLookAt creates a matrix and multiplies it onto the current matrix stack. Typical usage would be as follows: glMatrixMode (GL_MODELVIEW); // Define the usual view transformation here using // gluLookAt or whatever. glPushMatrix(); multLookAt (orig[0], orig[1], orig[2], at[0], at[1], at[2], up[0], up[1], up[2]); // Define "camera" object geometry here glPopMatrix(); Warning: Results become undefined as (at-eye) approaches coincidence with (up). */ void multLookAt (float m[], const QVector3D & ieye, const QVector3D & iat, const QVector3D & iup) { // Compute our new look at vector, which will be // the new negative Z axis of our transformed object. QVector3D atVec; atVec = ieye-iat; atVec.normalize(); QVector3D xVec = QVector3D::crossProduct(atVec, iup); xVec.normalize(); // Calculate the new up vector, which will be the // positive Y axis of our transformed object. Note // that it will lie in the same plane as the new // look at vector and the old up vector. QVector3D upVec = QVector3D::crossProduct(xVec, atVec); // Account for the fact that the geometry will be defined to // point along the negative Z axis. atVec *= -1.f; // Fill out the rest of the 4x4 matrix m[0] = xVec.x(); m[1] = xVec.y(); m[2] = xVec.z(); m[3] = 0.f; // xaxis is m[0..2] m[4] = upVec.x(); m[5] = upVec.y(); m[6] = upVec.z(); m[7] = 0.f; // up is m[4..6] m[8] = atVec.x(); m[9] = atVec.y(); m[10] = atVec.z(); m[11] = 0.f; // -at is m[8..10] m[12] = ieye.x(); m[13] = ieye.y(); m[14] = ieye.z(); m[15] = 1.f; }
QVector3D Bird::seek(QVector3D target) { //might be backwards QVector3D desired = target - position; desired.normalize(); desired*= maxvelocity; //might be backwards QVector3D steer = desired - velocity; limit(steer,maxforce); return steer; }
void Camera::mouseMoveEvent(int dX, int dY) { QVector3D rightVector = lookAtDirection.crossProduct(lookAtDirection, up); rightVector.normalize(); rotationMatrix.setToIdentity(); rotationMatrix.rotate(pitchSpeed*dY*mouseSensitivity, rightVector); rotationMatrix.rotate(yawSpeed*dX*mouseSensitivity, up); lookAtDirection = rotationMatrix*lookAtDirection; up = rotationMatrix*up; lookAtPoint = position + lookAtDirection; updateLookAt(); }
QVector3D Bird::separate(QVector<Bird> birds) { float desiredSeparation = 25.0f; QVector3D steer = QVector3D(0,0,0); int count = 0; QVector<Bird>::iterator bird; for(bird = birds.begin(); bird != birds.end(); bird++) { float d = (position-(*bird).position).length();//QVector3D::distanceToLine(this->position,(*bird).position); //QVector3D:: if(d>0 && d < desiredSeparation) { QVector3D diff = position - (*bird).position; diff.normalize(); diff /= d; steer += diff; count++; } } if(count > 0) { steer /= (float)count; } if(steer.length() > 0) { steer.normalize(); steer *= maxvelocity; steer -= velocity; } steer = limit(steer, maxforce); return steer; }