/
shader.cpp
51 lines (43 loc) · 1.04 KB
/
shader.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "shader.h"
#include <stdexcept>
#include <fstream>
#include <sstream>
#include <vector>
#include<iostream>
namespace mm
{
Shader::Shader(GLenum Type,std::string filename)
{
shader_type = Type;
std::ifstream ifs(filename);
if(!ifs)
throw(std::runtime_error("File:"+filename+" not opened."));
std::ostringstream stream;
stream<<ifs.rdbuf();
std::string strdata = stream.str();
const GLchar* data = strdata.c_str();
handle = glCreateShader(shader_type);
glShaderSource(handle,1,static_cast<const GLchar**> (&data),0);
glCompileShader(handle);
int status;
glGetShaderiv(handle,GL_COMPILE_STATUS,&status);
if(status == GL_FALSE)
{
int loglength;
glGetShaderiv(handle,GL_INFO_LOG_LENGTH,&loglength);
auto data = new char[loglength];
glGetShaderInfoLog(handle,loglength,&loglength,data);
std::string strdata(data);
delete [] data;
throw(std::runtime_error(strdata));
}
}
Shader::~Shader()
{
glDeleteShader(handle);
}
GLuint Shader::getHandle()const
{
return handle;
}
}