/** * @brief Updates any user interactions and model transformations. */ void OGLWidget::update() { Input::update(); flyThroughCamera(); if( Input::keyPressed( Qt::Key_Up ) ) ((PhysicsEntity*)renderables["Cube"])->RigidBody->applyCentralImpulse( btVector3( 0, 0, -.2 ) ); if( Input::keyPressed( Qt::Key_Down ) ) ((PhysicsEntity*)renderables["Cube"])->RigidBody->applyCentralImpulse( btVector3( 0, 0, .2 ) ); if( Input::keyPressed( Qt::Key_Left ) ) ((PhysicsEntity*)renderables["Cube"])->RigidBody->applyCentralImpulse( btVector3( -.2, 0, 0 ) ); if( Input::keyPressed( Qt::Key_Right ) ) ((PhysicsEntity*)renderables["Cube"])->RigidBody->applyCentralImpulse( btVector3( .2, 0, 0 ) ); for( QMap<QString, Renderable*>::iterator iter = renderables.begin(); iter != renderables.end(); iter++ ) { (*iter)->update(); } m_dynamicsWorld->stepSimulation( 1, 10 ); { btScalar rawMatrix[16]; ((PhysicsEntity*)renderables["Cube"])-> RigidBody->getWorldTransform().getOpenGLMatrix(rawMatrix); QMatrix4x4 newMatrix = QMatrix4x4(rawMatrix); newMatrix = newMatrix.transposed(); ((PhysicsEntity*)renderables["Cube"])->Transform = newMatrix; } { btScalar rawMatrix[16]; ((PhysicsEntity*)renderables["Board"])-> RigidBody->getWorldTransform().getOpenGLMatrix(rawMatrix); QMatrix4x4 newMatrix = QMatrix4x4(rawMatrix); newMatrix = newMatrix.transposed(); ((PhysicsEntity*)renderables["Board"])->Transform = newMatrix; } QOpenGLWidget::update(); }
QVector3D myCam::getPositionFromViewMatrix(QMatrix4x4 matrix) { QMatrix4x4 viewRot = matrix; viewRot.setColumn(3, QVector4D(0,0,0,1)); QVector4D p = -(viewRot.transposed() * matrix.column(3)); return QVector3D(p.x(), p.y(), p.z()); }
void OpenGLWidget::paintGL() { // Clear window color. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // View matrices. QMatrix4x4 mMat, vMat, pMat; mMat.rotate(theta, QVector3D(0.0f, 1.0f, 0.0f)); vMat.lookAt(QVector3D(3.0f, 4.0f, 5.0f), QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0f, 1.0f, 0.0f)); pMat.perspective(45.0f, (float)width() / (float)height(), 0.1f, 1000.0f); QMatrix4x4 mvMat = vMat * mMat; QMatrix4x4 mvpMat = pMat * mvMat; QMatrix4x4 normMat = mvMat.transposed().inverted(); // Draw color triangle. shader->bind(); vao->bind(); shader->setUniformValue("u_mvpMat", mvpMat); shader->setUniformValue("u_mvMat", mvMat); shader->setUniformValue("u_viewMat", vMat); shader->setUniformValue("u_normMat", normMat); shader->setUniformValue("u_lightPos", lightPos); texture->bind(0); shader->setUniformValue("u_texture", 0); glDrawElements(GL_TRIANGLES, ibo->size() / sizeof(uint32_t), GL_UNSIGNED_INT, 0); texture->release(); shader->release(); vao->release(); }
// override mouse move event void Renderer::mouseMoveEvent(QMouseEvent * event) { if(translate) { m_theta_x += event->x() - initial_x; m_theta_y += event->y() - initial_y; } else if(RMB) { float fDiameter; QMatrix4x4 trans; float nWinWidth = width(), nWinHeight=height(); int iCenterX, iCenterY; float fRotVecX, fRotVecY, fRotVecZ; float fNewModX, fNewModY, fOldModX, fOldModY; fDiameter = (nWinWidth < nWinHeight) ? nWinWidth * 0.5 : nWinHeight * 0.5; iCenterX = nWinWidth / 2; iCenterY = nWinHeight / 2; fOldModX = initial_x - iCenterX; fOldModY = initial_y - iCenterY; fNewModX = event->x() - iCenterX; fNewModY = event->y() - iCenterY; vCalcRotVec(fNewModX, fNewModY, fOldModX, fOldModY, fDiameter, &fRotVecX, &fRotVecY, &fRotVecZ); vAxisRotMatrix(fRotVecX, -fRotVecY, fRotVecZ, trans); trans.transposed(); rotation*=trans; } initial_x=event->x(); initial_y=event->y(); }
void MyWindow::printMatrix(const QMatrix4x4& mat) { const float *locMat = mat.transposed().constData(); for (int i=0; i<4; i++) { qDebug() << locMat[i*4] << " " << locMat[i*4+1] << " " << locMat[i*4+2] << " " << locMat[i*4+3]; } }
void RemoteGL::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); QMatrix4x4 mrot = realToOGL(quatToRotMatrix(rotationQuat)); glLoadIdentity(); glTranslatef(offset.x(), offset.y(), offset.z()); #ifdef KDMODE glRotatef(-90,0,1,0); #else glRotatef(90,0,1,0); #endif glMultMatrixd((const double*)mrot.transposed().constData()); glDisable(GL_CULL_FACE); glEnable(GL_TEXTURE_2D); //glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_texture); float wi = 575; float he = 1127; float d = 200; glRotatef(90,0,1,0); drawPhone(wi / d, 100 / d, he / d); glDisable(GL_TEXTURE_2D); glColor3f(1,0,0); glPushMatrix(); QColor colorsX[] = { Qt::red, Qt::red, Qt::red, Qt::red, Qt::red, Qt::red }; glTranslatef(5, 0, 0); drawBox(colorsX, 5, 0.3, 0.3, offset); glPopMatrix(); glPushMatrix(); QColor colorsY[] = { Qt::green, Qt::green, Qt::green, Qt::green, Qt::green, Qt::green }; glTranslatef(0, -5, 0); drawBox(colorsY, 0.3, 5, 0.3, offset); glPopMatrix(); glPushMatrix(); QColor colorsZ[] = { Qt::blue, Qt::blue, Qt::blue, Qt::blue, Qt::blue, Qt::blue }; glTranslatef(0, 0, 5); drawBox(colorsZ, 0.3, 0.3, 5, offset); glPopMatrix(); }
void ObjectTransform::updateTransformMatrix() { m_rotMatrix.setToIdentity(); m_rotMatrix.rotate(m_rotation); m_transformMatrix.setToIdentity(); m_transformMatrix.translate(m_translation); m_transformMatrix *= m_rotMatrix; m_norTransformMatrix = m_transformMatrix; m_transRotMatrix = m_transformMatrix; m_transformMatrix.scale(m_scale); m_norTransformMatrix.scale(1 / m_scale.x(), 1 / m_scale.y(), 1 / m_scale.z()); m_invTransformMatrix.setToIdentity(); m_invTransformMatrix.scale(1 / m_scale.x(), 1 / m_scale.y(), 1 / m_scale.z()); QMatrix4x4 transposeRot = m_rotMatrix; m_invTransformMatrix *= transposeRot.transposed(); m_invTransformMatrix.translate(-m_translation); // 还要更新场景的GeometryImage paint3DApp->scene->updateGeometryImage(); }
void Sphere::render(QMatrix4x4& projection, QMatrix4x4& matrix, Light* light){ program->addShader(&vShader); program->addShader(&fShader); program->link(); program->bind(); program->setUniformValue("mvp_matrix", projection * matrix); program->setUniformValue("mv_matrix", matrix); program->setUniformValue("normal_matrix", matrix.transposed().inverted()); tt += delta; if(tt > 1.0f){ delta = -delta; tt += delta; } if(tt < 0.0f){ delta = -delta; tt += delta; } int timeLocation = program->attributeLocation("a_time"); program->setAttributeValue(timeLocation, tt); // Tell OpenGL which VBOs to use arrayBuf.bind(); indexBuf.bind(); // Offset for position quintptr offset = 0; // Tell OpenGL programmable pipeline how to locate vertex position data int vertexLocation = program->attributeLocation("a_position"); program->enableAttributeArray(vertexLocation); program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData)); // Offset for texture coordinate offset += sizeof(QVector3D); // Tell OpenGL programmable pipeline how to locate vertex texture coordinate data int texcoordLocation = program->attributeLocation("a_texcoord"); program->enableAttributeArray(texcoordLocation); program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData)); offset += sizeof(QVector2D); int normalLocation = program->attributeLocation("a_normal"); program->enableAttributeArray(normalLocation); program->setAttributeBuffer(normalLocation, GL_FLOAT, offset, 3, sizeof(VertexData)); light->load(program); material->load(program); // Draw cube geometry using indices from VBO 1 glDrawElements(mode, indicesSize, GL_UNSIGNED_INT, 0); program->release(); }
void GLClass::uniformMatrix4fv(const QString& location, bool transpose, const QMatrix4x4& value) { if (transpose) d_ptr->renderWidget->shaderProgram()->setUniformValue(location.toLatin1().constData(), value.transposed()); else d_ptr->renderWidget->shaderProgram()->setUniformValue(location.toLatin1().constData(), value); }
void FiberRenderer::draw( QMatrix4x4 p_matrix, QMatrix4x4 mv_matrix, int width, int height, int renderMode, PropertyGroup& props ) { float alpha = props.get( Fn::Property::D_ALPHA ).toFloat(); if ( renderMode == 0 ) // picking { return; } else if ( renderMode == 1 ) // we are drawing opaque objects { if ( alpha < 1.0 ) { // obviously not opaque return; } } else // we are drawing tranparent objects { if ( !(alpha < 1.0 ) ) { // not transparent return; } } if ( m_updateExtraData ) { updateExtraData( m_selectedExtraData ); } QGLShaderProgram* program = GLFunctions::getShader( "fiber" ); program->bind(); GLFunctions::setupTextures(); GLFunctions::setTextureUniforms( GLFunctions::getShader( "fiber" ), "maingl" ); // Set modelview-projection matrix program->setUniformValue( "mvp_matrix", p_matrix * mv_matrix ); program->setUniformValue( "mv_matrixInvert", mv_matrix.inverted() ); program->setUniformValue( "mv_matrixTI", mv_matrix.transposed().inverted() ); program->setUniformValue( "userTransformMatrix", props.get( Fn::Property::D_TRANSFORM ).value<QMatrix4x4>() ); initGeometry(); glBindBuffer( GL_ARRAY_BUFFER, vbo ); setShaderVars( props ); glBindBuffer( GL_ARRAY_BUFFER, dataVbo ); int extraLocation = program->attributeLocation( "a_extra" ); program->enableAttributeArray( extraLocation ); glVertexAttribPointer( extraLocation, 1, GL_FLOAT, GL_FALSE, sizeof(float), 0 ); glBindBuffer( GL_ARRAY_BUFFER, indexVbo ); int indexLocation = program->attributeLocation( "a_indexes" ); program->enableAttributeArray( indexLocation ); glVertexAttribPointer( indexLocation, 1, GL_FLOAT, GL_FALSE, sizeof(float), 0 ); program->setUniformValue( "u_alpha", alpha ); program->setUniformValue( "u_renderMode", renderMode ); program->setUniformValue( "u_canvasSize", width, height ); program->setUniformValue( "D0", 9 ); program->setUniformValue( "D1", 10 ); program->setUniformValue( "D2", 11 ); program->setUniformValue( "P0", 12 ); program->setUniformValue( "C5", 13 ); program->setUniformValue( "u_fibGrowth", props.get( Fn::Property::D_FIBER_GROW_LENGTH).toFloat() ); program->setUniformValue( "u_lighting", props.get( Fn::Property::D_LIGHT_SWITCH ).toBool() ); program->setUniformValue( "u_lightAmbient", props.get( Fn::Property::D_LIGHT_AMBIENT ).toFloat() ); program->setUniformValue( "u_lightDiffuse", props.get( Fn::Property::D_LIGHT_DIFFUSE ).toFloat() ); program->setUniformValue( "u_materialAmbient", props.get( Fn::Property::D_MATERIAL_AMBIENT ).toFloat() ); program->setUniformValue( "u_materialDiffuse", props.get( Fn::Property::D_MATERIAL_DIFFUSE ).toFloat() ); program->setUniformValue( "u_materialSpecular", props.get( Fn::Property::D_MATERIAL_SPECULAR ).toFloat() ); program->setUniformValue( "u_materialShininess", props.get( Fn::Property::D_MATERIAL_SHININESS ).toFloat() ); glLineWidth( props.get( Fn::Property::D_FIBER_THICKNESS ).toFloat() ); std::vector<bool>*selected = m_selector->getSelection(); int percent = props.get( Fn::Property::D_FIBER_THIN_OUT ).toFloat() * 10; for ( unsigned int i = 0; i < m_fibs->size(); ++i ) { if ( ( i % 1000 ) > percent ) { continue; } if ( selected->at( i ) ) { QColor c = m_fibs->at( i ).customColor(); program->setUniformValue( "u_color", c.redF(), c.greenF(), c.blueF(), 1.0 ); c = m_fibs->at( i ).globalColor(); program->setUniformValue( "u_globalColor", c.redF(), c.greenF(), c.blueF(), 1.0 ); glDrawArrays( GL_LINE_STRIP, m_startIndexes[i], m_pointsPerLine[i] ); } else { if ( Models::getGlobal( Fn::Property::G_UNSELECTED_FIBERS_GREY ).toBool() ) { program->setUniformValue( "u_color", .4f, .4f, .4f, 1.0 ); program->setUniformValue( "u_globalColor", .4f, .4f, .4f, 1.0 ); glDrawArrays( GL_LINE_STRIP, m_startIndexes[i], m_pointsPerLine[i] ); } } } glBindBuffer( GL_ARRAY_BUFFER, 0 ); }