Added projection matrices code, tweaked stuff

simple_math_single_header
Martin Felis 2018-02-16 11:35:09 +01:00
parent 7ac6e36c1f
commit 48c8cde66b
9 changed files with 104 additions and 44 deletions

View File

@ -1,17 +1,36 @@
#version 330 core
in vec2 uv;
in vec2 ioUV;
out vec3 color;
out vec3 outColor;
uniform sampler2D rendered_texture;
uniform float near;
uniform float far;
uniform sampler2D uTexture;
uniform float uNear;
uniform float uFar;
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));
float z = texture(uTexture, ioUV).r;
float c = (2.0 * uNear) / (uFar + uNear - z * (uFar - uNear));
// 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);
}

View File

@ -1,7 +1,7 @@
#version 330 core
layout(location = 0) out vec3 color;
layout(location = 0) out vec3 outColor;
void main() {
color = vec3(1, 0, 0);
outColor = vec3(1, 0, 0);
}

View File

@ -1,17 +1,17 @@
#version 330 core
in vec2 uv;
in vec2 ioUV;
out vec3 color;
out vec3 outColor;
uniform sampler2D rendered_texture;
uniform float time;
uniform sampler2D uTexture;
uniform float uTime;
void main() {
color = texture(rendered_texture, uv
outColor = texture(uTexture, ioUV
+ 0.00 * vec2(
sin(time + 1024.0 * uv.x),
cos(time + 768.0 * uv.y)
sin(uTime + 1024.0 * ioUV.x),
cos(uTime + 768.0 * ioUV.y)
)
).xyz;
}

View File

@ -1,10 +1,11 @@
#version 330 core
in vec3 vertex_position_modelspace;
in vec3 inVertex;
uniform mat4 uModelViewProj;
out vec2 uv;
out vec2 ioUV;
void main() {
gl_Position = vec4(vertex_position_modelspace, 1);
uv = (vertex_position_modelspace.xy + vec2(1,1)) / 2.0;
gl_Position = uModelViewProj * vec4(inVertex, 1);
ioUV = (inVertex.xy + vec2(1,1)) / 2.0;
}

View File

@ -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
*
* order: x,y,z,w

View File

@ -230,10 +230,6 @@ int main(void)
usleep(16000);
if (frame_counter > 1) {
gLog ("End of frame %d", frame_counter);
}
glfwSwapBuffers(gWindow);
}

View File

@ -3,12 +3,14 @@
#include "RenderModule.h"
#include <GLFW/glfw3.h>
using namespace SimpleMath::GL;
struct Renderer;
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, -1.2f,
0.0f, 1.0f, 0.0f
-0.9f, -0.9f, 1.0f,
0.9f, -0.9f, -1.0f,
0.0f, 0.9f, 0.0f
};
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");
load_result = mRenderQuadProgramColor.Load();
assert(load_result);
muRenderQuadTexture = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "rendered_texture");
muRenderQuadTime = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "time");
muRenderQuadModelViewProj = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "uModelViewProj");
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
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");
muRenderQuadDepthModelViewProj = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "uModelViewProj");
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() {
@ -208,20 +218,26 @@ void Renderer::RenderGui() {
glActiveTexture(GL_TEXTURE0);
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) {
// Render the full screen quad
glUseProgram(mRenderQuadProgramColor.mProgramId);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture);
glUniformMatrix4fv(muRenderQuadModelViewProj, 1, GL_FALSE, model_view_projection.data());
glUniform1i(muRenderQuadTexture, 0);
glUniform1f(muRenderQuadTime, (float)(glfwGetTime() * 10.0f));
} else {
// render depth texture
glUseProgram(mRenderQuadProgramDepth.mProgramId);
glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture);
glUniformMatrix4fv(muRenderQuadModelViewProj, 1, GL_FALSE, model_view_projection.data());
glUniform1i(muRenderQuadTexture, 0);
glUniform1f(muRenderQuadDepthNear, 1.0);
glUniform1f(muRenderQuadDepthFar, -1.0);
glUniform1f(muRenderQuadDepthNear, -1.0);
glUniform1f(muRenderQuadDepthFar, 1.0);
}
glEnableVertexAttribArray(0);

View File

@ -111,7 +111,7 @@ struct Renderer {
uint32_t mWidth = 1;
uint32_t mHeight = 1;
std::vector<Camera> cameras;
Camera mCamera;
Mesh mMesh;
RenderProgram mProgram;
RenderTarget mRenderTarget;
@ -120,10 +120,12 @@ struct Renderer {
GLuint mRenderQuadVertexBufferId;
RenderProgram mRenderQuadProgramColor;
GLuint muRenderQuadModelViewProj;
GLuint muRenderQuadTexture;
GLuint muRenderQuadTime;
RenderProgram mRenderQuadProgramDepth;
GLuint muRenderQuadDepthModelViewProj;
GLuint muRenderQuadDepthNear;
GLuint muRenderQuadDepthFar;

View File

@ -33,7 +33,7 @@ bool RenderProgram::Load() {
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());
gLog("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !", mVertexShaderFilename.c_str());
getchar();
return false;
}
@ -53,7 +53,7 @@ bool RenderProgram::Load() {
// Compile Vertex Shader
gLog("Compiling shader : %s\n", mVertexShaderFilename.c_str());
gLog("Compiling shader : %s", mVertexShaderFilename.c_str());
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);
@ -64,13 +64,13 @@ bool RenderProgram::Load() {
if ( InfoLogLength > 0 ){
std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
gLog("%s\n", &VertexShaderErrorMessage[0]);
gLog("%s", &VertexShaderErrorMessage[0]);
}
// Compile Fragment Shader
gLog("Compiling shader : %s\n", mFragmentShaderFilename.c_str());
gLog("Compiling shader : %s", mFragmentShaderFilename.c_str());
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
glCompileShader(FragmentShaderID);
@ -81,13 +81,13 @@ bool RenderProgram::Load() {
if ( InfoLogLength > 0 ){
std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
gLog("%s\n", &FragmentShaderErrorMessage[0]);
gLog("%s", &FragmentShaderErrorMessage[0]);
}
// Link the program
gLog("Linking program\n");
gLog("Linking program");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
@ -99,7 +99,7 @@ bool RenderProgram::Load() {
if ( InfoLogLength > 0 ){
std::vector<char> ProgramErrorMessage(InfoLogLength+1);
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
gLog("%s\n", &ProgramErrorMessage[0]);
gLog("%s", &ProgramErrorMessage[0]);
}