MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
shader_program.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <iostream>
11#include <fstream>
12#include <vector>
13#include <stdexcept>
14
15#include <cstring>
16#include <cerrno>
17
18#include "util/exception.h"
19#include "util/file_system.h"
20#include "ogl/shader_program.h"
21
23
24void
25ShaderProgram::load_shader_file (GLuint& shader_id, GLuint shader_type,
26 std::string const& filename)
27{
28 std::string shader_code;
29 util::fs::read_file_to_string(filename, &shader_code);
30
31 try
32 {
33 this->load_shader_code(shader_id, shader_type, shader_code);
34 }
35 catch (util::Exception& e)
36 {
37 throw util::Exception(filename + ": " + e);
38 }
39}
40
41void
42ShaderProgram::load_shader_code (GLuint& shader_id, GLuint shader_type,
43 std::string const& code)
44{
45 if (shader_id == 0)
46 {
47 shader_id = glCreateShader(shader_type);
49
50 glAttachShader(this->prog_id, shader_id);
52 }
53
54 this->compile_shader(shader_id, code);
55 this->need_to_link = true;
56}
57
58/* ---------------------------------------------------------------- */
59
60void
61ShaderProgram::unload_shader (GLuint& shader_id)
62{
63 if (shader_id != 0)
64 {
65 glDetachShader(this->prog_id, shader_id);
67 glDeleteShader(shader_id);
69 shader_id = 0;
70 }
71}
72
73/* ---------------------------------------------------------------- */
74
75void
76ShaderProgram::compile_shader (GLuint shader_id, std::string const& code)
77{
78 /* Pass code to OpenGL. */
79 char const* data[1] = { code.c_str() };
80 glShaderSource(shader_id, 1, data, nullptr);
82
83 /* Compile shader. */
84 glCompileShader(shader_id);
86 if (this->get_shader_property(shader_id, GL_COMPILE_STATUS) == GL_FALSE)
87 {
88 GLint log_size = this->get_shader_property(shader_id, GL_INFO_LOG_LENGTH);
89 if (log_size == 0)
90 throw util::Exception("Shader compilation failed (no message).");
91
92 std::string log;
93 log.append(log_size + 1, '\0');
94 glGetShaderInfoLog(shader_id, log_size + 1, nullptr, &log[0]);
95 throw util::Exception(log);
96 }
97}
98
99void
100ShaderProgram::ensure_linked (void)
101{
102 if (this->need_to_link)
103 {
104 glLinkProgram(this->prog_id);
106 if (this->get_program_property(GL_LINK_STATUS) == GL_FALSE)
107 {
108 GLint log_size = this->get_program_property(GL_INFO_LOG_LENGTH);
109 if (log_size == 0)
110 throw util::Exception("Failed to link program (no message).");
111
112 std::string log;
113 log.append(log_size + 1, '\0');
114 glGetProgramInfoLog(this->prog_id, log_size + 1, nullptr, &log[0]);
115 throw util::Exception(log);
116 }
117 this->need_to_link = false;
118 }
119}
120
121/* ---------------------------------------------------------------- */
122
123bool
124ShaderProgram::try_load_all (std::string const& basename)
125{
126 std::string vert_filename = basename + ".vert";
127 std::string geom_filename = basename + ".geom";
128 std::string frag_filename = basename + ".frag";
129
130 if (!util::fs::file_exists(vert_filename.c_str())
131 || !util::fs::file_exists(frag_filename.c_str()))
132 {
133 std::cerr << "Skipping shaders from " << basename << ".*" << std::endl;
134 return false;
135 }
136
137 std::cerr << "Loading shaders from " << basename << ".*" << std::endl;
138
139 this->load_vert_file(vert_filename);
140
141 if (util::fs::file_exists(geom_filename.c_str()))
142 this->load_geom_file(geom_filename);
143
144 this->load_frag_file(frag_filename);
145
146 return true;
147}
148
Universal, simple exception class.
Definition exception.h:24
void check_gl_error()
bool file_exists(char const *pathname)
Determines if the given path is a file.
void read_file_to_string(std::string const &filename, std::string *data)
Reads the whole file into a string.
#define OGL_NAMESPACE_END
Definition defines.h:14
#define OGL_NAMESPACE_BEGIN
Definition defines.h:13