-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.cpp
executable file
·116 lines (92 loc) · 2.86 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "shader.h"
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <cstdio>
using namespace std;
static char* textFileRead(const char *fileName) {
char* text;
if (fileName != NULL) {
FILE *file = fopen(fileName, "rt");
if (file != NULL) {
fseek(file, 0, SEEK_END);
int count = ftell(file);
rewind(file);
if (count > 0) {
text = (char*)malloc(sizeof(char) * (count + 1));
count = fread(text, sizeof(char), count, file);
text[count] = '\0';
}
fclose(file);
}
}
return text;
}
static void validateShader(GLuint shader, const char* file = 0) {
const unsigned int BUFFER_SIZE = 512;
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
GLsizei length = 0;
/* glGetShaderInfoLog(shader, BUFFER_SIZE, &length, buffer);
if (length > 0) {
cout << "Shader " << shader << " (" << (file?file:"") << ") compile error: " << buffer << endl;
}*/
}
static void validateProgram(GLuint program) {
const unsigned int BUFFER_SIZE = 512;
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
GLsizei length = 0;
memset(buffer, 0, BUFFER_SIZE);
/* glGetProgramInfoLog(program, BUFFER_SIZE, &length, buffer);
if (length > 0)
cout << "Program " << program << " link error: " << buffer << endl;
glValidateProgram(program);
GLint status;
glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
if (status == GL_FALSE)
cout << "Error validating shader " << program << endl;*/
}
Shader::Shader() {
}
Shader::Shader(const char *vsFile, const char *fsFile) {
init(vsFile, fsFile);
}
void Shader::init(const char *vsFile, const char *fsFile)
{
const char* vsText = textFileRead(vsFile);
const char* fsText = textFileRead(fsFile);
if (vsText == NULL || fsText == NULL) {
cerr << "Either vertex shader or fragment shader file not found." << endl;
return;
}
GLuint shader_vp = glCreateShader(GL_VERTEX_SHADER);
GLuint shader_fp = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shader_vp, 1, &vsText, 0);
glShaderSource(shader_fp, 1, &fsText, 0);
glCompileShader(shader_vp);
validateShader(shader_vp, vsFile);
glCompileShader(shader_fp);
validateShader(shader_fp, fsFile);
shader_id = glCreateProgram();
glAttachShader(shader_id, shader_fp);
glAttachShader(shader_id, shader_vp);
glLinkProgram(shader_id);
validateProgram(shader_id);
}
Shader::~Shader() {
glDetachShader(shader_id, shader_fp);
glDetachShader(shader_id, shader_vp);
glDeleteShader(shader_fp);
glDeleteShader(shader_vp);
glDeleteProgram(shader_id);
}
unsigned int Shader::id() {
return shader_id;
}
void Shader::bind() {
glUseProgram(shader_id);
}
void Shader::unbind() {
glUseProgram(0);
}