-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGLShader.cpp
More file actions
149 lines (117 loc) · 4.08 KB
/
OpenGLShader.cpp
File metadata and controls
149 lines (117 loc) · 4.08 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// OpenGLShader.cpp
// OpenGLTutorial
//
// Created by Robby Tong on 12/24/18.
// Copyright © 2018 Robby Tong. All rights reserved.
//
#include "OpenGLShader.hpp"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
static GLuint compile_shader_source(const char * filename, GLenum shader_source_type);
static void read_shader_file(const char * filename, char * buffer, size_t len);
static GLuint link_shaders_to_program(GLuint vertex_shader, GLuint fragment_shader);
OpenGLShader::OpenGLShader(const char vertex_shader_filename[], const char fragme_shader_filename[])
{
GLuint vertex_shader;
GLuint fragme_shader;
vertex_shader = compile_shader_source(vertex_shader_filename, GL_VERTEX_SHADER);
fragme_shader = compile_shader_source(fragme_shader_filename, GL_FRAGMENT_SHADER);
mShaderProgram = link_shaders_to_program(vertex_shader, fragme_shader);
glDeleteShader(vertex_shader);
glDeleteShader(fragme_shader);
}
OpenGLShader::~OpenGLShader()
{
glDeleteProgram(mShaderProgram);
}
void OpenGLShader::use(void)
{
glUseProgram(mShaderProgram);
}
void OpenGLShader::setBool(const char name[], bool value) const
{
GLint uniform_id = glGetUniformLocation(mShaderProgram, name);
glUniform1i(uniform_id, value);
}
void OpenGLShader::setInt(const char name[], int value) const
{
GLint uniform_id = glGetUniformLocation(mShaderProgram, name);
glUniform1i(uniform_id, value);
}
void OpenGLShader::setFloat(const char name[], float value) const
{
GLint uniform_id = glGetUniformLocation(mShaderProgram, name);
glUniform1f(uniform_id, value);
}
void OpenGLShader::setVec3(const char name[], float x, float y, float z) const
{
GLint uniform_id = glGetUniformLocation(mShaderProgram, name);
glUniform3f(uniform_id, x, y, z);
}
void OpenGLShader::setMat4(const char name[], glm::mat4 & value) const
{
GLint uniform_id = glGetUniformLocation(mShaderProgram, name);
glUniformMatrix4fv(uniform_id, 1, GL_FALSE, glm::value_ptr(value));
}
static GLuint compile_shader_source(const char * filename, GLenum shader_source_type)
{
GLuint shader_id;
GLchar info_log[512];
GLchar shader_source[512];
read_shader_file(filename, shader_source, sizeof(shader_source));
shader_id = glCreateShader(shader_source_type);
const GLchar * shader_source_ptr = shader_source;
glShaderSource(shader_id, 1, &shader_source_ptr, NULL);
glCompileShader(shader_id);
GLint success;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &success);
if(0 == success)
{
glGetShaderInfoLog(shader_id, sizeof(info_log), NULL, info_log);
printf("------------------------\n");
printf("SHADER COMPILATION ERROR\n");
printf("------------------------\n");
printf("%s\n", info_log);
exit(-1);
}
printf("Compile success!\n");
return shader_id;
}
static void read_shader_file(const char * filename, char * buffer, size_t len)
{
FILE * shader_file = fopen(filename, "r");
assert(shader_file != NULL);
memset(buffer, 0, len);
size_t bytes_read = fread(buffer, len, 1, shader_file);
assert(bytes_read <= len);
#if 0
printf("--------------------\n");
printf("SHADER FILE CONTENTS\n");
printf("--------------------\n");
printf("%s\n", buffer);
#endif
}
static GLuint link_shaders_to_program(GLuint vertex_shader, GLuint fragment_shader)
{
GLuint shader_program;
shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
char info_log[512] = {0};
GLint success;
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
if(0 == success)
{
glGetProgramInfoLog(shader_program, sizeof(info_log), NULL, info_log);
printf("------------------\n");
printf("PROGRAM LINK ERROR\n");
printf("------------------\n");
printf("%s\n", info_log);
exit(-1);
}
printf("Program link success!\n");
return shader_program;
}