2016-08-29 22:31:11 +02:00
|
|
|
#include <string.h> // strlen
|
|
|
|
#include <locale.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
2018-02-13 12:05:07 +01:00
|
|
|
#include <fstream>
|
2016-08-29 22:31:11 +02:00
|
|
|
|
2016-10-21 22:07:23 +02:00
|
|
|
#include "RenderModule.h"
|
2016-08-29 22:31:11 +02:00
|
|
|
#include "RenderUtils.h"
|
2017-02-05 10:36:37 +01:00
|
|
|
#include "Globals.h"
|
2016-11-06 22:30:09 +01:00
|
|
|
|
|
|
|
using namespace SimpleMath;
|
2016-08-29 22:31:11 +02:00
|
|
|
|
2018-02-13 12:44:34 +01:00
|
|
|
//
|
|
|
|
// RenderProgram
|
|
|
|
//
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
RenderProgram::~RenderProgram() {
|
|
|
|
if (mProgramId > 0)
|
|
|
|
glDeleteProgram(mProgramId);
|
2016-08-29 22:31:11 +02:00
|
|
|
}
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
bool RenderProgram::Load() {
|
|
|
|
// Create the shaders
|
|
|
|
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
2016-08-29 22:31:11 +02:00
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
// Read the Vertex Shader code from the file
|
|
|
|
std::string VertexShaderCode;
|
|
|
|
std::ifstream VertexShaderStream(mVertexShaderFilename.c_str(), std::ios::in);
|
|
|
|
if(VertexShaderStream.is_open()){
|
|
|
|
std::stringstream sstr;
|
|
|
|
sstr << VertexShaderStream.rdbuf();
|
|
|
|
VertexShaderCode = sstr.str();
|
|
|
|
VertexShaderStream.close();
|
|
|
|
}else{
|
|
|
|
gLog("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", mVertexShaderFilename.c_str());
|
|
|
|
getchar();
|
|
|
|
return false;
|
2016-08-29 22:31:11 +02:00
|
|
|
}
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
// Read the Fragment Shader code from the file
|
|
|
|
std::string FragmentShaderCode;
|
|
|
|
std::ifstream FragmentShaderStream(mFragmentShaderFilename.c_str(), std::ios::in);
|
|
|
|
if(FragmentShaderStream.is_open()){
|
|
|
|
std::stringstream sstr;
|
|
|
|
sstr << FragmentShaderStream.rdbuf();
|
|
|
|
FragmentShaderCode = sstr.str();
|
|
|
|
FragmentShaderStream.close();
|
2016-08-29 22:31:11 +02:00
|
|
|
}
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
GLint Result = GL_FALSE;
|
|
|
|
int InfoLogLength;
|
2016-08-29 22:31:11 +02:00
|
|
|
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
// Compile Vertex Shader
|
|
|
|
gLog("Compiling shader : %s\n", mVertexShaderFilename.c_str());
|
|
|
|
char const * VertexSourcePointer = VertexShaderCode.c_str();
|
|
|
|
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
|
|
|
|
glCompileShader(VertexShaderID);
|
2016-08-29 22:31:11 +02:00
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
// Check Vertex Shader
|
|
|
|
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
|
|
|
if ( InfoLogLength > 0 ){
|
|
|
|
std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
|
|
|
|
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
|
|
|
|
gLog("%s\n", &VertexShaderErrorMessage[0]);
|
2017-02-18 17:38:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
// Compile Fragment Shader
|
|
|
|
gLog("Compiling shader : %s\n", mFragmentShaderFilename.c_str());
|
|
|
|
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
|
|
|
|
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
|
|
|
|
glCompileShader(FragmentShaderID);
|
2017-02-26 14:26:12 +01:00
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
// Check Fragment Shader
|
|
|
|
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
|
|
|
if ( InfoLogLength > 0 ){
|
|
|
|
std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
|
|
|
|
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
|
|
|
|
gLog("%s\n", &FragmentShaderErrorMessage[0]);
|
2017-02-26 14:26:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-18 17:38:21 +01:00
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
// Link the program
|
|
|
|
gLog("Linking program\n");
|
|
|
|
GLuint ProgramID = glCreateProgram();
|
|
|
|
glAttachShader(ProgramID, VertexShaderID);
|
|
|
|
glAttachShader(ProgramID, FragmentShaderID);
|
|
|
|
glLinkProgram(ProgramID);
|
2017-02-18 17:38:21 +01:00
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
// Check the program
|
|
|
|
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
|
|
|
|
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
|
|
|
if ( InfoLogLength > 0 ){
|
|
|
|
std::vector<char> ProgramErrorMessage(InfoLogLength+1);
|
|
|
|
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
|
|
|
|
gLog("%s\n", &ProgramErrorMessage[0]);
|
2017-02-26 14:26:12 +01:00
|
|
|
}
|
2016-11-28 22:19:09 +01:00
|
|
|
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
glDetachShader(ProgramID, VertexShaderID);
|
|
|
|
glDetachShader(ProgramID, FragmentShaderID);
|
2016-11-28 22:19:09 +01:00
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
glDeleteShader(VertexShaderID);
|
|
|
|
glDeleteShader(FragmentShaderID);
|
2016-11-28 22:19:09 +01:00
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
mProgramId = ProgramID;
|
|
|
|
return true;
|
2016-11-28 22:19:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-13 12:44:34 +01:00
|
|
|
//
|
|
|
|
// RenderTarget
|
|
|
|
//
|
|
|
|
RenderTarget::RenderTarget(int width, int height, int flags) {
|
2018-02-13 14:27:16 +01:00
|
|
|
mFlags = flags;
|
|
|
|
|
|
|
|
Cleanup();
|
|
|
|
Resize(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderTarget::~RenderTarget() {
|
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderTarget::Cleanup() {
|
|
|
|
if (mFrameBufferId != -1) {
|
|
|
|
glDeleteFramebuffers(1, &mFrameBufferId);
|
|
|
|
mFrameBufferId = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mColorTexture != -1) {
|
|
|
|
glDeleteTextures(1, &mColorTexture);
|
|
|
|
mColorTexture = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDepthBuffer != -1) {
|
|
|
|
glDeleteRenderbuffers(1, &mDepthBuffer);
|
|
|
|
mDepthBuffer = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderTarget::Resize(int width, int height) {
|
|
|
|
if (width == mWidth || height == mHeight)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Cleanup();
|
|
|
|
|
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
|
|
|
|
2018-02-13 12:44:34 +01:00
|
|
|
glGenFramebuffers(1, &mFrameBufferId);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferId);
|
|
|
|
|
2018-02-13 14:27:16 +01:00
|
|
|
if (mFlags & EnableColor) {
|
2018-02-13 12:44:34 +01:00
|
|
|
glGenTextures(1, &mColorTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mColorTexture);
|
|
|
|
|
2018-02-13 14:27:16 +01:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
2018-02-13 12:44:34 +01:00
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
|
|
|
|
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mColorTexture, 0);
|
|
|
|
}
|
|
|
|
|
2018-02-13 14:27:16 +01:00
|
|
|
if (mFlags & EnableDepth) {
|
2018-02-13 12:44:34 +01:00
|
|
|
glGenRenderbuffers(1, &mDepthBuffer);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, mDepthBuffer);
|
2018-02-13 14:27:16 +01:00
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, mWidth, mHeight);
|
2018-02-13 12:44:34 +01:00
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:19:38 +01:00
|
|
|
|