From ed9b5d97f81b9b7fdb7af0ebb6b77de32b9a704c Mon Sep 17 00:00:00 2001 From: hiimgoodpack Date: Thu, 4 Feb 2021 14:16:27 -0500 Subject: [PATCH] &globe &globe v2 --- native/main.cpp | 27 +++++++++++++++++---------- shaders/fragment.glsl | 42 ++++++++++++++++++++++++++++++++++++------ shaders/vertex.glsl | 5 +---- 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/native/main.cpp b/native/main.cpp index a03eda8..17bf03b 100644 --- a/native/main.cpp +++ b/native/main.cpp @@ -218,29 +218,36 @@ int main() { } glClearColor(0.25, 0.25, 0.25, 1); - glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - const float increment = -1.0f/30.0f; - float offset = 0; - while (!glfwWindowShouldClose(window)) { - glUniform1f(glGetUniformLocation(shader.id, "aHorizontalOffset"), offset); - offset += increment; - if (offset <= -1) - offset = 0; + glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); + glClear(GL_COLOR_BUFFER_BIT); + + const float increment = -1.0f/60.0f; + float globe0Offset = 0; + float globe1Offset = 0; + while (!glfwWindowShouldClose(window)) { glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); - glUniform1f(glGetUniformLocation(shader.id, "aTexCoordYScale"), -1); + glUniform1f(glGetUniformLocation(shader.id, "aTexCoordYScale"), 1); + glUniform1f(glGetUniformLocation(shader.id, "aHorizontalOffset"), globe0Offset); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); square.draw(); glBindFramebuffer(GL_FRAMEBUFFER, 0); - glUniform1f(glGetUniformLocation(shader.id, "aTexCoordYScale"), 1); glClear(GL_COLOR_BUFFER_BIT); + glUniform1f(glGetUniformLocation(shader.id, "aTexCoordYScale"), -1); + glUniform1f(glGetUniformLocation(shader.id, "aHorizontalOffset"), globe1Offset); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, colorBuffer); square.draw(); + + globe0Offset += increment; + if (globe0Offset <= -2) + globe0Offset = 0; + globe1Offset = globe0Offset * 0.5f; + glfwSwapBuffers(window); glfwPollEvents(); } diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 8d6bcd3..3ad546b 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -17,9 +17,19 @@ #version 330 core in vec2 vFragCoord; - -uniform float aHorizontalOffset; +in vec2 vTexCoord; uniform sampler2D aTexture; +uniform float aHorizontalOffset; + +// Set to 1 or -1 to flip Y +uniform float aTexCoordYScale; + +// Adds the Z value to a vec2, calculating it assuming +// it's a sphere with a radius of 2 +vec3 getZInSphere(vec2 v) { + const float radius = 2; + return vec3(v, sqrt(radius-dot(v,v))); +} void main() { float distanceFromCenterSquared = dot(vFragCoord, vFragCoord); @@ -28,10 +38,30 @@ void main() { // alpha = 1 if fragment is inside circle float alpha = distanceFromCenterSquared > 1 ? 0 : 1; - // An equation which seems to work - float specular = distanceFromCenterSquared * (-0.5f*vFragCoord.x + vFragCoord.y) + 1; + vec3 normal; + { + // Form a triangle to approximate the normal or something like that + const float offset = 0.001; + vec3 p0 = getZInSphere(vFragCoord + vec2(0, offset)); + vec3 p1 = getZInSphere(vFragCoord + vec2(offset, -offset)); + vec3 p2 = getZInSphere(vFragCoord + vec2(-offset, -offset)); - vec2 texCoord = vFragCoord * vec2(0.5,0.5) + vec2(0.5+aHorizontalOffset,0.5); + // Now calculate the normal of the triangle + vec3 a = p0 - p1; + vec3 b = p2 - p1; + normal = normalize(cross(a, b)); + } - gl_FragColor = vec4(texture(aTexture, texCoord).xyz*specular, alpha); + const float pi = radians(180); + // Based off of https://stackoverflow.com/questions/41957890/how-to-properly-map-a-2d-image-texture-to-an-icosphere + vec2 texCoord = vec2( + atan(normal.x, normal.z) * (1.0f/pi), + asin(-normal.y) * (2.0f/pi) * aTexCoordYScale + ) + vec2(0.5f+aHorizontalOffset, 0.5f); + + // Some equation which seems to work good enough + float specular = (dot(normal, normalize(vec3(-1, 2, 0.5f))) - 0.25f) * 1.3f + 0.1f; + specular = specular*specular * sign(specular); + + gl_FragColor = vec4(texture(aTexture, texCoord).rgb + (vec3(1,1,1) * specular), alpha); } diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 2ac7401..477c182 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -20,10 +20,7 @@ layout (location = 0) in vec2 aPosition; out vec2 vFragCoord; -// Set to 1 or -1 to flip Y -uniform float aTexCoordYScale; - void main() { gl_Position = vec4(aPosition, 0, 1); - vFragCoord = aPosition * vec2(1, aTexCoordYScale); + vFragCoord = aPosition; }