17 GLenum error = glGetError();
18 if (error != GL_NO_ERROR) {
21 std::cerr <<
"GL error: " << prefix <<
": " << int(error) <<
'\n';
50 int mode = interpolation ? GL_LINEAR : GL_NEAREST;
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mode);
52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mode);
58 int mode = wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE;
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mode);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mode);
93 glGenFramebuffers(1, &bufferId);
95 glFramebufferTexture2D(GL_FRAMEBUFFER,
98 bool success = glCheckFramebufferStatus(GL_FRAMEBUFFER) ==
99 GL_FRAMEBUFFER_COMPLETE;
103 "Your OpenGL implementation support for "
104 "framebuffer objects is too limited.");
111 glDeleteFramebuffers(1, &bufferId);
116 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previousId);
117 glBindFramebuffer(GL_FRAMEBUFFER, bufferId);
122 glBindFramebuffer(GL_FRAMEBUFFER, GLuint(previousId));
128void Shader::init(GLenum type, std::string_view header, std::string_view filename)
133 source +=
"#version 100\n";
134 if (type == GL_FRAGMENT_SHADER) {
135 source +=
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
136 " precision highp float;\n"
138 " precision mediump float;\n"
142 source +=
"#version 110\n";
147 auto mmap = file.mmap();
148 source.append(
reinterpret_cast<const char*
>(mmap.data()),
151 std::cerr <<
"Cannot find shader: " <<
e.getMessage() <<
'\n';
157 handle = glCreateShader(type);
159 std::cerr <<
"Failed to allocate shader\n";
164 const char* sourcePtr = source.c_str();
165 glShaderSource(handle, 1, &sourcePtr,
nullptr);
168 glCompileShader(handle);
169 const bool ok =
isOK();
170 GLint infoLogLength = 0;
171 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
174 VLA(GLchar, infoLog, infoLogLength);
175 glGetShaderInfoLog(handle, infoLogLength,
nullptr, infoLog.data());
176 std::cerr << (ok ?
"Warning" :
"Error") <<
"(s) compiling shader \""
177 << filename <<
"\":\n"
178 << (infoLogLength > 1 ? infoLog.data() :
"(no details available)\n");
184 glDeleteShader(handle);
189 if (handle == 0)
return false;
190 GLint compileStatus = GL_FALSE;
191 glGetShaderiv(handle, GL_COMPILE_STATUS, &compileStatus);
192 return compileStatus == GL_TRUE;
200 handle = glCreateProgram();
202 std::cerr <<
"Failed to allocate program\n";
208 glDeleteProgram(handle);
214 if (handle == 0)
return false;
215 GLint linkStatus = GL_FALSE;
216 glGetProgramiv(handle, GL_LINK_STATUS, &linkStatus);
217 return linkStatus == GL_TRUE;
223 if (handle == 0)
return;
226 if (!shader.
isOK())
return;
229 glAttachShader(handle, shader.handle);
235 if (handle == 0)
return;
238 glLinkProgram(handle);
239 const bool ok =
isOK();
240 GLint infoLogLength = 0;
241 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
244 VLA(GLchar, infoLog, infoLogLength);
245 glGetProgramInfoLog(handle, infoLogLength,
nullptr, infoLog.data());
246 fprintf(stderr,
"%s(s) linking shader program:\n%s\n",
247 ok ?
"Warning" :
"Error",
248 infoLogLength > 1 ? infoLog.data() :
"(no details available)\n");
254 glBindAttribLocation(handle, index, name);
260 if (!
isOK())
return -1;
262 return glGetUniformLocation(handle, name);
267 glUseProgram(handle);
273 glValidateProgram(handle);
274 GLint validateStatus = GL_FALSE;
275 glGetProgramiv(handle, GL_VALIDATE_STATUS, &validateStatus);
276 GLint infoLogLength = 0;
277 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
279 VLA(GLchar, infoLog, infoLogLength);
280 glGetProgramInfoLog(handle, infoLogLength,
nullptr, infoLog.data());
281 std::cout <<
"Validate "
282 << ((validateStatus == GL_TRUE) ?
"OK" :
"FAIL")
283 <<
": " << infoLog.data() <<
'\n';
291 glGenBuffers(1, &bufferId);
296 glDeleteBuffers(1, &bufferId);
void resize(GLsizei width, GLsizei height)
ColorTexture()=default
Default constructor, zero-sized texture.
FrameBufferObject()=default
void activate() const
Makes this program the active shader program.
bool isOK() const
Returns true iff this program was linked without errors.
void attach(const Shader &shader)
Adds a given shader to this program.
void allocate()
Allocate a shader program handle.
void reset()
Release the shader program handle.
void link()
Links all attached shaders together into one program.
void bindAttribLocation(unsigned index, const char *name)
Bind the given name for a vertex shader attribute to the given location.
GLint getUniformLocation(const char *name) const
Gets a reference to a uniform variable declared in the shader source.
Wrapper around an OpenGL shader: a program executed on the GPU.
bool isOK() const
Returns true iff this shader is loaded and compiled without errors.
Most basic/generic texture: only contains a texture ID.
void reset()
Release openGL texture name.
Texture(const Texture &)=delete
void setWrapMode(bool wrap)
void setInterpolation(bool interpolation)
Enable/disable bilinear interpolation for this texture.
void bind()
Makes this texture the active GL texture.
void allocate()
Allocate an openGL texture name.
Thrown when a subsystem initialisation fails.
void checkGLError(std::string_view prefix)
This file implemented 3 utility functions:
const FileContext & systemFileContext()
TemporaryString tmpStrCat(Ts &&... ts)
#define VLA(TYPE, NAME, LENGTH)