Skip to content

Commit

Permalink
# - shaders!
Browse files Browse the repository at this point in the history
  • Loading branch information
en-software committed Nov 30, 2023
1 parent cbff460 commit 98d238a
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 9 deletions.
4 changes: 3 additions & 1 deletion blooDot.vcxproj
Expand Up @@ -139,7 +139,7 @@
<GenerateWindowsMetadata>false</GenerateWindowsMetadata>
<AdditionalLibraryDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
</AdditionalLibraryDirectories>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">version.lib;dinput8.lib;dxguid.lib;user32.lib;gdi32.lib;winmm.lib;imm32.lib;ole32.lib;oleaut32.lib;shell32.lib;uuid.lib;iconv.lib;advapi32.lib;setupapi.lib</AdditionalDependencies>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">version.lib;dinput8.lib;dxguid.lib;user32.lib;gdi32.lib;winmm.lib;imm32.lib;ole32.lib;oleaut32.lib;shell32.lib;uuid.lib;iconv.lib;advapi32.lib;setupapi.lib;opengl32.lib</AdditionalDependencies>
<TurnOffAssemblyGeneration Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</TurnOffAssemblyGeneration>
<EnableUAC Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</EnableUAC>
<DelayLoadDLLs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand Down Expand Up @@ -288,6 +288,7 @@
<ClCompile Include="polypartition.cpp" />
<ClCompile Include="procedurally-generate.cpp" />
<ClCompile Include="region-banner.cpp" />
<ClCompile Include="shader-engine.cpp" />
<ClCompile Include="xlations.cpp" />
<ClInclude Include="boydsa.h" />
<ClInclude Include="chunk-sizes.h" />
Expand Down Expand Up @@ -371,6 +372,7 @@
<ClInclude Include="scripture.h" />
<ClInclude Include="settings.h" />
<ClInclude Include="sfx.h" />
<ClInclude Include="shader-engine.h" />
<ClInclude Include="splash.h" />
<ClInclude Include="xlations-de.h" />
<ClInclude Include="xlations-en.h" />
Expand Down
9 changes: 9 additions & 0 deletions blooDot.vcxproj.filters
Expand Up @@ -111,6 +111,9 @@
<Filter Include="_Source Files\maker">
<UniqueIdentifier>{7ef47bbd-1de2-40da-80e9-b90b6f3ff8f4}</UniqueIdentifier>
</Filter>
<Filter Include="Resource Files\shaders">
<UniqueIdentifier>{e251f7b8-66a4-4676-a581-53c32622d568}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="menu-settings.h">
Expand Down Expand Up @@ -296,6 +299,9 @@
<ClInclude Include="makersettings.h">
<Filter>_Headers\maker</Filter>
</ClInclude>
<ClInclude Include="shader-engine.h">
<Filter>_Headers\core</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dialogcontrols.cpp">
Expand Down Expand Up @@ -454,6 +460,9 @@
<ClCompile Include="makersettings.cpp">
<Filter>_Source Files\maker</Filter>
</ClCompile>
<ClCompile Include="shader-engine.cpp">
<Filter>_Source Files\core</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="planning\menus.ligma">
Expand Down
2 changes: 2 additions & 0 deletions chunk-constants.h
Expand Up @@ -51,3 +51,5 @@ constexpr int const CHUNK_KEY_LANG_FLAG_UA_PNG = 47;
constexpr int const CHUNK_KEY_DINGS_SNURCH_SNAVIOR_HEAD_ROOOF = 48;
constexpr int const CHUNK_KEY_DINGS_SNURCH_SNAVIOR_FLOOR = 49;
constexpr int const CHUNK_KEY_DINGS_SNURCH_SNAVIOR_COLLISION = 50;
constexpr int const CHUNK_KEY_SHADER_VERTEX = 51;
constexpr int const CHUNK_KEY_SHADER_FRAGMENT = 52;
4 changes: 3 additions & 1 deletion chunk-sizes.h
Expand Up @@ -51,5 +51,7 @@ size_t chunkSizes[] = {
2041, // CHUNK_KEY_LANG_FLAG_UA_PNG
10422, // CHUNK_KEY_DINGS_SNURCH_SNAVIOR_HEAD_ROOOF
106415, // CHUNK_KEY_DINGS_SNURCH_SNAVIOR_FLOOR
702 // CHUNK_KEY_DINGS_SNURCH_SNAVIOR_COLLISION
702, // CHUNK_KEY_DINGS_SNURCH_SNAVIOR_COLLISION
882, // CHUNK_KEY_SHADER_VERTEX
4648 // CHUNK_KEY_SHADER_FRAGMENT
};
4 changes: 3 additions & 1 deletion gameres.xassy.recipe
Expand Up @@ -48,4 +48,6 @@ langflag-fi.png CHUNK_KEY_LANG_FLAG_FI_PNG
langflag-ua.png CHUNK_KEY_LANG_FLAG_UA_PNG
snurch-snavior-head-rooof.png CHUNK_KEY_DINGS_SNURCH_SNAVIOR_HEAD_ROOOF
snurch-snavior-floor.png CHUNK_KEY_DINGS_SNURCH_SNAVIOR_FLOOR
snurch-snavior-collision.csv CHUNK_KEY_DINGS_SNURCH_SNAVIOR_COLLISION
snurch-snavior-collision.csv CHUNK_KEY_DINGS_SNURCH_SNAVIOR_COLLISION
vertex.glsl CHUNK_KEY_SHADER_VERTEX
fragment.glsl CHUNK_KEY_SHADER_FRAGMENT
13 changes: 11 additions & 2 deletions main.cpp
Expand Up @@ -16,6 +16,8 @@
#include "enxassy.h"
#include "enxlate.h"
#endif
#include "shader-engine.h"
using namespace blooDot::ShaderEngine;

