Added projection matrices code, tweaked stuff
parent
7ac6e36c1f
commit
48c8cde66b
|
@ -1,17 +1,36 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
in vec2 uv;
|
in vec2 ioUV;
|
||||||
|
|
||||||
out vec3 color;
|
out vec3 outColor;
|
||||||
|
|
||||||
uniform sampler2D rendered_texture;
|
uniform sampler2D uTexture;
|
||||||
uniform float near;
|
uniform float uNear;
|
||||||
uniform float far;
|
uniform float uFar;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
float z = texture(rendered_texture, uv).r;
|
float z = texture(uTexture, ioUV).r;
|
||||||
float c = (2.0 * near) / (far + near - z * (far - near));
|
float c = (2.0 * uNear) / (uFar + uNear - z * (uFar - uNear));
|
||||||
c = 2.0 * near * far / (far + near - z * (far - near));
|
// c = 2.0 * uNear * uFar / (uFar + uNear - z * (uFar - uNear));
|
||||||
|
// c = (uNear + (z - uNear) / (uFar - uNear);
|
||||||
|
|
||||||
color = vec3(z);
|
c = (z - uNear) / (uFar - uNear);
|
||||||
|
|
||||||
|
outColor = vec3(c);
|
||||||
|
|
||||||
|
if (abs(c + 1) < 0.2)
|
||||||
|
outColor = vec3(1, 0, 0);
|
||||||
|
// if (abs(c - 0.1) < 0.1)
|
||||||
|
// outColor = vec3(0, 0, 1);
|
||||||
|
// if (abs(c - 0.8) < 0.1)
|
||||||
|
// outColor = vec3(1, 0, 0);
|
||||||
|
|
||||||
|
|
||||||
|
// n ---- z ------- f
|
||||||
|
// 0 1
|
||||||
|
//
|
||||||
|
// (n + (z - n)) (f - n)
|
||||||
|
//
|
||||||
|
// (f - n)
|
||||||
|
// outColor = vec3(z);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
layout(location = 0) out vec3 color;
|
layout(location = 0) out vec3 outColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = vec3(1, 0, 0);
|
outColor = vec3(1, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
in vec2 uv;
|
in vec2 ioUV;
|
||||||
|
|
||||||
out vec3 color;
|
out vec3 outColor;
|
||||||
|
|
||||||
uniform sampler2D rendered_texture;
|
uniform sampler2D uTexture;
|
||||||
uniform float time;
|
uniform float uTime;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = texture(rendered_texture, uv
|
outColor = texture(uTexture, ioUV
|
||||||
+ 0.00 * vec2(
|
+ 0.00 * vec2(
|
||||||
sin(time + 1024.0 * uv.x),
|
sin(uTime + 1024.0 * ioUV.x),
|
||||||
cos(time + 768.0 * uv.y)
|
cos(uTime + 768.0 * ioUV.y)
|
||||||
)
|
)
|
||||||
).xyz;
|
).xyz;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
in vec3 vertex_position_modelspace;
|
in vec3 inVertex;
|
||||||
|
uniform mat4 uModelViewProj;
|
||||||
|
|
||||||
out vec2 uv;
|
out vec2 ioUV;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(vertex_position_modelspace, 1);
|
gl_Position = uModelViewProj * vec4(inVertex, 1);
|
||||||
uv = (vertex_position_modelspace.xy + vec2(1,1)) / 2.0;
|
ioUV = (inVertex.xy + vec2(1,1)) / 2.0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,6 +75,32 @@ inline Matrix44f ScaleMat44 (float x, float y, float z) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Matrix44f Ortho(float left, float right,
|
||||||
|
float bottom, float top,
|
||||||
|
float near, float far) {
|
||||||
|
float tx = -(right + left) / (right - left);
|
||||||
|
float ty = -(top + bottom) / (top - bottom);
|
||||||
|
float tz = -(far + near) / (far - near);
|
||||||
|
return Matrix44f(
|
||||||
|
2.0f / (right - left), 0.0f, 0.0f, 0.0f,
|
||||||
|
0, 2.0f / (top - bottom), 0.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, -2.0f / (far - near), 0.0f,
|
||||||
|
tx, ty, tz, 1.0f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Matrix44f Perspective(float fovy, float aspect,
|
||||||
|
float near, float far) {
|
||||||
|
float x = fovy / 2.0f;
|
||||||
|
float f = cos(x) / sin(x);
|
||||||
|
return Matrix44f(
|
||||||
|
f / aspect, 0.0f, 0.0f, 0.0f,
|
||||||
|
0.0f, f, 0.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, (far + near) / (near - far), -1.0f,
|
||||||
|
0.0f, 0.0f, (2.0f * far * near) / (near - far), 0.0f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/** Quaternion
|
/** Quaternion
|
||||||
*
|
*
|
||||||
* order: x,y,z,w
|
* order: x,y,z,w
|
||||||
|
|
|
@ -230,10 +230,6 @@ int main(void)
|
||||||
|
|
||||||
usleep(16000);
|
usleep(16000);
|
||||||
|
|
||||||
if (frame_counter > 1) {
|
|
||||||
gLog ("End of frame %d", frame_counter);
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwSwapBuffers(gWindow);
|
glfwSwapBuffers(gWindow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,14 @@
|
||||||
#include "RenderModule.h"
|
#include "RenderModule.h"
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
using namespace SimpleMath::GL;
|
||||||
|
|
||||||
struct Renderer;
|
struct Renderer;
|
||||||
|
|
||||||
static const GLfloat g_vertex_buffer_data[] = {
|
static const GLfloat g_vertex_buffer_data[] = {
|
||||||
-1.0f, -1.0f, 0.0f,
|
-0.9f, -0.9f, 1.0f,
|
||||||
1.0f, -1.0f, -1.2f,
|
0.9f, -0.9f, -1.0f,
|
||||||
0.0f, 1.0f, 0.0f
|
0.0f, 0.9f, 0.0f
|
||||||
};
|
};
|
||||||
|
|
||||||
static const GLfloat g_quad_vertex_buffer_data[] = {
|
static const GLfloat g_quad_vertex_buffer_data[] = {
|
||||||
|
@ -149,15 +151,23 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data
|
||||||
mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl");
|
mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl");
|
||||||
load_result = mRenderQuadProgramColor.Load();
|
load_result = mRenderQuadProgramColor.Load();
|
||||||
assert(load_result);
|
assert(load_result);
|
||||||
muRenderQuadTexture = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "rendered_texture");
|
muRenderQuadModelViewProj = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uModelViewProj");
|
||||||
muRenderQuadTime = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "time");
|
gLog("muRenderQuadModelViewProj %d", muRenderQuadModelViewProj);
|
||||||
|
muRenderQuadTexture = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uTexture");
|
||||||
|
gLog("muRenderQuadTexture %d", muRenderQuadTexture );
|
||||||
|
muRenderQuadTime = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uTime");
|
||||||
|
gLog("muRenderQuadTime %d", muRenderQuadTime);
|
||||||
|
|
||||||
// Program for depth texture rendering
|
// Program for depth texture rendering
|
||||||
mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl");
|
mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl");
|
||||||
load_result = mRenderQuadProgramDepth.Load();
|
load_result = mRenderQuadProgramDepth.Load();
|
||||||
assert(load_result);
|
assert(load_result);
|
||||||
muRenderQuadDepthNear = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "near");
|
muRenderQuadDepthModelViewProj = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uModelViewProj");
|
||||||
muRenderQuadDepthFar = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "far");
|
gLog("muModelViewProj %d", muRenderQuadDepthModelViewProj);
|
||||||
|
muRenderQuadDepthNear = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uNear");
|
||||||
|
gLog("muRenderQuadDepthNear %d", muRenderQuadDepthNear);
|
||||||
|
muRenderQuadDepthFar = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uFar");
|
||||||
|
gLog("muRenderQuadDepthFar %d", muRenderQuadDepthFar);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::Shutdown() {
|
void Renderer::Shutdown() {
|
||||||
|
@ -209,19 +219,25 @@ void Renderer::RenderGui() {
|
||||||
|
|
||||||
bool render_color = false;
|
bool render_color = false;
|
||||||
|
|
||||||
|
mCamera.mtxProj = Ortho(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f);
|
||||||
|
|
||||||
|
Matrix44f model_view_projection = mCamera.mtxProj * mCamera.mtxView;
|
||||||
|
|
||||||
if (render_color) {
|
if (render_color) {
|
||||||
// Render the full screen quad
|
// Render the full screen quad
|
||||||
glUseProgram(mRenderQuadProgramColor.mProgramId);
|
glUseProgram(mRenderQuadProgramColor.mProgramId);
|
||||||
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture);
|
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture);
|
||||||
|
glUniformMatrix4fv(muRenderQuadModelViewProj, 1, GL_FALSE, model_view_projection.data());
|
||||||
glUniform1i(muRenderQuadTexture, 0);
|
glUniform1i(muRenderQuadTexture, 0);
|
||||||
glUniform1f(muRenderQuadTime, (float)(glfwGetTime() * 10.0f));
|
glUniform1f(muRenderQuadTime, (float)(glfwGetTime() * 10.0f));
|
||||||
} else {
|
} else {
|
||||||
// render depth texture
|
// render depth texture
|
||||||
glUseProgram(mRenderQuadProgramDepth.mProgramId);
|
glUseProgram(mRenderQuadProgramDepth.mProgramId);
|
||||||
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture);
|
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture);
|
||||||
|
glUniformMatrix4fv(muRenderQuadModelViewProj, 1, GL_FALSE, model_view_projection.data());
|
||||||
glUniform1i(muRenderQuadTexture, 0);
|
glUniform1i(muRenderQuadTexture, 0);
|
||||||
glUniform1f(muRenderQuadDepthNear, 1.0);
|
glUniform1f(muRenderQuadDepthNear, -1.0);
|
||||||
glUniform1f(muRenderQuadDepthFar, -1.0);
|
glUniform1f(muRenderQuadDepthFar, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
|
@ -111,7 +111,7 @@ struct Renderer {
|
||||||
uint32_t mWidth = 1;
|
uint32_t mWidth = 1;
|
||||||
uint32_t mHeight = 1;
|
uint32_t mHeight = 1;
|
||||||
|
|
||||||
std::vector<Camera> cameras;
|
Camera mCamera;
|
||||||
Mesh mMesh;
|
Mesh mMesh;
|
||||||
RenderProgram mProgram;
|
RenderProgram mProgram;
|
||||||
RenderTarget mRenderTarget;
|
RenderTarget mRenderTarget;
|
||||||
|
@ -120,10 +120,12 @@ struct Renderer {
|
||||||
GLuint mRenderQuadVertexBufferId;
|
GLuint mRenderQuadVertexBufferId;
|
||||||
|
|
||||||
RenderProgram mRenderQuadProgramColor;
|
RenderProgram mRenderQuadProgramColor;
|
||||||
|
GLuint muRenderQuadModelViewProj;
|
||||||
GLuint muRenderQuadTexture;
|
GLuint muRenderQuadTexture;
|
||||||
GLuint muRenderQuadTime;
|
GLuint muRenderQuadTime;
|
||||||
|
|
||||||
RenderProgram mRenderQuadProgramDepth;
|
RenderProgram mRenderQuadProgramDepth;
|
||||||
|
GLuint muRenderQuadDepthModelViewProj;
|
||||||
GLuint muRenderQuadDepthNear;
|
GLuint muRenderQuadDepthNear;
|
||||||
GLuint muRenderQuadDepthFar;
|
GLuint muRenderQuadDepthFar;
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ bool RenderProgram::Load() {
|
||||||
VertexShaderCode = sstr.str();
|
VertexShaderCode = sstr.str();
|
||||||
VertexShaderStream.close();
|
VertexShaderStream.close();
|
||||||
}else{
|
}else{
|
||||||
gLog("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", mVertexShaderFilename.c_str());
|
gLog("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !", mVertexShaderFilename.c_str());
|
||||||
getchar();
|
getchar();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ bool RenderProgram::Load() {
|
||||||
|
|
||||||
|
|
||||||
// Compile Vertex Shader
|
// Compile Vertex Shader
|
||||||
gLog("Compiling shader : %s\n", mVertexShaderFilename.c_str());
|
gLog("Compiling shader : %s", mVertexShaderFilename.c_str());
|
||||||
char const * VertexSourcePointer = VertexShaderCode.c_str();
|
char const * VertexSourcePointer = VertexShaderCode.c_str();
|
||||||
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
|
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
|
||||||
glCompileShader(VertexShaderID);
|
glCompileShader(VertexShaderID);
|
||||||
|
@ -64,13 +64,13 @@ bool RenderProgram::Load() {
|
||||||
if ( InfoLogLength > 0 ){
|
if ( InfoLogLength > 0 ){
|
||||||
std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
|
std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
|
||||||
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
|
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
|
||||||
gLog("%s\n", &VertexShaderErrorMessage[0]);
|
gLog("%s", &VertexShaderErrorMessage[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Compile Fragment Shader
|
// Compile Fragment Shader
|
||||||
gLog("Compiling shader : %s\n", mFragmentShaderFilename.c_str());
|
gLog("Compiling shader : %s", mFragmentShaderFilename.c_str());
|
||||||
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
|
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
|
||||||
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
|
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
|
||||||
glCompileShader(FragmentShaderID);
|
glCompileShader(FragmentShaderID);
|
||||||
|
@ -81,13 +81,13 @@ bool RenderProgram::Load() {
|
||||||
if ( InfoLogLength > 0 ){
|
if ( InfoLogLength > 0 ){
|
||||||
std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
|
std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
|
||||||
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
|
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
|
||||||
gLog("%s\n", &FragmentShaderErrorMessage[0]);
|
gLog("%s", &FragmentShaderErrorMessage[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Link the program
|
// Link the program
|
||||||
gLog("Linking program\n");
|
gLog("Linking program");
|
||||||
GLuint ProgramID = glCreateProgram();
|
GLuint ProgramID = glCreateProgram();
|
||||||
glAttachShader(ProgramID, VertexShaderID);
|
glAttachShader(ProgramID, VertexShaderID);
|
||||||
glAttachShader(ProgramID, FragmentShaderID);
|
glAttachShader(ProgramID, FragmentShaderID);
|
||||||
|
@ -99,7 +99,7 @@ bool RenderProgram::Load() {
|
||||||
if ( InfoLogLength > 0 ){
|
if ( InfoLogLength > 0 ){
|
||||||
std::vector<char> ProgramErrorMessage(InfoLogLength+1);
|
std::vector<char> ProgramErrorMessage(InfoLogLength+1);
|
||||||
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
|
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
|
||||||
gLog("%s\n", &ProgramErrorMessage[0]);
|
gLog("%s", &ProgramErrorMessage[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue