rendering depth buffer

simple_math_single_header
Martin Felis 2018-02-15 09:59:38 +01:00
parent b5f3370570
commit 7ac6e36c1f
5 changed files with 133 additions and 11 deletions

View File

@ -0,0 +1,17 @@
#version 330 core
in vec2 uv;
out vec3 color;
uniform sampler2D rendered_texture;
uniform float near;
uniform float far;
void main() {
float z = texture(rendered_texture, uv).r;
float c = (2.0 * near) / (far + near - z * (far - near));
c = 2.0 * near * far / (far + near - z * (far - near));
color = vec3(z);
}

View File

@ -14,6 +14,4 @@ void main() {
cos(time + 768.0 * uv.y)
)
).xyz;
color = vec3((texture(rendered_texture, uv).x + 1) * 0.5 );
// * 1 * (cos(time * 0.3) * 0.5) + 0.5);
}

View File

@ -0,0 +1,85 @@
#pragma once
#include<unordered_map>
#include<vector>
struct AFileModificationListener {
virtual bool OnTrigger() = 0;
};
struct FileModificationObserver {
struct FileObserverInfo {
ino_t mFileId = 0;
int mFileMTime = 0;
int mFileMTimeNSec = 0;
off_t mFileSize = 0;
std::vector<AFileModificationListener*> mListeners;
};
typedef std::unordered_map<std::string, FileObserverInfo > FilenameListenerMap;
FilenameListenerMap mObserverInfos;
void AddListener(const std::string &filename, AFileModificationListener* listener) {
FilenameListenerMap::iterator iter;
iter = mObserverInfos.find(filename);
if (iter == mObserverInfos.end()) {
FileObserverInfo observer_info;
observer_info.mListeners.push_back(listener);
mObserverInfos[filename] = observer_info;
} else {
iter->second.mListeners.push_back(listener);
}
}
void Trigger(const std::string &filename) {
FilenameListenerMap::iterator iter;
iter = mObserverInfos.find(filename);
assert (iter != mObserverInfos.end());
std::vector<AFileModificationListener*>::iterator listener_iter = iter->second.mListeners.begin();
while (listener_iter != iter->second.mListeners.end()) {
if ((*listener_iter)->OnTrigger())
break;
listener_iter++;
}
}
void CheckFileModification(const std::string& filename, FileObserverInfo& observer_info) {
struct stat attr;
bool stat_result = stat(filename.c_str(), &attr);
if (stat_result != 0) {
gLog ("Error: could not stat watched file %s", filename.c_str());
abort();
}
if ( observer_info.mFileId != attr.st_ino
|| observer_info.mFileMTime != attr.st_mtime
|| observer_info.mFileMTimeNSec != attr.st_mtim.tv_nsec
|| observer_info.mFileSize != attr.st_size
|| observer_info.mFileSize == 0
) {
observer_info.mFileId = attr.st_ino;
observer_info.mFileMTime = attr.st_mtime;
observer_info.mFileMTimeNSec = attr.st_mtim.tv_nsec;
observer_info.mFileSize = attr.st_size;
gLog ("Detected file change of %s: new size %d",
filename.c_str(), attr.st_size);
Trigger(filename);
}
}
void Update() {
FilenameListenerMap::iterator iter = mObserverInfos.begin();
while (iter != mObserverInfos.end()) {
CheckFileModification(iter->first, iter->second);
iter++;
}
}
};

View File

@ -144,12 +144,20 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data
glGenBuffers(1, &mRenderQuadVertexBufferId);
glBindBuffer(GL_ARRAY_BUFFER, mRenderQuadVertexBufferId);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW);
// Program for color texture rendering
mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl");
load_result = mRenderQuadProgramColor.Load();
assert(load_result);
muRenderQuadTexture = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "rendered_texture");
muRenderQuadTime = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "time");
// Program for depth texture rendering
mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl");
load_result = mRenderQuadProgramDepth.Load();
assert(load_result);
muRenderQuadDepthNear = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "near");
muRenderQuadDepthFar = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "far");
}
void Renderer::Shutdown() {
@ -197,14 +205,24 @@ void Renderer::RenderGui() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render the full screen quad
glUseProgram(mRenderQuadProgramColor.mProgramId);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture);
// glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture);
glUniform1i(muRenderQuadTexture, 0);
glUniform1f(muRenderQuadTime, (float)(glfwGetTime() * 10.0f));
bool render_color = false;
if (render_color) {
// Render the full screen quad
glUseProgram(mRenderQuadProgramColor.mProgramId);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture);
glUniform1i(muRenderQuadTexture, 0);
glUniform1f(muRenderQuadTime, (float)(glfwGetTime() * 10.0f));
} else {
// render depth texture
glUseProgram(mRenderQuadProgramDepth.mProgramId);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture);
glUniform1i(muRenderQuadTexture, 0);
glUniform1f(muRenderQuadDepthNear, 1.0);
glUniform1f(muRenderQuadDepthFar, -1.0);
}
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mRenderQuadVertexBufferId);

View File

@ -118,11 +118,15 @@ struct Renderer {
GLuint mRenderQuadVertexArrayId;
GLuint mRenderQuadVertexBufferId;
RenderProgram mRenderQuadProgramColor;
RenderProgram mRenderQuadProgramDepth;
GLuint muRenderQuadTexture;
GLuint muRenderQuadTime;
RenderProgram mRenderQuadProgramDepth;
GLuint muRenderQuadDepthNear;
GLuint muRenderQuadDepthFar;
Renderer() :
mInitialized(false),
mWidth (0),