diff options
author | Sebastian Park <51029066+Seb-Park@users.noreply.github.com> | 2024-04-22 00:59:29 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-22 00:59:29 -0400 |
commit | cd7c76017a12bb548036571c1ff13e551369d06d (patch) | |
tree | 03cd022c7625c5c5682d21c20b0a8b8532e57140 /src/graphics | |
parent | 5233a708a165ba8a3153e054ce74eb11084c0158 (diff) | |
parent | 28d74097815a8d52b8f47f6eae6464005a6bc552 (diff) |
Merge pull request #2 from Seb-Park/shaders
Shaders
Diffstat (limited to 'src/graphics')
-rw-r--r-- | src/graphics/shader.cpp | 8 | ||||
-rw-r--r-- | src/graphics/shader.h | 2 | ||||
-rw-r--r-- | src/graphics/shape.cpp | 44 | ||||
-rw-r--r-- | src/graphics/shape.h | 6 |
4 files changed, 60 insertions, 0 deletions
diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 6ac9949..161eeae 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -83,6 +83,14 @@ void Shader::setUniform(const std::string &name, bool b) { glUniform1i(m_uniforms[name], static_cast<GLint>(b)); } +void Shader::setUniform(const std::string &name, Eigen::Vector3f v) { + glUniform3f(m_uniforms[name], static_cast<GLfloat>(v[0]), static_cast<GLfloat>(v[1]), static_cast<GLfloat>(v[2])); +} + +void Shader::setUniform(const std::string &name, float a, float b) { + glUniform2f(m_uniforms[name], static_cast<GLfloat>(a), static_cast<GLfloat>(b)); +} + void Shader::setUniformArrayByIndex(const std::string &name, float f, size_t index) { glUniform1f(m_uniformArrays[std::make_tuple(name, index)], f); } diff --git a/src/graphics/shader.h b/src/graphics/shader.h index bc3c0c1..7e9dad4 100644 --- a/src/graphics/shader.h +++ b/src/graphics/shader.h @@ -32,6 +32,8 @@ public: void setUniform(const std::string &name, float f); void setUniform(const std::string &name, int i); void setUniform(const std::string &name, bool b); + void setUniform(const std::string &name, Eigen::Vector3f v); + void setUniform(const std::string &name, float a, float b); void setUniformArrayByIndex(const std::string &name, float f, size_t index); void setUniformArrayByIndex(const std::string &name, const Eigen::Vector2f &vec2, size_t index); diff --git a/src/graphics/shape.cpp b/src/graphics/shape.cpp index 9459306..c5dde0d 100644 --- a/src/graphics/shape.cpp +++ b/src/graphics/shape.cpp @@ -1,6 +1,7 @@ #include "shape.h" #include <iostream> +//#include <QImage> #include "graphics/shader.h" using namespace Eigen; @@ -126,8 +127,12 @@ void Shape::draw(Shader *shader, GLenum mode) shader->setUniform("blue", m_blue); shader->setUniform("alpha", m_alpha); glBindVertexArray(m_surfaceVao); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, ocean_floor_texture); + shader->setUniform("sampler", 1); glDrawElements(mode, m_numSurfaceVertices, GL_UNSIGNED_INT, reinterpret_cast<GLvoid *>(0)); glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D, 0); break; } case GL_POINTS: @@ -276,3 +281,42 @@ void Shape::updateMesh(const std::vector<Eigen::Vector3i> &faces, } } +void Shape::initGroundPlane(std::string texturePath, float depth, Shader* shader) { + + // Prepare filepath + QString ocean_floor_filepath = QString(texturePath.c_str()); + + // TASK 1: Obtain image from filepath + this->ocean_floor_image = QImage(ocean_floor_filepath); + + // TASK 2: Format image to fit OpenGL + ocean_floor_image = ocean_floor_image.convertToFormat(QImage::Format_RGBA8888).mirrored(); + auto bits = this->ocean_floor_image.bits(); + auto dat = ocean_floor_image.data_ptr(); + + // TASK 3: Generate texture + glGenTextures(1, &ocean_floor_texture); + + // TASK 9: Set the active texture slot to texture slot 0 + glActiveTexture(GL_TEXTURE0); + + // TASK 4: Bind kitten texture + glBindTexture(GL_TEXTURE_2D, ocean_floor_texture); + + // TASK 5: Load image into kitten texture + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ocean_floor_image.width(), ocean_floor_image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, ocean_floor_image.bits()); + + // TASK 6: Set min and mag filters' interpolation mode to linear + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // TASK 7: Unbind kitten texture + glBindTexture(GL_TEXTURE_2D, 0); + + // TASK 10: set the texture.frag uniform for our texture + glUseProgram(shader->id()); +// glUniform1i(glGetUniformLocation(shader->id(), "sampler"), 0); + shader->setUniform("sampler", 0); + glUseProgram(0); +} + diff --git a/src/graphics/shape.h b/src/graphics/shape.h index 1451c85..b909606 100644 --- a/src/graphics/shape.h +++ b/src/graphics/shape.h @@ -3,6 +3,8 @@ #include <GL/glew.h> #include <vector> #include <unordered_set> +#include <QString> +#include <QImage> #define EIGEN_DONT_VECTORIZE #define EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT @@ -33,6 +35,8 @@ public: void setColor(float r, float g, float b); + void initGroundPlane(std::string texturePath, float depth, Shader* shader); + void draw(Shader *shader, GLenum mode); SelectMode select(Shader *shader, int vertex); bool selectWithSpecifiedMode(Shader *shader, int vertex, SelectMode mode); @@ -47,6 +51,8 @@ private: GLuint m_surfaceVao; GLuint m_surfaceVbo; GLuint m_surfaceIbo; + GLuint ocean_floor_texture; + QImage ocean_floor_image; unsigned int m_numSurfaceVertices; unsigned int m_verticesSize; |