Shading

Texture

struct glen::Texture

A texture is used to prepare sampler objects for shaders

Public Functions

Texture()

Constructor.

Texture(const char *file_name)

Create a texture from an image file located at file_name.

Texture(const char *file_name, const bool is_srgb)

Create a texture from an image file located at file_name.

Parameters
  • is_srgb: If this flag is False, image is assumed to be linear.

Texture(const GLenum target)

Construct an internal texture.

Texture(const glm::vec3 &color)

Construct color solid color texture.

Texture(const glm::vec4 &color)

Construct color solid color texture with an alpha.

Texture(const Texture &other) = delete

Use move constructor instead of copy constructor.

Texture(Texture &&other) noexcept

Move constructor.

Texture &operator=(const Texture &other) = delete

Use move assign instead of copy assign.

Texture &operator=(Texture &&other) noexcept

Move assign.

~Texture()
void process()

Tell OpenGL about all properties of the texture.

void bind()

Make this texture ‘active’ in relation to OpenGL.

void bind(GLuint texture_unit)

Make the texture associated with texture_unit active in relation to OpenGL.

void unbind()

Unbind this texture.

bool is_framebuffer()

Return

True if the texture contains no data.

const std::string name() const

Return

The texture name

const GLuint id() const

Return

The GL Texture ID.

const GLsizei width() const

Return

The width of the texture

const GLsizei height() const

Return

The height of the texture

const GLenum target() const

Return

The OpenGL target (e.g. GL_TEXTURE_2D)

const GLint level() const

Return

The mipmap level

const GLenum internal_format() const

Return

The internal OpenGL format (e.g. GL_RGB)

const GLint border() const

Return

The size in pixels of the border

const GLenum format() const

Return