const int major = 3;
const int minor = 1;
Expand Down Expand Up @@ -91,7 +93,7 @@ int main(int, char**)
<< unsigned(patch)
<< "\n";

const auto initResult = SDL_Init(SDL_INIT_EVERYTHING);
const auto initResult = SDL_Init(SDL_INIT_EVERYTHING | SDL_VIDEO_OPENGL);
if (initResult < 0)
{
const auto errorMessage = SDL_GetError();
Expand Down Expand Up @@ -409,6 +411,7 @@ void _Launch()
return;
}

SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
renderer = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
if (!renderer)
{
Expand Down Expand Up @@ -461,6 +464,12 @@ void _Launch()
return;
}

if (!InitGLExtensions())
{
std::cout << "Could not initialize OpenGL extensions" << std::endl;
return;
}

SDL_ShowWindow(mainWindow);
blooDot::Orchestrator::MainLoop(renderer);
blooDot::Orchestrator::MainLoop(renderer, mainWindow);
}
30 changes: 27 additions & 3 deletions orchestrator.cpp
@@ -1,13 +1,15 @@
#include "pch.h"
#include "orchestrator.h"
#include "physics-render-binding.h"
#include "shader-engine.h"

#ifndef NDEBUG
#include "physicsdebugdraw.h"
#endif

using namespace blooDot::Dings;
using namespace blooDot::World;
using namespace blooDot::ShaderEngine;

Uint32 SDL_USEREVENT_SAVE = 0;
Uint32 SDL_USEREVENT_AUTOSAVE = 0;
Expand Down Expand Up @@ -36,7 +38,7 @@ namespace blooDot::Orchestrator
int32 positionIterations = 2;
SDL_GameController* controller = nullptr;

void MainLoop(SDL_Renderer* renderer)
void MainLoop(SDL_Renderer* renderer, SDL_Window* mainWindow)
{
toggleDebugView = isCreatorMode;

Expand All @@ -51,6 +53,7 @@ namespace blooDot::Orchestrator
}

GameViewRenderer = renderer;
SDL_Texture* texTarget = nullptr;
if (mainRunning && !GameviewEnterWorld())
{
mainRunning = false;
Expand Down Expand Up @@ -135,6 +138,23 @@ namespace blooDot::Orchestrator
}
}

