From f2d61fc06387ccb22ecb5cb6c42210736ac64c9f Mon Sep 17 00:00:00 2001 From: Sebastian Park Date: Tue, 23 Apr 2024 00:30:37 -0400 Subject: Get ground textures working, but not yet abstracted to the shape class. --- .DS_Store | Bin 10244 -> 10244 bytes CMakeLists.txt | 5 ++ CMakeLists.txt.user | 2 +- resources/images/sand_text.jpg | Bin 0 -> 23150 bytes resources/images/uv1 copy.png | Bin 48020 -> 0 bytes resources/images/uv1.png | Bin 86064 -> 48020 bytes resources/images/uv1_cut.png | Bin 0 -> 86064 bytes resources/shaders/shader.frag | 23 +++++++- resources/shaders/shader.vert | 14 ++--- resources/shaders/texture.frag | 24 ++++++++ resources/shaders/texture.vert | 15 +++++ src/glwidget.cpp | 122 ++++++++++++++++++++++++++++++++++++----- src/glwidget.h | 7 +++ src/graphics/shape.cpp | 8 +-- 14 files changed, 193 insertions(+), 27 deletions(-) create mode 100644 resources/images/sand_text.jpg delete mode 100644 resources/images/uv1 copy.png create mode 100644 resources/images/uv1_cut.png create mode 100644 resources/shaders/texture.frag create mode 100644 resources/shaders/texture.vert diff --git a/.DS_Store b/.DS_Store index 8e365cf..a6de465 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/CMakeLists.txt b/CMakeLists.txt index 35ac139..cdacb7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,11 +84,16 @@ qt6_add_resources(${PROJECT_NAME} "Resources" FILES resources/images/uv1.png resources/images/kitten.png + resources/images/hello.png + resources/images/sand_text.jpg +# resources/images/ground.png resources/shaders/shader.frag resources/shaders/shader.vert resources/shaders/anchorPoint.vert resources/shaders/anchorPoint.geom resources/shaders/anchorPoint.frag + resources/shaders/texture.vert + resources/shaders/texture.frag ) # GLEW: this provides support for Windows (including 64-bit) diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user index 24cd5bb..fdc72cf 100644 --- a/CMakeLists.txt.user +++ b/CMakeLists.txt.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/resources/images/sand_text.jpg b/resources/images/sand_text.jpg new file mode 100644 index 0000000..6a64486 Binary files /dev/null and b/resources/images/sand_text.jpg differ diff --git a/resources/images/uv1 copy.png b/resources/images/uv1 copy.png deleted file mode 100644 index fcf4825..0000000 Binary files a/resources/images/uv1 copy.png and /dev/null differ diff --git a/resources/images/uv1.png b/resources/images/uv1.png index 82ff8ac..fcf4825 100644 Binary files a/resources/images/uv1.png and b/resources/images/uv1.png differ diff --git a/resources/images/uv1_cut.png b/resources/images/uv1_cut.png new file mode 100644 index 0000000..82ff8ac Binary files /dev/null and b/resources/images/uv1_cut.png differ diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag index 82a0387..2e4e780 100755 --- a/resources/shaders/shader.frag +++ b/resources/shaders/shader.frag @@ -20,6 +20,20 @@ uniform vec2 widthBounds; uniform vec2 lengthBounds; //uniform float test = 0; +float rand(vec2 n) { + return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453); +} + +float rand(float n) { + return fract(sin(n) * 43758.5453123); +} + +float rand(vec4 n) { +// vec2 first2 = vec2(n[0], n[1]); +//// return 1.f; + return rand(vec2(n[0] * rand(n[2]), n[1] * rand(n[3]))); +} + vec2 uvFromWorldPoint(vec3 point) { float u = (point.x - widthBounds[0]) / (widthBounds[1] - widthBounds[0]); float v = (point.z - lengthBounds[0]) / (lengthBounds[1] - lengthBounds[0]); @@ -45,11 +59,14 @@ void main() { // fragColor = vec4(fragColor.x, 0.f, fragColor.z, 1.f); // fragColor = vec4(test, test, test, 1.f); vec2 refrUV = uvFromWorldPoint(refrPos); - float beerAtt = exp(-length((pos - refrPos)) * 2.0f); // TODO: Make uniform + float beerAtt = exp(-length((pos - refrPos)) * 0.2f); // TODO: Make uniform vec4 diffuse = vec4(red * d, green * d, blue * d, 1.0f); vec4 specular = vec4(1, 1, 1, 1) * pow(spec, 10.f); - vec4 transmissive = vec4(vec3(refrUV, 1.f - refrUV.y), 1.f); +// vec4 transmissive = vec4(vec3(refrUV, 1.f - refrUV.y), 1.f); + float waterBlurriness = 0.1f; + vec2 refrUVBlurry = (1 - beerAtt) * vec2(rand(refrUV), rand(vec4(pos, d))) * waterBlurriness + refrUV; + vec4 transmissive = texture(sampler, vec2(refrUVBlurry)); // refrProb *= beerAtt; @@ -68,4 +85,6 @@ void main() { // VELOCITY * DIFFUSE // (1 - refrProb) * SPECULAR // refrProb * (BEER * TRANSMISSIVE + (1 - beerAtt) * VOLUME (which is somewhat diffuse too?)) + // Transmissive shouldn't just get darker, but blurrier as beer attenuation lowers. +// fragColor = texture(sampler, vec2(refrUV)); } diff --git a/resources/shaders/shader.vert b/resources/shaders/shader.vert index fbb9950..3271498 100755 --- a/resources/shaders/shader.vert +++ b/resources/shaders/shader.vert @@ -22,7 +22,7 @@ out vec2 uv; out float matIor; vec4 getRefrPos() { - float depth = -1.f; // TODO: Pass as uniform + float depth = -3.f; // TODO: Pass as uniform vec3 w_o = normalize(pos - camera_worldSpace); float cos_theta_i = dot(-w_o, normal_worldSpace); float n_i = 1; @@ -52,8 +52,8 @@ vec4 getRefrPos() { } void main() { - float depth = -2.f; - float dist = position.y - depth; +// float depth = -4.f; +// float dist = position.y - depth; float width = 81.f * 2.f; float length = 81.f * 2.f; matIor = 1.33f; @@ -64,10 +64,10 @@ void main() { pos = vec3(model * vec4(position, 1.f)); //vec3(model * vec4(objSpacePos, 1.f)); // pos = position; - float depthScale = dist / normal.y; - vec3 groundContactPoint = -(normal * depthScale) + position; // carries down to ground - groundContactPoint = vec3(model * vec4(position, 1)); - uv = vec2((position.x + 81.f) / (162.f), groundContactPoint.z); +// float depthScale = dist / normal.y; +// vec3 groundContactPoint = -(normal * depthScale) + position; // carries down to ground +// groundContactPoint = vec3(model * vec4(position, 1)); +// uv = vec2((position.x + 81.f) / (162.f), groundContactPoint.z); // uv = vec2(normal); vec4 refrPos_and_prob = getRefrPos(); refrPos = vec3(refrPos_and_prob); diff --git a/resources/shaders/texture.frag b/resources/shaders/texture.frag new file mode 100644 index 0000000..c7a5956 --- /dev/null +++ b/resources/shaders/texture.frag @@ -0,0 +1,24 @@ +#version 330 core + +// TASK 16: Create a UV coordinate in variable +in vec2 uv; + +// TASK 8: Add a sampler2D uniform +uniform sampler2D sampler; + +// TASK 29: Add a bool on whether or not to filter the texture +uniform bool filtered; + +out vec4 fragColor; + +void main() +{ + // TASK 17: Set fragColor using the sampler2D at the UV coordinate + fragColor = texture(sampler, uv); + + // TASK 33: Invert fragColor's r, g, and b color channels if your bool is true + if(filtered){ + fragColor = vec4(1) - fragColor; + fragColor.w = 1; + } +} diff --git a/resources/shaders/texture.vert b/resources/shaders/texture.vert new file mode 100644 index 0000000..c87f366 --- /dev/null +++ b/resources/shaders/texture.vert @@ -0,0 +1,15 @@ +#version 330 core + +// TASK 15: add a second layout variable representing a UV coordinate +layout (location = 0) in vec3 position; +layout (location = 1) in vec2 uv_raw; + +// TASK 16: create an "out" variable representing a UV coordinate +out vec2 uv; + +void main() { + // TASK 16: assign the UV layout variable to the UV "out" variable + uv = uv_raw; + + gl_Position = vec4(position, 1.0); +} diff --git a/src/glwidget.cpp b/src/glwidget.cpp index 263b7dd..472966b 100755 --- a/src/glwidget.cpp +++ b/src/glwidget.cpp @@ -70,12 +70,87 @@ void GLWidget::initializeGL() glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); - // glShadeModel(GL_SMOOTH); +// glShadeModel(GL_SMOOTH); // Initialize shaders m_defaultShader = new Shader(":resources/shaders/shader.vert", ":resources/shaders/shader.frag"); m_pointShader = new Shader(":resources/shaders/anchorPoint.vert", ":resources/shaders/anchorPoint.geom", ":resources/shaders/anchorPoint.frag"); +// m_texture_shader = new Shader(":/resources/shaders/texture.vert", ":/resources/shaders/texture.frag"); + + // INITIALIZE TEXTURE STUFF + + // Prepare filepath + QString kitten_filepath = QString(":/resources/images/kitten.png"); + + // TASK 1: Obtain image from filepath + m_image = QImage(kitten_filepath); + + // TASK 2: Format image to fit OpenGL + m_image = m_image.convertToFormat(QImage::Format_RGBA8888).mirrored(); + + auto bits = m_image.bits(); + + // TASK 3: Generate kitten texture + glGenTextures(1, &m_kitten_texture); + + // TASK 9: Set the active texture slot to texture slot 0 + glActiveTexture(GL_TEXTURE0); + + // TASK 4: Bind kitten texture + glBindTexture(GL_TEXTURE_2D, m_kitten_texture); + + // TASK 5: Load image into kitten texture + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_image.width(), m_image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, m_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(m_texture_shader->id()); +// glUniform1i(glGetUniformLocation(m_texture_shader->id(), "sampler"), 0); +// glUseProgram(0); + + glUseProgram(m_defaultShader->id()); + glUniform1i(glGetUniformLocation(m_defaultShader->id(), "sampler"), 0); + glUseProgram(0); + +// // TASK 11: Fix this "fullscreen" quad's vertex data +// // TASK 12: Play around with different values! +// // TASK 13: Add UV coordinates +// std::vector fullscreen_quad_data = +// { // POSITIONS // UVs // +// -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, +// -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, +// 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, +// 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, +// -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, +// 1.0f, -1.0f, 0.0f, 1.0f, 0.0f +// }; + +// // Generate and bind a VBO and a VAO for a fullscreen quad +// glGenBuffers(1, &m_fullscreen_vbo); +// glBindBuffer(GL_ARRAY_BUFFER, m_fullscreen_vbo); +// glBufferData(GL_ARRAY_BUFFER, fullscreen_quad_data.size()*sizeof(GLfloat), fullscreen_quad_data.data(), GL_STATIC_DRAW); +// glGenVertexArrays(1, &m_fullscreen_vao); +// glBindVertexArray(m_fullscreen_vao); + +// // TASK 14: modify the code below to add a second attribute to the vertex attribute array +// glEnableVertexAttribArray(0); +// glEnableVertexAttribArray(1); +// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), nullptr); +// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), reinterpret_cast(3 * sizeof(GLfloat))); + +// // Unbind the fullscreen quad's VBO and VAO +// glBindBuffer(GL_ARRAY_BUFFER, 0); +// glBindVertexArray(0); + + // END INITIALIZE TEXTURE STUFF + // Initialize ARAP, and get parameters needed to decide the camera position, etc Vector3f coeffMin, coeffMax; @@ -111,11 +186,30 @@ void GLWidget::initializeGL() m_arap.initGroundPlane(":resources/images/kitty.png", 2, m_defaultShader); } +//void GLWidget::paintTexture(GLuint texture, bool filtered){ +//// glUseProgram(m_texture_shader->id()); +// m_texture_shader->bind(); +// // TASK 32: Set your bool uniform on whether or not to filter the texture drawn +//// glUniform1i(glGetUniformLocation(m_texture_shader->id(), "filtered"), filtered); +// m_texture_shader->setUniform("filtered", filtered); +// // TASK 10: Bind "texture" to slot 0 +// glActiveTexture(GL_TEXTURE0); +// glBindTexture(GL_TEXTURE_2D, texture); +// glBindVertexArray(m_fullscreen_vao); +//// std::cout << texture << std::endl; +// glDrawArrays(GL_TRIANGLES, 0, 6); +// glBindTexture(GL_TEXTURE_2D, 0); +// glBindVertexArray(0); +// glUseProgram(0); +// m_texture_shader->unbind(); +//} + void GLWidget::paintGL() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable( GL_BLEND ); +// paintTexture(m_kitten_texture, false); +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +// glEnable( GL_BLEND ); m_defaultShader->bind(); m_defaultShader->setUniform("proj", m_camera.getProjection()); @@ -124,7 +218,9 @@ void GLWidget::paintGL() m_defaultShader->setUniform("inverseView", inverseView); m_defaultShader->setUniform("widthBounds", m_arap.minCorner[0], m_arap.maxCorner[0]); m_defaultShader->setUniform("lengthBounds", m_arap.minCorner[2], m_arap.maxCorner[2]); -// m_defaultShader->setUniform(""); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_kitten_texture); m_arap.draw(m_defaultShader, GL_TRIANGLES); m_defaultShader->unbind(); @@ -132,14 +228,14 @@ void GLWidget::paintGL() glClear(GL_DEPTH_BUFFER_BIT); -// m_pointShader->bind(); -// m_pointShader->setUniform("proj", m_camera.getProjection()); -// m_pointShader->setUniform("view", m_camera.getView()); -// m_pointShader->setUniform("vSize", m_vSize); -// m_pointShader->setUniform("width", width()); -// m_pointShader->setUniform("height", height()); -// m_arap.draw(m_pointShader, GL_POINTS); -// m_pointShader->unbind(); +//// m_pointShader->bind(); +//// m_pointShader->setUniform("proj", m_camera.getProjection()); +//// m_pointShader->setUniform("view", m_camera.getView()); +//// m_pointShader->setUniform("vSize", m_vSize); +//// m_pointShader->setUniform("width", width()); +//// m_pointShader->setUniform("height", height()); +//// m_arap.draw(m_pointShader, GL_POINTS); +//// m_pointShader->unbind(); } void GLWidget::resizeGL(int w, int h) diff --git a/src/glwidget.h b/src/glwidget.h index b319756..6433e22 100755 --- a/src/glwidget.h +++ b/src/glwidget.h @@ -29,6 +29,7 @@ private: // Basic OpenGL Overrides void initializeGL() override; void paintGL() override; + void paintTexture(GLuint texture, bool filtered); void resizeGL(int w, int h) override; // Event Listeners @@ -48,6 +49,12 @@ private: Camera m_camera; Shader *m_defaultShader; Shader *m_pointShader; + Shader *m_texture_shader; + + GLuint m_fullscreen_vbo; + GLuint m_fullscreen_vao; + QImage m_image; + GLuint m_kitten_texture; float m_movementScaling; float m_vertexSelectionThreshold; diff --git a/src/graphics/shape.cpp b/src/graphics/shape.cpp index c5dde0d..824a012 100644 --- a/src/graphics/shape.cpp +++ b/src/graphics/shape.cpp @@ -127,12 +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); +// glActiveTexture(GL_TEXTURE0); +// glBindTexture(GL_TEXTURE_2D, ocean_floor_texture); +// shader->setUniform("sampler", 0); glDrawElements(mode, m_numSurfaceVertices, GL_UNSIGNED_INT, reinterpret_cast(0)); glBindVertexArray(0); - glBindTexture(GL_TEXTURE_2D, 0); +// glBindTexture(GL_TEXTURE_2D, 0); break; } case GL_POINTS: -- cgit v1.2.3-70-g09d2