| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the demonstration applications of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 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 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "roundedbox.h" | 
|---|
| 43 |  | 
|---|
| 44 | //============================================================================// | 
|---|
| 45 | //                                P3T2N3Vertex                                // | 
|---|
| 46 | //============================================================================// | 
|---|
| 47 |  | 
|---|
| 48 | VertexDescription P3T2N3Vertex::description[] = { | 
|---|
| 49 | {VertexDescription::Position, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, position) / sizeof(float), 0, 0}, | 
|---|
| 50 | {VertexDescription::TexCoord, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, texCoord) / sizeof(float), sizeof(QVector3D), 0}, | 
|---|
| 51 | {VertexDescription::Normal, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, normal) / sizeof(float), sizeof(QVector3D) + sizeof(QVector2D), 0}, | 
|---|
| 52 |  | 
|---|
| 53 | {VertexDescription::Null, 0, 0, 0, 0}, | 
|---|
| 54 | }; | 
|---|
| 55 |  | 
|---|
| 56 | //============================================================================// | 
|---|
| 57 | //                                GLRoundedBox                                // | 
|---|
| 58 | //============================================================================// | 
|---|
| 59 |  | 
|---|
| 60 | float lerp(float a, float b, float t) | 
|---|
| 61 | { | 
|---|
| 62 | return a * (1.0f - t) + b * t; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | GLRoundedBox::GLRoundedBox(float r, float scale, int n) | 
|---|
| 66 | : GLTriangleMesh<P3T2N3Vertex, unsigned short>((n+2)*(n+3)*4, (n+1)*(n+1)*24+36+72*(n+1)) | 
|---|
| 67 | { | 
|---|
| 68 | int vidx = 0, iidx = 0; | 
|---|
| 69 | int vertexCountPerCorner = (n + 2) * (n + 3) / 2; | 
|---|
| 70 |  | 
|---|
| 71 | P3T2N3Vertex *vp = m_vb.lock(); | 
|---|
| 72 | unsigned short *ip = m_ib.lock(); | 
|---|
| 73 |  | 
|---|
| 74 | if (!vp || !ip) { | 
|---|
| 75 | qWarning("GLRoundedBox::GLRoundedBox: Failed to lock vertex buffer and/or index buffer."); | 
|---|
| 76 | m_ib.unlock(); | 
|---|
| 77 | m_vb.unlock(); | 
|---|
| 78 | return; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | for (int corner = 0; corner < 8; ++corner) { | 
|---|
| 82 | QVector3D centre(corner & 1 ? 1.0f : -1.0f, | 
|---|
| 83 | corner & 2 ? 1.0f : -1.0f, | 
|---|
| 84 | corner & 4 ? 1.0f : -1.0f); | 
|---|
| 85 | int winding = (corner & 1) ^ ((corner >> 1) & 1) ^ (corner >> 2); | 
|---|
| 86 | int offsX = ((corner ^ 1) - corner) * vertexCountPerCorner; | 
|---|
| 87 | int offsY = ((corner ^ 2) - corner) * vertexCountPerCorner; | 
|---|
| 88 | int offsZ = ((corner ^ 4) - corner) * vertexCountPerCorner; | 
|---|
| 89 |  | 
|---|
| 90 | // Face polygons | 
|---|
| 91 | if (winding) { | 
|---|
| 92 | ip[iidx++] = vidx; | 
|---|
| 93 | ip[iidx++] = vidx + offsX; | 
|---|
| 94 | ip[iidx++] = vidx + offsY; | 
|---|
| 95 |  | 
|---|
| 96 | ip[iidx++] = vidx + vertexCountPerCorner - n - 2; | 
|---|
| 97 | ip[iidx++] = vidx + vertexCountPerCorner - n - 2 + offsY; | 
|---|
| 98 | ip[iidx++] = vidx + vertexCountPerCorner - n - 2 + offsZ; | 
|---|
| 99 |  | 
|---|
| 100 | ip[iidx++] = vidx + vertexCountPerCorner - 1; | 
|---|
| 101 | ip[iidx++] = vidx + vertexCountPerCorner - 1 + offsZ; | 
|---|
| 102 | ip[iidx++] = vidx + vertexCountPerCorner - 1 + offsX; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | for (int i = 0; i < n + 2; ++i) { | 
|---|
| 106 |  | 
|---|
| 107 | // Edge polygons | 
|---|
| 108 | if (winding && i < n + 1) { | 
|---|
| 109 | ip[iidx++] = vidx + i + 1; | 
|---|
| 110 | ip[iidx++] = vidx; | 
|---|
| 111 | ip[iidx++] = vidx + offsY + i + 1; | 
|---|
| 112 | ip[iidx++] = vidx + offsY; | 
|---|
| 113 | ip[iidx++] = vidx + offsY + i + 1; | 
|---|
| 114 | ip[iidx++] = vidx; | 
|---|
| 115 |  | 
|---|
| 116 | ip[iidx++] = vidx + i; | 
|---|
| 117 | ip[iidx++] = vidx + 2 * i + 2; | 
|---|
| 118 | ip[iidx++] = vidx + i + offsX; | 
|---|
| 119 | ip[iidx++] = vidx + 2 * i + offsX + 2; | 
|---|
| 120 | ip[iidx++] = vidx + i + offsX; | 
|---|
| 121 | ip[iidx++] = vidx + 2 * i + 2; | 
|---|
| 122 |  | 
|---|
| 123 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 1 - i; | 
|---|
| 124 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 2 - i; | 
|---|
| 125 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 1 - i + offsZ; | 
|---|
| 126 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 2 - i + offsZ; | 
|---|
| 127 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 1 - i + offsZ; | 
|---|
| 128 | ip[iidx++] = (corner + 1) * vertexCountPerCorner - 2 - i; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | for (int j = 0; j <= i; ++j) { | 
|---|
| 132 | QVector3D normal = QVector3D(i - j, j, n + 1 - i).normalized(); | 
|---|
| 133 | QVector3D offset(0.5f - r, 0.5f - r, 0.5f - r); | 
|---|
| 134 | QVector3D pos = centre * (offset + r * normal); | 
|---|
| 135 |  | 
|---|
| 136 | vp[vidx].position = scale * pos; | 
|---|
| 137 | vp[vidx].normal = centre * normal; | 
|---|
| 138 | vp[vidx].texCoord = QVector2D(pos.x() + 0.5f, pos.y() + 0.5f); | 
|---|
| 139 |  | 
|---|
| 140 | // Corner polygons | 
|---|
| 141 | if (i < n + 1) { | 
|---|
| 142 | ip[iidx++] = vidx; | 
|---|
| 143 | ip[iidx++] = vidx + i + 2 - winding; | 
|---|
| 144 | ip[iidx++] = vidx + i + 1 + winding; | 
|---|
| 145 | } | 
|---|
| 146 | if (i < n) { | 
|---|
| 147 | ip[iidx++] = vidx + i + 1 + winding; | 
|---|
| 148 | ip[iidx++] = vidx + i + 2 - winding; | 
|---|
| 149 | ip[iidx++] = vidx + 2 * i + 4; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | ++vidx; | 
|---|
| 153 | } | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | m_ib.unlock(); | 
|---|
| 159 | m_vb.unlock(); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|