// shaders
auto programId = CompileProgram
(
CHUNK_KEY_SHADER_VERTEX,
CHUNK_KEY_SHADER_FRAGMENT
);

texTarget = SDL_CreateTexture
(
renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
1024,
768
);

// go!
while (mainRunning)
{
frameStart = SDL_GetPerformanceCounter();
Expand Down Expand Up @@ -422,6 +442,7 @@ namespace blooDot::Orchestrator
}

NEXTFRAME:
SDL_SetRenderTarget(renderer, texTarget);
SDL_RenderClear(renderer);
GameViewRenderFrame();
#ifndef NDEBUG
Expand All @@ -431,7 +452,9 @@ namespace blooDot::Orchestrator
}

#endif
SDL_RenderPresent(renderer);
//SDL_RenderPresent(renderer);
PresentBackBuffer(renderer, mainWindow, texTarget, programId);

frameEnded = SDL_GetPerformanceCounter();
const auto& frameTime = (frameEnded - frameStart) / frequencyMill;
const auto& frameSlack = static_cast<int>(MillisecondsPerFrame - frameTime);
Expand All @@ -454,7 +477,8 @@ namespace blooDot::Orchestrator
{
SDL_GameControllerClose(controller);
}


DestroyTexture(&texTarget);
TeardownDingSheets();
GameviewTeardown();
ClearWorldData();
Expand Down
2 changes: 1 addition & 1 deletion orchestrator.h
Expand Up @@ -22,7 +22,7 @@ extern SDL_Texture* NewTexture(SDL_Renderer* renderer, int w, int h, bool transp

namespace blooDot::Orchestrator
{
void MainLoop(SDL_Renderer*);
void MainLoop(SDL_Renderer*, SDL_Window*);

void _InitiatePlayerMovement(b2Body* body, float impulseX, float impulseY);
void _HandleSave(bool isAutosave = false);
Expand Down
172 changes: 172 additions & 0 deletions res/fragment.glsl
@@ -0,0 +1,172 @@
// PUBLIC DOMAIN CRT STYLED SCAN-LINE SHADER
//
// by Timothy Lottes
//
// This is more along the style of a really good CGA arcade monitor.
// With RGB inputs instead of NTSC.
// The shadow mask example has the mask rotated 90 degrees for less chromatic aberration.
//
// Left it unoptimized to show the theory behind the algorithm.
//
// It is an example what I personally would want as a display option for pixel art games.
// Please take and use, change, or whatever

varying vec4 v_color;
varying vec2 v_texCoord;

uniform sampler2D tex0;

// Hardness of scanline.
// -8.0 = soft
// -16.0 = medium
float hardScan=-8.0;

// Hardness of pixels in scanline.
// -2.0 = soft
// -4.0 = hard
float hardPix=-2.0;

// Display warp.
// 0.0 = none
// 1.0/8.0 = extreme
vec2 warp=vec2(1.0/32.0,1.0/24.0);

// Amount of shadow mask.
float maskDark=1.0;
float maskLight=1.5;

vec2 res = vec2(640.0,480.0); // /3.0

//------------------------------------------------------------------------
// sRGB to Linear
// Assuming using sRGB typed textures this should not be needed.
float ToLinear1(float c){return(c<=0.04045)?c/12.92:pow((c+0.055)/1.055,2.4);}
vec3 ToLinear(vec3 c)
{
return vec3(ToLinear1(c.r),ToLinear1(c.g),ToLinear1(c.b));
}

// Linear to sRGB
// Assuming using sRGB typed textures this should not be needed
float ToSrgb1(float c)
{
return(c<0.0031308?c*12.92:1.055*pow(c,0.41666)-0.055);
}

vec3 ToSrgb(vec3 c)
{
return vec3(ToSrgb1(c.r),ToSrgb1(c.g),ToSrgb1(c.b));
}

// Nearest emulated sample given floating point position and texel offset
// Also zeroes off screen
vec3 Fetch(vec2 pos,vec2 off)
{
pos=floor(pos*res+off)/res;
if(max(abs(pos.x-0.5),abs(pos.y-0.5))>0.5)return vec3(0.0,0.0,0.0);
return ToLinear(texture2D(tex0,pos.xy,-16.0).rgb);
}

// Distance in emulated pixels to nearest texel
vec2 Dist(vec2 pos)
{
pos=pos*res;return -((pos-floor(pos))-vec2(0.5));
}

// 1D Gaussian
float Gaus(float pos,float scale)
{
return exp2(scale*pos*pos);
}

// 3-tap Gaussian filter along horz line
vec3 Horz3(vec2 pos,float off)
{
vec3 b=Fetch(pos,vec2(-1.0,off));
vec3 c=Fetch(pos,vec2( 0.0,off));
vec3 d=Fetch(pos,vec2( 1.0,off));
float dst=Dist(pos).x;
// Convert distance to weight
float scale=hardPix;
float wb=Gaus(dst-1.0,scale);
float wc=Gaus(dst+0.0,scale);
float wd=Gaus(dst+1.0,scale);
// Return filtered sample
return (b*wb+c*wc+d*wd)/(wb+wc+wd);
}

// 5-tap Gaussian filter along horz line
vec3 Horz5(vec2 pos,float off)
{
vec3 a=Fetch(pos,vec2(-2.0,off));
vec3 b=Fetch(pos,vec2(-1.0,off));
vec3 c=Fetch(pos,vec2( 0.0,off));
vec3 d=Fetch(pos,vec2( 1.0,off));
vec3 e=Fetch(pos,vec2( 2.0,off));
float dst=Dist(pos).x;
// Convert distance to weight.
float scale=hardPix;
float wa=Gaus(dst-2.0,scale);
float wb=Gaus(dst-1.0,scale);
float wc=Gaus(dst+0.0,scale);
float wd=Gaus(dst+1.0,scale);
float we=Gaus(dst+2.0,scale);
// Return filtered sample.
return (a*wa+b*wb+c*wc+d*wd+e*we)/(wa+wb+wc+wd+we);
}

// Return scanline weight
float Scan(vec2 pos,float off)
{
float dst=Dist(pos).y;
return Gaus(dst+off,hardScan);
}

// Allow nearest three lines to effect pixel.
vec3 Tri(vec2 pos)
{
vec3 a=Horz3(pos,-1.0);
vec3 b=Horz5(pos, 0.0);
vec3 c=Horz3(pos, 1.0);
float wa=Scan(pos,-1.0);
float wb=Scan(pos, 0.0);
float wc=Scan(pos, 1.0);
return a*wa+b*wb+c*wc;
}

// Distortion of scanlines, and end of screen alpha
vec2 Warp(vec2 pos)
{
pos=pos*2.0-1.0;
pos*=vec2(1.0+(pos.y*pos.y)*warp.x,1.0+(pos.x*pos.x)*warp.y);
return pos*0.5+0.5;
}

// Shadow mask
vec3 Mask(vec2 pos)
{
pos.x+=pos.y*3.0;
vec3 mask=vec3(maskDark,maskDark,maskDark);
pos.x=fract(pos.x/6.0);
if(pos.x<0.333)mask.r=maskLight;
else if(pos.x<0.666)mask.g=maskLight;
else mask.b=maskLight;
return mask;
}

// Draw dividing bars
float Bar(float pos,float bar)
{
pos-=bar;return pos*pos<4.0?0.0:1.0;
}

// Entry
void main()
{
// Unmodified
vec2 pos=Warp(v_texCoord);
vec4 fragColor;
fragColor.rgb=Tri(pos)*Mask(gl_FragCoord.xy);
fragColor.rgb=ToSrgb(fragColor.rgb);
gl_FragColor=v_color * vec4(fragColor.rgb, 1.0);
}
9 changes: 9 additions & 0 deletions res/vertex.glsl
@@ -0,0 +1,9 @@
varying vec4 v_color;
varying vec2 v_texCoord;

void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
v_color = gl_Color;
v_texCoord = vec2(gl_MultiTexCoord0);
}

0 comments on commit 98d238a

Please sign in to comment.