Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

Simple Example of 3d Cube inside SceneGraph needed

$
0
0
I just got a lot closer. Now I am able to rotate on the y axis. The trick was to multiply by a matrix in my vertex shader. My last remaining problem is getting my rotated squared to render in a perspective mode rather than an orthogonal fashion. In other words, when I rotate by 45 degrees, my rectangle becomes less wide. However, what I want to happen is I want the left side of my square to get taller and right side of my square to get shorter after rotation (hope that makes sense). Does anyone know of a magic initialization function to setup perspective mode or do I have to modify the vertex shader again? Here is my new code (I only included paint() function and newly added initView() function this time): // added to help set perspective void Squircle::initView() {     qreal ratio = window()->devicePixelRatio();     int w = int(ratio * window()->width());     int h = int(ratio * window()->height());     glViewport(0, 0, w, h);       // Calculate aspect ratio     qreal aspect = qreal(w) / qreal(h ? h : 1);       // Set near plane to 3.0, far plane to 7.0, field of view 45 degrees     const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;       // Reset projection     projection.setToIdentity();       // Set perspective projection     projection.perspective(fov, aspect, zNear, zFar);   }   //! [3] //! [4] void Squircle::paint() {     if (!m_program) {         m_program = new QOpenGLShaderProgram();        m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,                                            "attribute highp vec4 vertices;"                                           // added for rotation                                            "uniform mat4 mvp_matrix;"                                            "varying highp vec2 coords;"                                            "void main() {"                                            "    gl_Position = vertices * mvp_matrix;"                                            "    coords = vertices.xy;"                                            "}");         m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,                                            "uniform lowp float t;"                                            "varying highp vec2 coords;"                                            "void main() {"                                            "    gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);"                                            "}");           m_program->bindAttributeLocation("vertices", 0);         m_program->link();         initView();         connect(window()->openglContext(), SIGNAL(aboutToBeDestroyed()),                 this, SLOT(cleanup()), Qt::DirectConnection);     } //! [4] //! [5]     m_program->bind();       m_program->enableAttributeArray(0);       float values[] = {         -.5, -.5,0, // bottom line         .5, -.5,0,         -.5, .5,0, // top line         .5, .5,0,         .5, .5,0, // right line         .5, -.5,0,         -.5, -.5,0, // left line         -.5, .5,0,     };         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     glEnable(GL_DEPTH_TEST);     glEnable(GL_CULL_FACE);       m_program->setAttributeArray(0, GL_FLOAT, values, 3); //    m_program->setUniformValue("t", (float) m_thread_t);     QMatrix4x4 matrix;     matrix.translate(0.0, 0.0, -5.0);     matrix.rotate(45,0,1,0);     m_program->setUniformValue("mvp_matrix", projection  * matrix );       glDrawArrays(GL_LINES, 0,8);         m_program->disableAttributeArray(0);     m_program->release(); }

Viewing all articles
Browse latest Browse all 4972

Trending Articles