The format of the texture (e.g. `GL_RGB)

const GLenum type() const

Return

The data type of the pixels (e.g. GL_UNSIGNED_BYTE)

const GLenum min_filter() const

Return

The filtering method for when the texture is smaller than the screen (e.g. GL_LINEAR_MIPMAP_LINEAR)

const GLenum mag_filter() const

Return

The filtering method for when the texture is larger than the screen (e.g. GL_LINEAR)

const GLenum wrap_s() const

Return

What to do at the edges of the texture in the s / u direction (e.g. GL_REPEAT)

const GLenum wrap_t() const

Return

What to do at the edges of the texture in the t / v direction (e.g. GL_REPEAT)

const GLenum wrap_r() const

Return

What to do at the edges of the texture in the r direction (e.g. GL_REPEAT)

const SDL_Surface *surface() const

Return

The SDL surface being used to hold the texture information

const std::vector<SDL_Surface*> surfaces() const

Return

The list of SDL surfaces being used to hold all textures

const glm::tvec4<GLubyte> color() const

Return

The solid color being used.

bool has_alpha()

Return

True if texture is making use of an alpha channel.

void set_name(const std::string &name)

Set the texture name.

void set_new_id(const GLuint id)

Set the texture ID.

void set_target(const GLenum target)

Set the target (e.g. GL_TEXTURE_2D).

void set_width(const GLsizei width)

Set the width in pixels of the texture.

void set_height(const GLsizei height)

Set the height in pixels of the texture.

void set_internal_format(const GLint internal_format)

Set the internal format (e.g. GL_RGB).

void set_format(const GLenum format)

Set the format (e.g. GL_RGB).

void set_type(const GLenum type)

Set the pixel data type (e.g. GL_UNSIGNED_BYTE).

void set_data(void *data)

Set the actual texture data.

void set_min_filter(const GLenum min_filter)

Set the filtering method for when the texture is smaller than the screen (e.g. GL_LINEAR_MIPMAP_LINEAR)

void set_mag_filter(const GLenum max_filter)

Set the filtering method for when the texture is larger than the screen (e.g. GL_LINEAR)

void set_st_wrap(const GLenum wrap)

Set the behaviour when the uv/uvw texture space has been exceeded (e.g. GL_REPEAT).

void set_mipmap(const bool value)

Set true if you want to generate mipmaps. This usually make textures look better on screen.

void set_border_color(const GLfloat r, const GLfloat g, const GLfloat b, const GLfloat a)

Set the r, g, b, a border color values/.

void add_surface(SDL_Surface *surface)

Add a SDL surface to this texture.

void resize(const GLsizei width, const GLsizei height)

Change the dimensions of this texture to width x height.

Public Static Functions

Texture create_16bit_rgba_null_texture(const GLenum target, const glm::uvec2 &dimensions)
Texture create_16bit_rgba_null_texture(const std::string &name, const GLenum target, const glm::uvec2 &dimensions)
Texture create_16bit_rgb_null_texture(const GLenum target, const glm::uvec2 &dimensions)
Texture create_16bit_rgb_null_texture(const std::string &name, const GLenum target, const glm::uvec2 &dimensions)
Texture create_8bit_rgba_null_texture(const GLenum target, const glm::uvec2 &dimensions)
Texture create_8bit_rgba_null_texture(const std::string &name, const GLenum target, const glm::uvec2 &dimensions)
Texture create_8bit_rgb_null_texture(const GLenum target, const glm::uvec2 &dimensions)
Texture create_8bit_rgb_null_texture(const std::string &name, const GLenum target, const glm::uvec2 &dimensions)
Texture create_depth_null_texture(const GLenum target, const glm::uvec2 &dimensions)
Texture create_depth_null_texture(const std::string &name, const GLenum target, const glm::uvec2 &dimensions)
Texture create_stencil_texture(const GLenum target, const glm::uvec2 &dimensions)
Texture create_stencil_texture(const std::string &name, const GLenum target, const glm::uvec2 &dimensions)
Texture create_depth_null_texture_for_shadow(const GLenum target, const glm::uvec2 &dimensions)
Texture create_depth_null_texture_for_shadow(const std::string &name, const GLenum target, const glm::uvec2 &dimensions)
Texture create_square_noise_tile_texture(const GLenum target, const glm::uvec2 &dimensions, const std::vector<glm::vec3> &data)
Texture create_square_noise_tile_texture(const std::string &name, const GLenum target, const glm::uvec2 &dimensions, const std::vector<glm::vec3> &data)
Texture create_bw_null_texture(const GLenum target, const glm::uvec2 &dimensions)
Texture create_bw_null_texture(const std::string &name, const GLenum target, const glm::uvec2 &dimensions)
Texture create_cubemap_texture(const std::vector<const char*> face_paths)

Framebuffer

struct glen::Framebuffer

Framebuffers are used to define a GL Framebuffer to render to. This has the option of being something other than the main window.

Public Functions

Framebuffer()

Constructor.

Framebuffer(const GLenum target)

Constructor with custom GL target. By default the target is GL_FRAMEBUFFER.

Parameters
  • target:

Framebuffer(const Framebuffer &other) = delete

Use move constructor instead of copy constructor.

Framebuffer(Framebuffer &&other)

Move constructor.

Framebuffer &operator=(const Framebuffer &other) = delete

Use move assign instead of copy assign.

Framebuffer &operator=(Framebuffer &&other)

Move assign.

~Framebuffer()
void process_texture(Texture *texture)

Attach texture to this framebuffer.

void process_texture(Texture *texture, GLint layer)

Attach texture to layer at index layer of this framebuffer.

void bind()

Make this framebuffer ‘active’ with regards to OpenGL.

void unbind()

Unbind this framebuffer.

bool check_bound_framebuffer()

Run diagnostics to check if this framebuffer is valid. If not, an appropriate message will be printed to the terminal.

Return

False if framebuffer not valid.

void init_color_attachments(GLuint amount, GLuint offset)

Initialize the buffer for all color attachments so that it can be rendered to.

Parameters
  • amount: The number of buffers to draw.

  • offset: The number of bytes to skip before sending data (for each buffer).

void blit_color_to_default(const glm::uvec2 &dimensions, const size_t loc)

Send all color output to the main window.

Parameters
  • dimensions: Dimensions of the window.

  • loc: The index of the texture.

void blit_stencil_to_default(const glm::uvec2 &dimensions)

Send the stencil buffer output to the main window.

Parameters
  • dimensions: Dimensions of the window.

void blit_depth_to_default(const glm::uvec2 &dimensions)

Send the depth buffer output to the main window.

Parameters
  • dimensions: Dimensions of the window.

const GLuint id() const

Return

The GL Framebuffer ID.

const GLenum target() const

Return

The GL Target.

const GLenum attachment() const

Return

The ‘type’ of framebuffer attachment (e.g. GL_DEPTH_ATTACHMENT).

const GLint level() const

Return

The mipmap level.

const GLuint num_color_textures() const

Return

The number of color textures associated with this framebuffer.

const Texture *color_texture_at(const size_t loc)

Return

The color texture at index loc.

Exceptions
  • Out: of bounds exception if loc is not valid.

const Texture *depth_texture()

Return

The depth texture assicated with this framebuffer (or NULL if it doesn’t exist).

const Texture *stencil_texture()

Return

The stencil texture associated with this framebuffer (or NULL if it doesn’t exist)

std::vector<const Texture*> color_textures()

Return

All color textures associated with this framebuffer.

void set_attachment(GLenum attachent)

Set the ‘type’ of the framebuffer attachment (eg GL_DEPTH_ATTACHMENT).

void push_back_color_buffer_textures(const std::vector<const Texture*> textures)

Append textures to the end of the existing list of textures.

void push_back_color_buffer_texture(const Texture *texture)

Append texture to the end of the existing list of textures.

Parameters
  • texture:

void set_color_buffer_texture(const Texture *texture, const GLuint loc)

Replace the existing texture at index loc with texture.

void set_color_buffer_texture(const Texture *texture)

Replace the first texture with texture.

void set_depth_buffer_texture(const Texture *texture)

Replace the existing depth texture with texture.

void set_stencil_buffer_texture(const Texture *texture)

Replace the existing stencil texture with texture.

Load Shader

GLuint glen::LoadShaders::load(const char *vertex_file_path, const char *fragment_file_path)

Construct a shader program from a vertex and fragment shader.

Return

The ID of the newly created shader program.

GLuint glen::LoadShaders::load(const char *vertex_file_path, const char *geometry_file_path, const char *fragment_file_path)

Construct a shader program from a vertex, geometry and fragment shader.

Return

The ID of the newly created shader program.

Shadow Map

struct glen::ShadowMap

Shadow map struct. For dealing with, creating and rendering of shadow maps

Public Functions

ShadowMap(LightNode *lightNode)

Constructor.

Parameters
  • lightNode: The LightNode that should be casting this shadow.

ShadowMap(LightNode *lightNode, const GLuint resolution)

Constructor.

Parameters
  • lightNode: The LightNode that should be casting this shadow.

  • resolution: The width and height dimensions of the shadow map.

ShadowMap(LightNode *lightNode, const glm::uvec2 &dimensions)

Constructor.

Parameters
  • lightNode: The LightNode that should be casting this shadow.

  • dimensions: uvec2 with width and height dimensions of the shadow map.

void init_materials(std::vector<Material*> &materials)

Set up a list of materials to be affected by this shadow map.

void update_materials(std::vector<Material*> &materials)

Update a list of materials with this shadow map.

void render_shadowMap(std::map<std::string, Node*> &root_nodes)

Render all pre-passes required for making the shadow map.

Parameters
  • root_nodes: All nodes that should be affected by this shadow. Children of the nodes will be traversed automatically and don’t need to be added seperately.

bool check_bound_framebuffer()

Run diagnostics to check if this framebuffer is valid. If not, an appropriate message will be printed to the terminal.

Return

False if framebuffer not valid.

bool is_directional()

Return

True if the light this shadow is associated with is a directional light.

bool is_point()

Return

True if the light this shadow is associated with is a point light.

const GLfloat bias() const

Return

The offset from the mesh from which the shadow is calculated.

const GLfloat radius() const

Return

The radius of the simulated light. The larger the radius, the softer the shadows. Keep in mind that a large radius will require more samples to look smooth.

const GLint num_samples() const

The square root of the number of samples with which to calculate the shadow.

A value of 3 means 9 samples are used.

const GLuint resolution() const

The width and height of the depth texture calculated by the shadow map.

void set_clip_near(const GLfloat)

Set the near clipping plane. Items closer than this to the light will not cast shadows.

void set_clip_far(const GLfloat)

Set the far clipping plane. Items further than this from the light will not cast shadows.

void set_bias(const GLfloat)

Set the offset from the mesh from which the shadow is calculated.

If the bias is too low, you’ll see artefacts known as ‘shadowmap acne’. Too high and the shadows won’t appear connected to the object casting the shadow. This often requires a bit of tweaking. Often a greater difference between the near and far clipping planes will require a smaller bias.

void set_radius(const GLfloat)

Set the radius of the simulated light. The larger the radius, the softer the shadows. Keep in mind that a large radius will require more samples to look smooth. This can get easily get very expensive to render.

void set_num_samples(const GLint)

Set the number of samples used to calculate the shadow.

The actual number of samples will be the square of this number so a number of 4 will reault in 16 pre-passes required to render the shadow map. As you can imagine, too many samples can easily slow things down considerably

Text 2D

struct glen::Text2D

Used for printing 2D text to the opengl screen

Public Functions

Text2D(const char *s_texture_path)

Constructor.

Parameters
  • s_texture_path: Path to an image file which contains appropriately spaced letters.

~Text2D()
void init(int s_x, int s_y, int size, int s_screen_width, int s_screen_height)

Initialize.

Parameters
  • s_x: The start screen x coordinate

  • s_y: The start screen y coordinate

  • size: Size of the letters.

  • s_screen_width: Screen width

  • s_screen_height: Screen height

void print(const char *s_text)

Print s_text to the window.

void draw()

Render existing text to the window.

void convert_string(const char *s_text)

Convert s_text to a renderable mesh.