/** * Copyright (c) 2021 Dominic Msters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #pragma once #include "../../libs.h" #include "../matrix.h" #include "../camera.h" #include "../texture.h" #include "../../util/array.h" #define SHADER_UNIFORM_NAME_MAX 24 #define SHADER_UNIFORM_COUNT 16 /** Representation of a shader uniform */ typedef GLuint shaderuniform_t; /** * Structure containing information about an OpenGL Shader. For simplicity sake * we demand certain uninforms to be present on the shader target. */ typedef struct { /** Pointer to an uploaded vertex shader program */ shaderuniform_t shaderVertex; /** Pointer to an uploaded fragment shader program */ GLuint shaderFrag; /** Pointer to an uploaded shader program linked */ GLuint shaderProgram; /** Buffer of chars where we store the uniform names */ char uniformBuffer[SHADER_UNIFORM_NAME_MAX * SHADER_UNIFORM_COUNT]; /** Array of strings (pointers to the above buffer) of the uniform names */ char *uniformNames[SHADER_UNIFORM_COUNT]; int32_t uniformCount; /** Type of each uniform */ GLenum types[SHADER_UNIFORM_COUNT]; /** Texture Slots (which texture slot for GL to use for each uniform) */ uint8_t textureSlots[SHADER_UNIFORM_COUNT]; } shader_t; /** * Compiles a shader from vertex and fragment shader code. * @param shader Shader to compile into. * @param vertexShaderSource The raw vertex shader code. * @param fragmentShaderSource The raw fragment shader code. */ void shaderInit(shader_t *shader, char *vertexShaderSource, char* fragmentShaderSource ); /** * Return the shaderuniform_t for a given shader uniform name. * * @param shader Shader to get from * @param name Name to look for * @return The shader uniform, or -1 if not found. */ shaderuniform_t shaderGetUniform(shader_t *shader, char *name); /** * Cleanup and unload a previously loaded shader. * @param shader The shader to unload */ void shaderDispose(shader_t *shader); /** * Attaches the supplied shader as the current shader. * @param shader The shader to attach */ void shaderUse(shader_t *shader); /** * Attaches a texture to the shader. * * @param shader Shader to attach to. * @param uniform Uniform on the shader to set. * @param texture Texture to attach. */ void shaderUseTexture( shader_t *shader, shaderuniform_t uniform, texture_t *texture ); /** * Set's a specific shader uniform to a matrix. * * @param shader Shader to apply to. * @param uniform Uniform on the shader to set. * @param matrix Matrix to apply. */ void shaderUseMatrix( shader_t *shader, shaderuniform_t uniform, matrix_t *matrix ); /** * Set's a specific shader uniform to a color. * * @param shader Shader to apply to. * @param uniform Uniform on the shader to set. * @param color Color to set on to the uniform. */ void shaderUseColor(shader_t *shader, shaderuniform_t uniform, pixel_t color); /** * Set's the current translation matrix onto the shader for the next * render to use. Rotation order is set to YZX. * * @param shader Shader to attach to. * @param uniform Uniform on the shader to set. * @param x X coordinate (world space). * @param y Y coordinate (world space). * @param z Z coordinate (world space). * @param pitch Pitch of the object (local space). * @param yaw Yaw of the object (local space). * @param roll Roll of the object (local space). */ void shaderUsePosition( shader_t *shader, shaderuniform_t uniform, float x, float y, float z, float pitch, float yaw, float roll ); /** * Set's the current translation matrix onto the shader for the next * render to use. Also provides scaling controls. * * @param shader Shader to attach to. * @param uniform Uniform on the shader to set. * @param x X coordinate (world space). * @param y Y coordinate (world space). * @param z Z coordinate (world space). * @param pitch Pitch of the object (local space). * @param yaw Yaw of the object (local space). * @param roll Roll of the object (local space). * @param scaleX X scale of model (1 being 100% scaled). * @param scaleY Y scale of model (1 being 100% scaled). * @param scaleZ Z scale of model (1 being 100% scaled). */ void shaderUsePositionAndScale( shader_t *shader, shaderuniform_t uniform, float x, float y, float z, float pitch, float yaw, float roll, float scaleX, float scaleY, float scaleZ ); /** * Attaches a camera to the shader. * * @param shader Shader to attach to. * @param uniformView Shader Uniform for the view matrix. * @param uniformProjection Shader Uniform for the view matrix. * @param camera Camera to attach. */ void shaderUseCamera( shader_t *shader, shaderuniform_t uniformView, shaderuniform_t uniformProjection, camera_t *camera );