Skip to content

Commit

Permalink
Add simple shader wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinW1998 committed Oct 30, 2015
1 parent e2b89ac commit 4f07ce9
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 1 deletion.
14 changes: 14 additions & 0 deletions LunaDll/GlobalFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,20 @@ bool readFile(std::wstring &content, std::wstring path, std::wstring errMsg /*=
return true;
}

bool readFile(std::string &content, std::string path, std::string errMsg /*= std::string()*/)
{
ifstream theFile(path, ios::binary | ios::in);
if (!theFile.is_open()) {
theFile.close();
if (!errMsg.empty())
MessageBoxA(NULL, errMsg.c_str(), "Error", NULL);
return false;
}

content = std::string((std::istreambuf_iterator<char>(theFile)), std::istreambuf_iterator<char>());
return true;
}

bool isAbsolutePath(const std::wstring& path)
{
return std::iswalpha(path[0]) && path[1] == L':' && path[2] == L'\\';
Expand Down
1 change: 1 addition & 0 deletions LunaDll/GlobalFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ HMODULE getModule(std::string moduleName);

// File/Path Helper Funcs
bool readFile(std::wstring &content, std::wstring path, std::wstring errMsg = std::wstring());
bool readFile(std::string &content, std::string path, std::string errMsg = std::string());
bool writeFile(const std::string &content, const std::string &path);
std::vector<std::string> listFilesOfDir(const std::string& path);
std::vector<std::string> listOfDir(const std::string& path, DWORD fileAttributes);
Expand Down
2 changes: 2 additions & 0 deletions LunaDll/LunaDll.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
<ClInclude Include="Misc\RuntimeHookUtils\Toolhelp.h" />
<ClInclude Include="Rendering\AsyncGifRecorder.h" />
<ClInclude Include="Rendering\RenderOverrideManager.h" />
<ClInclude Include="Rendering\Shaders\GLShader.h" />
<ClInclude Include="SMBXInternal\CustomGraphics.h" />
<ClInclude Include="SMBXInternal\CameraInfo.h" />
<ClInclude Include="GameConfig\GameAutostart.h" />
Expand Down Expand Up @@ -276,6 +277,7 @@
<ClCompile Include="Autocode\Commands\AC_LunaControl.cpp" />
<ClCompile Include="LuaMain\LuaProxyComponent\LuaProxyPlayerSettings.cpp" />
<ClCompile Include="Rendering\RenderOverrideManager.cpp" />
<ClCompile Include="Rendering\Shaders\GLShader.cpp" />
<ClCompile Include="SMBXInternal\CustomGraphics.cpp" />
<ClCompile Include="GameConfig\GameAutostart.cpp" />
<ClCompile Include="GameConfig\GameConfiguration.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions LunaDll/LunaDll.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@
<ClInclude Include="Rendering\RenderOps\RenderStringOp.h">
<Filter>Header Files\Rendering</Filter>
</ClInclude>
<ClInclude Include="Rendering\Shaders\GLShader.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cpp">
Expand Down Expand Up @@ -872,6 +875,9 @@
<ClCompile Include="Misc\RuntimeHookUtils\APIHook.cpp">
<Filter>Header Files\Misc\RuntimeHookUtils</Filter>
</ClCompile>
<ClCompile Include="Rendering\Shaders\GLShader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="Misc\scratch.txt">
Expand Down
6 changes: 6 additions & 0 deletions LunaDll/Rendering/GLDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ void GLDraw::DrawRectangle(int nXDest, int nYDest, int nWidth, int nHeight)

void GLDraw::DrawStretched(int nXDest, int nYDest, int nWidth, int nHeight, const Texture* tex, int nXSrc, int nYSrc, int nSrcWidth, int nSrcHeight, float opacity)
{
// Bind Post-Processing Shader here

// Generate our floating point coordinates
float texw = (float)tex->pw;
float texh = (float)tex->ph;
Expand All @@ -181,6 +183,9 @@ void GLDraw::DrawStretched(int nXDest, int nYDest, int nWidth, int nHeight, cons
GLERRORCHECK();

BindTexture(tex);
GLERRORCHECK();

// Set Post-Processing uniforms/attributes here

GLfloat Vertices[] = {
x1, y1, 0,
Expand Down Expand Up @@ -211,6 +216,7 @@ void GLDraw::DrawStretched(int nXDest, int nYDest, int nWidth, int nHeight, cons
GLERRORCHECK();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
GLERRORCHECK();
// Unbind Post-Processing Shader here
}


3 changes: 2 additions & 1 deletion LunaDll/Rendering/GLEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ BOOL GLEngine::EmulatedStretchBlt(HDC hdcDest, int nXOriginDest, int nYOriginDes
HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
DWORD dwRop)
{
// TODO: This block of code will need to be changed once
// Load Post-Processing Shader somewhere here
// TODO: This block of code will need to be changed once
// GLContextManager is modified to handle re-init
// with a new hDC
if (!g_GLContextManager.IsInitialized()) {
Expand Down
137 changes: 137 additions & 0 deletions LunaDll/Rendering/Shaders/GLShader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include "GLShader.h"

#include "../../GlobalFuncs.h"

#include <iostream>

bool GLShader::compileShaderSource(GLuint shaderID, const std::string& source)
{
const char* sources[] = { source.c_str() };
glShaderSource(shaderID, 1, sources, NULL);
glCompileShader(shaderID);

GLint result;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &result);
return result != GL_FALSE;
}

std::string GLShader::getLastShaderError(GLuint shaderID)
{
GLint length;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &length);
std::string errorStr(length, ' ');
glGetShaderInfoLog(shaderID, length, &length, &errorStr[0]);
return errorStr;
}

GLShader::GLShader(const std::string & name, const std::string & vertexSource, const std::string & fragementSource) :
m_name(name),
m_vertexSource(vertexSource),
m_fragmentSource(fragementSource),
m_isValid(false)
{
load();
}

GLShader::~GLShader()
{
glDeleteProgram(m_shaderID);
}

void GLShader::bind()
{
glUseProgram(m_shaderID);
}

void GLShader::unbind()
{
glUseProgram(0);
}

GLuint GLShader::getAttribute(const std::string& name)
{
std::cout << "Get Attribute: " << name << std::endl;
GLuint ret = glGetAttribLocation(m_shaderID, name.c_str());
GLERRORCHECK();
return ret;
}

GLuint GLShader::getUniform(const std::string& name)
{
std::cout << "Get Uniform: " << name << std::endl;
GLuint ret = glGetUniformLocation(m_shaderID, name.c_str());
GLERRORCHECK();
return ret;
}

void GLShader::load()
{
bool isVertexSourceValid = m_vertexSource != "";
bool isFragmentSourceValid = m_vertexSource != "";
if (!isVertexSourceValid && !isFragmentSourceValid)
return;

GLuint program = glCreateProgram();
GLuint vertex;
GLuint fragment;
if(isVertexSourceValid)
vertex = glCreateShader(GL_VERTEX_SHADER);
if (isVertexSourceValid)
fragment = glCreateShader(GL_FRAGMENT_SHADER);


if (isVertexSourceValid) {
if (!compileShaderSource(vertex, m_vertexSource))
{
std::cout << "Failed to compile vertex shader: " << std::endl
<< getLastShaderError(vertex) << std::endl;
glDeleteShader(vertex);
return;
}
}
if (isFragmentSourceValid) {
if (!compileShaderSource(fragment, m_fragmentSource))
{
std::cout << "Failed to compile fragment shader: " << std::endl
<< getLastShaderError(fragment) << std::endl;
glDeleteShader(fragment);
return;
}
}

if(isVertexSourceValid)
glAttachShader(program, vertex);
GLERRORCHECK();
if(isFragmentSourceValid)
glAttachShader(program, fragment);
GLERRORCHECK();

glLinkProgram(program);
GLERRORCHECK();
glValidateProgram(program);
GLERRORCHECK();

if(isVertexSourceValid)
glDeleteShader(vertex);
GLERRORCHECK();
if(isFragmentSourceValid)
glDeleteShader(fragment);
GLERRORCHECK();

m_isValid = true; // Success!
m_shaderID = program;
}

GLShader* GLShader::fromData(const std::string& name, const std::string& vertexSource, const std::string& fragementSource)
{
return new GLShader(name, vertexSource, fragementSource);
}

GLShader* GLShader::fromFile(const std::string& name, const std::string& vertexFile, const std::string& fragmentFile)
{
std::string vertexSource("");
std::string fragementSource("");
readFile(vertexSource, vertexFile);
readFile(fragementSource, fragmentFile);
return new GLShader(name, vertexSource, fragementSource);
}
39 changes: 39 additions & 0 deletions LunaDll/Rendering/Shaders/GLShader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef GLShader_hhhhh
#define GLShader_hhhhh

#include "../GLCompat.h"
#include <string>

class GLShader
{
private:
static bool compileShaderSource(GLuint shaderID, const std::string& source);
static std::string getLastShaderError(GLuint shaderID);

GLuint m_shaderID;
std::string m_name;
std::string m_vertexSource;
std::string m_fragmentSource;
bool m_isValid;

public:
GLShader(const std::string& name, const std::string& vertexSource, const std::string& fragmentSource);
GLShader(const GLShader& other) = delete;
~GLShader();

inline bool isValid() { return m_isValid; }
void bind();
void unbind();

GLuint getAttribute(const std::string& name);
GLuint getUniform(const std::string& name);
private:
void load();

public:
static GLShader* fromData(const std::string& name, const std::string& vertexSource, const std::string& fragementSource);
static GLShader* fromFile(const std::string& name, const std::string& vertexFile, const std::string& fragmentFile);

};

#endif

0 comments on commit 4f07ce9

Please sign in to comment.