-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderLoader.cpp
More file actions
75 lines (59 loc) · 2.54 KB
/
Copy pathShaderLoader.cpp
File metadata and controls
75 lines (59 loc) · 2.54 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
//
// Created by roudn on 18/06/2022.
//
#include "ShaderLoader.hpp"
#include <iostream>
#include <fstream>
#include <vector>
std::string ShaderLoader::readShader(const char *filename) {
std::string shaderCode;
std::ifstream file(filename, std::ios::in);
if(!file.good()) {
std::cout << "Impossible de lire le fichier " << filename << std::endl;
std::terminate();
}
file.seekg(0,std::ios::end);
shaderCode.resize((unsigned int)file.tellg());
file.seekg(0,std::ios::beg);
file.read(&shaderCode[0], shaderCode.size());
file.close();
return shaderCode;
}
GLuint ShaderLoader::createShader(GLenum shaderType, std::string source, const char *shaderName) {
int compile_result = 0;
GLuint shader = glCreateShader(shaderType);
const char *shader_code_ptr = source.c_str();
const int shader_code_size = source.size();
glShaderSource(shader, 1, &shader_code_ptr, &shader_code_size);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_result);
if(compile_result == GL_FALSE) {
int info_log_length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector<char> shader_log(info_log_length);
glGetShaderInfoLog(shader,info_log_length, nullptr, &shader_log[0]);
std::cout << "ERREUR dans la compilation des shaders: " << shaderName << std::endl << &shader_log[0] << std::endl;
return 0;
} return shader;
}
GLuint ShaderLoader::createProgram(const char *vertexShaderFilename, const char *fragmentShaderFilename) {
std::string vertex_shader_code = readShader(vertexShaderFilename);
std::string fragment_shader_code = readShader(fragmentShaderFilename);
GLuint vertex_shader = createShader(GL_VERTEX_SHADER, vertex_shader_code, "vertex shader");
GLuint fragment_shader = createShader(GL_FRAGMENT_SHADER, fragment_shader_code, "fragment shader");
int link_result = 0;
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_result);
if (link_result == GL_FALSE) {
int info_log_length = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector<char> program_log(info_log_length);
glGetProgramInfoLog(program, info_log_length, nullptr, &program_log[0]);
std::cout << "Sahder Loader: ERREUR LINK" << std::endl << &program_log[0] << std::endl;
return 0;
}
return program;
}