-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShaderManager.cpp
35 lines (30 loc) · 1.18 KB
/
ShaderManager.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
#include "ShaderManager.h"
#include <fstream>
#include <sstream>
#include <iostream>
std::string readShaderFile(const char* filename) {
std::ifstream file(filename);
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
GLuint compileShader(GLenum shaderType, const std::string& source) {
GLuint shader = glCreateShader(shaderType);
const char* sourceCStr = source.c_str();
glShaderSource(shader, 1, &sourceCStr, nullptr);
glCompileShader(shader);
return shader;
}
GLuint createShaderProgram(const char* vertexShaderPath, const char* fragmentShaderPath) {
std::string vertexShaderSource = readShaderFile(vertexShaderPath);
std::string fragmentShaderSource = readShaderFile(fragmentShaderPath);
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}