-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDrawHelper.cpp
More file actions
93 lines (69 loc) · 2.99 KB
/
DrawHelper.cpp
File metadata and controls
93 lines (69 loc) · 2.99 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
#include "DrawHelper.h"
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <OVR_LogUtils.h>
using namespace OVRFW;
const char VERTEX_SHADER_TEXTURE[] =
"#version 330 core\n"
"layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>\n"
"out vec2 TexCoords;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n"
" TexCoords = vertex.zw;\n"
"}\n";
const char FRAGMENT_SHADER_TEXTURE[] =
"#version 330 core\n"
"in vec2 TexCoords;\n"
"out vec4 color;\n"
"uniform sampler2D text;\n"
"uniform vec4 textColor;\n"
"void main()\n"
"{\n"
" vec4 tex_sample = texture(text, TexCoords);\n"
" color = tex_sample * textColor * tex_sample.a;\n"
"}\n";
void DrawHelper::Free() {
OVR_LOG("draw helper destruction");
GlProgram::Free(glTextureProgram);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glDeleteVertexArrays(1, &texture_vao);
glDeleteBuffers(1, &texture_vbo);
}
void DrawHelper::Init(glm::mat4 projection) {
glTextureProgram = GlProgram::Build(VERTEX_SHADER_TEXTURE, FRAGMENT_SHADER_TEXTURE, NULL, 0);
glUseProgram(glTextureProgram.Program);
glUniformMatrix4fv(glGetUniformLocation(glTextureProgram.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
// Create Vertex Array Object
glGenVertexArrays(1, &texture_vao);
glBindVertexArray(texture_vao);
// Create a Vertex Buffer Object and copy the vertex data to it
glGenBuffers(1, &texture_vbo);
glBindBuffer(GL_ARRAY_BUFFER, texture_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
// Specify the layout of the vertex data
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
}
void DrawHelper::DrawTexture(GLuint textureId, GLfloat posX, GLfloat posY, GLfloat width, GLfloat height, ovrVector4f color, float transparency) {
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(texture_vao);
glUseProgram(glTextureProgram.Program);
glBindTexture(GL_TEXTURE_2D, textureId);
GLfloat charVertices[6][4] = {
{posX, posY + height, 0.0f, 1.0f},
{posX, posY, 0.0f, 0.0f},
{posX + width, posY, 1.0f, 0.0f},
{posX, posY + height, 0.0f, 1.0f},
{posX + width, posY, 1.0f, 0.0f},
{posX + width, posY + height, 1.0f, 1.0f}};
glBindBuffer(GL_ARRAY_BUFFER, texture_vbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(charVertices), charVertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glUniform4f(glGetUniformLocation(glTextureProgram.Program, "textColor"),
color.x * transparency, color.y * transparency, color.z * transparency, color.w * transparency);
// Draw a triangle from the 3 vertices
glDrawArrays(GL_TRIANGLES, 0, 6);
}