Created
July 8, 2011 07:26
-
-
Save soundofjw/1071308 to your computer and use it in GitHub Desktop.
Basic (very basic) Monocle Engine Shader Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FrameBuffer.cpp | |
// Teelo | |
// | |
// Created by Josh Whelchel on 5/31/11. | |
// Copyright 2011 jwmusic.org. All rights reserved. | |
// | |
#include "FrameBuffer.h" | |
#include "Debug.h" | |
using namespace Monocle; | |
namespace Teelo | |
{ | |
FrameBuffer::FrameBuffer( int w, int h, bool highFilter ) | |
{ | |
GLenum mode; | |
height = h; | |
width = w; | |
mode = highFilter ? GL_LINEAR : GL_NEAREST; | |
glGenTextures(1, &texID); | |
glBindTexture(GL_TEXTURE_2D, texID); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mode); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mode); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); | |
glGenBuffers(1, &fboID); | |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT ,fboID); | |
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texID, 0); | |
glGenRenderbuffersEXT(1, &depID); | |
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depID); | |
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height); | |
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depID); | |
if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)!=GL_FRAMEBUFFER_COMPLETE_EXT) | |
{ | |
Debug::Log("FrameBuffer failed setup"); | |
// bad? | |
} | |
else | |
{ | |
Debug::Log("new FrameBuffer("+StringOf(width)+"x"+StringOf(height)+") (texID:"+StringOf(texID)+") = ID: "+StringOf(fboID)); | |
} | |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); | |
originalSize = Vector2(Graphics::GetVirtualWidth(), Graphics::GetVirtualHeight()); | |
} | |
FrameBuffer::~FrameBuffer() | |
{ | |
glDeleteFramebuffers(1, &fboID); | |
glDeleteTextures(1, &texID); | |
} | |
void FrameBuffer::Use() | |
{ | |
originalSize = Vector2(Platform::GetWidth(), Platform::GetHeight()); | |
originalVirtualSize = Vector2(Graphics::GetVirtualWidth(), Graphics::GetVirtualHeight()); | |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID); | |
glPushAttrib(GL_VIEWPORT_BIT); | |
Graphics::Resize(width,height); | |
// glViewport(0,0, width, height); | |
// Graphics::Set2D(width,height); | |
// Render as normal here | |
// output goes to the FBO and it’s attached buffers | |
} | |
void FrameBuffer::Bind() | |
{ | |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID); | |
} | |
void FrameBuffer::Stop() | |
{ | |
glPopAttrib(); | |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); | |
Graphics::Resize(originalSize.x,originalSize.y); | |
Graphics::Set2D(originalVirtualSize.x,originalVirtualSize.y); | |
} | |
void FrameBuffer::BindTexture() | |
{ | |
glBindTexture(GL_TEXTURE_2D, texID); | |
} | |
FrameBuffer *FrameBuffer::NewFullBuffer( bool highFilter ) | |
{ | |
return new FrameBuffer(Graphics::GetVirtualWidth(),Graphics::GetVirtualHeight(),highFilter); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FrameBuffer.h | |
// Teelo | |
// | |
// Created by Josh Whelchel on 5/31/11. | |
// Copyright 2011 jwmusic.org. All rights reserved. | |
// | |
#include "Main.h" | |
using namespace Monocle; | |
namespace Teelo { | |
class FrameBuffer | |
{ | |
public: | |
FrameBuffer( int w, int h, bool highFilter = true ); | |
~FrameBuffer(); | |
static FrameBuffer *NewFullBuffer( bool highFilter = true ); | |
Vector2 originalSize; | |
Vector2 originalVirtualSize; | |
void Use(); | |
void Stop(); | |
void Bind(); | |
void BindTexture(); | |
GLuint fboID; | |
int width; | |
int height; | |
GLuint texID; | |
GLuint depID; | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Shader.cpp | |
// Rokkit | |
// | |
// Created by Josh Whelchel on 5/10/11. | |
// Copyright 2011 jwmusic.org. All rights reserved. | |
// | |
#include "Shader.h" | |
#include <stdlib.h> | |
using namespace Monocle; | |
namespace Teelo { | |
char *file2string(std::string sPath) | |
{ | |
const char *path = sPath.c_str(); | |
FILE *fd; | |
long len, | |
r; | |
char *str; | |
if (!(fd = fopen(path, "r"))) | |
{ | |
fprintf(stderr, "Can't open file '%s' for reading\n", path); | |
return NULL; | |
} | |
fseek(fd, 0, SEEK_END); | |
len = ftell(fd); | |
printf("File '%s' is %ld long\n", path, len); | |
fseek(fd, 0, SEEK_SET); | |
if (!(str = (char*)malloc(len * sizeof(char)))) | |
{ | |
fprintf(stderr, "Can't malloc space for '%s'\n", path); | |
return NULL; | |
} | |
r = fread(str, sizeof(char), len, fd); | |
str[r - 1] = '\0'; /* Shader sources have to term with null */ | |
fclose(fd); | |
return str; | |
} | |
void printLog(GLuint obj) | |
{ | |
int infologLength = 0; | |
char infoLog[1024]; | |
if (glIsShader(obj)) | |
glGetShaderInfoLog(obj, 1024, &infologLength, infoLog); | |
else | |
glGetProgramInfoLog(obj, 1024, &infologLength, infoLog); | |
if (infologLength > 0) | |
printf("%s\n", infoLog); | |
} | |
Shader::Shader( std::string vertexPath, std::string fragmentPath ) | |
{ | |
this->glFShader = glCreateShader(GL_FRAGMENT_SHADER); | |
this->glVShader = glCreateShader(GL_VERTEX_SHADER); | |
this->glProgram = glCreateProgram(); | |
if (vertexPath != "") | |
{ | |
const char *vShader = file2string(Assets::GetContentPath() + vertexPath); | |
if (vShader){ | |
glShaderSource(this->glVShader, 1, &vShader, NULL); | |
glCompileShader(this->glVShader); | |
printLog(glVShader); | |
free((void*)vShader); | |
glAttachShader(this->glProgram, this->glVShader); | |
} | |
} | |
if (fragmentPath != "") | |
{ | |
const char *fShader = file2string(Assets::GetContentPath() + fragmentPath); | |
if (fShader){ | |
glShaderSource(this->glFShader, 1, &fShader, NULL); | |
glCompileShader(this->glFShader); | |
printLog(glFShader); | |
free((void*)fShader); | |
glAttachShader(this->glProgram, this->glFShader); | |
} | |
} | |
glLinkProgram(this->glProgram); | |
printLog(glProgram); | |
} | |
Shader::~Shader() | |
{ | |
glDeleteShader(this->glVShader); | |
glDeleteShader(this->glFShader); | |
glDeleteProgram(this->glProgram); | |
} | |
void Shader::Use() | |
{ | |
glUseProgram(this->glProgram); | |
GLint sampler1U = glGetUniformLocation(glProgram, "sampler1"); | |
glUniform1i(sampler1U, 1); | |
GLint sampler2U = glGetUniformLocation(glProgram, "sampler2"); | |
glUniform1i(sampler2U, 2); | |
GLint dT = glGetUniformLocation(this->glProgram, "deltaTime"); | |
glUniform1f(dT,Monocle::deltaTime); | |
GLint tT = glGetUniformLocation(this->glProgram, "totalTime"); | |
glUniform1f(tT,Monocle::timeSinceStart); | |
} | |
void Shader::SetUniform( std::string name, float value ) | |
{ | |
GLint var = glGetUniformLocation(this->glProgram, name.c_str()); | |
glUniform1f(var,value); | |
} | |
void Shader::SetUniform3Vec( std::string name, Vector3 vec ) | |
{ | |
GLint var = glGetUniformLocation(this->glProgram, name.c_str()); | |
glUniform3f(var,vec.x,vec.y,vec.z); | |
} | |
void Shader::SetUniform2Vec( std::string name, Vector2 vec ) | |
{ | |
GLint var = glGetUniformLocation(this->glProgram, name.c_str()); | |
glUniform2f(var,vec.x,vec.y); | |
} | |
void Shader::SetUniformColor( std::string name, Color vec, bool ignoreAlpha ) | |
{ | |
GLint var = glGetUniformLocation(this->glProgram, name.c_str()); | |
if (ignoreAlpha) | |
glUniform4f(var,vec.r,vec.g,vec.b,1.0); | |
else | |
glUniform4f(var,vec.r,vec.g,vec.b,vec.a); | |
} | |
void Shader::SetUniformInt( std::string name, int value ) | |
{ | |
GLint var = glGetUniformLocation(this->glProgram, name.c_str()); | |
glUniform1i(var,value); | |
} | |
void Shader::None() | |
{ | |
glUseProgram(0); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Shader.h | |
// Rokkit | |
// | |
// Created by Josh Whelchel on 5/10/11. | |
// Copyright 2011 jwmusic.org. All rights reserved. | |
// | |
#include "Monocle.h" | |
#define GLEW_STATIC | |
#include <GL/glew.h> | |
#include <map> | |
using namespace Monocle; | |
namespace Teelo { | |
class Shader | |
{ | |
public: | |
Shader( std::string vertexPath, std::string fragmentPath ); | |
~Shader(); | |
void Use(); | |
void SetUniform( std::string name, float value ); | |
void SetUniform3Vec( std::string name, Vector3 vec ); | |
void SetUniform2Vec( std::string name, Vector2 vec ); | |
void SetUniformColor( std::string name, Color vec, bool ignoreAlpha = false ); | |
void SetUniformInt( std::string name, int value ); | |
static void None(); | |
GLuint glVShader; | |
GLuint glFShader; | |
GLuint glProgram; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment