20 GLenum error = glGetError();
21 if (error != GL_NO_ERROR) {
24 std::cerr <<
"GL error: " << prefix <<
": " << int(error) <<
'\n';
64 int mode = interpolation ? GL_LINEAR : GL_NEAREST;
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mode);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mode);
72 int mode = wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE;
73 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mode);
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mode);
107 glGenFramebuffers(1, &bufferId);
109 glFramebufferTexture2D(GL_FRAMEBUFFER,
110 GL_COLOR_ATTACHMENT0,
112 bool success = glCheckFramebufferStatus(GL_FRAMEBUFFER) ==
113 GL_FRAMEBUFFER_COMPLETE;
117 "Your OpenGL implementation support for "
118 "framebuffer objects is too limited.");
125 glDeleteFramebuffers(1, &bufferId);
130 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &previousId);
131 glBindFramebuffer(GL_FRAMEBUFFER, bufferId);
136 glBindFramebuffer(GL_FRAMEBUFFER, GLuint(previousId));
142void Shader::init(GLenum type, std::string_view header, std::string_view filename)
147 source +=
"#version 100\n";
148 if (type == GL_FRAGMENT_SHADER) {
149 source +=
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
150 " precision highp float;\n"
152 " precision mediump float;\n"
156 source +=
"#version 110\n";
161 auto mmap = file.mmap();
162 source.append(std::bit_cast<const char*>(mmap.data()),
165 std::cerr <<
"Cannot find shader: " <<
e.getMessage() <<
'\n';
171 handle = glCreateShader(type);
173 std::cerr <<
"Failed to allocate shader\n";
178 const char* sourcePtr = source.c_str();
179 glShaderSource(handle, 1, &sourcePtr,
nullptr);
182 glCompileShader(handle);
183 const bool ok =
isOK();
184 GLint infoLogLength = 0;
185 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
188 std::string infoLog(infoLogLength - 1,
'\0');
189 glGetShaderInfoLog(handle, infoLogLength,
nullptr, infoLog.data());
190 std::cerr << (ok ?
"Warning" :
"Error") <<
"(s) compiling shader \""
191 << filename <<
"\":\n"
192 << (infoLogLength > 1 ? infoLog.data() :
"(no details available)\n");
198 glDeleteShader(handle);
203 if (handle == 0)
return false;
204 GLint compileStatus = GL_FALSE;
205 glGetShaderiv(handle, GL_COMPILE_STATUS, &compileStatus);
206 return compileStatus == GL_TRUE;
214 handle = glCreateProgram();
216 std::cerr <<
"Failed to allocate program\n";
224 glDeleteProgram(handle);
231 if (handle == 0)
return false;
232 GLint linkStatus = GL_FALSE;
233 glGetProgramiv(handle, GL_LINK_STATUS, &linkStatus);
234 return linkStatus == GL_TRUE;
240 if (handle == 0)
return;
243 if (!shader.
isOK())
return;
246 glAttachShader(handle, shader.handle);
252 if (handle == 0)
return;
255 glLinkProgram(handle);
256 const bool ok =
isOK();
257 GLint infoLogLength = 0;
258 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
261 std::string infoLog(infoLogLength - 1,
'\0');
262 glGetProgramInfoLog(handle, infoLogLength,
nullptr, infoLog.data());
263 fprintf(stderr,
"%s(s) linking shader program:\n%s\n",
264 ok ?
"Warning" :
"Error",
265 infoLogLength > 1 ? infoLog.data() :
"(no details available)\n");
271 glBindAttribLocation(handle, index, name);
277 if (!
isOK())
return -1;
279 return glGetUniformLocation(handle, name);
284 glUseProgram(handle);
290 glValidateProgram(handle);
291 GLint validateStatus = GL_FALSE;
292 glGetProgramiv(handle, GL_VALIDATE_STATUS, &validateStatus);
293 GLint infoLogLength = 0;
294 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
296 std::string infoLog(infoLogLength - 1,
'\0');
297 glGetProgramInfoLog(handle, infoLogLength,
nullptr, infoLog.data());
298 std::cout <<
"Validate "
299 << ((validateStatus == GL_TRUE) ?
"OK" :
"FAIL")
300 <<
": " << infoLog.data() <<
'\n';
308 glGenBuffers(1, &bufferId);
313 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)