| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the demonstration applications of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "roundedbox.h" |
| 52 | |
| 53 | //============================================================================// |
| 54 | // P3T2N3Vertex // |
| 55 | //============================================================================// |
| 56 | |
| 57 | VertexDescription P3T2N3Vertex::description[] = { |
| 58 | {.field: VertexDescription::Position, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, position) / sizeof(float), .offset: 0, .index: 0}, |
| 59 | {.field: VertexDescription::TexCoord, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, texCoord) / sizeof(float), .offset: sizeof(QVector3D), .index: 0}, |
| 60 | {.field: VertexDescription::Normal, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, normal) / sizeof(float), .offset: sizeof(QVector3D) + sizeof(QVector2D), .index: 0}, |
| 61 | |
| 62 | {.field: VertexDescription::Null, .type: 0, .count: 0, .offset: 0, .index: 0}, |
| 63 | }; |
| 64 | |
| 65 | //============================================================================// |
| 66 | // GLRoundedBox // |
| 67 | //============================================================================// |
| 68 | |
| 69 | float lerp(float a, float b, float t) |
| 70 | { |
| 71 | return a * (1.0f - t) + b * t; |
| 72 | } |
| 73 | |
| 74 | GLRoundedBox::GLRoundedBox(float r, float scale, int n) |
| 75 | : GLTriangleMesh<P3T2N3Vertex, unsigned short>((n+2)*(n+3)*4, (n+1)*(n+1)*24+36+72*(n+1)) |
| 76 | { |
| 77 | int vidx = 0, iidx = 0; |
| 78 | int vertexCountPerCorner = (n + 2) * (n + 3) / 2; |
| 79 | |
| 80 | P3T2N3Vertex *vp = m_vb.lock(); |
| 81 | unsigned short *ip = m_ib.lock(); |
| 82 | |
| 83 | if (!vp || !ip) { |
| 84 | qWarning(msg: "GLRoundedBox::GLRoundedBox: Failed to lock vertex buffer and/or index buffer." ); |
| 85 | m_ib.unlock(); |
| 86 | m_vb.unlock(); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | for (int corner = 0; corner < 8; ++corner) { |
| 91 | QVector3D centre(corner & 1 ? 1.0f : -1.0f, |
| 92 | corner & 2 ? 1.0f : -1.0f, |
| 93 | corner & 4 ? 1.0f : -1.0f); |
| 94 | int winding = (corner & 1) ^ ((corner >> 1) & 1) ^ (corner >> 2); |
| 95 | int offsX = ((corner ^ 1) - corner) * vertexCountPerCorner; |
| 96 | int offsY = ((corner ^ 2) - corner) * vertexCountPerCorner; |
| 97 | int offsZ = ((corner ^ 4) - corner) * vertexCountPerCorner; |
| 98 | |
| 99 | // Face polygons |
| 100 | if (winding) { |
| 101 | ip[iidx++] = vidx; |
| 102 | ip[iidx++] = vidx + offsX; |
| 103 | ip[iidx++] = vidx + offsY; |
| 104 | |
| 105 | ip[iidx++] = vidx + vertexCountPerCorner - n - 2; |
| 106 | ip[iidx++] = vidx + vertexCountPerCorner - n - 2 + offsY; |
| 107 | ip[iidx++] = vidx + vertexCountPerCorner - n - 2 + offsZ; |
| 108 | |
| 109 | ip[iidx++] = vidx + vertexCountPerCorner - 1; |
| 110 | ip[iidx++] = vidx + vertexCountPerCorner - 1 + offsZ; |
| 111 | ip[iidx++] = vidx + vertexCountPerCorner - 1 + offsX; |
| 112 | } |
| 113 | |
| 114 | for (int i = 0; i < n + 2; ++i) { |
| 115 | |
| 116 | // Edge polygons |
| 117 | if (winding && i < n + 1) { |
| 118 | ip[iidx++] = vidx + i + 1; |
| 119 | ip[iidx++] = vidx; |
| 120 | ip[iidx++] = vidx + offsY + i + 1; |
| 121 | ip[iidx++] = vidx + offsY; |
| 122 | ip[iidx++] = vidx + offsY + i + 1; |
| 123 | ip[iidx++] = vidx; |
| 124 | |
| 125 | ip[iidx++] = vidx + i; |
| 126 | ip[iidx++] = vidx + 2 * i + 2; |
| 127 | ip[iidx++] = vidx + i + offsX; |
| 128 | ip[iidx++] = vidx + 2 * i + offsX + 2; |
| 129 | ip[iidx++] = vidx + i + offsX; |
| 130 | ip[iidx++] = vidx + 2 * i + 2; |
| 131 | |
| 132 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 1 - i; |
| 133 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 2 - i; |
| 134 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 1 - i + offsZ; |
| 135 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 2 - i + offsZ; |
| 136 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 1 - i + offsZ; |
| 137 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 2 - i; |
| 138 | } |
| 139 | |
| 140 | for (int j = 0; j <= i; ++j) { |
| 141 | QVector3D normal = QVector3D(i - j, j, n + 1 - i).normalized(); |
| 142 | QVector3D offset(0.5f - r, 0.5f - r, 0.5f - r); |
| 143 | QVector3D pos = centre * (offset + r * normal); |
| 144 | |
| 145 | vp[vidx].position = scale * pos; |
| 146 | vp[vidx].normal = centre * normal; |
| 147 | vp[vidx].texCoord = QVector2D(pos.x() + 0.5f, pos.y() + 0.5f); |
| 148 | |
| 149 | // Corner polygons |
| 150 | if (i < n + 1) { |
| 151 | ip[iidx++] = vidx; |
| 152 | ip[iidx++] = vidx + i + 2 - winding; |
| 153 | ip[iidx++] = vidx + i + 1 + winding; |
| 154 | } |
| 155 | if (i < n) { |
| 156 | ip[iidx++] = vidx + i + 1 + winding; |
| 157 | ip[iidx++] = vidx + i + 2 - winding; |
| 158 | ip[iidx++] = vidx + 2 * i + 4; |
| 159 | } |
| 160 | |
| 161 | ++vidx; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | m_ib.unlock(); |
| 168 | m_vb.unlock(); |
| 169 | } |
| 170 | |
| 171 | |