&globe &globe

This commit is contained in:
hiimgoodpack 2021-02-01 16:07:15 -05:00
parent 1fdde43b57
commit 7a0731545f
Signed by: hiimgoodpack
GPG key ID: 4E0E62733C14AE69
5 changed files with 78 additions and 18 deletions

3
.gitmodules vendored
View file

@ -1,3 +1,6 @@
[submodule "digraphene-headers"]
path = digraphene-headers
url = https://gitlab.com/TestingPlant/digraphene-headers.git
[submodule "stb"]
path = stb
url = https://github.com/nothings/stb.git

View file

@ -14,13 +14,14 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include <mesh.h>
#include <GLFW/glfw3.h>
#include <shader.h>
#include <cstring>
#include <cassert>
#include <iostream>
#include <type_traits>
typedef struct {
linalg::aliases::float2 position;
@ -122,6 +123,16 @@ int main() {
GLFWwindow* window = glfwCreateWindow(resultLength, resultLength, "You aren't supposed to see this", NULL, NULL);
glfwMakeContextCurrent(window);
int contextFlags;
glGetIntegerv(GL_CONTEXT_FLAGS, &contextFlags);
if (contextFlags & GL_CONTEXT_FLAG_DEBUG_BIT) {
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(onDebugOutput, NULL);
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
} else {
std::cerr << "Warning: Debug context could not be created.\n";
}
// Load a square
/*
0--1
@ -170,13 +181,15 @@ int main() {
shader.use();
// Set up a framebuffer
/*unsigned int frameBuffer;
unsigned int frameBuffer;
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
unsigned int colorBuffer;
glGenTextures(1, &colorBuffer);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, resultLength, resultLength, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, resultLength, resultLength, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorBuffer, 0);
unsigned int renderBuffer;
glGenRenderbuffers(1, &renderBuffer);
@ -185,13 +198,48 @@ int main() {
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);
assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE && "Generated framebuffer is incomplete");
*/
glClearColor(0.5, 0, 0, 1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA);
while (!glfwWindowShouldClose(window)) {
unsigned int texture, normal;
// Load the image
{
int width, height;
unsigned char* data = stbi_load("../test.png", &width, &height, nullptr, 0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
glUniform1i(glGetUniformLocation(shader.id, "aTexture"), 0);
}
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);
glUniform1f(glGetUniformLocation(shader.id, "aTexCoordYScale"), -1);
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);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
square.draw();
glfwSwapBuffers(window);
glfwPollEvents();

View file

@ -5,28 +5,33 @@
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 330 core
in vec2 vTexCoord;
in vec2 vFragCoord;
uniform sampler2D aTexture0;
uniform float aHorizontalOffset;
uniform sampler2D aTexture;
void main() {
/*
dot(vTexCoord, vTexCoord)
essentially does pow(length(gl_FragCoord.xy),2), but
(probably) more optimized
float distanceFromCenterSquared = dot(vFragCoord, vFragCoord);
We're doing this since we don't want fragments outside a circle
*/
float alpha = dot(vTexCoord, vTexCoord) > 1 ? 1 : 0;
// alpha = 0 if fragment is outside circle
// alpha = 1 if fragment is inside circle
float alpha = distanceFromCenterSquared > 1 ? 0 : 1;
gl_FragColor = vec4(0, 0, 0, alpha);
// An equation which seems to work
float specular = distanceFromCenterSquared * (-0.5f*vFragCoord.x + vFragCoord.y) + 1;
vec2 texCoord = vFragCoord * vec2(0.5,0.5) + vec2(0.5+aHorizontalOffset,0.5);
gl_FragColor = vec4(texture(aTexture, texCoord).xyz*specular, alpha);
}

View file

@ -18,9 +18,12 @@
layout (location = 0) in vec2 aPosition;
out vec2 vTexCoord;
out vec2 vFragCoord;
// Set to 1 or -1 to flip Y
uniform float aTexCoordYScale;
void main() {
gl_Position = vec4(aPosition, 0, 1);
vTexCoord = aPosition;
vFragCoord = aPosition * vec2(1, aTexCoordYScale);
}

1
stb Submodule

@ -0,0 +1 @@
Subproject commit b42009b3b9d4ca35bc703f5310eedc74f584be58