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 "glbuffers.h"
52
53void qgluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
54{
55 const GLdouble ymax = zNear * tan(x: qDegreesToRadians(degrees: fovy) / 2.0);
56 const GLdouble ymin = -ymax;
57 const GLdouble xmin = ymin * aspect;
58 const GLdouble xmax = ymax * aspect;
59 glFrustum(left: xmin, right: xmax, bottom: ymin, top: ymax, near_val: zNear, far_val: zFar);
60}
61
62//============================================================================//
63// GLTexture //
64//============================================================================//
65
66GLTexture::GLTexture()
67{
68 glGenTextures(n: 1, textures: &m_texture);
69}
70
71GLTexture::~GLTexture()
72{
73 glDeleteTextures(n: 1, textures: &m_texture);
74}
75
76//============================================================================//
77// GLTexture2D //
78//============================================================================//
79
80GLTexture2D::GLTexture2D(int width, int height)
81{
82 glBindTexture(GL_TEXTURE_2D, texture: m_texture);
83 glTexImage2D(GL_TEXTURE_2D, level: 0, internalFormat: 4, width, height, border: 0,
84 GL_BGRA, GL_UNSIGNED_BYTE, pixels: nullptr);
85
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
90 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
91 //glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
92 glBindTexture(GL_TEXTURE_2D, texture: 0);
93}
94
95
96GLTexture2D::GLTexture2D(const QString &fileName, int width, int height)
97{
98 // TODO: Add error handling.
99 QImage image(fileName);
100
101 if (image.isNull()) {
102 m_failed = true;
103 return;
104 }
105
106 image = image.convertToFormat(f: QImage::Format_ARGB32);
107
108 //qDebug() << "Image size:" << image.width() << "x" << image.height();
109 if (width <= 0)
110 width = image.width();
111 if (height <= 0)
112 height = image.height();
113 if (width != image.width() || height != image.height())
114 image = image.scaled(w: width, h: height, aspectMode: Qt::IgnoreAspectRatio, mode: Qt::SmoothTransformation);
115
116 glBindTexture(GL_TEXTURE_2D, texture: m_texture);
117
118 // Works on x86, so probably works on all little-endian systems.
119 // Does it work on big-endian systems?
120 glTexImage2D(GL_TEXTURE_2D, level: 0, internalFormat: 4, width: image.width(), height: image.height(), border: 0,
121 GL_BGRA, GL_UNSIGNED_BYTE, pixels: image.bits());
122
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
127 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
128 //glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
129 glBindTexture(GL_TEXTURE_2D, texture: 0);
130}
131
132void GLTexture2D::load(int width, int height, QRgb *data)
133{
134 glBindTexture(GL_TEXTURE_2D, texture: m_texture);
135 glTexImage2D(GL_TEXTURE_2D, level: 0, internalFormat: 4, width, height, border: 0,
136 GL_BGRA, GL_UNSIGNED_BYTE, pixels: data);
137 glBindTexture(GL_TEXTURE_2D, texture: 0);
138}
139
140void GLTexture2D::bind()
141{
142 glBindTexture(GL_TEXTURE_2D, texture: m_texture);
143 glEnable(GL_TEXTURE_2D);
144}
145
146void GLTexture2D::unbind()
147{
148 glBindTexture(GL_TEXTURE_2D, texture: 0);
149 glDisable(GL_TEXTURE_2D);
150}
151
152
153//============================================================================//
154// GLTexture3D //
155//============================================================================//
156
157GLTexture3D::GLTexture3D(int width, int height, int depth)
158{
159 GLBUFFERS_ASSERT_OPENGL("GLTexture3D::GLTexture3D", glTexImage3D, return)
160
161 glBindTexture(GL_TEXTURE_3D, texture: m_texture);
162 glTexImage3D(GL_TEXTURE_3D, 0, 4, width, height, depth, 0,
163 GL_BGRA, GL_UNSIGNED_BYTE, nullptr);
164
165 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
166 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
167 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
168 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
169 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
170 //glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
171 //glTexParameteri(GL_TEXTURE_3D, GL_GENERATE_MIPMAP, GL_TRUE);
172 glBindTexture(GL_TEXTURE_3D, texture: 0);
173}
174
175void GLTexture3D::load(int width, int height, int depth, QRgb *data)
176{
177 GLBUFFERS_ASSERT_OPENGL("GLTexture3D::load", glTexImage3D, return)
178
179 glBindTexture(GL_TEXTURE_3D, texture: m_texture);
180 glTexImage3D(GL_TEXTURE_3D, 0, 4, width, height, depth, 0,
181 GL_BGRA, GL_UNSIGNED_BYTE, data);
182 glBindTexture(GL_TEXTURE_3D, texture: 0);
183}
184
185void GLTexture3D::bind()
186{
187 glBindTexture(GL_TEXTURE_3D, texture: m_texture);
188 glEnable(GL_TEXTURE_3D);
189}
190
191void GLTexture3D::unbind()
192{
193 glBindTexture(GL_TEXTURE_3D, texture: 0);
194 glDisable(GL_TEXTURE_3D);
195}
196
197//============================================================================//
198// GLTextureCube //
199//============================================================================//
200
201GLTextureCube::GLTextureCube(int size)
202{
203 glBindTexture(GL_TEXTURE_CUBE_MAP, texture: m_texture);
204
205 for (int i = 0; i < 6; ++i)
206 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, level: 0, internalFormat: 4, width: size, height: size, border: 0,
207 GL_BGRA, GL_UNSIGNED_BYTE, pixels: nullptr);
208
209 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
210 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
211 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
212 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
213 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
214 //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
215 //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP, GL_TRUE);
216 glBindTexture(GL_TEXTURE_CUBE_MAP, texture: 0);
217}
218
219GLTextureCube::GLTextureCube(const QStringList &fileNames, int size)
220{
221 // TODO: Add error handling.
222
223 glBindTexture(GL_TEXTURE_CUBE_MAP, texture: m_texture);
224
225 int index = 0;
226 for (const QString &file : fileNames) {
227 QImage image(file);
228 if (image.isNull()) {
229 m_failed = true;
230 break;
231 }
232
233 image = image.convertToFormat(f: QImage::Format_ARGB32);
234
235 //qDebug() << "Image size:" << image.width() << "x" << image.height();
236 if (size <= 0)
237 size = image.width();
238 if (size != image.width() || size != image.height())
239 image = image.scaled(w: size, h: size, aspectMode: Qt::IgnoreAspectRatio, mode: Qt::SmoothTransformation);
240
241 // Works on x86, so probably works on all little-endian systems.
242 // Does it work on big-endian systems?
243 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + index, level: 0, internalFormat: 4, width: image.width(), height: image.height(), border: 0,
244 GL_BGRA, GL_UNSIGNED_BYTE, pixels: image.bits());
245
246 if (++index == 6)
247 break;
248 }
249
250 // Clear remaining faces.
251 while (index < 6) {
252 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + index, level: 0, internalFormat: 4, width: size, height: size, border: 0,
253 GL_BGRA, GL_UNSIGNED_BYTE, pixels: nullptr);
254 ++index;
255 }
256
257 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
258 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
259 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
260 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
261 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
262 //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
263 //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP, GL_TRUE);
264 glBindTexture(GL_TEXTURE_CUBE_MAP, texture: 0);
265}
266
267void GLTextureCube::load(int size, int face, QRgb *data)
268{
269 glBindTexture(GL_TEXTURE_CUBE_MAP, texture: m_texture);
270 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level: 0, internalFormat: 4, width: size, height: size, border: 0,
271 GL_BGRA, GL_UNSIGNED_BYTE, pixels: data);
272 glBindTexture(GL_TEXTURE_CUBE_MAP, texture: 0);
273}
274
275void GLTextureCube::bind()
276{
277 glBindTexture(GL_TEXTURE_CUBE_MAP, texture: m_texture);
278 glEnable(GL_TEXTURE_CUBE_MAP);
279}
280
281void GLTextureCube::unbind()
282{
283 glBindTexture(GL_TEXTURE_CUBE_MAP, texture: 0);
284 glDisable(GL_TEXTURE_CUBE_MAP);
285}
286
287//============================================================================//
288// GLFrameBufferObject //
289//============================================================================//
290
291GLFrameBufferObject::GLFrameBufferObject(int width, int height)
292 : m_width(width)
293 , m_height(height)
294{
295 GLBUFFERS_ASSERT_OPENGL("GLFrameBufferObject::GLFrameBufferObject",
296 glGenFramebuffersEXT && glGenRenderbuffersEXT && glBindRenderbufferEXT && glRenderbufferStorageEXT, return)
297
298 // TODO: share depth buffers of same size
299 glGenFramebuffersEXT(1, &m_fbo);
300 //glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
301 glGenRenderbuffersEXT(1, &m_depthBuffer);
302 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_depthBuffer);
303 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, m_width, m_height);
304 //glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_depthBuffer);
305 //glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
306}
307
308GLFrameBufferObject::~GLFrameBufferObject()
309{
310 GLBUFFERS_ASSERT_OPENGL("GLFrameBufferObject::~GLFrameBufferObject",
311 glDeleteFramebuffersEXT && glDeleteRenderbuffersEXT, return)
312
313 glDeleteFramebuffersEXT(1, &m_fbo);
314 glDeleteRenderbuffersEXT(1, &m_depthBuffer);
315}
316
317void GLFrameBufferObject::setAsRenderTarget(bool state)
318{
319 GLBUFFERS_ASSERT_OPENGL("GLFrameBufferObject::setAsRenderTarget", glBindFramebufferEXT, return)
320
321 if (state) {
322 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
323 glPushAttrib(GL_VIEWPORT_BIT);
324 glViewport(x: 0, y: 0, width: m_width, height: m_height);
325 } else {
326 glPopAttrib();
327 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
328 }
329}
330
331bool GLFrameBufferObject::isComplete()
332{
333 GLBUFFERS_ASSERT_OPENGL("GLFrameBufferObject::isComplete", glCheckFramebufferStatusEXT, return false)
334
335 return GL_FRAMEBUFFER_COMPLETE_EXT == glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
336}
337
338//============================================================================//
339// GLRenderTargetCube //
340//============================================================================//
341
342GLRenderTargetCube::GLRenderTargetCube(int size)
343 : GLTextureCube(size)
344 , m_fbo(size, size)
345{
346}
347
348void GLRenderTargetCube::begin(int face)
349{
350 GLBUFFERS_ASSERT_OPENGL("GLRenderTargetCube::begin",
351 glFramebufferTexture2DEXT && glFramebufferRenderbufferEXT, return)
352
353 m_fbo.setAsRenderTarget(true);
354 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
355 GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, m_texture, 0);
356 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_fbo.m_depthBuffer);
357}
358
359void GLRenderTargetCube::end()
360{
361 m_fbo.setAsRenderTarget(false);
362}
363
364void GLRenderTargetCube::getViewMatrix(QMatrix4x4& mat, int face)
365{
366 if (face < 0 || face >= 6) {
367 qWarning(msg: "GLRenderTargetCube::getViewMatrix: 'face' must be in the range [0, 6). (face == %d)", face);
368 return;
369 }
370
371 static constexpr int perm[6][3] = {
372 {2, 1, 0},
373 {2, 1, 0},
374 {0, 2, 1},
375 {0, 2, 1},
376 {0, 1, 2},
377 {0, 1, 2},
378 };
379
380 static constexpr float signs[6][3] = {
381 {-1.0f, -1.0f, -1.0f},
382 {+1.0f, -1.0f, +1.0f},
383 {+1.0f, +1.0f, -1.0f},
384 {+1.0f, -1.0f, +1.0f},
385 {+1.0f, -1.0f, -1.0f},
386 {-1.0f, -1.0f, +1.0f},
387 };
388
389 mat.fill(value: 0.0f);
390 for (int i = 0; i < 3; ++i)
391 mat(i, perm[face][i]) = signs[face][i];
392 mat(3, 3) = 1.0f;
393}
394
395void GLRenderTargetCube::getProjectionMatrix(QMatrix4x4& mat, float nearZ, float farZ)
396{
397 static const QMatrix4x4 reference(
398 1.0f, 0.0f, 0.0f, 0.0f,
399 0.0f, 1.0f, 0.0f, 0.0f,
400 0.0f, 0.0f, 0.0f, 0.0f,
401 0.0f, 0.0f, -1.0f, 0.0f);
402
403 mat = reference;
404 mat(2, 2) = (nearZ+farZ)/(nearZ-farZ);
405 mat(2, 3) = 2.0f*nearZ*farZ/(nearZ-farZ);
406}
407

source code of qtbase/examples/widgets/graphicsview/boxes/glbuffers.cpp