diff options
author | Sebastian Park <SebPark03@gmail.com> | 2024-04-24 03:29:09 -0400 |
---|---|---|
committer | Sebastian Park <SebPark03@gmail.com> | 2024-04-24 03:29:09 -0400 |
commit | ef8abb44ca5cbf62a24b265aedc62f1348ae8091 (patch) | |
tree | 51dec7207123017d2012722d7acdfdb7ba2857ae | |
parent | d2356bae7beb968ad333f3baf21b4830d647f8ce (diff) |
ADD MULTIPLE TEXTURES AFTER MANY FREAKING HOURS.
-rwxr-xr-x | resources/shaders/shader.frag | 11 | ||||
-rwxr-xr-x | resources/shaders/shader.vert | 14 | ||||
-rwxr-xr-x | src/glwidget.cpp | 3 | ||||
-rw-r--r-- | src/graphics/shape.cpp | 43 | ||||
-rw-r--r-- | src/graphics/shape.h | 3 |
5 files changed, 61 insertions, 13 deletions
diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag index 80a65a2..0697359 100755 --- a/resources/shaders/shader.frag +++ b/resources/shaders/shader.frag @@ -5,6 +5,7 @@ in vec3 normal_cameraSpace; in vec3 camera_worldSpace; in vec3 normal_worldSpace; in vec3 pos; +in vec3 reflPos; in vec3 refrPos; in float refrProb; in vec2 uv; @@ -18,12 +19,16 @@ uniform float red = 1.0; uniform float green = 1.0; uniform float blue = 1.0; uniform float alpha = 1.0; +//layout(binding = 0) uniform sampler2D groundSampler; +//layout(binding = 1) uniform sampler2D skySampler; uniform sampler2D groundSampler; uniform sampler2D skySampler; uniform vec2 widthBounds; uniform vec2 lengthBounds; //uniform float test = 0; +// Random methods from https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83 + float rand(vec2 n) { return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453); } @@ -63,6 +68,8 @@ void main() { // fragColor = vec4(fragColor.x, 0.f, fragColor.z, 1.f); // fragColor = vec4(test, test, test, 1.f); vec2 refrUV = uvFromWorldPoint(refrPos); + vec2 reflUV = uvFromWorldPoint(reflPos); + float waterMurkiness = 0.002f; // TODO: Make uniform vec3 waterVolumeColor = vec3(red * 0.1f, green * 0.2f, blue * 0.2f); float murkDiffuse = 0.3f; @@ -79,6 +86,7 @@ void main() { vec4 transmissive = texture(groundSampler, vec2(refrUVBlurry)); vec4 murk = (vec4(waterVolumeColor * d * murkDiffuse + waterVolumeColor * murkAmbient, 1.0f)); + vec4 skyRefl = texture(skySampler, vec2(reflUV)); // refrProb *= beerAtt; fragColor = 0.75f * diffuse; // Diffuse @@ -87,6 +95,9 @@ void main() { fragColor = vec4(red * .2f, green * .2f, blue * .2f,1.f); fragColor += 1.f * specular; // Specular TODO: Pass multiplications as uniforms. fragColor = clamp(fragColor, 0.f, 1.f); // Clamp + fragColor += 0.2f * skyRefl * vec4(0.8f, 0.9f, 1.f, 1.f); + fragColor = clamp(fragColor, 0.f, 1.f); // Clamp + fragColor *= ((1 - refrProb) / 1.f); vec4 volumetric = beerAtt * transmissive; diff --git a/resources/shaders/shader.vert b/resources/shaders/shader.vert index 7a7dde5..e78ddc6 100755 --- a/resources/shaders/shader.vert +++ b/resources/shaders/shader.vert @@ -18,6 +18,7 @@ out vec3 normal_cameraSpace; out vec3 normal_worldSpace; out vec3 camera_worldSpace; out vec3 pos; +out vec3 reflPos; out vec3 refrPos; out float refrProb; out vec2 uv; @@ -53,6 +54,17 @@ vec4 getRefrPos() { } } +vec3 getReflPos() { + float depth = 5000.f; // TODO: Pass as uniform + vec3 w_o = normalize(pos - camera_worldSpace); + vec3 reflectedRay = 2 * dot(-w_o, normal_worldSpace) * normal_worldSpace + w_o; + + float dist = depth - position.y; + float depthScale = dist / reflectedRay.y; + vec3 skyContactPoint = (reflectedRay * depthScale) + position; + return skyContactPoint; +} + void main() { // float depth = -4.f; // float dist = position.y - depth; @@ -75,5 +87,7 @@ void main() { refrPos = vec3(refrPos_and_prob); refrProb = clamp(refrPos_and_prob.w, 0.f, 1.f); + reflPos = getReflPos(); + gl_Position = proj * view * model * vec4(position, 1); } diff --git a/src/glwidget.cpp b/src/glwidget.cpp index a9fe4b1..f39b613 100755 --- a/src/glwidget.cpp +++ b/src/glwidget.cpp @@ -95,7 +95,8 @@ void GLWidget::initializeGL() // -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // 1.0f, -1.0f, 0.0f, 1.0f, 0.0f // }; - m_arap.initGroundPlane(":/resources/images/anamorphic.jpg", 2, m_defaultShader); + m_arap.initSkyPlane(":/resources/images/uv1.png", 2, m_defaultShader); + m_arap.initGroundPlane(":/resources/images/daniel.jpg", 2, m_defaultShader); // // Generate and bind a VBO and a VAO for a fullscreen quad // glGenBuffers(1, &m_fullscreen_vbo); diff --git a/src/graphics/shape.cpp b/src/graphics/shape.cpp index e3612e5..f983848 100644 --- a/src/graphics/shape.cpp +++ b/src/graphics/shape.cpp @@ -133,9 +133,24 @@ void Shape::setColor(float r, float g, float b) { void Shape::draw(Shader *shader, GLenum mode) { + // Texture problem + // Not that the texture is not being loaded, because if we bind m_sky_texture it works + // Not that one texture is overwriting the other, because if we just load sky it doesn't work + // Draws whatever is bound to texture0 no matter what. // Drawing the ground texture. glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_ground_texture); +// glBindTexture(GL_TEXTURE_2D, 0); + shader->setUniform("groundSampler", 0); + glUniform1i(glGetUniformLocation(shader->id(), "groundSampler"), 0); + + // https://stackoverflow.com/questions/67277087/opengl-glsl-multiple-texture-binding-not-working + // FIGURED OUT THE PROBLEM. it was that SAMPLERS WERE DEFAULTING TO SLOT 0 AND SETUNIFORM WASN'T WORKING + // BECAUSE IT WAS CALLING SETUNIFORM WITH FREAKING FLOATS. WHAT THE FRICK C++. + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, m_sky_texture); + shader->setUniform("skySampler", 1); + glUniform1i(glGetUniformLocation(shader->id(), "skySampler"), 1); Eigen::Matrix3f m3 = m_modelMatrix.topLeftCorner(3, 3); Eigen::Matrix3f inverseTransposeModel = m3.inverse().transpose(); @@ -151,9 +166,6 @@ 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", 0); glDrawElements(mode, m_numSurfaceVertices, GL_UNSIGNED_INT, reinterpret_cast<GLvoid *>(0)); glBindVertexArray(0); // glBindTexture(GL_TEXTURE_2D, 0); @@ -169,6 +181,10 @@ void Shape::draw(Shader *shader, GLenum mode) break; } } + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, 0); + + glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); } @@ -359,49 +375,52 @@ void Shape::initGroundPlane(std::string texturePath, float depth, Shader* shader 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 shader->bind(); shader->setUniform("groundSampler", 0); shader->unbind(); + + glBindTexture(GL_TEXTURE_2D, 0); } void Shape::initSkyPlane(std::string texturePath, float height, Shader* shader) { //TODO: Complete - QString ground_texture_filepath = QString(texturePath.c_str()); + QString sky_texture_filepath = QString(texturePath.c_str()); // TASK 1: Obtain image from filepath - m_ground_image = QImage(ground_texture_filepath); + m_sky_image = QImage(sky_texture_filepath); // TASK 2: Format image to fit OpenGL - m_ground_image = m_ground_image.convertToFormat(QImage::Format_RGBA8888).mirrored(); + m_sky_image = m_sky_image.convertToFormat(QImage::Format_RGBA8888).mirrored(); - auto bits = m_ground_image.bits(); + auto bits = m_sky_image.bits(); // TASK 3: Generate kitten texture - glGenTextures(1, &m_ground_texture); + glGenTextures(1, &m_sky_texture); // TASK 9: Set the active texture slot to texture slot 0 glActiveTexture(GL_TEXTURE1); // TASK 4: Bind kitten texture - glBindTexture(GL_TEXTURE_2D, m_ground_texture); + glBindTexture(GL_TEXTURE_2D, m_sky_texture); // TASK 5: Load image into kitten texture - glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, m_ground_image.width(), m_ground_image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, m_ground_image.bits()); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_sky_image.width(), m_sky_image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, m_sky_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 shader->bind(); shader->setUniform("skySampler", 1); shader->unbind(); + + glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/src/graphics/shape.h b/src/graphics/shape.h index b993b28..2915190 100644 --- a/src/graphics/shape.h +++ b/src/graphics/shape.h @@ -57,6 +57,9 @@ private: GLuint m_ground_texture; QImage m_ground_image; + GLuint m_sky_texture; + QImage m_sky_image; + unsigned int m_numSurfaceVertices; unsigned int m_verticesSize; float m_red; |