Native mosaic PGE fragment shader implementation. Co-authored-by: sigonasr2 <sigonasr2@gmail.com>

main
Nic0Nic0Nii 9 months ago
parent a38ef4e71d
commit dca53a3df0
  1. 2
      C++ProjectTemplate.js
  2. BIN
      C++ProjectTemplate.wasm
  3. 11
      main.cpp
  4. 22
      pixelGameEngine.h
  5. BIN
      pixelGameEngine_wasm.o

File diff suppressed because one or more lines are too long

Binary file not shown.

@ -37,12 +37,17 @@ public:
}else{ }else{
lastIncrease=std::max(0.f,lastIncrease-fElapsedTime); lastIncrease=std::max(0.f,lastIncrease-fElapsedTime);
} }
Clear(WHITE); Clear(BLACK);
srand(48190); srand(48190);
for(int i=0;i<100;i++){ for(int i=0;i<500;i++){
if(rand()%2){
DrawCircle({rand()%640,rand()%480},rand()%30,{uint8_t(rand()%255),uint8_t(rand()%255),uint8_t(rand()%255)}); DrawCircle({rand()%640,rand()%480},rand()%30,{uint8_t(rand()%255),uint8_t(rand()%255),uint8_t(rand()%255)});
}else{
FillCircle({rand()%640,rand()%480},rand()%30,{uint8_t(rand()%255),uint8_t(rand()%255),uint8_t(rand()%255)});
}
} }
DrawStringDecal({0,0},"Mosaic Effect: "+std::to_string(GetMosaicEffect()),BLACK); DrawStringDecal({1,1},"Mosaic Effect: "+std::to_string(GetMosaicEffect()),BLACK);
DrawStringDecal({0,0},"Mosaic Effect: "+std::to_string(GetMosaicEffect()),WHITE);
return true; return true;
} }

@ -1248,7 +1248,7 @@ namespace olc
std::vector<std::string> vDroppedFilesCache; std::vector<std::string> vDroppedFilesCache;
olc::vi2d vDroppedFilesPoint; olc::vi2d vDroppedFilesPoint;
olc::vi2d vDroppedFilesPointCache; olc::vi2d vDroppedFilesPointCache;
uint8_t mosaic=0; uint8_t mosaic=1;
// Command Console Specific // Command Console Specific
bool bConsoleShow = false; bool bConsoleShow = false;
@ -3596,7 +3596,7 @@ namespace olc
return mosaic; return mosaic;
} }
void PixelGameEngine::SetMosaicEffect(uint8_t effectLevel){ void PixelGameEngine::SetMosaicEffect(uint8_t effectLevel){
mosaic=effectLevel; mosaic=std::max(uint8_t(1),effectLevel);
} }
@ -4865,8 +4865,17 @@ namespace olc
#else #else
"#version 330 core\n" "#version 330 core\n"
#endif #endif
"out vec4 pixel;\n""in vec2 oTex;\n" "out vec4 pixel;\n"
"in vec4 oCol;\n""uniform sampler2D sprTex;\n""void main(){pixel = texture(sprTex, oTex) * oCol;}"; "in vec2 oTex;\n"
"in vec4 oCol;\n"
"uniform int mosaic;\n"
"uniform vec2 size;\n"
"uniform sampler2D sprTex;\n"
"void main(){\n"
"vec2 texelSize = 1.0 / size; \n"
"vec2 pos = oTex / texelSize; \n""vec2 outputPos = vec2(floor(pos.x/float(mosaic))*float(mosaic),floor(pos.y/float(mosaic))*float(mosaic));\n"
"pixel = texture(sprTex, outputPos*texelSize) * oCol;\n"
"}";
locShaderSource(m_nFS, 1, &strFS, NULL); locShaderSource(m_nFS, 1, &strFS, NULL);
locCompileShader(m_nFS); locCompileShader(m_nFS);
@ -4883,13 +4892,12 @@ namespace olc
"layout(location = 2) in vec4 aCol;\n" "layout(location = 2) in vec4 aCol;\n"
"out vec2 oTex;\n" "out vec2 oTex;\n"
"out vec4 oCol;\n" "out vec4 oCol;\n"
"uniform int mosaic;\n"
"\n" "\n"
"void main(){\n" "void main(){\n"
"float p = 1.0 / aPos.z;\n" "float p = 1.0 / aPos.z;\n"
"gl_Position = p * vec4(aPos.x, aPos.y, 0.0, 1.0);\n" "gl_Position = p * vec4(aPos.x, aPos.y, 0.0, 1.0);\n"
"oTex = p * aTex;\n" "oTex = p * aTex;\n"
"oCol = vec4(aCol.r,float(mosaic)/255.0,0,aCol.a);\n" "oCol = aCol;\n"
"}"; "}";
locShaderSource(m_nVS, 1, &strVS, NULL); locShaderSource(m_nVS, 1, &strVS, NULL);
locCompileShader(m_nVS); locCompileShader(m_nVS);
@ -4974,7 +4982,9 @@ namespace olc
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
locUseProgram(m_nQuadShader); locUseProgram(m_nQuadShader);
GLint myUniformLocation = glGetUniformLocation(m_nQuadShader, "mosaic"); GLint myUniformLocation = glGetUniformLocation(m_nQuadShader, "mosaic");
GLint myUniformLocation2 = glGetUniformLocation(m_nQuadShader, "size");
glUniform1i(myUniformLocation,ptrPGE->GetMosaicEffect()); glUniform1i(myUniformLocation,ptrPGE->GetMosaicEffect());
glUniform2f(myUniformLocation2,ptrPGE->ScreenWidth(),ptrPGE->ScreenHeight());
locBindVertexArray(m_vaQuad); locBindVertexArray(m_vaQuad);
#if defined(OLC_PLATFORM_EMSCRIPTEN) #if defined(OLC_PLATFORM_EMSCRIPTEN)

Binary file not shown.
Loading…
Cancel
Save