-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPlane.cpp
More file actions
219 lines (195 loc) · 7.74 KB
/
Plane.cpp
File metadata and controls
219 lines (195 loc) · 7.74 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <math.h>
#include "OpenGLWidget.h"
#include "Plane.h"
#define VERTEX_COUNT 6
#define CUBE_LENGTH 25.0
#define TEXTURE_UNIT GL_TEXTURE0
#define SHADOW_TEXTURE_UNIT GL_TEXTURE1
QOpenGLShaderProgram* Plane::s_program = Q_NULLPTR;
int Plane::s_positionLoc,
Plane::s_normalLoc,
Plane::s_texCoordLoc,
Plane::s_modelMatrixLoc,
Plane::s_viewMatrixLoc,
Plane::s_projectionMatrixLoc,
Plane::s_lightViewMatrixLoc,
Plane::s_lightProjectionMatrixLoc,
Plane::s_lightPositionLoc,
Plane::s_modelViewNormalMatrixLoc,
Plane::s_shadowTypeLoc,
Plane::s_count = 0;
Plane::Plane( QMatrix4x4& modelMatrix,
const QString& fileName,
ShadowType shadowType ):
m_modelMatrix( modelMatrix ),
m_shadowType( shadowType ),
m_vertexBuffer( QOpenGLBuffer::VertexBuffer ),
m_texture( QOpenGLTexture::Target2D )
{
initializeOpenGLFunctions( );
// 根据创建的次数来创建着色器
if ( s_count++ == 0 )
{
s_program = new QOpenGLShaderProgram;
s_program->addShaderFromSourceFile( QOpenGLShader::Vertex,
":/Common.vert" );
s_program->addShaderFromSourceFile( QOpenGLShader::Fragment,
":/Common.frag" );
s_program->link( );
s_program->bind( );
s_positionLoc = s_program->attributeLocation( "position" );
s_normalLoc = s_program->attributeLocation( "normal" );
s_texCoordLoc = s_program->attributeLocation( "texCoord" );
s_modelMatrixLoc = s_program->uniformLocation( "modelMatrix" );
s_viewMatrixLoc = s_program->uniformLocation( "viewMatrix" );
s_projectionMatrixLoc = s_program->uniformLocation( "projectionMatrix" );
s_lightPositionLoc = s_program->uniformLocation( "lightPosition" );
s_lightViewMatrixLoc = s_program->uniformLocation( "lightViewMatrix" );
s_lightProjectionMatrixLoc =
s_program->uniformLocation( "lightProjectionMatrix" );
s_modelViewNormalMatrixLoc =
s_program->uniformLocation( "modelViewNormalMatrix" );
s_shadowTypeLoc = s_program->uniformLocation( "shadowType" );
int textureLoc = s_program->uniformLocation( "texture" );
int shadowLoc = s_program->uniformLocation( "shadowTexture" );
s_program->setUniformValue( textureLoc,
TEXTURE_UNIT - GL_TEXTURE0 );
s_program->setUniformValue( shadowLoc,
SHADOW_TEXTURE_UNIT - GL_TEXTURE0 );
s_program->release( );
}
// 设置顶点坐标
qreal semi = CUBE_LENGTH / 2.0;
const QVector3D vertices[] =
{
QVector3D( semi, 0.0f, -semi ),
QVector3D( semi, 0.0f, semi ),
QVector3D( -semi, 0.0f, -semi ),
QVector3D( -semi, 0.0f, semi )
};
const QVector2D texCoords[] =
{
QVector2D( 0.0, 0.0 ),
QVector2D( 0.0, 1.0 ),
QVector2D( 1.0, 0.0 ),
QVector2D( 1.0, 1.0 )
};
m_vertices = new Vertex[VERTEX_COUNT];
Vertex* v = m_vertices;
v[0].set( vertices[2], QVector3D( 0.0f, 1.0f, 0.0f ), texCoords[1] );
v[1].set( vertices[1], QVector3D( 0.0f, 1.0f, 0.0f ), texCoords[2] );
v[2].set( vertices[0], QVector3D( 0.0f, 1.0f, 0.0f ), texCoords[0] );
v[3].set( vertices[2], QVector3D( 0.0f, 1.0f, 0.0f ), texCoords[1] );
v[4].set( vertices[3], QVector3D( 0.0f, 1.0f, 0.0f ), texCoords[3] );
v[5].set( vertices[1], QVector3D( 0.0f, 1.0f, 0.0f ), texCoords[2] );
m_vertexBuffer.setUsagePattern( QOpenGLBuffer::DynamicDraw );
m_vertexBuffer.create( );
m_vertexBuffer.bind( );
m_vertexBuffer.allocate( v, VERTEX_COUNT * sizeof( Vertex ) );
m_vertexBuffer.release( );
// 设置纹理滤波
m_texture.setMinificationFilter( QOpenGLTexture::LinearMipMapLinear );
m_texture.setMagnificationFilter( QOpenGLTexture::Linear );
QImage image( fileName );
Q_ASSERT( !fileName.isNull( ) );
m_texture.setData( image );
}
Plane::~Plane( void )
{
m_vertexBuffer.destroy( );
m_texture.destroy( );
delete []m_vertices;
if ( --s_count == 0 )
{
delete s_program;
}
}
void Plane::render( OpenGLWidget* widget )
{
s_program->bind( );
m_vertexBuffer.bind( );
// 绘制box
int offset = 0;
setVertexAttribute( s_positionLoc, GL_FLOAT, 3, offset );
offset += 3 * sizeof( GLfloat );
setVertexAttribute( s_normalLoc, GL_FLOAT, 3, offset );
offset += 3 * sizeof( GLfloat );
setVertexAttribute( s_texCoordLoc, GL_FLOAT, 2, offset );
// 摄像机的MVP矩阵
QMatrix4x4& viewMatrix = widget->viewMatrix( );
s_program->setUniformValue( s_modelMatrixLoc, m_modelMatrix );
s_program->setUniformValue( s_viewMatrixLoc, viewMatrix );
s_program->setUniformValue( s_projectionMatrixLoc, widget->projectionMatrix( ) );
s_program->setUniformValue( s_modelViewNormalMatrixLoc,
( viewMatrix * m_modelMatrix ).normalMatrix( ) );
// 是否启用实时阴影
//s_program->setUniformValue( s_shadowTypeLoc, m_shadowType );
m_texture.bind( );
if ( m_shadowType != NoShadow )
{
s_program->setUniformValue( s_lightPositionLoc, widget->lightPosition( ) );
s_program->setUniformValue( s_lightViewMatrixLoc, widget->lightViewMatrix( ) );
s_program->setUniformValue( s_lightProjectionMatrixLoc, widget->lightProjectionMatrix( ) );
glActiveTexture( SHADOW_TEXTURE_UNIT );
glBindTexture( GL_TEXTURE_2D, widget->shadowTexture( ) );
glDrawArrays( GL_TRIANGLES, 0, VERTEX_COUNT );
glBindTexture( GL_TEXTURE_2D, 0 );
glActiveTexture( TEXTURE_UNIT );
}
else
{
glDrawArrays( GL_TRIANGLES, 0, VERTEX_COUNT );
}
m_texture.release( );
m_vertexBuffer.release( );
s_program->release( );
}
void Plane::renderShadow( OpenGLWidget* widget )
{
m_vertexBuffer.bind( );
QOpenGLShaderProgram& depthProgram = widget->depthProgram( );
depthProgram.enableAttributeArray( "position" );
depthProgram.setAttributeBuffer(
"position", // 位置
GL_FLOAT, // 类型
0, // 偏移
3, // 元大小
sizeof( Vertex ) ); // 迈
depthProgram.setUniformValue( "modelMatrix", m_modelMatrix );
glDrawArrays( GL_TRIANGLES, 0, VERTEX_COUNT );
m_vertexBuffer.release( );
}
void Plane::setVertexAttribute( int attributeLocation,
GLenum elementType,
quint32 elementSize,
quint32 offset )
{
s_program->enableAttributeArray( attributeLocation );
s_program->setAttributeBuffer( attributeLocation, // 位置
elementType, // 类型
offset, // 偏移
elementSize, // 元大小
sizeof( Vertex ) ); // 迈
}
static void canonicalPosition( QVector3D& position )
{
if ( !qFuzzyIsNull( position.x( ) ) )
position.setX( position.x( ) / fabsf( position.x( ) ) );
if ( !qFuzzyIsNull( position.y( ) ) )
position.setY( position.y( ) / fabsf( position.y( ) ) );
if ( !qFuzzyIsNull( position.z( ) ) )
position.setZ( position.z( ) / fabsf( position.z( ) ) );
}
void Plane::resize( qreal length )
{
qreal semi = length / 2.0;
m_vertexBuffer.bind( );
Vertex* v = (Vertex*)m_vertexBuffer.map( QOpenGLBuffer::WriteOnly );
for ( int i = 0; i < VERTEX_COUNT; ++i )
{
canonicalPosition( v[i].position );
v[i].position *= semi;
}
m_vertexBuffer.unmap( );
m_vertexBuffer.release( );
}