| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the demonstration applications of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 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. | 
|---|
| 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 are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@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), offsetof(P3T2N3Vertex, position), 0}, | 
|---|
| 50 | {VertexDescription::TexCoord, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, texCoord) / sizeof(float), offsetof(P3T2N3Vertex, texCoord), 0}, | 
|---|
| 51 | {VertexDescription::Normal, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, normal) / sizeof(float), offsetof(P3T2N3Vertex, normal), 0}, | 
|---|
| 52 | {VertexDescription::Null, 0, 0, 0, 0}, | 
|---|
| 53 | }; | 
|---|
| 54 |  | 
|---|
| 55 | //============================================================================// | 
|---|
| 56 | //                                GLRoundedBox                                // | 
|---|
| 57 | //============================================================================// | 
|---|
| 58 |  | 
|---|
| 59 | float lerp(float a, float b, float t) | 
|---|
| 60 | { | 
|---|
| 61 | return a * (1.0f - t) + b * t; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | GLRoundedBox::GLRoundedBox(float r, float scale, int n) | 
|---|
| 65 | : GLTriangleMesh<P3T2N3Vertex, unsigned short>((n+2)*(n+3)*4, (n+1)*(n+1)*24+36+72*(n+1)) | 
|---|
| 66 | { | 
|---|
| 67 | int vidx = 0, iidx = 0; | 
|---|
| 68 | int vertexCountPerCorner = (n + 2) * (n + 3) / 2; | 
|---|
| 69 |  | 
|---|
| 70 | P3T2N3Vertex *vp = m_vb.lock(); | 
|---|
| 71 | unsigned short *ip = m_ib.lock(); | 
|---|
| 72 |  | 
|---|
| 73 | if (!vp || !ip) { | 
|---|
| 74 | qWarning("GLRoundedBox::GLRoundedBox: Failed to lock vertex buffer and/or index buffer."); | 
|---|
| 75 | m_ib.unlock(); | 
|---|
| 76 | m_vb.unlock(); | 
|---|
| 77 | return; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | for (int corner = 0; corner < 8; ++corner) { | 
|---|
| 81 | gfx::Vector3f centre; | 
|---|
| 82 | centre[0] = (corner & 1 ? 1.0f : -1.0f); | 
|---|
| 83 | centre[1] = (corner & 2 ? 1.0f : -1.0f); | 
|---|
| 84 | centre[2] = (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 | gfx::Vector3f normal = gfx::Vector3f::vector(i - j, j, n + 1 - i).normalized(); | 
|---|
| 133 | gfx::Vector3f pos = centre * (0.5f - r + r * normal); | 
|---|
| 134 |  | 
|---|
| 135 | vp[vidx].position = scale * pos; | 
|---|
| 136 | vp[vidx].normal = centre * normal; | 
|---|
| 137 | vp[vidx].texCoord = gfx::Vector2f::vector(pos[0], pos[1]) + 0.5f; | 
|---|
| 138 |  | 
|---|
| 139 | // Corner polygons | 
|---|
| 140 | if (i < n + 1) { | 
|---|
| 141 | ip[iidx++] = vidx; | 
|---|
| 142 | ip[iidx++] = vidx + i + 2 - winding; | 
|---|
| 143 | ip[iidx++] = vidx + i + 1 + winding; | 
|---|
| 144 | } | 
|---|
| 145 | if (i < n) { | 
|---|
| 146 | ip[iidx++] = vidx + i + 1 + winding; | 
|---|
| 147 | ip[iidx++] = vidx + i + 2 - winding; | 
|---|
| 148 | ip[iidx++] = vidx + 2 * i + 4; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | ++vidx; | 
|---|
| 152 | } | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | m_ib.unlock(); | 
|---|
| 158 | m_vb.unlock(); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|