/*! Returns the transformation to apply to the modelview matrix to present the scene as viewed from the eye() position. \sa apply(), projectionMatrix() */ QMatrix4x4 Camera::modelViewMatrix() const { Q_D(const Camera); QMatrix4x4 m; if (d->motionQuaternion.isIdentity()) { m.lookAt(d->eye, d->center, d->upVector); } else { QVector3D up = d->motionQuaternion.rotatedVector(d->upVector); QVector3D view = d->motionQuaternion.rotatedVector(d->viewVector); QVector3D eye = d->center - view; m.lookAt(eye, d->center, up); } return m; }
void GlWidget::paintGL() { setMatrixes(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); QMatrix4x4 vMatrix; vMatrix.lookAt(camera[0], camera[1], camera[2]); for(int i=0; i<objects.size(); i++){ if(i!=3 || canonFire==true){ shaderProgram.bind(); shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * *objects[i]->getMMatrix()); objects[i]->getTexture()->bind(); DrawReferenceGrid(); shaderProgram.setAttributeArray("vertex", objects[i]->getVertices()->constData()); shaderProgram.enableAttributeArray("vertex"); shaderProgram.setAttributeArray("textureCoordinate", objects[i]->getUVS()->constData()); shaderProgram.enableAttributeArray("textureCoordinate"); glDrawArrays(GL_TRIANGLES, 0, objects[i]->getVertices()->size()); shaderProgram.disableAttributeArray("vertex"); shaderProgram.disableAttributeArray("textureCoordinate"); } } shaderProgram.release(); }
void Painter::paint_3_4(float timef) { paint_3_3_shadowmap(timef); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_depthTex); glActiveTexture(GL_TEXTURE0); QOpenGLShaderProgram * program(m_programs[PaintMode1]); // Task_3_3 - ToDo Begin QVector3D light = m_light.normalized(); QMatrix4x4 L; // L.lookAt(light, center ,up); L.perspective(camera()->fovy(), camera()->viewport().width()/static_cast<float>(camera()->viewport().height()), camera()->zNear(), camera()->zFar()); L.lookAt(m_light,QVector3D(0.0,0.0,0.0),QVector3D(0.0,0.1,0.0)); // QMatrix4x4 ... program->bind(); program->setUniformValue("light", m_light); program->setUniformValue("viewProjection", L); program->release(); // Task_3_3 - ToDo End paint_3_1_scene(true, timef); paint_3_2_label(camera()->viewProjection(), timef); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0); }
void MyGLWidget::paintGL() { // Clear buffer to set color and alpha glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shaderProgram.setUniformValue(unifMatrixPerspective,projectionMatrix); // VIEW TRANSFORMATION QMatrix4x4 viewMatrix ; viewMatrix.lookAt(cameraPos,cameraPos+cameraFront,cameraUp); shaderProgram.setUniformValue(unifMatrixView,viewMatrix); // MODEL TRANSFORMATION (Neues OpenGL) QMatrix4x4 modelMatrix ; // Initialisierung des Modells modelMatrix.setToIdentity(); modelMatrixStack.push(modelMatrix); // Zeit zwischen den Render Bildern elapsedTime = tmrRender.elapsed(); //qDebug() << elapsedTime ; tmrRender.start(); // glBindTexture(GL_TEXTURE_2D,tList[sun]); //qTex->bind(); textures[1]->bind(); textures[1]->bind(); // Übergebe die Textur an die Uniform Variable // Die 0 steht dabei für die verwendete Unit (0=Standard) //shaderProgram.setUniformValue("texture",0); // Triggern des Renderns sonne.render(); // Stack wieder säubern. modelMatrixStack.pop(); //qTex->release(); }
/** * Sets mvMatrix to a lookAt transformation. * Call setPMatrix or setPerspective first. */ void GLESRenderer::setLookAt(const QVector3D & eye,const QVector3D & center,const QVector3D & up ) { QMatrix4x4 m; m.setToIdentity(); m.lookAt(eye, center, up); setMvMatrix(m); }
void Widget::paintGL() { glClearColor(.3, .4, .5, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); myShader.bind(); QMatrix4x4 modelview; modelview.perspective(70, 1.0 * this->width() / this->height(), 0.1, 100); QVector3D to = QVector3D(sin(yaw) * cos(pitch), cos(yaw) * cos(pitch), sin(pitch)); move(); modelview.lookAt(from, from + to, QVector3D(0, 0, 1)); myShader.setUniformValue("mvp", modelview); groundMesh.draw(this, &myShader); modelview.scale(2); myShader.setUniformValue("mvp", modelview); torusMesh.draw(this, &myShader); myShader.release(); // TODO: use QOpenGLDebugLogger }
void Scene::directionLightPass(RenderTarget *target) { ShaderProgram * shader =ShaderPool::getInstance ()->get("dir_light_pass"); shader->use (); int texture_offset = 5; for(int i = 0 ;i<4;i++) { directionLight.getCSM_FBO (i)->BindForReading(GL_TEXTURE0+i+texture_offset); char GLSL_shadowMap_name [30]; sprintf(GLSL_shadowMap_name,"g_shadow_map[%d]",i); directionLight.getCSM_FBO (i)->applyShadowMapTexture (shader,i+texture_offset,GLSL_shadowMap_name); } QMatrix4x4 m; m.setToIdentity (); m_quad->setShaderProgram (shader); shader->setUniformMat4v ("g_MVP_matrix",m.data ()); shader->setUniform2Float ("g_screen_size",1024,768); shader->setUniformInteger ("g_color_map",0); shader->setUniformInteger ("g_position_map",1); shader->setUniformInteger ("g_normal_map",2); shader->setUniformInteger ("g_depth_map",4);//for depth auto camera = target->camera (); if(camera) { shader->setUniform3Float ("g_eye_position", camera->pos ().x(), camera->pos ().y(), camera->pos ().z()); } QMatrix4x4 lightView; lightView.setToIdentity(); QVector3D lightDir = directionLight.getDirection (); QVector3D pos = QVector3D(0,0,0); lightView.lookAt(pos,lightDir,QVector3D(0,1,0)); for(int i =0 ;i <4 ;i++) { if(!camera) break; auto split_frustum_aabb = camera->getSplitFrustumAABB (i); split_frustum_aabb.transForm (target->camera()->getModelTrans ()); split_frustum_aabb.transForm (lightView); auto matrix = getCropMatrix (split_frustum_aabb); QMatrix4x4 light_vp; light_vp = matrix * lightView ; char GLSL_light_VP_name [30]; sprintf(GLSL_light_VP_name,"g_light_vp_matrix[%d]",i); shader->setUniformMat4v (GLSL_light_VP_name,light_vp.data ()); } this->directionLight.apply(shader); this->ambientLight.apply(shader); m_quad->draw (true); }
/*! Returns the transformation to apply to the modelview matrix to present the scene as viewed from the eye position. The \a eye parameter is used to adjust the camera's position horizontally by half of eyeSeparation() if \a eye is QGL::LeftEye or QGL::RightEye. \sa projectionMatrix() */ QMatrix4x4 QGLCamera::modelViewMatrix(QGL::Eye eye) const { Q_D(const QGLCamera); QMatrix4x4 m; QVector3D adjust; if (eye == QGL::LeftEye) adjust = translation(-d->eyeSeparation / 2.0f, 0.0f, 0.0f); else if (eye == QGL::RightEye) adjust = translation(d->eyeSeparation / 2.0f, 0.0f, 0.0f); if (d->motionQuaternion.isIdentity()) { m.lookAt(d->eye + adjust, d->center, d->upVector); } else { QVector3D up = d->motionQuaternion.rotatedVector(d->upVector); QVector3D view = d->motionQuaternion.rotatedVector(d->viewVector); m.lookAt(d->center - view + adjust, d->center, up); } return m; }
void Dragon2Widget::paintGL() { qglClearColor(Qt::white); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_ALPHA_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // use the OpenGL shader program for painting glUseProgram(_program); // set model matrix glUniformMatrix4fv(_modelMatrixLocation, 1, GL_FALSE, _modelMatrix.data()); // set view matrix QMatrix4x4 viewMatrix; viewMatrix.setToIdentity(); viewMatrix.lookAt(QVector3D(0, 0, -1000), QVector3D(0, 0, 0), QVector3D(0, -1, 0)); glUniformMatrix4fv(_viewMatrixLocation, 1, GL_FALSE, viewMatrix.data()); // set projection matrix QMatrix4x4 projectionMatrix; projectionMatrix.setToIdentity(); projectionMatrix.perspective(30, (float)width()/height(), 0.01f, 1e5f); glUniformMatrix4fv(_projectionMatrixLocation, 1, GL_FALSE, projectionMatrix.data()); // bind ArrayBuffer to _vertBuffer glBindBuffer(GL_ARRAY_BUFFER, _vertBuffer); // enable vertex attribute "position" (bound to 0 already) glEnableVertexAttribArray(0); // set the data of vertex attribute "position" using current ArrayBuffer glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(_vertices.first()), 0); // enable vertex attribute "normal" (bound to 1 already) glEnableVertexAttribArray(1); // set the data of vertex attribute "normal" using current ArrayBuffer glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(_vertices.first()), (void*)(3 * sizeof(float))); // bind ElementArrayBuffer to _triangleIndicesBuffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _triangleIndicesBuffer); // draw mesh using the indices stored in ElementArrayBuffer glDrawElements(GL_TRIANGLES, _triangleIndices.size(), GL_UNSIGNED_INT, 0); // disable vertex attributes glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); // unbind buffers glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // restore states glDisable(GL_DEPTH_TEST); glDisable(GL_ALPHA_TEST); glDisable(GL_BLEND); }
void ViewportViewPerspective::initializeViewport(const QVector3D& surfmin, const QVector3D& surfmax, int width, int height) { // add margin to max/min QVector3D diff = 0.01 * _margin * (surfmax - surfmin); QVector3D min = surfmin - diff; QVector3D max = surfmax + diff; // calculate the midpoint of the bounding box, which is used as the center of the // model for rotating the model _midpoint = 0.5 * (min + max); QVector3D panpoint = _midpoint; panpoint.setX(panpoint.x() + _panX); panpoint.setY(panpoint.y() + _panY); panpoint.setZ(panpoint.z() + _panZ); // calculate the distance of the camera to the center of the model, following from // the field of view from the camera float dist = sqrt((max.y() - min.y()) * (max.y() - min.y()) + (max.z() - min.z()) * (max.z() - min.z())); if (dist == 0) dist = 1E-2f; if (atan(_field_of_view) != 0) { _distance = 1.5 * dist / atan(_field_of_view); if (_distance > 1E5) _distance = 1E5; } else _distance = 1E5; // build the vertex transformation matrix from the perspective // and the angle, elevation float aspect_ratio = 1.0; if (height != 0) aspect_ratio = width / static_cast<float>(height); _proj = QMatrix4x4(); // create projection _proj.perspective(RadToDeg(_field_of_view) / _zoom, aspect_ratio, 0.1f, 40.0f); // find the camera location QMatrix4x4 model; model.translate(_panX, _panY, _panZ); model.rotate(-_elevation, 1, 0, 0); model.rotate(_angle, 0, 0, 1); _camera_location = model.map(QVector3D(max.x() + _distance, 0, 0)); // view matrix QMatrix4x4 view; view.lookAt(_camera_location, panpoint, QVector3D(0,0,1)); _view = view; finishSetup(); }
QMatrix4x4 Viewer::getCameraMatrix() { QMatrix4x4 vMatrix; QMatrix4x4 cameraTransformation; auto cameraPosition = QVector3D(0, 0, 2.0); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0); vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection); return mPerspMatrix * vMatrix * mTransformMatrix; }
void Tutorial03::initializeGL(void) { // initialize OpenGL initializeOpenGLFunctions(); bool success; // load and compile vertex shader success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/SimpleTransform.vert"); // load and compile fragment shader success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/SingleColor.frag"); // link the shader program shaderProgram.link(); GLuint programID = shaderProgram.programId(); // Get a handle for our buffers vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace"); // Get a handle to the colors color_location = glGetAttribLocation(programID, "v_color"); // Get a handle for our "MVP" uniform MatrixID = glGetUniformLocation(programID, "MVP"); // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units QMatrix4x4 projection; projection.perspective(45.0, 4.0/3.0, 0.1, 100.0); // Camera matrix QMatrix4x4 view; view.lookAt( QVector3D(4,3,3), // Camera is at (4,3,3), in World Space QVector3D(0,0,0), // and looks at the origin QVector3D(0,1,0) // Head is up (set to 0,-1,0 to look upside-down) ); // Model matrix : an identity matrix (model will be at the origin) QMatrix4x4 model; model.setToIdentity(); // Our ModelViewProjection : multiplication of our 3 matrices MVP = projection * view * model; // Remember, matrix multiplication is the other way around glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); glGenBuffers(1, &colorbuffer); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_colors), triangle_colors, GL_STATIC_DRAW); glClearColor(0.0f, 0.0f, 0.3f, 0.0f); }
void Visu3D::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 mMatrix; QMatrix4x4 vMatrix; QMatrix4x4 cameraTransformation; cameraTransformation.rotate(alpha, 0, 1, 0); cameraTransformation.rotate(beta, 1, 0, 0); QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0); vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection); mMatrix.setToIdentity(); QMatrix4x4 mvMatrix; mvMatrix = vMatrix * mMatrix; shaderProgram.bind(); shaderProgram.setUniformValue("mvpMatrix", pMatrix * mvMatrix); // modele-vue-projection shaderProgram.setUniformValue("greyMax", (float) lvlOfGreyMax); verticesDataBuffer.bind(); shaderProgram.setAttributeBuffer("vertex", GL_FLOAT, 0, 3, 0); shaderProgram.enableAttributeArray("vertex"); verticesDataBuffer.release(); greyDataBuffer.bind(); shaderProgram.setAttributeBuffer("inGrey", GL_FLOAT, 0, 1, 0); shaderProgram.enableAttributeArray("inGrey"); greyDataBuffer.release(); glDrawArrays(GL_TRIANGLES, 0, verticesData.size()); shaderProgram.disableAttributeArray("vertex"); shaderProgram.disableAttributeArray("inGrey"); shaderProgram.release(); glClear(GL_DEPTH_BUFFER_BIT); // On annule le test de profondeur afin //que le repere soit devant QMatrix4x4 mvpMatrix = pMatrix * mvMatrix; // Matrice de transfo pour que le repere tourne //en meme temps que la scene makeLandMark(mvpMatrix); }
void Scene::shadowPassDirectional(RenderTarget * target) { auto light = this->getDirectionalLight (); for(int j =0;j<4;j++) { // Clear color and depth buffer glDepthMask(GL_TRUE); glEnable(GL_DEPTH_TEST); light->getCSM_FBO ( j )->BindForWriting (); glClear(GL_DEPTH_BUFFER_BIT); ShaderProgram * shadow_shader = ShaderPool::getInstance ()->get("shadow"); PipeLine p; for (std::vector<Entity *>::iterator i =m_entityList.begin();i!=m_entityList.end();i++) { shadow_shader->use (); Entity * entity = (* i); if(!entity->isEnableShadow()) continue; if(target->isIgnoreEntity (entity))continue; p.setModelMatrix(target->auxMatrix ()*entity->getModelTrans()); QMatrix4x4 lightView; lightView.setToIdentity(); auto lightDir = directionLight.getDirection (); QVector3D pos = QVector3D(0,0,0); lightView.lookAt(pos,lightDir,QVector3D(0,1,0)); auto aabb = camera ()->getSplitFrustumAABB ( j ); aabb.transForm (camera()->getModelTrans ()); aabb.transForm (lightView); auto matrix = getCropMatrix (aabb); p.setProjectionMatrix(matrix); p.setViewMatrix(lightView); ShaderProgram * tmp_shader = entity->getShaderProgram (); entity->setShaderProgram (shadow_shader); setEntityBoneTransform(entity); p.apply(shadow_shader); entity->draw(); entity->setShaderProgram (tmp_shader); } glDepthMask(GL_FALSE); glDisable(GL_DEPTH_TEST); glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);//switch the frame buffer back. } }
void DragonWidget::paintGL() { makeCurrent(); qglClearColor(Qt::white); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_ALPHA_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glShadeModel(GL_SMOOTH); // set model-view matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); QMatrix4x4 viewMatrix; viewMatrix.lookAt(QVector3D(0, 0, -1000), QVector3D(0, 0, 0), QVector3D(0, -1, 0)); glMultMatrixf(viewMatrix.data()); glMultMatrixf(_modelMatrix.data()); // now model-view matrix = lookAt(...) * _modelMatrix // set projection matrix // (projection matrix is often set in resizeGL(width, height) functions, // since it only relies on the size of the window in most cases) glMatrixMode(GL_PROJECTION); glLoadIdentity(); QMatrix4x4 projectionMatrix; projectionMatrix.perspective(30.0f, (float)width() / height(), 0.01f, 1e5f); glMultMatrixf(projectionMatrix.data()); // draw mesh glBegin(GL_TRIANGLES); for(int vid : _triangleIndices) { glColor3fv(&(_vertices[vid].normal[0])); glVertex3fv(&(_vertices[vid].position[0])); } glEnd(); glDisable(GL_DEPTH_TEST); glDisable(GL_ALPHA_TEST); glDisable(GL_BLEND); }
void GlWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 mMatrix; QMatrix4x4 vMatrix; //std::vector<float> * euler_angles= new vector<float>(); //std::cout << "State is ";QuaternionToEuler2(*orientation, euler_angles); QMatrix4x4 cameraTransformation; cameraTransformation.rotate(alpha, 0, 1, 0); cameraTransformation.rotate(beta, 1, 0, 0); cameraTransformation.rotate(*orientation); QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0); vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection); shaderProgram.bind(); QMatrix4x4 quat_matrix; quat_matrix.rotate(0,0,1,0); shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix * quat_matrix); shaderProgram.setAttributeArray("vertex", vertices.constData()); shaderProgram.enableAttributeArray("vertex"); shaderProgram.setAttributeArray("color", colors.constData()); shaderProgram.enableAttributeArray("color"); glRotatef(90, 0, 0, 1.0); glDrawArrays(GL_TRIANGLES, 0, vertices.size()); shaderProgram.disableAttributeArray("vertex"); shaderProgram.disableAttributeArray("color"); shaderProgram.release(); }
void PreviewPane::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); program->bind(); { QMatrix4x4 projection; projection.ortho(-viewWidth / 2.0 * wheelValue, viewWidth / 2.0 * wheelValue, -viewHeight / 2.0 * wheelValue, viewHeight / 2.0 * wheelValue, 0.01f, 1000.0f); QMatrix4x4 view; view.lookAt(QVector3D(panX, panY, 1.0), QVector3D(panX, panY, 0.0), QVector3D(0.0, 1.0, 0.0)); QMatrix4x4 model; model.setToIdentity(); model.scale(float(imageWidth) / (float)imageHeight, 1.0f, 1.0f); mvp = projection * view * model; program->setUniformValue("MVP", mvp); object.bind(); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureRed); program->setUniformValue("texture1", 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textureGreen); program->setUniformValue("texture2", 1); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textureBlue); program->setUniformValue("texture3", 2); program->setUniformValue("redEnabled", (float)redChannel); program->setUniformValue("greenEnabled", (float)greenChannel); program->setUniformValue("blueEnabled", (float)blueChannel); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindTexture(GL_TEXTURE_2D, 0); object.release(); } program->release(); this->update(); }
void MainWidget::paintGL() { // Clear color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //! [6] QMatrix4x4 view; view.setToIdentity(); QMatrix4x4 model; model.setToIdentity(); // Calculate model view transformation //model.rotate(rotation); eye.setX(-distance*sin(angleYtmp)*cos(angleXtmp)+center.x() ); eye.setY( distance*cos(angleYtmp)+center.y() ); eye.setZ( distance*sin(angleYtmp)*sin(angleXtmp)+center.z() ); view.lookAt(eye, center, up); //matrix.translate(0.0, -5.0, -15.0); //matrix.translate(0.0, -5.0, 0.0); // Set modelview-projection matrix program->setUniformValue("mvp_matrix", projection * view * model); //! [6] // Using texture unit 0 which contains cube.png program->setUniformValue("texture", 0); // Draw cube geometry //geometries->drawCubeGeometry(program); if(drawState == 0){//POINT_CLOUD FSController::getInstance()->geometries->drawPointCloud(program); }else if(drawState == 1){//SURFACE_MESH FSController::getInstance()->geometries->drawSurfaceMesh(program); } FSController::getInstance()->geometries->drawGroundPlane(program); }
void ViewportViewPlan::initializeViewport(const QVector3D& min, const QVector3D& max, int width, int height) { float breadth = max.y() - min.y(); // calculate the midpoint of the bounding box _midpoint = 0.5 * (min + max); QVector3D panpoint(_midpoint); panpoint.setX(panpoint.x() + _panX); panpoint.setY(panpoint.y() + _panY); float aspect_ratio = 1.0; if (height != 0) aspect_ratio = width / static_cast<float>(height); float hi, margin; _proj = QMatrix4x4(); // find view extents using aspect ratio if (_midpoint.x() / aspect_ratio >= breadth) { margin = _midpoint.x() * .01 * _margin; hi = (_midpoint.x() / aspect_ratio + margin) / _zoom; float x = (_midpoint.x() + margin) / _zoom; _proj.ortho(-x, x, -hi, hi, 0.1f, 40.0f); } else { margin = breadth * .01 * _margin; hi = (breadth * aspect_ratio + margin) / _zoom; float y = (breadth + margin) / _zoom; _proj.ortho(-hi, hi, -y, y, 0.1f, 40.0f); } // find the camera location _camera_location = QVector3D(panpoint.x(), panpoint.y(), max.z() + 20); // view matrix QMatrix4x4 view; view.lookAt(_camera_location, QVector3D(panpoint.x(), panpoint.y(), 0), QVector3D(0,1,0)); _view = view; finishSetup(); }
void ViewportViewProfile::initializeViewport(const QVector3D& min, const QVector3D& max, int width, int height) { // calculate the midpoint of the bounding box, which is used as the center of the // model for rotating the model _midpoint = 0.5 * (min + max); QVector3D panpoint = _midpoint; panpoint.setX(panpoint.x() + _panX); panpoint.setZ(panpoint.z() + _panY); float aspect_ratio = 1.0; if (height != 0) aspect_ratio = width / static_cast<float>(height); float hi, margin; _proj = QMatrix4x4(); if (_midpoint.x() / aspect_ratio >= _midpoint.z()) { margin = _midpoint.x() * .01 * _margin; hi = (_midpoint.x() / aspect_ratio + margin) / _zoom; float x = (_midpoint.x() + margin) / _zoom; _proj.ortho(-x, x, -hi, hi, 0.1f, 40.0f); } else { margin = _midpoint.z() * .01 * _margin; hi = (_midpoint.z() * aspect_ratio + margin) / _zoom; float z = (_midpoint.z() + margin) / _zoom; _proj.ortho(-hi, hi, -z, z, 0.1f, 40.0f); } // find the camera location _camera_location = QVector3D(panpoint.x(), min.y() - 20, panpoint.z()); // view matrix QMatrix4x4 view; view.lookAt(_camera_location, QVector3D(panpoint.x(), 0, panpoint.z()), QVector3D(0,0,1)); _view = view; finishSetup(); }
void GlWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 mMatrix; QMatrix4x4 vMatrix; QMatrix4x4 cameraTransformation; cameraTransformation.rotate(alpha, 0, 1, 0); cameraTransformation.rotate(beta, 1, 0, 0); QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0); vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection); shaderProgram.bind(); shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix); shaderProgram.setUniformValue("texture", 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glActiveTexture(0); shaderProgram.setAttributeArray("vertex", vertices.constData()); shaderProgram.enableAttributeArray("vertex"); shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData()); shaderProgram.enableAttributeArray("textureCoordinate"); glDrawArrays(GL_TRIANGLES, 0, vertices.size()); shaderProgram.disableAttributeArray("vertex"); shaderProgram.disableAttributeArray("textureCoordinate"); shaderProgram.release(); }
void OpenGLWidget::paintGL() { if(!m_glFuncs) { return; } m_shaderProgram.bind(); m_vertexArray.bind(); m_texture.bind(); static const QVector3D cameraUp(0.0f, 1.0f, 0.0f); QVector3D cameraRight = QVector3D::crossProduct(m_cameraFront, cameraUp).normalized(); m_cameraPosition += m_forwardSpeed * 50 / 1000 * m_cameraFront; m_cameraPosition += m_strafeSpeed * 50 / 1000 * cameraRight; m_cameraPosition += m_upSpeed * 50 / 1000 * cameraUp; QMatrix4x4 viewMatrix; viewMatrix.lookAt(m_cameraPosition, m_cameraPosition + m_cameraFront, cameraUp); QMatrix4x4 projectionMatrix; projectionMatrix.perspective(45.0f, float(width()) / height(), 0.1f, 100.0f); m_shaderProgram.setUniformValue("viewMatrix", viewMatrix); m_shaderProgram.setUniformValue("projectionMatrix", projectionMatrix); m_glFuncs->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(!m_chunkMesh.isEmpty()) { m_shaderProgram.setUniformValue("modelMatrix", QMatrix4x4()); m_glFuncs->glDrawElements(GL_TRIANGLES, m_chunkMesh.getNumberOfElements(), GL_UNSIGNED_INT, 0); } m_texture.release(); m_vertexArray.release(); m_shaderProgram.release(); }
//motion: rotate and translate object. no moving camera void GlWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 mMatrix; QMatrix4x4 vMatrix; //QVector3D modelPosition = mMatrix * QVector3D(newX,newY,newZ); //QVector3D modelUpPosition = mMatrix * QVector3D(0,1,0); mMatrix.translate(newX,newY,newZ); mMatrix.rotate(alpha,0,1,0); mMatrix.rotate(beta,0,0,1); QMatrix4x4 cameraTransformation; //cameraTransformation.setToIdentity(); //cameraTransformation.rotate(alpha,0,1,0); //cameraTransformation.rotate(beta,1,0,0); QVector3D cameraPosition = cameraTransformation * QVector3D(0,0,5); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0,1,0); vMatrix.lookAt(cameraPosition,QVector3D(0,0,0), cameraUpDirection); shaderProgram.bind(); shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix); shaderProgram.setUniformValue("color", QColor(Qt::white)); shaderProgram.setAttributeArray("vertex",vertices.constData()); shaderProgram.enableAttributeArray("vertex"); glDrawArrays(GL_TRIANGLES,0,vertices.size()); shaderProgram.disableAttributeArray("vertex"); shaderProgram.release(); }
void Painter::paint_3_3_shadowmap(float timef) { glBindFramebuffer(GL_FRAMEBUFFER, m_depthFbo); glViewport(0, 0, ShadowMapSize, ShadowMapSize); glClearDepth(1.f); glClear(GL_DEPTH_BUFFER_BIT); QOpenGLShaderProgram * program(m_programs[ShadowMapProgram]); // Task_3_3 - ToDo Begin QMatrix4x4 L; QVector3D light = m_light.normalized(); QVector3D center(0.0, 0.0, 0.0); QVector3D up = QVector3D::crossProduct(QVector3D::crossProduct(light - center, QVector3D(0.0, 1.0, 0.0)), light - center); L.perspective(camera()->fovy(), camera()->viewport().width()/static_cast<float>(camera()->viewport().height()), camera()->zNear(), camera()->zFar()); // L.lookAt(light, center ,up); L.lookAt(m_light,QVector3D(0.f,0.f,0.f),QVector3D(0.f,0.1f,0.f)); // Task_3_3 - ToDo End program->bind(); program->setUniformValue("viewProjection", L); program->release(); m_hpicgs->draw(*this, *program); m_portcc->draw(*this, *program); paint_3_2_label(L, timef); glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, camera()->viewport().width(), camera()->viewport().height()); }
void Scene::spotLightPass(RenderTarget * target) { if(!this->spotLights.empty ()) { for(int i =0;i<spotLights.size ();i++) { PointLight * light = this->spotLights[i]; light->shadowFBO ()->BindForReading(GL_TEXTURE3); ShaderProgram * shader = ShaderPool::getInstance ()->get("spot_light_pass"); shader->use (); light->shadowFBO ()->applyShadowMapTexture (shader,3); light->apply (shader,0); m_quad->setShaderProgram (shader); QMatrix4x4 m; m.setToIdentity (); auto camera = target->camera (); shader->setUniformMat4v ("g_MVP_matrix",m.data ()); shader->setUniform2Float ("g_screen_size",1024,768); shader->setUniformInteger ("g_color_map",0); shader->setUniformInteger ("g_position_map",1); shader->setUniformInteger ("g_normal_map",2); shader->setUniform3Float ("g_eye_position", camera->pos ().x(), camera->pos ().y(), camera->pos ().z()); QMatrix4x4 lightView; lightView.setToIdentity(); lightView.lookAt(spotLights[0]->getPos(),this->spotLights[0]->getPos()+this->spotLights[0]->getDirection(),QVector3D(0,1,0)); QMatrix4x4 light_vp; light_vp = camera->getProjection () * lightView ; shader->setUniformMat4v ("g_light_vp_matrix",light_vp.data ()); m_quad->draw (true); } } }
void Scene::shadowPassForSpot(SpotLight * light,RenderTarget * target) { // Clear color and depth buffer glDepthMask(GL_TRUE); glEnable(GL_DEPTH_TEST); light->shadowFBO ()->BindForWriting(); glClear(GL_DEPTH_BUFFER_BIT); ShaderProgram * shadow_shader = ShaderPool::getInstance ()->get("shadow"); PipeLine p; for (std::vector<Entity *>::iterator i =m_entityList.begin();i!=m_entityList.end();i++) { shadow_shader->use (); Entity * entity = (* i); if(!entity->isEnableShadow()) continue; if(target->isIgnoreEntity (entity))continue; Camera * camera =entity->getCamera(); p.setModelMatrix(target->auxMatrix ()*entity->getModelTrans()); p.setProjectionMatrix(camera->getProjection()); QMatrix4x4 view; view.setToIdentity(); auto lightDir = light->getDirection (); lightDir.normalize (); view.lookAt(light->getPos(),light->getPos()+lightDir,QVector3D(0,1,0)); p.setViewMatrix(view); ShaderProgram * tmp_shader = entity->getShaderProgram (); entity->setShaderProgram (shadow_shader); setEntityBoneTransform(entity); p.apply(shadow_shader); entity->draw(); entity->setShaderProgram (tmp_shader); } glDepthMask(GL_FALSE); glDisable(GL_DEPTH_TEST); glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);//switch the frame buffer back. }
void GLWidget::paintMesh() { QMatrix4x4 mMatrix; QMatrix4x4 vMatrix; QMatrix4x4 cameraTransformation; cameraTransformation.rotate(alpha, 0, 1, 0); cameraTransformation.rotate(beta, 1, 0, 0); QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0); vMatrix.lookAt(cameraPosition, lastCenterPosition, cameraUpDirection); mMatrix.setToIdentity(); QMatrix4x4 mvMatrix; mvMatrix = vMatrix * mMatrix; QMatrix3x3 normalMatrix; normalMatrix = mvMatrix.normalMatrix(); QMatrix4x4 lightTransformation; lightTransformation.rotate(lightAngle, 0, 1, 0); QVector3D lightPosition = lightTransformation * QVector3D(0, 10, 10); shaderProgram.bind(); shaderProgram.setUniformValue("mvpMatrix", pMatrix * mvMatrix); shaderProgram.setUniformValue("mvMatrix", mvMatrix); shaderProgram.setUniformValue("normalMatrix", normalMatrix); shaderProgram.setUniformValue("lightPosition", vMatrix * lightPosition); shaderProgram.setUniformValue("ambientColor", QColor(32, 32, 32)); shaderProgram.setUniformValue("diffuseColor", QColor(128, 128, 128)); shaderProgram.setUniformValue("specularColor", QColor(255, 255, 255)); shaderProgram.setUniformValue("ambientReflection", (GLfloat) 1.0); shaderProgram.setUniformValue("diffuseReflection", (GLfloat) 1.0); shaderProgram.setUniformValue("specularReflection", (GLfloat) 1.0); shaderProgram.setUniformValue("shininess", (GLfloat) 100.0); glPointSize(INITIAL_POINT_SIZE); renderGrid(); renderMesh(mode); shaderProgram.release(); mMatrix.setToIdentity(); mMatrix.translate(lightPosition); mMatrix.rotate(lightAngle, 0, 1, 0); mMatrix.rotate(45, 1, 0, 0); // mMatrix.scale(0.1); coloringShaderProgram.bind(); coloringShaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix); coloringShaderProgram.setAttributeArray("vertex", lightSource.getSpotlightVertices().constData()); coloringShaderProgram.enableAttributeArray("vertex"); coloringShaderProgram.setAttributeArray("color", lightSource.getSpotlightColors().constData()); coloringShaderProgram.enableAttributeArray("color"); glDrawArrays(GL_TRIANGLES, 0, lightSource.getSpotlightVertices().size()); coloringShaderProgram.disableAttributeArray("vertex"); coloringShaderProgram.disableAttributeArray("color"); coloringShaderProgram.release(); }
void TApplication::RenderToFBO() { Fbo->bind(); glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); Shader.bind(); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); QMatrix4x4 view; view.lookAt(QVector3D(0.0, 0.0, 6.0), QVector3D(0.0, 0.0, 0.0), QVector3D(0.0, 1.0, 0.0)); QMatrix4x4 projection; projection.perspective(45.0f, float(WINDOW_WIDTH) / float(WINDOW_HEIGHT), 0.1f, 10.0f); Shader.setUniformValue("projection", projection); Shader.setUniformValue("view", view); Shader.setUniformValue("lightIntensities", QVector3D(1.0, 1.0, 1.0)); Shader.setUniformValue("lightPosition", QVector3D(-30.0, 30.0, 30.0)); QMatrix4x4 model; model.translate(0, 0, 0.0); model.rotate(AngleX, 1.0, 0.0, 0.0); model.rotate(AngleY, 0.0, 1.0, 0.0); model.rotate(AngleZ, 0.0, 0.0, 1.0); model.scale(0.42); QMatrix3x3 normalMatrix = model.normalMatrix(); Shader.setUniformValue("model", model); Shader.setUniformValue("normalMatrix", normalMatrix); VertexBuff->bind(); GLint attribute; GLint offset = 0; attribute = Shader.attributeLocation("gl_Vertex"); Shader.enableAttributeArray(attribute); Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 3, sizeof(TVertex)); offset += sizeof(QVector3D); attribute = Shader.attributeLocation("gl_Normal"); Shader.enableAttributeArray(attribute); Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 3, sizeof(TVertex)); offset += sizeof(QVector3D); attribute = Shader.attributeLocation("vertTexCoord"); Shader.enableAttributeArray(attribute); Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 2, sizeof(TVertex)); offset += sizeof(QVector2D); attribute = Shader.attributeLocation("tangent"); Shader.enableAttributeArray(attribute); Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 3, sizeof(TVertex)); offset += sizeof(QVector3D); attribute = Shader.attributeLocation("bitangent"); Shader.enableAttributeArray(attribute); Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 3, sizeof(TVertex)); offset += sizeof(QVector3D); VertexBuff->release(); Texture->bind(0, QOpenGLTexture::ResetTextureUnit); Shader.setUniformValue("texture", 0); NormalMap->bind(1, QOpenGLTexture::ResetTextureUnit); Shader.setUniformValue("normalMap", 1); glDrawArrays(GL_TRIANGLES, 0, 3 * ObjectSize); Shader.disableAttributeArray("gl_Vertex"); Shader.disableAttributeArray("gl_Normal"); Shader.disableAttributeArray("vertTexCoord"); Shader.disableAttributeArray("tangent"); Shader.disableAttributeArray("bitangent"); Texture->release(0, QOpenGLTexture::ResetTextureUnit); NormalMap->release(1, QOpenGLTexture::ResetTextureUnit); Shader.disableAttributeArray("coord2d"); Shader.disableAttributeArray("v_color"); Shader.release(); /* /// DEBUG INFO QMatrix4x4 modelView = view * model; glMatrixMode(GL_PROJECTION); glLoadMatrixf(projection.data()); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(modelView.data()); // glDisable(GL_DEPTH_TEST); glColor3f(1, 1, 1); glBegin(GL_LINES); for (size_t i = 0; i < Obj.size(); i += 3) { const TVertex& v1 = Obj[i]; const TVertex& v2 = Obj[i + 1]; const TVertex& v3 = Obj[i + 2]; glVertex3f(v1.Position.x(), v1.Position.y(), v1.Position.z()); glVertex3f(v2.Position.x(), v2.Position.y(), v2.Position.z()); glVertex3f(v1.Position.x(), v1.Position.y(), v1.Position.z()); glVertex3f(v3.Position.x(), v3.Position.y(), v3.Position.z()); glVertex3f(v2.Position.x(), v2.Position.y(), v2.Position.z()); glVertex3f(v3.Position.x(), v3.Position.y(), v3.Position.z()); } glEnd(); // normals glColor3f(0,0,1); glBegin(GL_LINES); for (size_t i = 0; i < Obj.size(); ++i) { const TVertex& v = Obj[i]; QVector3D p = v.Position; p *= 1.02; glVertex3f(p.x(), p.y(), p.z()); QVector3D n = v.Normal.normalized(); p += n * 0.8; glVertex3f(p.x(), p.y(), p.z()); } glEnd(); // tangent glColor3f(1,0,0); glBegin(GL_LINES); for (size_t i = 0; i < Obj.size(); ++i) { const TVertex& v = Obj[i]; QVector3D p = v.Position; p *= 1.02; glVertex3f(p.x(), p.y(), p.z()); QVector3D n = v.Tangent.normalized(); p += n * 0.8; glVertex3f(p.x(), p.y(), p.z()); } glEnd(); // bitangent glColor3f(0,1,0); glBegin(GL_LINES); for (size_t i = 0; i < Obj.size(); ++i) { const TVertex& v = Obj[i]; QVector3D p = v.Position; p *= 1.02; glVertex3f(p.x(), p.y(), p.z()); QVector3D n = v.Bitangent.normalized(); p += n * 0.3; glVertex3f(p.x(), p.y(), p.z()); } glEnd(); */ Fbo->release(); }
//! [3] void GlWidget::paintGL() { //! [3] glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); QMatrix4x4 mMatrix; QMatrix4x4 vMatrix; QMatrix4x4 cameraTransformation; cameraTransformation.rotate(alpha, 0, 1, 0); cameraTransformation.rotate(beta, 1, 0, 0); QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance); QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0); vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection); mMatrix.setToIdentity(); QMatrix4x4 mvMatrix; mvMatrix = vMatrix * mMatrix; QMatrix3x3 normalMatrix; normalMatrix = mvMatrix.normalMatrix(); QMatrix4x4 lightTransformation; lightTransformation.rotate(lightAngle, 0, 1, 0); QVector3D lightPosition = lightTransformation * QVector3D(0, 1, 1); lightingShaderProgram.bind(); lightingShaderProgram.setUniformValue("mvpMatrix", pMatrix * mvMatrix); lightingShaderProgram.setUniformValue("mvMatrix", mvMatrix); lightingShaderProgram.setUniformValue("normalMatrix", normalMatrix); lightingShaderProgram.setUniformValue("lightPosition", vMatrix * lightPosition); lightingShaderProgram.setUniformValue("ambientColor", QColor(32, 32, 32)); lightingShaderProgram.setUniformValue("diffuseColor", QColor(128, 128, 128)); lightingShaderProgram.setUniformValue("specularColor", QColor(255, 255, 255)); lightingShaderProgram.setUniformValue("ambientReflection", (GLfloat) 1.0); lightingShaderProgram.setUniformValue("diffuseReflection", (GLfloat) 1.0); lightingShaderProgram.setUniformValue("specularReflection", (GLfloat) 1.0); lightingShaderProgram.setUniformValue("shininess", (GLfloat) 100.0); lightingShaderProgram.setUniformValue("texture", 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, cubeTexture); glActiveTexture(0); //! [4] cubeBuffer.bind(); int offset = 0; lightingShaderProgram.setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0); lightingShaderProgram.enableAttributeArray("vertex"); offset += numCubeVertices * 3 * sizeof(GLfloat); lightingShaderProgram.setAttributeBuffer("normal", GL_FLOAT, offset, 3, 0); lightingShaderProgram.enableAttributeArray("normal"); offset += numCubeVertices * 3 * sizeof(GLfloat); lightingShaderProgram.setAttributeBuffer("textureCoordinate", GL_FLOAT, offset, 2, 0); lightingShaderProgram.enableAttributeArray("textureCoordinate"); cubeBuffer.release(); glDrawArrays(GL_TRIANGLES, 0, numCubeVertices); //! [4] lightingShaderProgram.disableAttributeArray("vertex"); lightingShaderProgram.disableAttributeArray("normal"); lightingShaderProgram.disableAttributeArray("textureCoordinate"); lightingShaderProgram.release(); mMatrix.setToIdentity(); mMatrix.translate(lightPosition); mMatrix.rotate(lightAngle, 0, 1, 0); mMatrix.rotate(45, 1, 0, 0); mMatrix.scale(0.1); coloringShaderProgram.bind(); coloringShaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix); //! [5] spotlightBuffer.bind(); offset = 0; coloringShaderProgram.setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0); coloringShaderProgram.enableAttributeArray("vertex"); offset += numSpotlightVertices * 3 * sizeof(GLfloat); coloringShaderProgram.setAttributeBuffer("color", GL_FLOAT, offset, 3, 0); coloringShaderProgram.enableAttributeArray("color"); spotlightBuffer.release(); glDrawArrays(GL_TRIANGLES, 0, numSpotlightVertices); //! [5] coloringShaderProgram.disableAttributeArray("vertex"); coloringShaderProgram.disableAttributeArray("color"); coloringShaderProgram.release(); //! [6] }
QMatrix4x4 PerspectiveCamera::getViewMatrix(int width, int height) { QMatrix4x4 m; m.lookAt(eye(), lookat(), upDir()); return m; }