diff options
Diffstat (limited to 'src/render/Examples')
-rw-r--r-- | src/render/Examples/Qt3DSRenderClearColorExample.cpp | 63 | ||||
-rw-r--r-- | src/render/Examples/Qt3DSRenderExample.cpp | 491 | ||||
-rw-r--r-- | src/render/Examples/Qt3DSRenderExample.h | 123 | ||||
-rw-r--r-- | src/render/Examples/Qt3DSRenderExampleTools.cpp | 188 | ||||
-rw-r--r-- | src/render/Examples/Qt3DSRenderExampleTools.h | 89 | ||||
-rw-r--r-- | src/render/Examples/Qt3DSRenderRenderToTextureExample.cpp | 178 | ||||
-rw-r--r-- | src/render/Examples/Qt3DSRenderSpinningCubeExample.cpp | 104 |
7 files changed, 1236 insertions, 0 deletions
diff --git a/src/render/Examples/Qt3DSRenderClearColorExample.cpp b/src/render/Examples/Qt3DSRenderClearColorExample.cpp new file mode 100644 index 0000000..f67cf71 --- /dev/null +++ b/src/render/Examples/Qt3DSRenderClearColorExample.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "Qt3DSRenderExample.h" +#include "foundation/Qt3DSVec4.h" + +using namespace qt3ds; +using namespace qt3ds::render; + +namespace { +class ClearColor : public NVRenderExample +{ + NVRenderContext &m_Context; + +public: + ClearColor(NVRenderContext &ctx) + : m_Context(ctx) + { + } + virtual void drawFrame(double currentSeconds) + { + // Apply this value immediately but track it so that a later pop will in fact + // restore this value. + if (currentSeconds < 1) + m_Context.SetClearColor(QT3DSVec4(.8f, .0f, .0f, 1.f)); + else if (currentSeconds < 2) + m_Context.SetClearColor(QT3DSVec4(0.f, .0f, 1.f, 1.f)); + else + m_Context.SetClearColor(QT3DSVec4(0.f, 1.0f, 1.f, 1.f)); + m_Context.Clear(NVRenderClearFlags(NVRenderClearValues::Color)); + } + virtual QT3DSU32 getRuntimeInSeconds() { return 2; } + virtual void release() { NVDelete(m_Context.GetFoundation(), this); } +}; +} + +// QT3DS_RENDER_REGISTER_EXAMPLE( ClearColor );
\ No newline at end of file diff --git a/src/render/Examples/Qt3DSRenderExample.cpp b/src/render/Examples/Qt3DSRenderExample.cpp new file mode 100644 index 0000000..d866cda --- /dev/null +++ b/src/render/Examples/Qt3DSRenderExample.cpp @@ -0,0 +1,491 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "Qt3DSRenderExample.h" +#include "render/NvRenderBaseTypes.h" +#include "render_util/NVRenderAllocator.h" +#include "render_util/NVRenderErrorStream.h" +#include "render_util/NVRenderUtils.h" +#include "foundation/Qt3DSFoundation.h" +#include "foundation/Qt3DSBroadcastingAllocator.h" +#include "foundation/Qt3DSTime.h" +#include "render/Qt3DSRenderContext.h" +#include "foundation/Qt3DSMath.h" + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +using namespace qt3ds; +using namespace qt3ds::render; + +static TExampleCreateFunc gCreateFuncs[128]; + +TExampleCreateFunc *NVRenderExampleFactory::mExampleCreators = gCreateFuncs; +QT3DSU32 NVRenderExampleFactory::mMaxCreators = 128; +QT3DSU32 NVRenderExampleFactory::mNumCreators = 0; +namespace { +NVRenderErrorStream g_errorStream; +NVRenderAllocator g_allocator(g_errorStream); +NVFoundation *g_foundation(NULL); +NVRenderContext *g_renderContext(NULL); +NVRenderExample *g_example(NULL); +QT3DSI32 g_exampleIdx(0); +Time::Second g_runTime; +Time g_timer; +} + +bool NVRenderExampleFactory::nextExample() +{ + if (g_example) + g_example->release(); + g_example = 0; + if (g_exampleIdx < (int)mNumCreators) { + g_example = mExampleCreators[g_exampleIdx](*g_renderContext); + ++g_exampleIdx; + g_runTime = g_example->getRuntimeInSeconds(); + g_timer = Time(); + return true; + } + return false; +} + +void NVRenderExampleFactory::beginExamples() +{ + g_foundation = NVCreateFoundation(QT3DS_FOUNDATION_VERSION, g_allocator, g_errorStream); + g_renderContext = + &NVRenderContext::CreateGL(*g_foundation, NVRenderContextType(NVRenderContextValues::GL2)); + nextExample(); +} +bool NVRenderExampleFactory::update() +{ + Time::Second currentTime = g_timer.peekElapsedSeconds(); + if (currentTime > g_runTime) + nextExample(); + if (g_example) + g_example->drawFrame(currentTime); + + return g_example != NULL; +} + +void NVRenderExampleFactory::endExamples() +{ + g_exampleIdx = mNumCreators; + nextExample(); + if (g_renderContext) + g_renderContext->release(); + if (g_foundation) + g_foundation->release(); + + g_renderContext = NULL; + g_foundation = NULL; +} + +namespace qt3ds { +namespace render { + /////////////////////////////////////////////////////////////////////////////// + + // Math stuff + + int eq(float a, float b) + { + float diff = a - b; + if (diff < 0) { + diff = -diff; + } + return diff <= eps; + } + + // + // Matrix functions, since GLES 2.x doesn't provide them + // + + void NvGl2DemoMatrixIdentity(float m[16]) + { + memset(m, 0, sizeof(float) * 16); + m[4 * 0 + 0] = m[4 * 1 + 1] = m[4 * 2 + 2] = m[4 * 3 + 3] = 1.0; + } + + int NvGl2DemoMatrixEquals(float a[16], float b[16]) + { + int i; + for (i = 0; i < 16; ++i) + if (!eq(a[i], b[i])) + return 0; + + return 1; + } + + void NvGl2DemoMatrixTranspose(float m[16]) + { + int i, j; + float t; + for (i = 1; i < 4; ++i) + for (j = 0; j < i; ++j) { + t = m[4 * i + j]; + m[4 * i + j] = m[4 * j + i]; + m[4 * j + i] = t; + } + } + + void NvGl2DemoMatrixMultiply(float m0[16], float m1[16]) + { + int r, c, i; + for (r = 0; r < 4; r++) { + float m[4] = { 0.0, 0.0, 0.0, 0.0 }; + for (c = 0; c < 4; c++) { + for (i = 0; i < 4; i++) { + m[c] += m0[4 * i + r] * m1[4 * c + i]; + } + } + for (c = 0; c < 4; c++) { + m0[4 * c + r] = m[c]; + } + } + } + + void NvGl2DemoMatrixMultiply_4x4_3x3(float m0[16], float m1[9]) + { + int r, c, i; + for (r = 0; r < 4; r++) { + float m[3] = { 0.0, 0.0, 0.0 }; + for (c = 0; c < 3; c++) { + for (i = 0; i < 3; i++) { + m[c] += m0[4 * i + r] * m1[3 * c + i]; + } + } + for (c = 0; c < 3; c++) { + m0[4 * c + r] = m[c]; + } + } + } + + void NvGl2DemoMatrixMultiply_3x3(float m0[9], float m1[9]) + { + int r, c, i; + for (r = 0; r < 3; r++) { + float m[3] = { 0.0, 0.0, 0.0 }; + for (c = 0; c < 3; c++) { + for (i = 0; i < 3; i++) { + m[c] += m0[3 * i + r] * m1[3 * c + i]; + } + } + for (c = 0; c < 3; c++) { + m0[3 * c + r] = m[c]; + } + } + } + + void NvGl2DemoMatrixFrustum(float m[16], float l, float r, float b, float t, float n, float f) + { + float m1[16]; + float rightMinusLeftInv, topMinusBottomInv, farMinusNearInv, twoNear; + + rightMinusLeftInv = 1.0f / (r - l); + topMinusBottomInv = 1.0f / (t - b); + farMinusNearInv = 1.0f / (f - n); + twoNear = 2.0f * n; + + m1[0] = twoNear * rightMinusLeftInv; + m1[1] = 0.0f; + m1[2] = 0.0f; + m1[3] = 0.0f; + + m1[4] = 0.0f; + m1[5] = twoNear * topMinusBottomInv; + m1[6] = 0.0f; + m1[7] = 0.0f; + + m1[8] = (r + l) * rightMinusLeftInv; + m1[9] = (t + b) * topMinusBottomInv; + m1[10] = -(f + n) * farMinusNearInv; + m1[11] = -1.0f; + + m1[12] = 0.0f; + m1[13] = 0.0f; + m1[14] = -(twoNear * f) * farMinusNearInv; + m1[15] = 0.0f; + + NvGl2DemoMatrixMultiply(m, m1); + } + + void NvGl2DemoMatrixOrtho(float m[16], float l, float r, float b, float t, float n, float f) + { + float m1[16]; + float rightMinusLeftInv, topMinusBottomInv, farMinusNearInv; + + rightMinusLeftInv = 1.0f / (r - l); + topMinusBottomInv = 1.0f / (t - b); + farMinusNearInv = 1.0f / (f - n); + + m1[0] = 2.0f * rightMinusLeftInv; + m1[1] = 0.0f; + m1[2] = 0.0f; + m1[3] = 0.0f; + + m1[4] = 0.0f; + m1[5] = 2.0f * topMinusBottomInv; + m1[6] = 0.0f; + m1[7] = 0.0f; + + m1[8] = 0.0f; + m1[9] = 0.0f; + m1[10] = -2.0f * farMinusNearInv; + m1[11] = 0.0f; + + m1[12] = -(r + l) * rightMinusLeftInv; + m1[13] = -(t + b) * topMinusBottomInv; + m1[14] = -(f + n) * farMinusNearInv; + m1[15] = 1.0f; + + NvGl2DemoMatrixMultiply(m, m1); + } + + void NvGl2DemoMatrixTranslate(float m[16], float x, float y, float z) + { + float m1[16]; + NvGl2DemoMatrixIdentity(m1); + + m1[4 * 3 + 0] = x; + m1[4 * 3 + 1] = y; + m1[4 * 3 + 2] = z; + + NvGl2DemoMatrixMultiply(m, m1); + } + + void NvGl2DemoMatrixRotate_create3x3(float m[9], float theta, float x, float y, float z) + { + float len = (float)sqrtf(x * x + y * y + z * z); + float u0 = x / len; + float u1 = y / len; + float u2 = z / len; + float rad = (float)(theta / 180 * NVPi); + float c = (float)cosf(rad); + float s = (float)sinf(rad); + m[3 * 0 + 0] = u0 * u0 + c * (1 - u0 * u0) + s * 0; + m[3 * 0 + 1] = u0 * u1 + c * (0 - u0 * u1) + s * u2; + m[3 * 0 + 2] = u0 * u2 + c * (0 - u0 * u2) - s * u1; + + m[3 * 1 + 0] = u1 * u0 + c * (0 - u1 * u0) - s * u2; + m[3 * 1 + 1] = u1 * u1 + c * (1 - u1 * u1) + s * 0; + m[3 * 1 + 2] = u1 * u2 + c * (0 - u1 * u2) + s * u0; + + m[3 * 2 + 0] = u2 * u0 + c * (0 - u2 * u0) + s * u1; + m[3 * 2 + 1] = u2 * u1 + c * (0 - u2 * u1) - s * u0; + m[3 * 2 + 2] = u2 * u2 + c * (1 - u2 * u2) + s * 0; + } + + void NvGl2DemoMatrixRotate(float m[16], float theta, float x, float y, float z) + { + float r[9]; + NvGl2DemoMatrixRotate_create3x3(r, theta, x, y, z); + NvGl2DemoMatrixMultiply_4x4_3x3(m, r); + } + + void NvGl2DemoMatrixRotate_3x3(float m[9], float theta, float x, float y, float z) + { + float r[9]; + NvGl2DemoMatrixRotate_create3x3(r, theta, x, y, z); + NvGl2DemoMatrixMultiply_3x3(m, r); + } + + float NvGl2DemoMatrixDeterminant(float m[16]) + { + return m[4 * 0 + 3] * m[4 * 1 + 2] * m[4 * 2 + 1] * m[4 * 3 + 0] + - m[4 * 0 + 2] * m[4 * 1 + 3] * m[4 * 2 + 1] * m[4 * 3 + 0] + - m[4 * 0 + 3] * m[4 * 1 + 1] * m[4 * 2 + 2] * m[4 * 3 + 0] + + m[4 * 0 + 1] * m[4 * 1 + 3] * m[4 * 2 + 2] * m[4 * 3 + 0] + + m[4 * 0 + 2] * m[4 * 1 + 1] * m[4 * 2 + 3] * m[4 * 3 + 0] + - m[4 * 0 + 1] * m[4 * 1 + 2] * m[4 * 2 + 3] * m[4 * 3 + 0] + - m[4 * 0 + 3] * m[4 * 1 + 2] * m[4 * 2 + 0] * m[4 * 3 + 1] + + m[4 * 0 + 2] * m[4 * 1 + 3] * m[4 * 2 + 0] * m[4 * 3 + 1] + + m[4 * 0 + 3] * m[4 * 1 + 0] * m[4 * 2 + 2] * m[4 * 3 + 1] + - m[4 * 0 + 0] * m[4 * 1 + 3] * m[4 * 2 + 2] * m[4 * 3 + 1] + - m[4 * 0 + 2] * m[4 * 1 + 0] * m[4 * 2 + 3] * m[4 * 3 + 1] + + m[4 * 0 + 0] * m[4 * 1 + 2] * m[4 * 2 + 3] * m[4 * 3 + 1] + + m[4 * 0 + 3] * m[4 * 1 + 1] * m[4 * 2 + 0] * m[4 * 3 + 2] + - m[4 * 0 + 1] * m[4 * 1 + 3] * m[4 * 2 + 0] * m[4 * 3 + 2] + - m[4 * 0 + 3] * m[4 * 1 + 0] * m[4 * 2 + 1] * m[4 * 3 + 2] + + m[4 * 0 + 0] * m[4 * 1 + 3] * m[4 * 2 + 1] * m[4 * 3 + 2] + + m[4 * 0 + 1] * m[4 * 1 + 0] * m[4 * 2 + 3] * m[4 * 3 + 2] + - m[4 * 0 + 0] * m[4 * 1 + 1] * m[4 * 2 + 3] * m[4 * 3 + 2] + - m[4 * 0 + 2] * m[4 * 1 + 1] * m[4 * 2 + 0] * m[4 * 3 + 3] + + m[4 * 0 + 1] * m[4 * 1 + 2] * m[4 * 2 + 0] * m[4 * 3 + 3] + + m[4 * 0 + 2] * m[4 * 1 + 0] * m[4 * 2 + 1] * m[4 * 3 + 3] + - m[4 * 0 + 0] * m[4 * 1 + 2] * m[4 * 2 + 1] * m[4 * 3 + 3] + - m[4 * 0 + 1] * m[4 * 1 + 0] * m[4 * 2 + 2] * m[4 * 3 + 3] + + m[4 * 0 + 0] * m[4 * 1 + 1] * m[4 * 2 + 2] * m[4 * 3 + 3]; + } + + void NvGl2DemoMatrixInverse(float m[16]) + { + float a[16]; + float det; + int i; + float b[16], e[16]; + + a[4 * 0 + 0] = m[4 * 1 + 2] * m[4 * 2 + 3] * m[4 * 3 + 1] + - m[4 * 1 + 3] * m[4 * 2 + 2] * m[4 * 3 + 1] + + m[4 * 1 + 3] * m[4 * 2 + 1] * m[4 * 3 + 2] + - m[4 * 1 + 1] * m[4 * 2 + 3] * m[4 * 3 + 2] + - m[4 * 1 + 2] * m[4 * 2 + 1] * m[4 * 3 + 3] + + m[4 * 1 + 1] * m[4 * 2 + 2] * m[4 * 3 + 3]; + a[4 * 0 + 1] = m[4 * 0 + 3] * m[4 * 2 + 2] * m[4 * 3 + 1] + - m[4 * 0 + 2] * m[4 * 2 + 3] * m[4 * 3 + 1] + - m[4 * 0 + 3] * m[4 * 2 + 1] * m[4 * 3 + 2] + + m[4 * 0 + 1] * m[4 * 2 + 3] * m[4 * 3 + 2] + + m[4 * 0 + 2] * m[4 * 2 + 1] * m[4 * 3 + 3] + - m[4 * 0 + 1] * m[4 * 2 + 2] * m[4 * 3 + 3]; + a[4 * 0 + 2] = m[4 * 0 + 2] * m[4 * 1 + 3] * m[4 * 3 + 1] + - m[4 * 0 + 3] * m[4 * 1 + 2] * m[4 * 3 + 1] + + m[4 * 0 + 3] * m[4 * 1 + 1] * m[4 * 3 + 2] + - m[4 * 0 + 1] * m[4 * 1 + 3] * m[4 * 3 + 2] + - m[4 * 0 + 2] * m[4 * 1 + 1] * m[4 * 3 + 3] + + m[4 * 0 + 1] * m[4 * 1 + 2] * m[4 * 3 + 3]; + a[4 * 0 + 3] = m[4 * 0 + 3] * m[4 * 1 + 2] * m[4 * 2 + 1] + - m[4 * 0 + 2] * m[4 * 1 + 3] * m[4 * 2 + 1] + - m[4 * 0 + 3] * m[4 * 1 + 1] * m[4 * 2 + 2] + + m[4 * 0 + 1] * m[4 * 1 + 3] * m[4 * 2 + 2] + + m[4 * 0 + 2] * m[4 * 1 + 1] * m[4 * 2 + 3] + - m[4 * 0 + 1] * m[4 * 1 + 2] * m[4 * 2 + 3]; + a[4 * 1 + 0] = m[4 * 1 + 3] * m[4 * 2 + 2] * m[4 * 3 + 0] + - m[4 * 1 + 2] * m[4 * 2 + 3] * m[4 * 3 + 0] + - m[4 * 1 + 3] * m[4 * 2 + 0] * m[4 * 3 + 2] + + m[4 * 1 + 0] * m[4 * 2 + 3] * m[4 * 3 + 2] + + m[4 * 1 + 2] * m[4 * 2 + 0] * m[4 * 3 + 3] + - m[4 * 1 + 0] * m[4 * 2 + 2] * m[4 * 3 + 3]; + a[4 * 1 + 1] = m[4 * 0 + 2] * m[4 * 2 + 3] * m[4 * 3 + 0] + - m[4 * 0 + 3] * m[4 * 2 + 2] * m[4 * 3 + 0] + + m[4 * 0 + 3] * m[4 * 2 + 0] * m[4 * 3 + 2] + - m[4 * 0 + 0] * m[4 * 2 + 3] * m[4 * 3 + 2] + - m[4 * 0 + 2] * m[4 * 2 + 0] * m[4 * 3 + 3] + + m[4 * 0 + 0] * m[4 * 2 + 2] * m[4 * 3 + 3]; + a[4 * 1 + 2] = m[4 * 0 + 3] * m[4 * 1 + 2] * m[4 * 3 + 0] + - m[4 * 0 + 2] * m[4 * 1 + 3] * m[4 * 3 + 0] + - m[4 * 0 + 3] * m[4 * 1 + 0] * m[4 * 3 + 2] + + m[4 * 0 + 0] * m[4 * 1 + 3] * m[4 * 3 + 2] + + m[4 * 0 + 2] * m[4 * 1 + 0] * m[4 * 3 + 3] + - m[4 * 0 + 0] * m[4 * 1 + 2] * m[4 * 3 + 3]; + a[4 * 1 + 3] = m[4 * 0 + 2] * m[4 * 1 + 3] * m[4 * 2 + 0] + - m[4 * 0 + 3] * m[4 * 1 + 2] * m[4 * 2 + 0] + + m[4 * 0 + 3] * m[4 * 1 + 0] * m[4 * 2 + 2] + - m[4 * 0 + 0] * m[4 * 1 + 3] * m[4 * 2 + 2] + - m[4 * 0 + 2] * m[4 * 1 + 0] * m[4 * 2 + 3] + + m[4 * 0 + 0] * m[4 * 1 + 2] * m[4 * 2 + 3]; + a[4 * 2 + 0] = m[4 * 1 + 1] * m[4 * 2 + 3] * m[4 * 3 + 0] + - m[4 * 1 + 3] * m[4 * 2 + 1] * m[4 * 3 + 0] + + m[4 * 1 + 3] * m[4 * 2 + 0] * m[4 * 3 + 1] + - m[4 * 1 + 0] * m[4 * 2 + 3] * m[4 * 3 + 1] + - m[4 * 1 + 1] * m[4 * 2 + 0] * m[4 * 3 + 3] + + m[4 * 1 + 0] * m[4 * 2 + 1] * m[4 * 3 + 3]; + a[4 * 2 + 1] = m[4 * 0 + 3] * m[4 * 2 + 1] * m[4 * 3 + 0] + - m[4 * 0 + 1] * m[4 * 2 + 3] * m[4 * 3 + 0] + - m[4 * 0 + 3] * m[4 * 2 + 0] * m[4 * 3 + 1] + + m[4 * 0 + 0] * m[4 * 2 + 3] * m[4 * 3 + 1] + + m[4 * 0 + 1] * m[4 * 2 + 0] * m[4 * 3 + 3] + - m[4 * 0 + 0] * m[4 * 2 + 1] * m[4 * 3 + 3]; + a[4 * 2 + 2] = m[4 * 0 + 1] * m[4 * 1 + 3] * m[4 * 3 + 0] + - m[4 * 0 + 3] * m[4 * 1 + 1] * m[4 * 3 + 0] + + m[4 * 0 + 3] * m[4 * 1 + 0] * m[4 * 3 + 1] + - m[4 * 0 + 0] * m[4 * 1 + 3] * m[4 * 3 + 1] + - m[4 * 0 + 1] * m[4 * 1 + 0] * m[4 * 3 + 3] + + m[4 * 0 + 0] * m[4 * 1 + 1] * m[4 * 3 + 3]; + a[4 * 2 + 3] = m[4 * 0 + 3] * m[4 * 1 + 1] * m[4 * 2 + 0] + - m[4 * 0 + 1] * m[4 * 1 + 3] * m[4 * 2 + 0] + - m[4 * 0 + 3] * m[4 * 1 + 0] * m[4 * 2 + 1] + + m[4 * 0 + 0] * m[4 * 1 + 3] * m[4 * 2 + 1] + + m[4 * 0 + 1] * m[4 * 1 + 0] * m[4 * 2 + 3] + - m[4 * 0 + 0] * m[4 * 1 + 1] * m[4 * 2 + 3]; + a[4 * 3 + 0] = m[4 * 1 + 2] * m[4 * 2 + 1] * m[4 * 3 + 0] + - m[4 * 1 + 1] * m[4 * 2 + 2] * m[4 * 3 + 0] + - m[4 * 1 + 2] * m[4 * 2 + 0] * m[4 * 3 + 1] + + m[4 * 1 + 0] * m[4 * 2 + 2] * m[4 * 3 + 1] + + m[4 * 1 + 1] * m[4 * 2 + 0] * m[4 * 3 + 2] + - m[4 * 1 + 0] * m[4 * 2 + 1] * m[4 * 3 + 2]; + a[4 * 3 + 1] = m[4 * 0 + 1] * m[4 * 2 + 2] * m[4 * 3 + 0] + - m[4 * 0 + 2] * m[4 * 2 + 1] * m[4 * 3 + 0] + + m[4 * 0 + 2] * m[4 * 2 + 0] * m[4 * 3 + 1] + - m[4 * 0 + 0] * m[4 * 2 + 2] * m[4 * 3 + 1] + - m[4 * 0 + 1] * m[4 * 2 + 0] * m[4 * 3 + 2] + + m[4 * 0 + 0] * m[4 * 2 + 1] * m[4 * 3 + 2]; + a[4 * 3 + 2] = m[4 * 0 + 2] * m[4 * 1 + 1] * m[4 * 3 + 0] + - m[4 * 0 + 1] * m[4 * 1 + 2] * m[4 * 3 + 0] + - m[4 * 0 + 2] * m[4 * 1 + 0] * m[4 * 3 + 1] + + m[4 * 0 + 0] * m[4 * 1 + 2] * m[4 * 3 + 1] + + m[4 * 0 + 1] * m[4 * 1 + 0] * m[4 * 3 + 2] + - m[4 * 0 + 0] * m[4 * 1 + 1] * m[4 * 3 + 2]; + a[4 * 3 + 3] = m[4 * 0 + 1] * m[4 * 1 + 2] * m[4 * 2 + 0] + - m[4 * 0 + 2] * m[4 * 1 + 1] * m[4 * 2 + 0] + + m[4 * 0 + 2] * m[4 * 1 + 0] * m[4 * 2 + 1] + - m[4 * 0 + 0] * m[4 * 1 + 2] * m[4 * 2 + 1] + - m[4 * 0 + 1] * m[4 * 1 + 0] * m[4 * 2 + 2] + + m[4 * 0 + 0] * m[4 * 1 + 1] * m[4 * 2 + 2]; + + det = NvGl2DemoMatrixDeterminant(m); + + for (i = 0; i < 16; ++i) + a[i] /= det; + + NvGl2DemoMatrixIdentity(e); + + NvGl2DemoMatrixCopy(b, m); + NvGl2DemoMatrixMultiply(b, a); + + NvGl2DemoMatrixCopy(m, a); + } + + void NvGl2DemoMatrixCopy(float dest[16], float src[16]) + { + memcpy(dest, src, 16 * sizeof(float)); + } + + void NvGl2DemoMatrixPrint(float a[16]) + { + int i, j; + + for (i = 0; i < 4; ++i) + for (j = 0; j < 4; ++j) + printf("%f%c", a[4 * i + j], j == 3 ? '\n' : ' '); + } + + void NvGl2DemoMatrixVectorMultiply(float m[16], float v[4]) + { + float res[4]; + int i, j; + + for (i = 0; i < 4; ++i) { + res[i] = 0; + for (j = 0; j < 4; ++j) + res[i] += m[i * 4 + j] * v[j]; + } + + memcpy(v, res, sizeof(res)); + } +} +}
\ No newline at end of file diff --git a/src/render/Examples/Qt3DSRenderExample.h b/src/render/Examples/Qt3DSRenderExample.h new file mode 100644 index 0000000..79bfd91 --- /dev/null +++ b/src/render/Examples/Qt3DSRenderExample.h @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#pragma once +#ifndef QT3DS_RENDER_EXAMPLE_H +#define QT3DS_RENDER_EXAMPLE_H +#include "foundation/Qt3DS.h" +#include "foundation/Qt3DSAssert.h" +#include "foundation/Qt3DSFoundation.h" +#include "foundation/Qt3DSBroadcastingAllocator.h" +#include "render/NvRenderBaseTypes.h" +#include "render/Qt3DSRenderContext.h" + +namespace qt3ds { +namespace render { + + class NVRenderContext; + + class NVRenderExample + { + protected: + virtual ~NVRenderExample() {} + public: + virtual void drawFrame(double currentSeconds) = 0; + virtual QT3DSU32 getRuntimeInSeconds() { return 5; } + virtual void handleKeypress(int /*keypress*/) {} + virtual void release() = 0; + }; + + typedef NVRenderExample *(*TExampleCreateFunc)(NVRenderContext &context); + + struct NVRenderExampleFactory + { + static TExampleCreateFunc *mExampleCreators; + static QT3DSU32 mMaxCreators; + static QT3DSU32 mNumCreators; + + static void addCreator(TExampleCreateFunc creator) + { + if (mNumCreators < mMaxCreators) { + mExampleCreators[mNumCreators] = creator; + ++mNumCreators; + } else + QT3DS_ASSERT(false); + } + + // Assuming that the render context is egl + // Relies on the global structures defined in demo common + // to figure out window state. + static bool nextExample(); + static void beginExamples(); + static bool update(); + static void endExamples(); + }; + + template <typename TExample> + struct NVRenderExampleCreator + { + + static NVRenderExample *createExample(NVRenderContext &context) + { + return QT3DS_NEW(context.GetFoundation().getAllocator(), TExample)(context); + } + NVRenderExampleCreator() { NVRenderExampleFactory::addCreator(createExample); } + }; + +#define QT3DS_RENDER_REGISTER_EXAMPLE(dtype) static NVRenderExampleCreator<dtype> dtype##Creator + +#define eps 1e-4 + + int eq(float a, float b); + + // Matrix functions + void NvGl2DemoMatrixIdentity(float m[16]); + int NvGl2DemoMatrixEquals(float a[16], float b[16]); + void NvGl2DemoMatrixTranspose(float m[16]); + void NvGl2DemoMatrixMultiply(float m0[16], float m1[16]); + void NvGl2DemoMatrixMultiply_4x4_3x3(float m0[16], float m1[9]); + void NvGl2DemoMatrixMultiply_3x3(float m0[9], float m1[9]); + void NvGl2DemoMatrixFrustum(float m[16], float l, float r, float b, float t, float n, float f); + void NvGl2DemoMatrixOrtho(float m[16], float l, float r, float b, float t, float n, float f); + void NvGl2DemoMatrixTranslate(float m[16], float x, float y, float z); + void NvGl2DemoMatrixRotate_create3x3(float m[9], float theta, float x, float y, float z); + void NvGl2DemoMatrixRotate(float m[16], float theta, float x, float y, float z); + void NvGl2DemoMatrixRotate_3x3(float m[9], float theta, float x, float y, float z); + + float NvGl2DemoMatrixDeterminant(float m[16]); + void NvGl2DemoMatrixInverse(float m[16]); + void NvGl2DemoMatrixCopy(float dest[16], float src[16]); + + void NvGl2DemoMatrixPrint(float a[16]); + + void NvGl2DemoMatrixVectorMultiply(float m[16], float v[4]); +} +} + +#endif
\ No newline at end of file diff --git a/src/render/Examples/Qt3DSRenderExampleTools.cpp b/src/render/Examples/Qt3DSRenderExampleTools.cpp new file mode 100644 index 0000000..efae6f5 --- /dev/null +++ b/src/render/Examples/Qt3DSRenderExampleTools.cpp @@ -0,0 +1,188 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "Qt3DSRenderExampleTools.h" +#include "render/Qt3DSRenderIndexBuffer.h" +#include "render/Qt3DSRenderVertexBuffer.h" +#include "render_util/NVRenderUtils.h" +#include "foundation/Qt3DSVec3.h" +#include "foundation/Qt3DSVec4.h" + +using namespace qt3ds; +using namespace qt3ds::render; + +struct BoxFace +{ + QT3DSVec3 positions[4]; + QT3DSVec3 normal; +}; + +static const BoxFace g_BoxFaces[] = { + { // Z+ + QT3DSVec3(-1, -1, 1), QT3DSVec3(-1, 1, 1), QT3DSVec3(1, 1, 1), QT3DSVec3(1, -1, 1), QT3DSVec3(0, 0, 1) }, + { // X+ + QT3DSVec3(1, -1, 1), QT3DSVec3(1, 1, 1), QT3DSVec3(1, 1, -1), QT3DSVec3(1, -1, -1), QT3DSVec3(1, 0, 0) }, + { // Z- + QT3DSVec3(1, -1, -1), QT3DSVec3(1, 1, -1), QT3DSVec3(-1, 1, -1), QT3DSVec3(-1, -1, -1), + QT3DSVec3(0, 0, -1) }, + { // X- + QT3DSVec3(-1, -1, -1), QT3DSVec3(-1, 1, -1), QT3DSVec3(-1, 1, 1), QT3DSVec3(-1, -1, 1), + QT3DSVec3(-1, 0, 0) }, + { // Y+ + QT3DSVec3(-1, 1, 1), QT3DSVec3(-1, 1, -1), QT3DSVec3(1, 1, -1), QT3DSVec3(1, 1, 1), QT3DSVec3(0, 1, 0) }, + { // Y- + QT3DSVec3(-1, -1, -1), QT3DSVec3(-1, -1, 1), QT3DSVec3(1, -1, 1), QT3DSVec3(1, -1, -1), QT3DSVec3(0, -1, 0) } +}; + +static const QT3DSVec3 g_BoxUVs[] = { + QT3DSVec3(0, 1, 0), QT3DSVec3(0, 0, 0), QT3DSVec3(1, 0, 0), QT3DSVec3(1, 1, 0), +}; + +void NVRenderExampleTools::createBox(NVRenderContext &context, + NVRenderVertexBuffer *&outVertexBuffer, + NVRenderIndexBuffer *&outIndexBuffer, bool releaseMemory) +{ + const QT3DSU32 numVerts = 24; + const QT3DSU32 numIndices = 36; + QT3DSVec3 extents = QT3DSVec3(1, 1, 1); + + NVRenderVertexBufferEntry entries[] = { + NVRenderVertexBufferEntry("attr_pos", NVRenderComponentTypes::QT3DSF32, 3, 0), + NVRenderVertexBufferEntry("attr_norm", NVRenderComponentTypes::QT3DSF32, 3, 3 * sizeof(QT3DSF32)), + NVRenderVertexBufferEntry("attr_uv", NVRenderComponentTypes::QT3DSF32, 2, 6 * sizeof(QT3DSF32)), + }; + + QT3DSU32 bufStride = 8 * sizeof(QT3DSF32); + QT3DSU32 bufSize = bufStride * numVerts; + outVertexBuffer = context.CreateVertexBuffer( + NVRenderBufferUsageType::Static, NVConstDataRef<NVRenderVertexBufferEntry>(entries, 3), 0, + releaseMemory ? 0 : bufSize); + QT3DS_ASSERT(bufStride == outVertexBuffer->GetStride()); + NVDataRef<QT3DSU8> vertData; + if (releaseMemory) + vertData = NVDataRef<QT3DSU8>( + (QT3DSU8 *)QT3DS_ALLOC(context.GetFoundation().getAllocator(), bufSize, "VertexBufferData"), + bufSize); + else + vertData = outVertexBuffer->LockBuffer(bufSize); + QT3DSU8 *positions = (QT3DSU8 *)vertData.begin(); + QT3DSU8 *normals = positions + 3 * sizeof(QT3DSF32); + QT3DSU8 *uvs = normals + 3 * sizeof(QT3DSF32); + + for (QT3DSU32 i = 0; i < 6; i++) { + const BoxFace &bf = g_BoxFaces[i]; + for (QT3DSU32 j = 0; j < 4; j++) { + QT3DSVec3 &p = *(QT3DSVec3 *)positions; + positions = ((QT3DSU8 *)positions) + bufStride; + QT3DSVec3 &n = *(QT3DSVec3 *)normals; + normals = ((QT3DSU8 *)normals) + bufStride; + QT3DSF32 *uv = (QT3DSF32 *)uvs; + uvs = ((QT3DSU8 *)uvs) + bufStride; + n = bf.normal; + p = bf.positions[j].multiply(extents); + uv[0] = g_BoxUVs[j].x; + uv[1] = g_BoxUVs[j].y; + } + } + + if (releaseMemory) { + outVertexBuffer->SetBuffer(vertData, false); + context.GetFoundation().getAllocator().deallocate(vertData.begin()); + } else + outVertexBuffer->UnlockBuffer(); + + bufSize = numIndices * sizeof(QT3DSU16); + outIndexBuffer = + context.CreateIndexBuffer(NVRenderBufferUsageType::Static, NVRenderComponentTypes::QT3DSU16, + releaseMemory ? 0 : bufSize); + NVDataRef<QT3DSU8> indexData; + if (releaseMemory) + indexData = NVDataRef<QT3DSU8>( + (QT3DSU8 *)QT3DS_ALLOC(context.GetFoundation().getAllocator(), bufSize, "IndexData"), + bufSize); + else + indexData = outIndexBuffer->LockBuffer(bufSize); + QT3DSU16 *indices = reinterpret_cast<QT3DSU16 *>(indexData.begin()); + for (QT3DSU8 i = 0; i < 6; i++) { + const QT3DSU16 base = i * 4; + *(indices++) = base + 0; + *(indices++) = base + 1; + *(indices++) = base + 2; + *(indices++) = base + 0; + *(indices++) = base + 2; + *(indices++) = base + 3; + } + if (releaseMemory) { + outIndexBuffer->SetBuffer(indexData, false); + context.GetFoundation().getAllocator().deallocate(indexData.begin()); + } else + outIndexBuffer->UnlockBuffer(); +} + +namespace { + +inline NVConstDataRef<QT3DSI8> toRef(const char *data) +{ + size_t len = strlen(data) + 1; + return NVConstDataRef<QT3DSI8>((const QT3DSI8 *)data, len); +} + +static void dumpShaderOutput(NVRenderContext &ctx, + const NVRenderVertFragCompilationResult &compResult) +{ + if (!isTrivial(compResult.mFragCompilationOutput)) { + ctx.GetFoundation().error(QT3DS_WARN, "Frag output:\n%s", compResult.mFragCompilationOutput); + } + if (!isTrivial(compResult.mVertCompilationOutput)) { + ctx.GetFoundation().error(QT3DS_WARN, "Vert output:\n%s", compResult.mVertCompilationOutput); + } + if (!isTrivial(compResult.mLinkOutput)) { + ctx.GetFoundation().error(QT3DS_WARN, "Link output:\n%s", compResult.mLinkOutput); + } +} + +NVRenderVertFragShader *compileAndDump(NVRenderContext &ctx, const char *name, + const char *vertShader, const char *fragShader) +{ + NVRenderVertFragCompilationResult compResult = + ctx.CompileSource(name, toRef(vertShader), toRef(fragShader)); + dumpShaderOutput(ctx, compResult); + return compResult.mShader; +} +} + +NVRenderVertFragShader *NVRenderExampleTools::createSimpleShader(NVRenderContext &ctx) +{ + return compileAndDump(ctx, "SimpleShader", getSimpleVertShader(), getSimpleFragShader()); +} + +NVRenderVertFragShader *NVRenderExampleTools::createSimpleShaderTex(NVRenderContext &ctx) +{ + return compileAndDump(ctx, "SimpleShader", getSimpleVertShader(), getSimpleFragShaderTex()); +}
\ No newline at end of file diff --git a/src/render/Examples/Qt3DSRenderExampleTools.h b/src/render/Examples/Qt3DSRenderExampleTools.h new file mode 100644 index 0000000..5b40286 --- /dev/null +++ b/src/render/Examples/Qt3DSRenderExampleTools.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#pragma once +#ifndef QT3DS_RENDER_EXAMPLE_TOOLS_H +#define QT3DS_RENDER_EXAMPLE_TOOLS_H +#include "Qt3DSRenderExample.h" + +namespace qt3ds { +namespace render { + + class NVRenderExampleTools + { + public: + static const char *getSimpleVertShader() + { + return "uniform mat4 mat_mvp;\n" + "attribute vec3 attr_pos; // Vertex pos\n" + "attribute vec3 attr_norm; // Vertex pos\n" + "attribute vec2 attr_uv; // UV coords\n" + "varying vec4 color_to_add;\n" + "varying vec2 uv_coords;\n" + "void main()\n" + "{\n" + "gl_Position = mat_mvp * vec4(attr_pos, 1.0);\n" + "color_to_add.xyz = attr_norm * attr_norm;\n" + "color_to_add.a = 1.0;\n" + "uv_coords = attr_uv;\n" + "}\n"; + } + + static const char *getSimpleFragShader() + { + return "precision mediump sampler2D;\n" + "precision mediump float;\n" + "varying vec4 color_to_add;\n" + "void main()\n" + "{\n" + "gl_FragColor=color_to_add;\n" + "}\n"; + } + + static const char *getSimpleFragShaderTex() + { + return "precision mediump sampler2D;\n" + "precision mediump float;\n" + "uniform sampler2D image0;\n" + "varying vec2 uv_coords;\n" + "void main()\n" + "{\n" + "gl_FragColor=vec4(texture2D( image0, uv_coords ).xyz, 1.0 );\n" + "}\n"; + } + + static void createBox(NVRenderContext &context, NVRenderVertexBuffer *&outVertexBuffer, + NVRenderIndexBuffer *&outIndexBuffer, bool releaseMemory = true); + static NVRenderVertFragShader *createSimpleShader(NVRenderContext &context); + static NVRenderVertFragShader *createSimpleShaderTex(NVRenderContext &context); + }; +} +} + +#endif
\ No newline at end of file diff --git a/src/render/Examples/Qt3DSRenderRenderToTextureExample.cpp b/src/render/Examples/Qt3DSRenderRenderToTextureExample.cpp new file mode 100644 index 0000000..e2cb414 --- /dev/null +++ b/src/render/Examples/Qt3DSRenderRenderToTextureExample.cpp @@ -0,0 +1,178 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "Qt3DSRenderExample.h" +#include "Qt3DSRenderExampleTools.h" +#include "render/Qt3DSRenderFrameBuffer.h" +#include "render/Qt3DSRenderTexture2D.h" +#include "render/Qt3DSRenderIndexBuffer.h" +#include "render/Qt3DSRenderVertexBuffer.h" +#include "render/Qt3DSRenderFrameBuffer.h" +#include "render/Qt3DSRenderRenderBuffer.h" +#include "foundation/Qt3DSVec4.h" + +using namespace qt3ds; +using namespace qt3ds::render; + +namespace { +struct ShaderArgs +{ + float mvp[16]; + NVRenderTexture2DPtr texture; + NVRenderVertFragShaderPtr shader; + ShaderArgs() {} +}; +class RenderToTexture : public NVRenderExample +{ + NVRenderContext &m_Context; + NVScopedRefCounted<NVRenderVertexBuffer> mVertexBuffer; + NVScopedRefCounted<NVRenderIndexBuffer> mIndexBuffer; + // Simple shader + NVScopedRefCounted<NVRenderVertFragShader> mSimpleShader; + // Simple shader with texture lookup. + NVScopedRefCounted<NVRenderVertFragShader> mSimpleShaderTex; + + NVScopedRefCounted<NVRenderFrameBuffer> mFrameBuffer; + NVScopedRefCounted<NVRenderTexture2D> mColorBuffer; + NVScopedRefCounted<NVRenderTexture2D> mDepthBuffer; + + NVRenderHandle mGroupId; + QT3DSU32 mFBWidth; + QT3DSU32 mFBHeight; + + ShaderArgs mShaderArgs; + float frus[16]; + float model[16]; + float rot[9]; + +public: + RenderToTexture(NVRenderContext &context) + : m_Context(context) + , mFBWidth(400) + , mFBHeight(400) + { + NVRenderExampleTools::createBox(m_Context, mVertexBuffer.mPtr, mIndexBuffer.mPtr); + mVertexBuffer->addRef(); + mIndexBuffer->addRef(); + mSimpleShader = NVRenderExampleTools::createSimpleShader(m_Context); + mSimpleShaderTex = NVRenderExampleTools::createSimpleShaderTex(m_Context); + // If you don't want the depth buffer information back out of the system, then you can + // do this. + // mDepthBuffer = m_Context.CreateRenderBuffer( NVRenderRenderBufferFormats::Depth16, + // mFBWidth, mFBHeight ); + + mDepthBuffer = m_Context.CreateTexture2D(); + mDepthBuffer->SetTextureData(NVDataRef<QT3DSU8>(), 0, mFBWidth, mFBHeight, + NVRenderTextureFormats::Depth16); + mColorBuffer = m_Context.CreateTexture2D(); + mColorBuffer->SetTextureData(NVDataRef<QT3DSU8>(), 0, mFBWidth, mFBHeight, + NVRenderTextureFormats::RGBA8); + if (mDepthBuffer.mPtr && mColorBuffer.mPtr) { + // Creating objects tends to Bind them to their active state hooks. + // So to protect the rest of the system against what they are doing (if we care), we + // need + // to push the current state + // Auto-binds the framebuffer. + mFrameBuffer = m_Context.CreateFrameBuffer(); + mFrameBuffer->Attach(NVRenderFrameBufferAttachments::Color0, *mColorBuffer.mPtr); + mFrameBuffer->Attach(NVRenderFrameBufferAttachments::Depth, *mDepthBuffer.mPtr); + QT3DS_ASSERT(mFrameBuffer->IsComplete()); + + m_Context.SetRenderTarget(NULL); + } + mColorBuffer->SetMinFilter(NVRenderTextureMinifyingOp::Linear); + mColorBuffer->SetMagFilter(NVRenderTextureMagnifyingOp::Linear); + m_Context.SetVertexBuffer(mVertexBuffer); + m_Context.SetIndexBuffer(mIndexBuffer); + m_Context.SetDepthTestEnabled(true); + m_Context.SetDepthWriteEnabled(true); + m_Context.SetClearColor(QT3DSVec4(.3f)); + // Setup various matrici + NvGl2DemoMatrixIdentity(model); + NvGl2DemoMatrixIdentity(frus); + NvGl2DemoMatrixFrustum(frus, -1, 1, -1, 1, 1, 10); + NvGl2DemoMatrixTranslate(model, 0, 0, -4); + mShaderArgs.texture = mColorBuffer.mPtr; + } + void setupMVP(QT3DSVec3 translation) + { + float *mvp(mShaderArgs.mvp); + memCopy(mvp, frus, 16 * sizeof(float)); + NvGl2DemoMatrixMultiply(mvp, model); + NvGl2DemoMatrixTranslate(mvp, translation.x, translation.y, translation.z); + NvGl2DemoMatrixMultiply_4x4_3x3(mvp, rot); + } + void DrawIndexedArrays(QT3DSVec3 translation) + { + setupMVP(translation); + m_Context.SetActiveShader(mShaderArgs.shader); + mShaderArgs.shader->Bind(); + mShaderArgs.shader->SetPropertyValue("mat_mvp", + *reinterpret_cast<QT3DSMat44 *>(mShaderArgs.mvp)); + mShaderArgs.shader->SetPropertyValue("image0", mShaderArgs.texture); + m_Context.Draw(NVRenderDrawMode::Triangles, mIndexBuffer->GetNumIndices(), 0); + } + + virtual void drawFrame(double currentSeconds) + { + NvGl2DemoMatrixRotate_create3x3(rot, (float)currentSeconds * 50, .707f, .707f, 0); + NVRenderClearFlags clearFlags(NVRenderClearValues::Color | NVRenderClearValues::Depth); + // render to frame buffer + { + NVRenderContextScopedProperty<NVRenderFrameBufferPtr> __framebuffer( + m_Context, &NVRenderContext::GetRenderTarget, &NVRenderContext::SetRenderTarget, + mFrameBuffer); + NVRenderContextScopedProperty<NVRenderRect> __viewport( + m_Context, &NVRenderContext::GetViewport, &NVRenderContext::SetViewport, + NVRenderRect(0, 0, mFBWidth, mFBHeight)); + NVRenderContextScopedProperty<QT3DSVec4> __clearColor( + m_Context, &NVRenderContext::GetClearColor, &NVRenderContext::SetClearColor, + QT3DSVec4(.6f)); + m_Context.Clear(clearFlags); + mShaderArgs.shader = mSimpleShader; + DrawIndexedArrays(QT3DSVec3(0.f)); + } + m_Context.Clear(clearFlags); + mShaderArgs.texture = mColorBuffer; + mShaderArgs.shader = mSimpleShaderTex; + + DrawIndexedArrays(QT3DSVec3(-2.f, 0.f, 0.f)); + + mShaderArgs.texture = mDepthBuffer; + DrawIndexedArrays(QT3DSVec3(2.f, 0.f, 0.f)); + } + virtual QT3DSU32 getRuntimeInSeconds() + { + return mSimpleShader.mPtr && mSimpleShaderTex.mPtr ? 5 : 0; + } + virtual void release() { NVDelete(m_Context.GetFoundation(), this); } +}; +} + +QT3DS_RENDER_REGISTER_EXAMPLE(RenderToTexture);
\ No newline at end of file diff --git a/src/render/Examples/Qt3DSRenderSpinningCubeExample.cpp b/src/render/Examples/Qt3DSRenderSpinningCubeExample.cpp new file mode 100644 index 0000000..807f082 --- /dev/null +++ b/src/render/Examples/Qt3DSRenderSpinningCubeExample.cpp @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "Qt3DSRenderExample.h" +#include "render/Qt3DSRenderVertexBuffer.h" +#include "render/Qt3DSRenderIndexBuffer.h" +#include "render/NVRenderVertFragShader.h" +#include "render_util/NVRenderUtils.h" +#include "Qt3DSRenderExampleTools.h" +#include "foundation/Qt3DSMat44.h" + +using namespace qt3ds; +using namespace qt3ds::render; + +#pragma warning(disable : 4189) +#pragma warning(disable : 4100) + +namespace { + +struct ShaderArguments +{ + QT3DSMat44 mMatrix; +}; + +class SpinningCube : public NVRenderExample +{ + NVRenderContext &m_Context; + NVScopedRefCounted<NVRenderVertexBuffer> mVertexBuffer; + NVScopedRefCounted<NVRenderIndexBuffer> mIndexBuffer; + NVScopedRefCounted<NVRenderVertFragShader> mShader; + NVRenderHandle mShaderArgGroupId; + float frus[16]; + float model[16]; + float rot[9]; + +public: + SpinningCube(NVRenderContext &ctx) + : m_Context(ctx) + { + NVRenderExampleTools::createBox(ctx, mVertexBuffer.mPtr, mIndexBuffer.mPtr, false); + mVertexBuffer->addRef(); + mIndexBuffer->addRef(); + mShader = NVRenderExampleTools::createSimpleShader(ctx); + ctx.SetViewport(NVRenderRect(0, 0, 400, 400)); + // These properties will get applied just before render no matter what + // so we can just use the default settings here. + ctx.SetVertexBuffer(mVertexBuffer); + ctx.SetIndexBuffer(mIndexBuffer); + if (mShader) { + ctx.SetActiveShader(mShader); + } + ctx.SetDepthTestEnabled(true); + ctx.SetDepthWriteEnabled(true); + NvGl2DemoMatrixIdentity(model); + NvGl2DemoMatrixIdentity(frus); + NvGl2DemoMatrixFrustum(frus, -1, 1, -1, 1, 1, 10); + NvGl2DemoMatrixTranslate(model, 0, 0, -4); + } + virtual void drawFrame(double currentSeconds) + { + NvGl2DemoMatrixRotate_create3x3(rot, (float)currentSeconds * 50, .707f, .707f, 0); + float mvp[16]; + NvGl2DemoMatrixIdentity(mvp); + NvGl2DemoMatrixMultiply(mvp, frus); + NvGl2DemoMatrixMultiply(mvp, model); + NvGl2DemoMatrixMultiply_4x4_3x3(mvp, rot); + + NVConstDataRef<QT3DSU8> instance((QT3DSU8 *)mvp, 16 * sizeof(float)); + m_Context.Clear( + NVRenderClearFlags(NVRenderClearValues::Color | NVRenderClearValues::Depth)); + mShader->SetPropertyValue("mat_mvp", *reinterpret_cast<QT3DSMat44 *>(mvp)); + m_Context.Draw(NVRenderDrawMode::Triangles, mIndexBuffer->GetNumIndices(), 0); + } + virtual QT3DSU32 getRuntimeInSeconds() { return mShader.mPtr ? 5 : 0; } + virtual void release() { NVDelete(m_Context.GetFoundation(), this); } +}; +} +QT3DS_RENDER_REGISTER_EXAMPLE(SpinningCube);
\ No newline at end of file |