-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.cpp
More file actions
65 lines (62 loc) · 2.06 KB
/
shader.cpp
File metadata and controls
65 lines (62 loc) · 2.06 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
#include "shader.hpp"
#include <algorithm>
#include <string>
#include <cstring>
#include <cassert>
#include <span>
#include <memory>
#include <optional>
#include <iostream>
#include <GL/glew.h>
std::string read_file(std::string const& path) {
char buffer[512] = {0};
std::string file_content;
FILE* file = fopen(path.c_str(), "r");
assert(file);
size_t read_count = 0;
do {
memset(buffer, 0, sizeof(buffer));
read_count = fread(buffer, 1, sizeof(buffer), file);
std::string section(buffer, read_count);
file_content += section;
} while (read_count > 0);
fclose(file);
return file_content;
}
unsigned int create_shader(std::string const& path, unsigned int type) {
auto const text = read_file(path);
unsigned int shader = glCreateShader(type);
char const* cont = text.data();
int len = text.length();
glShaderSource(shader, 1, &cont, &len);
glCompileShader(shader);
// Check for compile status
int success = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success == GL_FALSE) {
constexpr size_t length = 512;
std::unique_ptr<char[]> buffer = std::make_unique<char[]>(length);
glGetShaderInfoLog(shader, length, NULL, buffer.get());
std::cout << "Shader Failure: " << std::string(buffer.get(), length);
exit(1); // panic
}
return shader;
}
unsigned int create_program(std::span<unsigned int> const& shaders) {
unsigned int program = glCreateProgram();
std::ranges::for_each(shaders, [=](unsigned int shader){
glAttachShader(program, shader);
});
glLinkProgram(program);
// Check for link status
int success = 0;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (success == GL_FALSE) {
constexpr size_t length = 512;
std::unique_ptr<char[]> buffer = std::make_unique<char[]>(length);
glGetProgramInfoLog(program, length, NULL, buffer.get());
std::cout << "Shader Failure: " << std::string(buffer.get(), length);
exit(1); // panic
}
return program;
}