Changeset 561 for trunk/demos/boxes/scene.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/demos/boxes/scene.cpp
r2 r561 2 2 ** 3 3 ** 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) 5 6 ** 6 7 ** This file is part of the demonstration applications of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 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. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** 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. 38 38 ** $QT_END_LICENSE$ 39 39 ** 40 40 ****************************************************************************/ 41 41 42 #include <QDebug> 42 43 #include "scene.h" 44 #include <QtGui/qmatrix4x4.h> 45 #include <QtGui/qvector3d.h> 43 46 44 47 #include "3rdparty/fbm.h" … … 343 346 m_parameterEdits << colorEdit; 344 347 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))); 346 349 ++row; 347 350 } else if (type == "float") { … … 351 354 m_parameterEdits << floatEdit; 352 355 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))); 354 357 ++row; 355 358 } … … 485 488 setSceneRect(0, 0, width, height); 486 489 487 m_trackBalls[0] = TrackBall(0.0 005f, gfx::Vector3f::vector(0, 1, 0), TrackBall::Sphere);488 m_trackBalls[1] = TrackBall(0.00 01f, 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); 490 493 491 494 m_renderOptions = new RenderOptionsDialog; … … 494 497 495 498 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))); 498 501 connect(m_renderOptions, SIGNAL(textureChanged(int)), this, SLOT(setTexture(int))); 499 502 connect(m_renderOptions, SIGNAL(shaderChanged(int)), this, SLOT(setShader(int))); … … 532 535 if (m_mainCubemap) 533 536 delete m_mainCubemap; 534 foreach ( GLProgram *program, m_programs)537 foreach (QGLShaderProgram *program, m_programs) 535 538 if (program) delete program; 536 539 if (m_vertexShader) 537 540 delete m_vertexShader; 538 foreach ( GLFragmentShader *shader, m_fragmentShaders)541 foreach (QGLShader *shader, m_fragmentShaders) 539 542 if (shader) delete shader; 540 543 foreach (GLRenderTargetCube *rt, m_cubemaps) … … 550 553 m_box = new GLRoundedBox(0.25f, 1.0f, 10); 551 554 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")); 553 557 554 558 QStringList list; … … 556 560 << ":/res/boxes/cubemap_negy.jpg" << ":/res/boxes/cubemap_posz.jpg" << ":/res/boxes/cubemap_negz.jpg"; 557 561 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(); 562 568 563 569 const int NOISE_SIZE = 128; // for a different size, B and BM in fbm.c must also be changed … … 611 617 files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable); 612 618 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()); 615 622 // The program does not take ownership over the shaders, so store them in a vector so they can be deleted afterwards. 616 program->a ttach(*m_vertexShader);617 program->a ttach(*shader);618 if ( program->failed()) {623 program->addShader(m_vertexShader); 624 program->addShader(shader); 625 if (!program->link()) { 619 626 qWarning("Failed to compile and link shader program"); 620 627 qWarning("Vertex shader log:"); … … 635 642 636 643 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(); 639 646 } 640 647 641 648 if (m_programs.size() == 0) 642 m_programs << new GLProgram;649 m_programs << new QGLShaderProgram; 643 650 644 651 m_renderOptions->emitParameterChanged(); 652 } 653 654 static 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 663 static 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); 645 670 } 646 671 647 672 // If one of the boxes should not be rendered, set excludeBox to its index. 648 673 // 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();674 void Scene::renderBoxes(const QMatrix4x4 &view, int excludeBox) 675 { 676 QMatrix4x4 invView = view.inverted(); 652 677 653 678 // If multi-texturing is supported, use three saplers. … … 665 690 glDisable(GL_CULL_FACE); 666 691 667 gfx::Matrix4x4fviewRotation(view);692 QMatrix4x4 viewRotation(view); 668 693 viewRotation(3, 0) = viewRotation(3, 1) = viewRotation(3, 2) = 0.0f; 669 694 viewRotation(0, 3) = viewRotation(1, 3) = viewRotation(2, 3) = 0.0f; 670 695 viewRotation(3, 3) = 1.0f; 671 glLoadMatrixf(viewRotation.bits());696 loadMatrix(viewRotation); 672 697 glScalef(20.0f, 20.0f, 20.0f); 673 698 … … 676 701 m_environment->bind(); 677 702 m_environmentProgram->bind(); 678 m_environmentProgram->set Int("tex", 0);679 m_environmentProgram->set Int("env", 1);680 m_environmentProgram->set Int("noise", 2);703 m_environmentProgram->setUniformValue("tex", GLint(0)); 704 m_environmentProgram->setUniformValue("env", GLint(1)); 705 m_environmentProgram->setUniformValue("noise", GLint(2)); 681 706 m_box->draw(); 682 m_environmentProgram-> unbind();707 m_environmentProgram->release(); 683 708 m_environment->unbind(); 684 709 } 685 710 686 glLoadMatrixf(view.bits());711 loadMatrix(view); 687 712 688 713 glEnable(GL_CULL_FACE); … … 694 719 695 720 glPushMatrix(); 696 gfx::Matrix4x4fm;697 m _trackBalls[1].rotation().matrix(m);698 glMultMatrixf(m.bits());721 QMatrix4x4 m; 722 m.rotate(m_trackBalls[1].rotation()); 723 multMatrix(m); 699 724 700 725 glRotatef(360.0f * i / m_programs.size(), 0.0f, 0.0f, 1.0f); … … 709 734 } 710 735 m_programs[i]->bind(); 711 m_programs[i]->set Int("tex", 0);712 m_programs[i]->set Int("env", 1);713 m_programs[i]->set Int("noise", 2);714 m_programs[i]->set Matrix("view", view);715 m_programs[i]->set Matrix("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); 716 741 m_box->draw(); 717 m_programs[i]-> unbind();742 m_programs[i]->release(); 718 743 719 744 if (glActiveTexture) { … … 727 752 728 753 if (-1 != excludeBox) { 729 gfx::Matrix4x4fm;730 m _trackBalls[0].rotation().matrix(m);731 glMultMatrixf(m.bits());754 QMatrix4x4 m; 755 m.rotate(m_trackBalls[0].rotation()); 756 multMatrix(m); 732 757 733 758 if (glActiveTexture) { … … 739 764 740 765 m_programs[m_currentShader]->bind(); 741 m_programs[m_currentShader]->set Int("tex", 0);742 m_programs[m_currentShader]->set Int("env", 1);743 m_programs[m_currentShader]->set Int("noise", 2);744 m_programs[m_currentShader]->set Matrix("view", view);745 m_programs[m_currentShader]->set Matrix("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); 746 771 m_box->draw(); 747 m_programs[m_currentShader]-> unbind();772 m_programs[m_currentShader]->release(); 748 773 749 774 if (glActiveTexture) { … … 830 855 const int N = (m_updateAllCubemaps ? 1 : 3); 831 856 832 gfx::Matrix4x4fmat;857 QMatrix4x4 mat; 833 858 GLRenderTargetCube::getProjectionMatrix(mat, 0.1f, 100.0f); 834 859 835 860 glMatrixMode(GL_PROJECTION); 836 861 glPushMatrix(); 837 glLoadMatrixf(mat.bits());862 loadMatrix(mat); 838 863 839 864 glMatrixMode(GL_MODELVIEW); 840 865 glPushMatrix(); 841 866 842 gfx::Vector3fcenter;867 QVector3D center; 843 868 844 869 for (int i = m_frame % N; i < m_cubemaps.size(); i += N) { … … 847 872 848 873 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)); 850 876 851 877 for (int face = 0; face < 6; ++face) { … … 853 879 854 880 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); 857 883 858 884 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); … … 886 912 float height = float(painter->device()->height()); 887 913 914 painter->beginNativePainting(); 888 915 setStates(); 889 916 … … 898 925 glMatrixMode(GL_MODELVIEW); 899 926 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); 906 930 renderBoxes(view); 907 931 908 932 defaultStates(); 909 933 ++m_frame; 934 935 painter->endNativePainting(); 910 936 } 911 937 … … 937 963 938 964 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()); 940 966 event->accept(); 941 967 } else { 942 m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity());968 m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), QQuaternion()); 943 969 } 944 970 } … … 961 987 962 988 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()); 964 990 event->accept(); 965 991 } … … 983 1009 984 1010 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()); 986 1012 event->accept(); 987 1013 } … … 1022 1048 { 1023 1049 // set the color in all programs 1024 foreach ( GLProgram *program, m_programs) {1050 foreach (QGLShaderProgram *program, m_programs) { 1025 1051 program->bind(); 1026 program->set Color(name, color);1027 program-> unbind();1052 program->setUniformValue(program->uniformLocation(name), QColor(color)); 1053 program->release(); 1028 1054 } 1029 1055 } … … 1032 1058 { 1033 1059 // set the color in all programs 1034 foreach ( GLProgram *program, m_programs) {1060 foreach (QGLShaderProgram *program, m_programs) { 1035 1061 program->bind(); 1036 program->set Float(name, value);1037 program-> unbind();1062 program->setUniformValue(program->uniformLocation(name), value); 1063 program->release(); 1038 1064 } 1039 1065 }
Note:
See TracChangeset
for help on using the changeset viewer.