From 224645d1071c14b4829dbb3ae35870868fcff85a Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 13 Feb 2018 23:00:10 -0500 Subject: Fixed inconsistent rendering failure The issue appears to have been caused by blending with unset alpha channels. Also included the re-ordered player character sprite image. The CMake file has been updated to include linking against some Apple libraries that are usually already included in GLFW3. refs #1 --- CMakeLists.txt | 11 ++++++++++- res/Starla.png | Bin 336 -> 3011 bytes shaders/bloom1.fragment | 11 ++++++----- shaders/bloom2.fragment | 26 ++++++++++++++------------ shaders/fill.fragment | 4 ++-- shaders/final.fragment | 4 ++-- shaders/ntsc.fragment | 4 ++-- src/game.cpp | 2 +- src/renderer.cpp | 34 +++++++++++++++++----------------- 9 files changed, 54 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b6ba2f..22bbc1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,13 +17,22 @@ find_package(portaudio REQUIRED) find_package(libsndfile REQUIRED) find_package(libxml2 REQUIRED) +IF(APPLE) + FIND_LIBRARY(COCOA_LIBRARY Cocoa) + FIND_LIBRARY(CV_LIBRARY CoreVideo) + FIND_LIBRARY(IO_LIBRARY IOKit) + MARK_AS_ADVANCED (COCOA_LIBRARY CV_LIBRARY IO_LIBRARY) + SET(EXTRA_LIBS ${COCOA_LIBRARY} ${CV_LIBRARY} ${IO_LIBRARY}) +ENDIF (APPLE) + set(ALL_LIBS - ${OPENGL_LIBRARIES} + ${OPENGL_gl_LIBRARY} ${GLEW_LIBRARIES} ${GLFW_LIBRARIES} ${PORTAUDIO_LIBRARIES} ${LIBSNDFILE_LIBRARY} ${LIBXML2_LIBRARIES} + ${EXTRA_LIBS} ) include_directories( diff --git a/res/Starla.png b/res/Starla.png index 7c98514..f5b41d7 100644 Binary files a/res/Starla.png and b/res/Starla.png differ diff --git a/shaders/bloom1.fragment b/shaders/bloom1.fragment index 0a89ea1..ed9e10d 100644 --- a/shaders/bloom1.fragment +++ b/shaders/bloom1.fragment @@ -2,15 +2,16 @@ in vec2 UV; -out vec3 color; +out vec4 color; uniform vec2 offset; uniform sampler2D inTex; void main() { - color = vec3(0.0); - color += (5.0/16.0) * texture(inTex, UV - offset).rgb; - color += (6.0/16.0) * texture(inTex, UV).rgb; - color += (5.0/16.0) * texture(inTex, UV + offset).rgb; + vec3 mval = vec3(0.0); + mval += (5.0/16.0) * texture(inTex, UV - offset).rgb; + mval += (6.0/16.0) * texture(inTex, UV).rgb; + mval += (5.0/16.0) * texture(inTex, UV + offset).rgb; + color = vec4(mval, 1.0); } diff --git a/shaders/bloom2.fragment b/shaders/bloom2.fragment index 4c50e86..6723c2c 100644 --- a/shaders/bloom2.fragment +++ b/shaders/bloom2.fragment @@ -2,7 +2,7 @@ in vec2 UV; -out vec3 color; +out vec4 color; uniform sampler2D clearTex; uniform sampler2D blurTex; @@ -15,19 +15,21 @@ float nrand(vec2 n) void main() { - color = vec3(0.0); - color += texture(clearTex, UV).rgb / 2.0; - color += texture(blurTex, UV).rgb; - color = max(vec3(0.0), color - 0.5); - color *= color; - + vec3 mval = vec3(0.0); + mval += texture(clearTex, UV).rgb / 2.0; + mval += texture(blurTex, UV).rgb; + mval = max(vec3(0.0), mval - 0.5); + mval *= mval; + float flicker = 0.5 + nrand(vec2(iGlobalTime)); flicker *= (flicker); //flicker = sqrt(flicker); //flicker = pow(flicker, 1.0/8.0); - //color *= flicker; - color *= mix(vec3(0.0), color, flicker); - - color += texture(clearTex, UV).rgb; - color = clamp(color, vec3(0.0), vec3(1.0)); + //mval *= flicker; + mval *= mix(vec3(0.0), mval, flicker); + + mval += texture(clearTex, UV).rgb; + mval = clamp(mval, vec3(0.0), vec3(1.0)); + + color = vec4(mval, 1.0); } diff --git a/shaders/fill.fragment b/shaders/fill.fragment index ab7d9c9..81443c4 100644 --- a/shaders/fill.fragment +++ b/shaders/fill.fragment @@ -1,10 +1,10 @@ #version 330 core -out vec3 color; +out vec4 color; uniform vec3 vecColor; void main() { - color = vecColor; + color = vec4(vecColor, 1.0); } diff --git a/shaders/final.fragment b/shaders/final.fragment index a3ee243..9a39597 100644 --- a/shaders/final.fragment +++ b/shaders/final.fragment @@ -5,7 +5,7 @@ in vec3 normIn; in vec3 camDirIn; in vec3 lightDirIn; -out vec3 color; +out vec4 color; uniform sampler2D rendertex; uniform sampler2D scanlinestex; @@ -71,5 +71,5 @@ void main() vec4 nearfinal = colorfres + colordiff + colorspec + emissive; //vec4 final = nearfinal * mix(vec4(1,1,1,1), vec4(0,0,0, 0), Tuning_Dimming); - color = nearfinal.rgb; + color = nearfinal; } diff --git a/shaders/ntsc.fragment b/shaders/ntsc.fragment index 56fb1c4..d6ae904 100644 --- a/shaders/ntsc.fragment +++ b/shaders/ntsc.fragment @@ -2,7 +2,7 @@ in vec2 UV; -out vec3 color; +out vec4 color; uniform sampler2D curFrameSampler; uniform sampler2D NTSCArtifactSampler; @@ -61,5 +61,5 @@ void main() Cur_Local = clamp(Cur_Local + (offset * Tuning_Sharp * mix(vec4(1,1,1,1), NTSCArtifact, Tuning_NTSC)), vec4(0,0,0,0), vec4(1,1,1,1)); Cur_Local = clamp(max(Cur_Local, Tuning_Persistence * (1.0 / (1.0 + (2.0 * Tuning_Bleed))) * (Prev_Local + ((Prev_Left + Prev_Right) * Tuning_Bleed))), vec4(0,0,0,0), vec4(1,1,1,1)); - color = Cur_Local.xyz; + color = vec4(Cur_Local.rgb, 1.0); } diff --git a/src/game.cpp b/src/game.cpp index 39bb3f1..1182689 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -40,7 +40,7 @@ Game::Game( int player = entityManager_.emplaceEntity(); - AnimationSet playerGraphics {"res/Starla2.bmp", 10, 12, 6}; + AnimationSet playerGraphics {"res/Starla.png", 10, 12, 6}; playerGraphics.emplaceAnimation("stillLeft", 3, 1, 1); playerGraphics.emplaceAnimation("stillRight", 0, 1, 1); playerGraphics.emplaceAnimation("walkingLeft", 4, 2, 10); diff --git a/src/renderer.cpp b/src/renderer.cpp index 3945e09..f840180 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -33,8 +33,6 @@ static GLuint bloom1Shader; static GLuint bloom2Shader; // The buffers for the NTSC rendering process -static GLuint renderedTex1; -static GLuint renderedTex2; static GLuint renderedTexBufs[2]; static int curBuf; static GLuint artifactsTex; @@ -148,6 +146,10 @@ void flipImageData(unsigned char* data, int width, int height, int comps) void loadMesh(const char* filename, std::vector& out_vertices, std::vector& out_uvs, std::vector& out_normals) { + out_vertices.clear(); + out_uvs.clear(); + out_normals.clear(); + FILE* file = fopen(filename, "r"); if (file == nullptr) { @@ -308,34 +310,33 @@ GLFWwindow* initRenderer() GLenum DrawBuffers2[1] = {GL_COLOR_ATTACHMENT1}; glDrawBuffers(1, DrawBuffers2); + glfwGetFramebufferSize(window, &buffer_width, &buffer_height); + glGenRenderbuffers(1, &bloom_depthbuffer); glBindRenderbuffer(GL_RENDERBUFFER, bloom_depthbuffer); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1024, 768); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, buffer_width, buffer_height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, bloom_depthbuffer); // Set up the NTSC rendering buffers - glGenTextures(1, &renderedTex1); - glBindTexture(GL_TEXTURE_2D, renderedTex1); + glGenTextures(2, renderedTexBufs); + glBindTexture(GL_TEXTURE_2D, renderedTexBufs[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, GAME_WIDTH, GAME_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - renderedTexBufs[0] = renderedTex1; - glGenTextures(1, &renderedTex2); - glBindTexture(GL_TEXTURE_2D, renderedTex2); + glBindTexture(GL_TEXTURE_2D, renderedTexBufs[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, GAME_WIDTH, GAME_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - renderedTexBufs[1] = renderedTex2; // Set up bloom rendering buffers glGenTextures(1, &preBloomTex); glBindTexture(GL_TEXTURE_2D, preBloomTex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 768, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, buffer_width, buffer_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -343,7 +344,7 @@ GLFWwindow* initRenderer() glGenTextures(1, &bloomPassTex1); glBindTexture(GL_TEXTURE_2D, bloomPassTex1); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024/4, 768/4, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, buffer_width/4, buffer_height/4, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -351,7 +352,7 @@ GLFWwindow* initRenderer() glGenTextures(1, &bloomPassTex2); glBindTexture(GL_TEXTURE_2D, bloomPassTex2); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024/4, 768/4, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, buffer_width/4, buffer_height/4, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -370,15 +371,15 @@ GLFWwindow* initRenderer() glGenBuffers(1, &mesh_vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, mesh_vertexbuffer); - glBufferData(GL_ARRAY_BUFFER, mesh_vertices.size() * sizeof(glm::vec3), &mesh_vertices[0], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh_vertices.size() * sizeof(glm::vec3), mesh_vertices.data(), GL_STATIC_DRAW); glGenBuffers(1, &mesh_uvbuffer); glBindBuffer(GL_ARRAY_BUFFER, mesh_uvbuffer); - glBufferData(GL_ARRAY_BUFFER, mesh_uvs.size() * sizeof(glm::vec3), &mesh_uvs[0], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh_uvs.size() * sizeof(glm::vec2), mesh_uvs.data(), GL_STATIC_DRAW); glGenBuffers(1, &mesh_normalbuffer); glBindBuffer(GL_ARRAY_BUFFER, mesh_normalbuffer); - glBufferData(GL_ARRAY_BUFFER, mesh_normals.size() * sizeof(glm::vec3), &mesh_normals[0], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh_normals.size() * sizeof(glm::vec3), mesh_normals.data(), GL_STATIC_DRAW); // Load the vertices of a flat surface GLfloat g_quad_vertex_buffer_data[] = { @@ -452,8 +453,7 @@ void destroyRenderer() glDeleteProgram(bloom2Shader); // Delete the NTSC rendering buffers - glDeleteTextures(1, &renderedTex1); - glDeleteTextures(1, &renderedTex2); + glDeleteTextures(2, renderedTexBufs); glDeleteTextures(1, &artifactsTex); glDeleteTextures(1, &scanlinesTex); glDeleteTextures(1, &preBloomTex); -- cgit 1.4.1