Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Shader.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 *
21 * Part of this file comes from SFML, with the same license:
22 * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23 */
24#ifndef GF_SHADER_H
25#define GF_SHADER_H
26
27#include <string>
28#include <map>
29
30#include "GraphicsApi.h"
31#include "Matrix.h"
32#include "Path.h"
33#include "Texture.h"
34#include "Vector.h"
35
36namespace gf {
37#ifndef DOXYGEN_SHOULD_SKIP_THIS
38inline namespace v1 {
39#endif
40
41 class InputStream;
42
119 class GF_GRAPHICS_API Shader {
120 public:
124 enum Type {
127 };
128
135
151 Shader(const Path& filename, Type type);
152
169 Shader(const Path& vertexShaderFilename, const Path& fragmentShaderFilename);
170
185 Shader(const char *shader, Type type);
186
203 Shader(const char *vertexShader, const char *fragmentShader);
204
219 Shader(InputStream& stream, Type type);
220
236 Shader(InputStream& vertexShaderStream, InputStream& fragmentShaderStream);
237
242
246 Shader(const Shader&) = delete;
247
251 Shader& operator=(const Shader&) = delete;
252
270 void setUniform(const std::string& name, float val);
271
278 void setUniform(const std::string& name, int val);
279
286 void setUniform(const std::string& name, const Vector2f& vec);
287
294 void setUniform(const std::string& name, const Vector3f& vec);
295
302 void setUniform(const std::string& name, const Vector4f& vec);
303
310 void setUniform(const std::string& name, const Vector2i& vec);
311
318 void setUniform(const std::string& name, const Vector3i& vec);
319
326 void setUniform(const std::string& name, const Vector4i& vec);
327
334 void setUniform(const std::string& name, const Matrix3f& mat);
335
342 void setUniform(const std::string& name, const Matrix4f& mat);
343
370 void setUniform(const std::string& name, const BareTexture& tex);
371
382 static void bind(const Shader *shader);
383
384 private:
385 friend class RenderTarget;
386 int getUniformLocation(const std::string& name);
387 int getAttributeLocation(const std::string& name);
388
389 struct Guard;
390
391 private:
392 unsigned m_program;
393
394 std::map<int, const BareTexture *> m_textures;
395 };
396
397#ifndef DOXYGEN_SHOULD_SKIP_THIS
398}
399#endif
400}
401
402#endif // GF_SHADER_H
An image that lives in the graphic memory that can be used for drawing.
Definition: Texture.h:79
Abstract class for custom file input streams.
Definition: Stream.h:54
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
An OpenGL vertex and/or fragment shader.
Definition: Shader.h:119
void setUniform(const std::string &name, float val)
Specify value for a float uniform.
void setUniform(const std::string &name, const Vector2f &vec)
Specify value for a vec2 uniform.
Shader(InputStream &stream, Type type)
Load the vertex or fragment shader from a custom stream.
static void bind(const Shader *shader)
Bind a shader for rendering.
void setUniform(const std::string &name, const Vector3f &vec)
Specify value for a vec3 uniform.
Shader()
Default constructor.
~Shader()
Destructor.
void setUniform(const std::string &name, const Matrix3f &mat)
Specify value for a mat3 uniform.
Type
Type of shaders.
Definition: Shader.h:124
@ Fragment
Type for a fragment (or pixel) shader.
Definition: Shader.h:126
@ Vertex
Type for a vertex shader.
Definition: Shader.h:125
void setUniform(const std::string &name, const Vector2i &vec)
Specify value for a ivec2 uniform.
Shader(InputStream &vertexShaderStream, InputStream &fragmentShaderStream)
Load both the vertex and fragment shaders from custom streams.
void setUniform(const std::string &name, const Vector4f &vec)
Specify value for a vec4 uniform.
Shader(const char *vertexShader, const char *fragmentShader)
Load both the vertex and fragment shaders from source codes in memory.
Shader(const char *shader, Type type)
Load the vertex or fragment shader from a source code in memory.
Shader(const Path &filename, Type type)
Load the vertex of fragment shader from a file.
Shader(const Shader &)=delete
Deleted copy constructor.
void setUniform(const std::string &name, const BareTexture &tex)
Specify a texture for a sampler2D uniform.
void setUniform(const std::string &name, int val)
Specify value for a int uniform.
void setUniform(const std::string &name, const Vector4i &vec)
Specify value for a ivec4 uniform.
void setUniform(const std::string &name, const Matrix4f &mat)
Specify value for a mat4 uniform.
Shader(const Path &vertexShaderFilename, const Path &fragmentShaderFilename)
Load both the vertex and fragment shaders from files.
Shader & operator=(const Shader &)=delete
Deleted copy assignment.
void setUniform(const std::string &name, const Vector3i &vec)
Specify value for a ivec3 uniform.
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
The namespace for gf classes.