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 QtCore module 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 "geometryengine.h" |
52 | |
53 | #include <QVector2D> |
54 | #include <QVector3D> |
55 | |
56 | struct VertexData |
57 | { |
58 | QVector3D position; |
59 | QVector2D texCoord; |
60 | }; |
61 | |
62 | //! [0] |
63 | GeometryEngine::GeometryEngine() |
64 | : indexBuf(QOpenGLBuffer::IndexBuffer) |
65 | { |
66 | initializeOpenGLFunctions(); |
67 | |
68 | // Generate 2 VBOs |
69 | arrayBuf.create(); |
70 | indexBuf.create(); |
71 | |
72 | // Initializes cube geometry and transfers it to VBOs |
73 | initCubeGeometry(); |
74 | } |
75 | |
76 | GeometryEngine::~GeometryEngine() |
77 | { |
78 | arrayBuf.destroy(); |
79 | indexBuf.destroy(); |
80 | } |
81 | //! [0] |
82 | |
83 | void GeometryEngine::initCubeGeometry() |
84 | { |
85 | // For cube we would need only 8 vertices but we have to |
86 | // duplicate vertex for each face because texture coordinate |
87 | // is different. |
88 | VertexData vertices[] = { |
89 | // Vertex data for face 0 |
90 | {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.0f, 0.0f)}, // v0 |
91 | {QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.0f)}, // v1 |
92 | {QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.0f, 0.5f)}, // v2 |
93 | {QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v3 |
94 | |
95 | // Vertex data for face 1 |
96 | {QVector3D( 1.0f, -1.0f, 1.0f), QVector2D( 0.0f, 0.5f)}, // v4 |
97 | {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.5f)}, // v5 |
98 | {QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.0f, 1.0f)}, // v6 |
99 | {QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v7 |
100 | |
101 | // Vertex data for face 2 |
102 | {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v8 |
103 | {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(1.0f, 0.5f)}, // v9 |
104 | {QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)}, // v10 |
105 | {QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(1.0f, 1.0f)}, // v11 |
106 | |
107 | // Vertex data for face 3 |
108 | {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v12 |
109 | {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(1.0f, 0.0f)}, // v13 |
110 | {QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v14 |
111 | {QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(1.0f, 0.5f)}, // v15 |
112 | |
113 | // Vertex data for face 4 |
114 | {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.0f)}, // v16 |
115 | {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v17 |
116 | {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v18 |
117 | {QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.66f, 0.5f)}, // v19 |
118 | |
119 | // Vertex data for face 5 |
120 | {QVector3D(-1.0f, 1.0f, 1.0f), QVector2D(0.33f, 0.5f)}, // v20 |
121 | {QVector3D( 1.0f, 1.0f, 1.0f), QVector2D(0.66f, 0.5f)}, // v21 |
122 | {QVector3D(-1.0f, 1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v22 |
123 | {QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)} // v23 |
124 | }; |
125 | |
126 | // Indices for drawing cube faces using triangle strips. |
127 | // Triangle strips can be connected by duplicating indices |
128 | // between the strips. If connecting strips have opposite |
129 | // vertex order then last index of the first strip and first |
130 | // index of the second strip needs to be duplicated. If |
131 | // connecting strips have same vertex order then only last |
132 | // index of the first strip needs to be duplicated. |
133 | GLushort indices[] = { |
134 | 0, 1, 2, 3, 3, // Face 0 - triangle strip ( v0, v1, v2, v3) |
135 | 4, 4, 5, 6, 7, 7, // Face 1 - triangle strip ( v4, v5, v6, v7) |
136 | 8, 8, 9, 10, 11, 11, // Face 2 - triangle strip ( v8, v9, v10, v11) |
137 | 12, 12, 13, 14, 15, 15, // Face 3 - triangle strip (v12, v13, v14, v15) |
138 | 16, 16, 17, 18, 19, 19, // Face 4 - triangle strip (v16, v17, v18, v19) |
139 | 20, 20, 21, 22, 23 // Face 5 - triangle strip (v20, v21, v22, v23) |
140 | }; |
141 | |
142 | //! [1] |
143 | // Transfer vertex data to VBO 0 |
144 | arrayBuf.bind(); |
145 | arrayBuf.allocate(vertices, 24 * sizeof(VertexData)); |
146 | |
147 | // Transfer index data to VBO 1 |
148 | indexBuf.bind(); |
149 | indexBuf.allocate(indices, 34 * sizeof(GLushort)); |
150 | //! [1] |
151 | } |
152 | |
153 | //! [2] |
154 | void GeometryEngine::drawCubeGeometry(QOpenGLShaderProgram *program) |
155 | { |
156 | // Tell OpenGL which VBOs to use |
157 | arrayBuf.bind(); |
158 | indexBuf.bind(); |
159 | |
160 | // Offset for position |
161 | quintptr offset = 0; |
162 | |
163 | // Tell OpenGL programmable pipeline how to locate vertex position data |
164 | int vertexLocation = program->attributeLocation("a_position" ); |
165 | program->enableAttributeArray(vertexLocation); |
166 | program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData)); |
167 | |
168 | // Offset for texture coordinate |
169 | offset += sizeof(QVector3D); |
170 | |
171 | // Tell OpenGL programmable pipeline how to locate vertex texture coordinate data |
172 | int texcoordLocation = program->attributeLocation("a_texcoord" ); |
173 | program->enableAttributeArray(texcoordLocation); |
174 | program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData)); |
175 | |
176 | // Draw cube geometry using indices from VBO 1 |
177 | glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, nullptr); |
178 | } |
179 | //! [2] |
180 | |