-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
74 lines (59 loc) · 1.94 KB
/
Shader.cpp
File metadata and controls
74 lines (59 loc) · 1.94 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
#pragma once
using namespace std;
#include "Shader.hpp"
Shader::Shader(const GLchar* vertexSourcePath, const GLchar* fragmentSourcePath)
{
string vertexCode;
string fragmentCode;
try
{
ifstream vShaderFile(vertexSourcePath);
ifstream fShaderFile(fragmentSourcePath);
stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch(exception e)
{
cout << "ERROR:SHADER:FILE_NOT_SUCCESFULLY_READ" << endl;
}
const GLchar* vShaderCode = vertexCode.c_str();
const GLchar* fShaderCode = fragmentCode.c_str();
GLuint vertex, fragment;
GLint compilationSuccess;
GLchar errorInfoLog[512];
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &compilationSuccess);
if(!compilationSuccess)
{
glGetShaderInfoLog(vertex, 512, NULL, errorInfoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << errorInfoLog << std::endl;
}
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &compilationSuccess);
if(!compilationSuccess)
{
glGetShaderInfoLog(fragment, 512, NULL, errorInfoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << errorInfoLog << std::endl;
}
this->Program = glCreateProgram();
glAttachShader(this->Program, vertex);
glAttachShader(this->Program, fragment);
glLinkProgram(this->Program);
glGetProgramiv(this->Program, GL_LINK_STATUS, &compilationSuccess);
if(!compilationSuccess)
{
glGetProgramInfoLog(this->Program, 512, NULL, errorInfoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << errorInfoLog << std::endl;
}
glDeleteShader(vertex);
glDeleteShader(fragment);
}