diff options
Diffstat (limited to 'engine-ocean/Graphics/GLWrappers')
-rw-r--r-- | engine-ocean/Graphics/GLWrappers/shader.cpp | 121 | ||||
-rw-r--r-- | engine-ocean/Graphics/GLWrappers/shader.h | 28 | ||||
-rw-r--r-- | engine-ocean/Graphics/GLWrappers/texture.cpp | 91 | ||||
-rw-r--r-- | engine-ocean/Graphics/GLWrappers/texture.h | 31 | ||||
-rw-r--r-- | engine-ocean/Graphics/GLWrappers/vao.cpp | 68 | ||||
-rw-r--r-- | engine-ocean/Graphics/GLWrappers/vao.h | 42 | ||||
-rw-r--r-- | engine-ocean/Graphics/GLWrappers/vbo.cpp | 27 | ||||
-rw-r--r-- | engine-ocean/Graphics/GLWrappers/vbo.h | 19 |
8 files changed, 427 insertions, 0 deletions
diff --git a/engine-ocean/Graphics/GLWrappers/shader.cpp b/engine-ocean/Graphics/GLWrappers/shader.cpp new file mode 100644 index 0000000..4ae1aa2 --- /dev/null +++ b/engine-ocean/Graphics/GLWrappers/shader.cpp @@ -0,0 +1,121 @@ +#include "shader.h" + +Shader::Shader(std::vector<GLenum> shaderTypes, std::vector<const char*> filepaths){ + m_handle = ShaderLoader::createShaderProgram(shaderTypes, filepaths); +} + +Shader::~Shader(){ + glDeleteProgram(m_handle); +} + +void Shader::bind(){ + glUseProgram(m_handle); +} + +void Shader::unbind(){ + glUseProgram(0); +} + +GLuint Shader::getHandle(){ + return m_handle; +} + +void Shader::setMaterial(std::shared_ptr<Material> material){ + ColorSource color_source = material->getColorSource(); + switch(color_source){ + case ColorSource::SOLID_COLOR: + glUniform1i(glGetUniformLocation(m_handle, "colorSource"), 0); + glUniform3f(glGetUniformLocation(m_handle, "objColor"), material->getColor().r, material->getColor().g, material->getColor().b); + break; + case ColorSource::TEXTURE_COLOR: + glUniform1i(glGetUniformLocation(m_handle, "colorSource"), 1); + material->getTexture()->bind(); + glUniform1i(glGetUniformLocation(m_handle, "objTexture"), material->getTexture()->getTexUnitUint()); + break; + case ColorSource::PER_VERTEX_COLOR: + glUniform1i(glGetUniformLocation(m_handle, "colorSource"), 2); + break; + } + float shininess = material->getShininess(); + glUniform1f(glGetUniformLocation(m_handle, "shininess"), shininess); +} + +void Shader::setCamera(std::shared_ptr<Camera> camera){ + glUniformMatrix4fv(glGetUniformLocation(m_handle, "view"), 1, GL_FALSE, glm::value_ptr(camera->getView()[0])); + glUniformMatrix4fv(glGetUniformLocation(m_handle, "projection"), 1, GL_FALSE, glm::value_ptr(camera->getProjection()[0])); + glUniform3f(glGetUniformLocation(m_handle, "worldSpace_camPos"), camera->getPos().x, camera->getPos().y, camera->getPos().z); + glUniform3f(glGetUniformLocation(m_handle, "skyColor"), 1.f, 1.f, 1.f); + +} + +void Shader::setModelTransform(std::shared_ptr<ModelTransform> modelTransform){ + glUniformMatrix4fv(glGetUniformLocation(m_handle, "model"), 1, GL_FALSE, glm::value_ptr(modelTransform->getModelMatrix()[0])); +} + +void Shader::setModelTransform(glm::mat4 modelMatrix){ + glUniformMatrix4fv(glGetUniformLocation(m_handle, "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); +} + +void Shader::setGlobalCoeffs(glm::vec3 coeffs){ + glUniform3f(glGetUniformLocation(m_handle, "coeffs"), coeffs.x, coeffs.y, coeffs.z); +} + +void Shader::setLights(std::vector<std::shared_ptr<Light>> lights){ + int numLights = std::min(int(lights.size()), 16); + std::vector<int> lightType; + std::vector<float> lightColor; + std::vector<float> lightFunction; + std::vector<float> worldSpace_lightPos; + std::vector<float> worldSpace_lightDir; + lightType.resize(numLights); + lightColor.resize(numLights*3); + lightFunction.resize(numLights*3); + worldSpace_lightPos.resize(numLights*3); + worldSpace_lightDir.resize(numLights*3); + for(int i = 0; i<numLights; i++){ + lightColor[i*3] = lights[i]->getColor().r; + lightColor[i*3+1] = lights[i]->getColor().g; + lightColor[i*3+2] = lights[i]->getColor().b; + glm::vec3 camLightData; + switch(lights[i]->getType()){ + case LightType::POINT: + lightType[i] = 0; + lightFunction[i*3] = lights[i]->getAttenuation().x; + lightFunction[i*3+1] = lights[i]->getAttenuation().y; + lightFunction[i*3+2] = lights[i]->getAttenuation().z; + worldSpace_lightPos[i*3] = lights[i]->getPos().x; + worldSpace_lightPos[i*3+1] = lights[i]->getPos().y; + worldSpace_lightPos[i*3+2] = lights[i]->getPos().z; + break; + case LightType::DIRECTIONAL: + lightType[i] = 1; + worldSpace_lightDir[i*3] = lights[i]->getDir().x; + worldSpace_lightDir[i*3+1] = lights[i]->getDir().y; + worldSpace_lightDir[i*3+2] = lights[i]->getDir().z; + break; + } + } + glUniform1i(glGetUniformLocation(m_handle, "numLights"), numLights); + Debug::checkGLError(); + glUniform1iv(glGetUniformLocation(m_handle, "lightType"), numLights, lightType.data()); + Debug::checkGLError(); + glUniform3fv(glGetUniformLocation(m_handle, "lightColor"), numLights, lightColor.data()); + Debug::checkGLError(); + glUniform3fv(glGetUniformLocation(m_handle, "lightFunction"), numLights, lightFunction.data()); + Debug::checkGLError(); + glUniform3fv(glGetUniformLocation(m_handle, "worldSpace_lightPos"), numLights, worldSpace_lightPos.data()); + Debug::checkGLError(); + std::cout<<worldSpace_lightDir[0]<<std::endl; + glUniform3fv(glGetUniformLocation(m_handle, "worldSpace_lightDir"), numLights, worldSpace_lightDir.data()); + Debug::checkGLError(); +} + +void Shader::clearLights(){ + glUniform1i(glGetUniformLocation(m_handle, "numLights"), 0); +} + +void Shader::setTextUniforms(float screenWidth, float screenHeight, glm::vec3 color){ + glm::mat4 projection = glm::ortho(0.0f, screenWidth, 0.0f, screenHeight); + glUniformMatrix4fv(glGetUniformLocation(m_handle, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); + glUniform3f(glGetUniformLocation(m_handle, "textColor"), color.r, color.g, color.b); +} diff --git a/engine-ocean/Graphics/GLWrappers/shader.h b/engine-ocean/Graphics/GLWrappers/shader.h new file mode 100644 index 0000000..da0ae5d --- /dev/null +++ b/engine-ocean/Graphics/GLWrappers/shader.h @@ -0,0 +1,28 @@ +#pragma once + +#include "../shaderloader.h" +#include "../camera.h" +#include "../material.h" +#include "../modeltransform.h" +#include "../light.h" + +class Shader{ +public: + Shader(std::vector<GLenum> shaderTypes, std::vector<const char*> filepaths); + ~Shader(); + + void bind(); + void unbind(); + GLuint getHandle(); + void setMaterial(std::shared_ptr<Material> material); + void setCamera(std::shared_ptr<Camera> camera); + void setModelTransform(std::shared_ptr<ModelTransform> modelTransform); + void setModelTransform(glm::mat4 modelMatrix); + void setGlobalCoeffs(glm::vec3 coeffs); + void setLights(std::vector<std::shared_ptr<Light>> lights); + void clearLights(); + void setTextUniforms(float screenWidth, float screenHeight, glm::vec3 color); + +private: + GLuint m_handle; +}; diff --git a/engine-ocean/Graphics/GLWrappers/texture.cpp b/engine-ocean/Graphics/GLWrappers/texture.cpp new file mode 100644 index 0000000..1c76165 --- /dev/null +++ b/engine-ocean/Graphics/GLWrappers/texture.cpp @@ -0,0 +1,91 @@ +#include "texture.h" + +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" + +#include <iostream> + +#include "../debug.h" + +Texture::Texture(int width, int height, GLenum texUnit, GLint internalFormat, GLenum texTarget): + m_texTarget(texTarget), + m_texUnit(texUnit) +{ + glGenTextures(1, &m_handle); + bind(); + glTexParameteri(m_texTarget, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(m_texTarget, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(m_texTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(m_texTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexImage2D(m_texTarget, 0, internalFormat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + m_height = height; + m_width = width; + unbind(); +} + +Texture::Texture(std::string filepath, GLenum texUnit, GLint internalFormat, GLenum texTarget): + m_texTarget(texTarget), + m_texUnit(texUnit) +{ + glGenTextures(1, &m_handle); + bind(); + glTexParameteri(m_texTarget, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(m_texTarget, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(m_texTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(m_texTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + stbi_set_flip_vertically_on_load(1); + int width, height, numChannels; + unsigned char* data = stbi_load(filepath.c_str(), &width, &height, &numChannels, 4); + if (stbi_failure_reason()){ + std::cout << stbi_failure_reason() << std::endl; + } + glTexImage2D(m_texTarget, 0, internalFormat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + m_height = height; + m_width = width; + stbi_image_free(data); + unbind(); +} + +Texture::~Texture(){ + glDeleteTextures(1, &m_handle); +} + +void Texture::bind(){ + glActiveTexture(m_texUnit); + glBindTexture(m_texTarget, m_handle); +} + +void Texture::bind(GLenum texUnit){ + glActiveTexture(texUnit); + glBindTexture(m_texTarget, m_handle); +} + +void Texture::unbind(){ + glActiveTexture(m_texUnit); + glBindTexture(m_texTarget, 0); +} + +void Texture::unbind(GLenum texUnit){ + glActiveTexture(texUnit); + glBindTexture(m_texTarget, 0); +} + +GLuint Texture::getHandle(){ + return m_handle; +} + +GLuint Texture::getTexUnitUint(){ + return GLuint(m_texUnit) - GLuint(GL_TEXTURE0); +} + +GLenum Texture::getTexUnitEnum(){ + return m_texUnit; +} + +int Texture::getWidth(){ + return m_width; +} + +int Texture::getHeight(){ + return m_width; +} diff --git a/engine-ocean/Graphics/GLWrappers/texture.h b/engine-ocean/Graphics/GLWrappers/texture.h new file mode 100644 index 0000000..3ad2f13 --- /dev/null +++ b/engine-ocean/Graphics/GLWrappers/texture.h @@ -0,0 +1,31 @@ +#pragma once + +#include "GL/glew.h" +#include <string> + +#include "glm/glm.hpp" + +class Texture +{ +public: + Texture(int width, int height, GLenum texUnit = GL_TEXTURE0, GLint internalFormat = GL_RGBA, GLenum texTarget = GL_TEXTURE_2D); + Texture(std::string filePath, GLenum texUnit = GL_TEXTURE0, GLint internalFormat = GL_RGBA, GLenum texTarget = GL_TEXTURE_2D); + ~Texture(); + + void bind(); + void bind(GLenum texUnit); + void unbind(); + void unbind(GLenum texUnit); + GLuint getHandle(); + GLuint getTexUnitUint(); + GLenum getTexUnitEnum(); + int getWidth(); + int getHeight(); + +private: + GLuint m_handle; + GLenum m_texUnit; + GLenum m_texTarget; + int m_width; + int m_height; +}; diff --git a/engine-ocean/Graphics/GLWrappers/vao.cpp b/engine-ocean/Graphics/GLWrappers/vao.cpp new file mode 100644 index 0000000..43a1062 --- /dev/null +++ b/engine-ocean/Graphics/GLWrappers/vao.cpp @@ -0,0 +1,68 @@ +#include "vao.h" +#include <iostream> + +VAO::VAO(std::shared_ptr<VBO> vbo, VAOAttrib attribs): + m_vbo(vbo), + m_curr_offset(0), + m_vert_size(0) +{ + glGenVertexArrays(1, &m_handle); + bind(); + + if(attribs & VAOAttrib::POS){ + m_vert_size += 3; + } + if(attribs & VAOAttrib::NORM){ + m_vert_size += 3; + } + if(attribs & VAOAttrib::UV){ + m_vert_size += 2; + } + if(attribs & VAOAttrib::COLOR){ + m_vert_size += 3; + } + + + //Attach layout to vbo depending on which of the above attributes it has + m_vbo->bind(); + + if(attribs & VAOAttrib::POS){ + addAttribute(0, 3); + } + if(attribs & VAOAttrib::NORM){ + addAttribute(1, 3); + } + if(attribs & VAOAttrib::UV){ + addAttribute(2, 2); + } + if(attribs & VAOAttrib::COLOR){ + addAttribute(3, 3); + } + + m_vbo->unbind(); + unbind(); +} + +VAO::~VAO(){ + glDeleteVertexArrays(1, &m_handle); +} + +void VAO::bind(){ + glBindVertexArray(m_handle); +} + +void VAO::unbind(){ + glBindVertexArray(0); +} + +void VAO::draw(){ + bind(); + glDrawArrays(GL_TRIANGLES, 0, m_vbo->getLength()/m_vert_size); + unbind(); +} + +void VAO::addAttribute(GLuint attrib_index, GLint attrib_size){ + glEnableVertexAttribArray(attrib_index); + glVertexAttribPointer(attrib_index, attrib_size, GL_FLOAT, GL_FALSE, m_vert_size*sizeof(GLfloat), reinterpret_cast<void*>(m_curr_offset*sizeof(GLfloat))); + m_curr_offset += attrib_size; +}
\ No newline at end of file diff --git a/engine-ocean/Graphics/GLWrappers/vao.h b/engine-ocean/Graphics/GLWrappers/vao.h new file mode 100644 index 0000000..8f9bc6a --- /dev/null +++ b/engine-ocean/Graphics/GLWrappers/vao.h @@ -0,0 +1,42 @@ +#pragma once + +#include "vbo.h" +#include <memory> + +enum class VAOAttrib{ + POS = 1, + NORM = 2, + UV = 4, + COLOR = 8 +}; + +inline VAOAttrib operator|(VAOAttrib lhs, VAOAttrib rhs){ + using VAOAttribType = std::underlying_type<VAOAttrib>::type; + return VAOAttrib(static_cast<VAOAttribType>(lhs) | static_cast<VAOAttribType>(rhs)); +} + +inline bool operator&(VAOAttrib lhs, VAOAttrib rhs){ + using VAOAttribType = std::underlying_type<VAOAttrib>::type; + return static_cast<bool>(VAOAttrib(static_cast<VAOAttribType>(lhs) & static_cast<VAOAttribType>(rhs))); +} + +class VAO +{ +public: + VAO(std::shared_ptr<VBO> vbo, VAOAttrib attribs); + ~VAO(); + + void bind(); + void unbind(); + + void draw(); + +private: + void addAttribute(GLuint attrib_index, GLint attrib_size); + + std::shared_ptr<VBO> m_vbo; + GLuint m_handle; + GLint m_curr_offset; + GLuint m_vert_size; + +};
\ No newline at end of file diff --git a/engine-ocean/Graphics/GLWrappers/vbo.cpp b/engine-ocean/Graphics/GLWrappers/vbo.cpp new file mode 100644 index 0000000..a8d08fb --- /dev/null +++ b/engine-ocean/Graphics/GLWrappers/vbo.cpp @@ -0,0 +1,27 @@ +#include "vbo.h" +#include <iostream> + +VBO::VBO(std::vector<float> data): + m_length(data.size()) +{ + glGenBuffers(1, &m_handle); + bind(); + glBufferData(GL_ARRAY_BUFFER, m_length*sizeof(float), data.data(), GL_STATIC_DRAW); + unbind(); +} + +VBO::~VBO(){ + glDeleteBuffers(1, &m_handle); +} + +void VBO::bind(){ + glBindBuffer(GL_ARRAY_BUFFER, m_handle); +} + +void VBO::unbind(){ + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +GLsizei VBO::getLength(){ + return m_length; +}
\ No newline at end of file diff --git a/engine-ocean/Graphics/GLWrappers/vbo.h b/engine-ocean/Graphics/GLWrappers/vbo.h new file mode 100644 index 0000000..bd63877 --- /dev/null +++ b/engine-ocean/Graphics/GLWrappers/vbo.h @@ -0,0 +1,19 @@ +#pragma once + +#include <GL/glew.h> +#include <vector> + +class VBO +{ +public: + VBO(std::vector<float> data); + ~VBO(); + + void bind(); + void unbind(); + GLsizei getLength(); + +private: + GLuint m_handle; + GLsizei m_length; +};
\ No newline at end of file |