-
Notifications
You must be signed in to change notification settings - Fork 0
/
vrrendersplit2d.cpp
151 lines (117 loc) · 3.96 KB
/
vrrendersplit2d.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
#include "vrrendersplit2d.h"
#include <QVector4D>
#define OPENGL_OFFSET(i) ((char *)NULL + (i))
VrRenderSplit2D::VrRenderSplit2D():
projection(NULL), resized(false), inited(false), texture_size(0,0)
{
}
VrRenderSplit2D::~VrRenderSplit2D()
{
}
void VrRenderSplit2D::render(GLuint texture)
{
if(!inited) init();
if(resized)
{
resizeGL();
resized=false;
}
program.bind();
if(!vao.isCreated())
{
vao.create();
vao.bind();
quad.bind();
program.enableAttributeArray(a_vertex);
glVertexAttribPointer(a_vertex, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float),0);
program.enableAttributeArray(a_coord);
glVertexAttribPointer(a_coord, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), OPENGL_OFFSET(3*sizeof(float)));
}
else
vao.bind();
program.setUniformValue(u_matrix, ortho);
glBindTexture(GL_TEXTURE_2D, texture);
glDrawArrays(GL_TRIANGLES, 0, 12);
vao.release();
program.release();
}
void VrRenderSplit2D::updateProjection(const VrProjection *projection)
{
this->projection=projection;
QSize canvas_size=projection->canvasSize();
float d = canvas_size.width()*projection->goggles()->interpupillaryDistance()/2.0;
texture_size.setHeight(canvas_size.height());
texture_size.setWidth(canvas_size.width()/2.0+2.0*d);
resized=true;
ortho.setToIdentity();
ortho.ortho(0, projection->canvasSize().width(), 0, projection->canvasSize().height(), 0.0, 1.0);
}
QSize VrRenderSplit2D::textureSize()
{
return texture_size;
}
bool VrRenderSplit2D::init()
{
inited=true;
initializeOpenGLFunctions();
program.addShaderFromSourceCode(QOpenGLShader::Vertex, vsh);
program.addShaderFromSourceCode(QOpenGLShader::Fragment, fsh);
if(!program.link())
{
qDebug()<<"Shader link error: "<<program.log();
return false;
}
a_vertex = program.attributeLocation("vertex");
a_coord = program.attributeLocation("coord");
u_source = program.uniformLocation("source");
u_matrix = program.uniformLocation("matrix");
resizeGL();
program.bind();
program.setUniformValue(u_source,0);
program.release();
return true;
}
void VrRenderSplit2D::resizeGL()
{
QSize canvas_size=projection->canvasSize();
float d = projection->goggles()->interpupillaryDistance()/2.0*canvas_size.width();
const float height=texture_size.height();
const float middle=canvas_size.width()/2.0;
float quad_data[]={
0.0f - d*2.0f , 0.0f , 0.0f, 0.0f, 0.0f,
middle , 0.0f , 0.0f, 1.0f, 0.0f,
0.0f - d*2.0f , height , 0.0f, 0.0f, 1.0f,
0.0f - d*2.0f , height , 0.0f, 0.0f, 1.0f,
middle , height , 0.0f, 1.0f, 1.0f,
middle , 0.0f , 0.0f, 1.0f, 0.0f,
middle , 0.0f , 0.0f, 0.0f, 0.0f,
middle+middle+d*2.0f , 0.0f , 0.0f, 1.0f, 0.0f,
middle , height , 0.0f, 0.0f, 1.0f,
middle , height , 0.0f, 0.0f, 1.0f,
middle+middle+d*2.0f , height , 0.0f, 1.0f, 1.0f,
middle+middle+d*2.0f , 0.0f , 0.0f, 1.0f, 0.0f
};
if(quad.isCreated())
quad.destroy();
quad.create();
quad.bind();
quad.allocate(quad_data, sizeof(quad_data));
quad.release();
vao.destroy();
}
const char *VrRenderSplit2D::vsh =
"attribute highp vec3 vertex;\n"
"attribute lowp vec2 coord;\n"
"uniform highp mat4 matrix;\n"
"varying lowp vec2 f_texcoord;\n"
"void main() {\n"
" f_texcoord = coord;\n"
" gl_Position = matrix * vec4(vertex,1.0);\n"
"}\n";
const char *VrRenderSplit2D::fsh =
"varying lowp vec2 f_texcoord;\n"
"uniform sampler2D source;\n"
"void main() {\n"
" lowp vec4 color=texture2D(source, f_texcoord);\n"
" gl_FragColor = color;\n"
"}\n";