Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/demos/boxes/scene.cpp

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information (qt-info@nokia.com)
     4** All rights reserved.
     5** Contact: Nokia Corporation (qt-info@nokia.com)
    56**
    67** This file is part of the demonstration applications of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     24** In addition, as a special exception, Nokia gives you certain additional
     25** rights.  These rights are described in the Nokia Qt LGPL Exception
     26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you have questions regarding the use of this file, please contact
     37** Nokia at qt-info@nokia.com.
    3838** $QT_END_LICENSE$
    3939**
    4040****************************************************************************/
    4141
     42#include <QDebug>
    4243#include "scene.h"
     44#include <QtGui/qmatrix4x4.h>
     45#include <QtGui/qvector3d.h>
    4346
    4447#include "3rdparty/fbm.h"
     
    343346                        m_parameterEdits << colorEdit;
    344347                        layout->addWidget(colorEdit);
    345                         connect(colorEdit, SIGNAL(colorChanged(QRgb, int)), this, SLOT(setColorParameter(QRgb, int)));
     348                        connect(colorEdit, SIGNAL(colorChanged(QRgb,int)), this, SLOT(setColorParameter(QRgb,int)));
    346349                        ++row;
    347350                    } else if (type == "float") {
     
    351354                        m_parameterEdits << floatEdit;
    352355                        layout->addWidget(floatEdit);
    353                         connect(floatEdit, SIGNAL(valueChanged(float, int)), this, SLOT(setFloatParameter(float, int)));
     356                        connect(floatEdit, SIGNAL(valueChanged(float,int)), this, SLOT(setFloatParameter(float,int)));
    354357                        ++row;
    355358                    }
     
    485488    setSceneRect(0, 0, width, height);
    486489
    487     m_trackBalls[0] = TrackBall(0.0005f, gfx::Vector3f::vector(0, 1, 0), TrackBall::Sphere);
    488     m_trackBalls[1] = TrackBall(0.0001f, gfx::Vector3f::vector(0, 0, 1), TrackBall::Sphere);
    489     m_trackBalls[2] = TrackBall(0.0f, gfx::Vector3f::vector(0, 1, 0), TrackBall::Plane);
     490    m_trackBalls[0] = TrackBall(0.05f, QVector3D(0, 1, 0), TrackBall::Sphere);
     491    m_trackBalls[1] = TrackBall(0.005f, QVector3D(0, 0, 1), TrackBall::Sphere);
     492    m_trackBalls[2] = TrackBall(0.0f, QVector3D(0, 1, 0), TrackBall::Plane);
    490493
    491494    m_renderOptions = new RenderOptionsDialog;
     
    494497
    495498    connect(m_renderOptions, SIGNAL(dynamicCubemapToggled(int)), this, SLOT(toggleDynamicCubemap(int)));
    496     connect(m_renderOptions, SIGNAL(colorParameterChanged(const QString &, QRgb)), this, SLOT(setColorParameter(const QString &, QRgb)));
    497     connect(m_renderOptions, SIGNAL(floatParameterChanged(const QString &, float)), this, SLOT(setFloatParameter(const QString &, float)));
     499    connect(m_renderOptions, SIGNAL(colorParameterChanged(QString,QRgb)), this, SLOT(setColorParameter(QString,QRgb)));
     500    connect(m_renderOptions, SIGNAL(floatParameterChanged(QString,float)), this, SLOT(setFloatParameter(QString,float)));
    498501    connect(m_renderOptions, SIGNAL(textureChanged(int)), this, SLOT(setTexture(int)));
    499502    connect(m_renderOptions, SIGNAL(shaderChanged(int)), this, SLOT(setShader(int)));
     
    532535    if (m_mainCubemap)
    533536        delete m_mainCubemap;
    534     foreach (GLProgram *program, m_programs)
     537    foreach (QGLShaderProgram *program, m_programs)
    535538        if (program) delete program;
    536539    if (m_vertexShader)
    537540        delete m_vertexShader;
    538     foreach (GLFragmentShader *shader, m_fragmentShaders)
     541    foreach (QGLShader *shader, m_fragmentShaders)
    539542        if (shader) delete shader;
    540543    foreach (GLRenderTargetCube *rt, m_cubemaps)
     
    550553    m_box = new GLRoundedBox(0.25f, 1.0f, 10);
    551554
    552     m_vertexShader = new GLVertexShader(":/res/boxes/basic.vsh");
     555    m_vertexShader = new QGLShader(QGLShader::Vertex);
     556    m_vertexShader->compileSourceFile(QLatin1String(":/res/boxes/basic.vsh"));
    553557
    554558    QStringList list;
     
    556560         << ":/res/boxes/cubemap_negy.jpg" << ":/res/boxes/cubemap_posz.jpg" << ":/res/boxes/cubemap_negz.jpg";
    557561    m_environment = new GLTextureCube(list, qMin(1024, m_maxTextureSize));
    558     m_environmentShader = new GLFragmentShader(environmentShaderText, strlen(environmentShaderText));
    559     m_environmentProgram = new GLProgram;
    560     m_environmentProgram->attach(*m_vertexShader);
    561     m_environmentProgram->attach(*m_environmentShader);
     562    m_environmentShader = new QGLShader(QGLShader::Fragment);
     563    m_environmentShader->compileSourceCode(environmentShaderText);
     564    m_environmentProgram = new QGLShaderProgram;
     565    m_environmentProgram->addShader(m_vertexShader);
     566    m_environmentProgram->addShader(m_environmentShader);
     567    m_environmentProgram->link();
    562568
    563569    const int NOISE_SIZE = 128; // for a different size, B and BM in fbm.c must also be changed
     
    611617    files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable);
    612618    foreach (QFileInfo file, files) {
    613         GLProgram *program = new GLProgram;
    614         GLFragmentShader* shader = new GLFragmentShader(file.absoluteFilePath());
     619        QGLShaderProgram *program = new QGLShaderProgram;
     620        QGLShader* shader = new QGLShader(QGLShader::Fragment);
     621        shader->compileSourceFile(file.absoluteFilePath());
    615622        // The program does not take ownership over the shaders, so store them in a vector so they can be deleted afterwards.
    616         program->attach(*m_vertexShader);
    617         program->attach(*shader);
    618         if (program->failed()) {
     623        program->addShader(m_vertexShader);
     624        program->addShader(shader);
     625        if (!program->link()) {
    619626            qWarning("Failed to compile and link shader program");
    620627            qWarning("Vertex shader log:");
     
    635642
    636643        program->bind();
    637         m_cubemaps << (program->hasParameter("env") ? new GLRenderTargetCube(qMin(256, m_maxTextureSize)) : 0);
    638         program->unbind();
     644        m_cubemaps << ((program->uniformLocation("env") != -1) ? new GLRenderTargetCube(qMin(256, m_maxTextureSize)) : 0);
     645        program->release();
    639646    }
    640647
    641648    if (m_programs.size() == 0)
    642         m_programs << new GLProgram;
     649        m_programs << new QGLShaderProgram;
    643650
    644651    m_renderOptions->emitParameterChanged();
     652}
     653
     654static void loadMatrix(const QMatrix4x4& m)
     655{
     656    GLfloat mat[16];
     657    const qreal *data = m.constData();
     658    for (int index = 0; index < 16; ++index)
     659        mat[index] = data[index];
     660    glLoadMatrixf(mat);
     661}
     662
     663static void multMatrix(const QMatrix4x4& m)
     664{
     665    GLfloat mat[16];
     666    const qreal *data = m.constData();
     667    for (int index = 0; index < 16; ++index)
     668        mat[index] = data[index];
     669    glMultMatrixf(mat);
    645670}
    646671
    647672// If one of the boxes should not be rendered, set excludeBox to its index.
    648673// If the main box should not be rendered, set excludeBox to -1.
    649 void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox)
    650 {
    651     gfx::Matrix4x4f invView = view.inverse();
     674void Scene::renderBoxes(const QMatrix4x4 &view, int excludeBox)
     675{
     676    QMatrix4x4 invView = view.inverted();
    652677
    653678    // If multi-texturing is supported, use three saplers.
     
    665690    glDisable(GL_CULL_FACE);
    666691
    667     gfx::Matrix4x4f viewRotation(view);
     692    QMatrix4x4 viewRotation(view);
    668693    viewRotation(3, 0) = viewRotation(3, 1) = viewRotation(3, 2) = 0.0f;
    669694    viewRotation(0, 3) = viewRotation(1, 3) = viewRotation(2, 3) = 0.0f;
    670695    viewRotation(3, 3) = 1.0f;
    671     glLoadMatrixf(viewRotation.bits());
     696    loadMatrix(viewRotation);
    672697    glScalef(20.0f, 20.0f, 20.0f);
    673698
     
    676701        m_environment->bind();
    677702        m_environmentProgram->bind();
    678         m_environmentProgram->setInt("tex", 0);
    679         m_environmentProgram->setInt("env", 1);
    680         m_environmentProgram->setInt("noise", 2);
     703        m_environmentProgram->setUniformValue("tex", GLint(0));
     704        m_environmentProgram->setUniformValue("env", GLint(1));
     705        m_environmentProgram->setUniformValue("noise", GLint(2));
    681706        m_box->draw();
    682         m_environmentProgram->unbind();
     707        m_environmentProgram->release();
    683708        m_environment->unbind();
    684709    }
    685710
    686     glLoadMatrixf(view.bits());
     711    loadMatrix(view);
    687712
    688713    glEnable(GL_CULL_FACE);
     
    694719
    695720        glPushMatrix();
    696         gfx::Matrix4x4f m;
    697         m_trackBalls[1].rotation().matrix(m);
    698         glMultMatrixf(m.bits());
     721        QMatrix4x4 m;
     722        m.rotate(m_trackBalls[1].rotation());
     723        multMatrix(m);
    699724
    700725        glRotatef(360.0f * i / m_programs.size(), 0.0f, 0.0f, 1.0f);
     
    709734        }
    710735        m_programs[i]->bind();
    711         m_programs[i]->setInt("tex", 0);
    712         m_programs[i]->setInt("env", 1);
    713         m_programs[i]->setInt("noise", 2);
    714         m_programs[i]->setMatrix("view", view);
    715         m_programs[i]->setMatrix("invView", invView);
     736        m_programs[i]->setUniformValue("tex", GLint(0));
     737        m_programs[i]->setUniformValue("env", GLint(1));
     738        m_programs[i]->setUniformValue("noise", GLint(2));
     739        m_programs[i]->setUniformValue("view", view);
     740        m_programs[i]->setUniformValue("invView", invView);
    716741        m_box->draw();
    717         m_programs[i]->unbind();
     742        m_programs[i]->release();
    718743
    719744        if (glActiveTexture) {
     
    727752
    728753    if (-1 != excludeBox) {
    729         gfx::Matrix4x4f m;
    730         m_trackBalls[0].rotation().matrix(m);
    731         glMultMatrixf(m.bits());
     754        QMatrix4x4 m;
     755        m.rotate(m_trackBalls[0].rotation());
     756        multMatrix(m);
    732757
    733758        if (glActiveTexture) {
     
    739764
    740765        m_programs[m_currentShader]->bind();
    741         m_programs[m_currentShader]->setInt("tex", 0);
    742         m_programs[m_currentShader]->setInt("env", 1);
    743         m_programs[m_currentShader]->setInt("noise", 2);
    744         m_programs[m_currentShader]->setMatrix("view", view);
    745         m_programs[m_currentShader]->setMatrix("invView", invView);
     766        m_programs[m_currentShader]->setUniformValue("tex", GLint(0));
     767        m_programs[m_currentShader]->setUniformValue("env", GLint(1));
     768        m_programs[m_currentShader]->setUniformValue("noise", GLint(2));
     769        m_programs[m_currentShader]->setUniformValue("view", view);
     770        m_programs[m_currentShader]->setUniformValue("invView", invView);
    746771        m_box->draw();
    747         m_programs[m_currentShader]->unbind();
     772        m_programs[m_currentShader]->release();
    748773
    749774        if (glActiveTexture) {
     
    830855    const int N = (m_updateAllCubemaps ? 1 : 3);
    831856
    832     gfx::Matrix4x4f mat;
     857    QMatrix4x4 mat;
    833858    GLRenderTargetCube::getProjectionMatrix(mat, 0.1f, 100.0f);
    834859
    835860    glMatrixMode(GL_PROJECTION);
    836861    glPushMatrix();
    837     glLoadMatrixf(mat.bits());
     862    loadMatrix(mat);
    838863
    839864    glMatrixMode(GL_MODELVIEW);
    840865    glPushMatrix();
    841866
    842     gfx::Vector3f center;
     867    QVector3D center;
    843868
    844869    for (int i = m_frame % N; i < m_cubemaps.size(); i += N) {
     
    847872
    848873        float angle = 2.0f * PI * i / m_cubemaps.size();
    849         center = m_trackBalls[1].rotation().transform(gfx::Vector3f::vector(cos(angle), sin(angle), 0));
     874
     875        center = m_trackBalls[1].rotation().rotatedVector(QVector3D(cos(angle), sin(angle), 0.0f));
    850876
    851877        for (int face = 0; face < 6; ++face) {
     
    853879
    854880            GLRenderTargetCube::getViewMatrix(mat, face);
    855             gfx::Vector4f v = gfx::Vector4f::vector(-center[0], -center[1], -center[2], 1.0);
    856             mat[3] = v * mat;
     881            QVector4D v = QVector4D(-center.x(), -center.y(), -center.z(), 1.0);
     882            mat.setColumn(3, mat * v);
    857883
    858884            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    886912    float height = float(painter->device()->height());
    887913
     914    painter->beginNativePainting();
    888915    setStates();
    889916
     
    898925    glMatrixMode(GL_MODELVIEW);
    899926
    900     //gfx::Matrix4x4f view = gfx::Matrix4x4f::identity();
    901     //view(3, 2) -= 2.0f * exp(m_distExp / 1200.0f);
    902 
    903     gfx::Matrix4x4f view;
    904     m_trackBalls[2].rotation().matrix(view);
    905     view(3, 2) -= 2.0f * exp(m_distExp / 1200.0f);
     927    QMatrix4x4 view;
     928    view.rotate(m_trackBalls[2].rotation());
     929    view(2, 3) -= 2.0f * exp(m_distExp / 1200.0f);
    906930    renderBoxes(view);
    907931
    908932    defaultStates();
    909933    ++m_frame;
     934
     935    painter->endNativePainting();
    910936}
    911937
     
    937963
    938964    if (event->buttons() & Qt::MidButton) {
    939         m_trackBalls[2].move(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity());
     965        m_trackBalls[2].move(pixelPosToViewPos(event->scenePos()), QQuaternion());
    940966        event->accept();
    941967    } else {
    942         m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity());
     968        m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), QQuaternion());
    943969    }
    944970}
     
    961987
    962988    if (event->buttons() & Qt::MidButton) {
    963         m_trackBalls[2].push(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity());
     989        m_trackBalls[2].push(pixelPosToViewPos(event->scenePos()), QQuaternion());
    964990        event->accept();
    965991    }
     
    9831009
    9841010    if (event->button() == Qt::MidButton) {
    985         m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity());
     1011        m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), QQuaternion());
    9861012        event->accept();
    9871013    }
     
    10221048{
    10231049    // set the color in all programs
    1024     foreach (GLProgram *program, m_programs) {
     1050    foreach (QGLShaderProgram *program, m_programs) {
    10251051        program->bind();
    1026         program->setColor(name, color);
    1027         program->unbind();
     1052        program->setUniformValue(program->uniformLocation(name), QColor(color));
     1053        program->release();
    10281054    }
    10291055}
     
    10321058{
    10331059    // set the color in all programs
    1034     foreach (GLProgram *program, m_programs) {
     1060    foreach (QGLShaderProgram *program, m_programs) {
    10351061        program->bind();
    1036         program->setFloat(name, value);
    1037         program->unbind();
     1062        program->setUniformValue(program->uniformLocation(name), value);
     1063        program->release();
    10381064    }
    10391065}
Note: See TracChangeset for help on using the changeset viewer.