-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenquad.cpp
More file actions
123 lines (100 loc) · 3.63 KB
/
Copy pathscreenquad.cpp
File metadata and controls
123 lines (100 loc) · 3.63 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
#include "screenquad.h"
screenQuad::screenQuad()
{
}
void screenQuad::init(QOpenGLFunctions_3_2_Core *f)
{
// generate and bind the buffers assosiated with this chunk in order to assign
// vertices and color to the mesh
f->glGenVertexArrays(1, &VAO);
f->glGenBuffers(1, &VBO);
// load the shaders from their corresponding files
GLuint vertexShader = loadShader(f, "shaders/screenVert.glsl", GL_VERTEX_SHADER);
GLuint fragmentShader =
loadShader(f, "shaders/screenFrag.glsl", GL_FRAGMENT_SHADER);
// compile the GPU programs
f->glCompileShader(vertexShader);
f->glCompileShader(fragmentShader);
// catch any errors
GLint success;
GLchar infoLog[512];
f->glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
f->glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
QMessageBox messageBox;
messageBox.critical(nullptr, "Error", "Vertex Shader Compile Fail");
std::cout << "message: " << infoLog << std::endl;
messageBox.setFixedSize(500, 200);
};
f->glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
f->glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
QMessageBox messageBox;
messageBox.critical(nullptr, "Error", "Fragment Shader compile fail");
messageBox.setFixedSize(500, 200);
};
// create a program from the shaders
shaderProgram = f->glCreateProgram();
f->glAttachShader(shaderProgram, vertexShader);
f->glAttachShader(shaderProgram, fragmentShader);
f->glBindFragDataLocation(shaderProgram, 0, "outColor");
// finilize the program and use it
f->glLinkProgram(shaderProgram);
f->glUseProgram(shaderProgram);
f->glBindVertexArray(VAO);
f->glBindBuffer(GL_ARRAY_BUFFER, VBO);
// set the array buffer to contain sections the size of a Vertex struct, and
// pass a pointer to the vector containing them
f->glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float),
&quadVertices[0], GL_STATIC_DRAW);
GLuint posAttrib = GLCheckError(f->glGetAttribLocation(shaderProgram, "position"));
GLuint texcoordsAttrib = GLCheckError(f->glGetAttribLocation(shaderProgram, "texCoords"));
f->glEnableVertexAttribArray(posAttrib);
f->glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
f->glEnableVertexAttribArray(texcoordsAttrib);
f->glVertexAttribPointer(texcoordsAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
uniTexture = f->glGetUniformLocation(shaderProgram, "screenTexture");
uniDepth = f->glGetUniformLocation(shaderProgram, "depthMap");
f->glUniform1i(uniTexture, 0);
}
void screenQuad::render(QOpenGLFunctions_3_2_Core *f)
{
f->glUseProgram(shaderProgram);
f->glBindVertexArray(VAO);
f->glBindBuffer(GL_ARRAY_BUFFER, VBO);
f->glDrawArrays(GL_TRIANGLES, 0, 6);
}
GLuint screenQuad::loadShader(QOpenGLFunctions_3_2_Core *f, const char *filepath, GLenum type)
{
FILE *file = fopen(filepath, "rb");
if (!file)
{
return 0;
}
long len;
if (fseek(file, 0, SEEK_END) != 0 || (len = ftell(file)) == -1L)
{
fclose(file);
return 0;
}
rewind(file);
char *buffer = (char *)malloc(len);
if (fread(buffer, 1, len, file) != len)
{
fclose(file);
free(buffer);
return 0;
}
fclose(file);
GLuint shader = f->glCreateShader(type);
if (shader == 0)
{
free(buffer);
return 0;
}
f->glShaderSource(shader, 1, (const char *const *)&buffer, (GLint *)&len);
free(buffer);
return shader;
}