-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskybox.cpp
208 lines (174 loc) · 6.29 KB
/
skybox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "skybox.h"
#include <QMatrix4x4>
#include <QOpenGLBuffer>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
struct SkyBoxData
{
SkyBoxData() {
for(int i=0; i<6; i++)
this->textures[i] = 0;
this->vertexBuffer = 0;
this->indexBuffer = 0;
this->shader = 0;
}
~SkyBoxData() {
delete this->shader;
delete this->indexBuffer;
delete this->vertexBuffer;
for(int i=5; i>=0; i--)
delete this->textures[i];
}
QOpenGLTexture *textures[6];
QOpenGLBuffer *vertexBuffer;
QOpenGLBuffer *indexBuffer;
QOpenGLShaderProgram *shader;
QMatrix4x4 matrix;
};
SkyBox::SkyBox()
{
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
d = new SkyBoxData;
}
SkyBox::~SkyBox()
{
delete d;
}
void SkyBox::render(const QMatrix4x4 &viewMatrix, const QMatrix4x4 &projectionMatrix)
{
this->init();
d->shader->bind();
d->vertexBuffer->bind();
d->indexBuffer->bind();
d->shader->enableAttributeArray("qt_Vertex");
d->shader->setAttributeBuffer("qt_Vertex", GL_FLOAT, 0, 3, 0);
d->shader->enableAttributeArray("qt_MultiTexCoord0");
const int texCoordsOffset = 24 * sizeof(QVector3D);
d->shader->setAttributeBuffer("qt_MultiTexCoord0", GL_FLOAT, texCoordsOffset, 2, 0);
// d->matrix.rotate(-1, 0, 1, 0);
QMatrix4x4 modelMatrix = d->matrix;
modelMatrix.scale(10, 10, 10);
const QMatrix4x4 mvp = projectionMatrix * viewMatrix * modelMatrix;
d->shader->setUniformValue("qt_ModelViewProjectionMatrix", mvp);
for(int i=0; i<6; i++)
{
d->textures[i]->bind(i+1);
d->shader->setUniformValue("qt_Texture0", (i+1));
const uint triangleOffset = i*6*sizeof(uint);
core->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)triangleOffset);
d->textures[i]->release(i+1);
}
d->indexBuffer->release();
d->vertexBuffer->release();
d->shader->release();
}
void SkyBox::init()
{
if(d->shader)
return;
// Load all texture images
const QImage posx = QImage(":/res/Model/skybox/posx.jpg").mirrored();
const QImage posy = QImage(":/res/Model/skybox/posy.jpg").mirrored();
const QImage posz = QImage(":/res/Model/skybox/posz.jpg").mirrored();
const QImage negx = QImage(":/res/Model/skybox/negx.jpg").mirrored();
const QImage negy = QImage(":/res/Model/skybox/negy.jpg").mirrored();
const QImage negz = QImage(":/res/Model/skybox/negz.jpg").mirrored();
// Load images as independent texture objects
d->textures[0] = new QOpenGLTexture(posx);
d->textures[1] = new QOpenGLTexture(posy);
d->textures[2] = new QOpenGLTexture(posz);
d->textures[3] = new QOpenGLTexture(negx);
d->textures[4] = new QOpenGLTexture(negy);
d->textures[5] = new QOpenGLTexture(negz);
for(int i=0; i<6; i++)
{
d->textures[i]->setWrapMode(QOpenGLTexture::ClampToEdge);
d->textures[i]->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
d->textures[i]->setMagnificationFilter(QOpenGLTexture::Linear);
}
// Construct a template square of size 2x2
const QVector3D p1(-1, 1, 0); // top-left
const QVector3D p2(-1, -1, 0); // bottom-left
const QVector3D p3(1, -1, 0); // bottom-right
const QVector3D p4(1, 1, 0); // top-right
// Array for storing geometry of the cube
QVector<QVector3D> geometry;
geometry.reserve(24);
// Transform p1 ... p4 for posx
QMatrix4x4 mat;
mat.translate(1, 0, 0);
mat.rotate(-90, 0, 1, 0);
geometry << mat.map(p1) << mat.map(p2)
<< mat.map(p3) << mat.map(p4);
// Transform p1 ... p4 for posy
mat.setToIdentity();
mat.translate(0, 1, 0);
mat.rotate(90, 1, 0, 0);
geometry << mat.map(p1) << mat.map(p2)
<< mat.map(p3) << mat.map(p4);
// Transform p2 ... p4 for posz
mat.setToIdentity();
mat.translate(0, 0, -1);
geometry << mat.map(p1) << mat.map(p2)
<< mat.map(p3) << mat.map(p4);
// Transform p2 ... p4 for negx
mat.setToIdentity();
mat.translate(-1, 0, 0);
mat.rotate(90, 0, 1, 0);
geometry << mat.map(p1) << mat.map(p2)
<< mat.map(p3) << mat.map(p4);
// Transform p2 ... p4 for negy
mat.setToIdentity();
mat.translate(0, -1, 0);
mat.rotate(-90, 1, 0, 0);
geometry << mat.map(p1) << mat.map(p2)
<< mat.map(p3) << mat.map(p4);
// Transform p2 ... p4 for negz
mat.setToIdentity();
mat.translate(0, 0, 1);
mat.rotate(180, 0, 1, 0);
geometry << mat.map(p1) << mat.map(p2)
<< mat.map(p3) << mat.map(p4);
// Texture coordinates
QVector<QVector2D> texCoords;
texCoords.reserve(24);
for(int i=0; i<6; i++)
{
texCoords << QVector2D(0, 1)
<< QVector2D(0, 0)
<< QVector2D(1, 0)
<< QVector2D(1, 1);
}
// Triangles
QVector<uint> triangles;
triangles.reserve(36);
for(int i=0; i<6; i++)
{
const int base = i*4;
triangles << base << base+1 << base+2;
triangles << base << base+2 << base+3;
}
// Store the arrays in buffers
d->vertexBuffer = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
d->vertexBuffer->create();
d->vertexBuffer->bind();
d->vertexBuffer->allocate( geometry.size()*sizeof(QVector3D) +
texCoords.size()*sizeof(QVector2D) );
d->vertexBuffer->write(0, (const void *)geometry.constData(),
geometry.size()*sizeof(QVector3D) );
d->vertexBuffer->write(geometry.size()*sizeof(QVector3D),
(const void *)texCoords.constData(),
texCoords.size()*sizeof(QVector2D) );
d->vertexBuffer->release();
d->indexBuffer = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
d->indexBuffer->create();
d->indexBuffer->bind();
d->indexBuffer->allocate((const void*)triangles.constData(),
triangles.size()*sizeof(uint));
d->indexBuffer->release();
// Create shaders
d->shader = new QOpenGLShaderProgram;
d->shader->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/res/shader/skyboxvert.glsl");
d->shader->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/res/shader/skyboxfrag.glsl");
d->shader->link();
}