21 GLenum error = glGetError();
22 if (error != GL_NO_ERROR) {
25 std::cerr <<
"GL error: " << prefix <<
": " << int(error) <<
'\n';
65 int mode = interpolation ? GL_LINEAR : GL_NEAREST;
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mode);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mode);
73 int mode = wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE;
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mode);
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mode);
108 glGenFramebuffers(1, &bufferId);
110 glFramebufferTexture2D(GL_FRAMEBUFFER,
111 GL_COLOR_ATTACHMENT0,
113 bool success = glCheckFramebufferStatus(GL_FRAMEBUFFER) ==
114 GL_FRAMEBUFFER_COMPLETE;
118 "Your OpenGL implementation support for "
119 "framebuffer objects is too limited.");
126 glDeleteFramebuffers(1, &bufferId);
131 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previousId);
132 glBindFramebuffer(GL_FRAMEBUFFER, bufferId);
137 glBindFramebuffer(GL_FRAMEBUFFER, GLuint(previousId));
143void Shader::init(GLenum type, std::string_view header, std::string_view filename)
148 source +=
"#version 100\n";
149 if (type == GL_FRAGMENT_SHADER) {
150 source +=
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
151 " precision highp float;\n"
153 " precision mediump float;\n"
157 source +=
"#version 110\n";
162 auto mmap = file.mmap();
163 source.append(std::bit_cast<const char*>(mmap.data()),
166 std::cerr <<
"Cannot find shader: " <<
e.getMessage() <<
'\n';
172 handle = glCreateShader(type);
174 std::cerr <<
"Failed to allocate shader\n";
179 const char* sourcePtr = source.c_str();
180 glShaderSource(handle, 1, &sourcePtr,
nullptr);
183 glCompileShader(handle);
184 const bool ok =
isOK();
185 GLint infoLogLength = 0;
186 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
189 VLA(GLchar, infoLog, infoLogLength);
190 glGetShaderInfoLog(handle, infoLogLength,
nullptr, infoLog.data());
191 std::cerr << (ok ?
"Warning" :
"Error") <<
"(s) compiling shader \""
192 << filename <<
"\":\n"
193 << (infoLogLength > 1 ? infoLog.data() :
"(no details available)\n");
199 glDeleteShader(handle);
204 if (handle == 0)
return false;
205 GLint compileStatus = GL_FALSE;
206 glGetShaderiv(handle, GL_COMPILE_STATUS, &compileStatus);
207 return compileStatus == GL_TRUE;
215 handle = glCreateProgram();
217 std::cerr <<
"Failed to allocate program\n";
225 glDeleteProgram(handle);
232 if (handle == 0)
return false;
233 GLint linkStatus = GL_FALSE;
234 glGetProgramiv(handle, GL_LINK_STATUS, &linkStatus);
235 return linkStatus == GL_TRUE;
241 if (handle == 0)
return;
244 if (!shader.
isOK())
return;
247 glAttachShader(handle, shader.handle);
253 if (handle == 0)
return;
256 glLinkProgram(handle);
257 const bool ok =
isOK();
258 GLint infoLogLength = 0;
259 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
262 VLA(GLchar, infoLog, infoLogLength);
263 glGetProgramInfoLog(handle, infoLogLength,
nullptr, infoLog.data());
264 fprintf(stderr,
"%s(s) linking shader program:\n%s\n",
265 ok ?
"Warning" :
"Error",
266 infoLogLength > 1 ? infoLog.data() :
"(no details available)\n");
272 glBindAttribLocation(handle, index, name);
278 if (!
isOK())
return -1;
280 return glGetUniformLocation(handle, name);
285 glUseProgram(handle);
291 glValidateProgram(handle);
292 GLint validateStatus = GL_FALSE;
293 glGetProgramiv(handle, GL_VALIDATE_STATUS, &validateStatus);
294 GLint infoLogLength = 0;
295 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
297 VLA(GLchar, infoLog, infoLogLength);
298 glGetProgramInfoLog(handle, infoLogLength,
nullptr, infoLog.data());
299 std::cout <<
"Validate "
300 << ((validateStatus == GL_TRUE) ?
"OK" :
"FAIL")
301 <<
": " << infoLog.data() <<
'\n';
309 glGenBuffers(1, &bufferId);
314 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 bind() const
Makes this texture the active GL texture.
void setWrapMode(bool wrap)
void setInterpolation(bool interpolation)
Enable/disable bilinear interpolation for this texture.
void allocate()
Allocate an openGL texture name.
Thrown when a subsystem initialisation fails.
static const bool RELEASE
void checkGLError(std::string_view prefix)
This file implemented 3 utility functions:
const FileContext & systemFileContext()
TemporaryString tmpStrCat(Ts &&... ts)
#define VLA(TYPE, NAME, LENGTH)