PDA

View Full Version : Regarding shader spaces



Murloc992
09-15-2014, 01:01 AM
Hello,

I have currently got into shaders again. Now, I want some clarification.

As I have been googling, reading and all I have come to conclusion about spaces:

Origin is just something you define, like (0,1,0), it's a position from OpenGL coordinate center.
Model transform is the so told model center's transform regarding the (0,0,0) coordinate
View transform is the Model's transform according to camera, as the camera doesn't move and is at the (0,0,0).
Projection is what projects the View to Screen.
(Please correct me if I am wrong.)

So now to the problem:

I am trying to implement a Deferred Rendering G-Buffer.
I have got a FBO with all the required textures: Diffuse, Depth, Normals, Position, UV(I am not using UVs in my particular sample).
The main issue starts here. I know that Diffuse and Depth are okay Out of the Box.
I need Normals in View-Space, as the normals according to the camera change on view changes. I get that by multiplying normals I have calculated in my Geometry shader by Transpose Inverse View*Model matrix.

But how do I need to calculate Positions texture?
Also, are there any clarifications what exactly are: Model-Space, View-Space, Camera-Space?

This is my G-Buffer Shader Set:

Vertex:


#version 330
// THE FOOD CHAIN GOES LIKE THIS
// Vert
// Geom
// Frag

layout (location=0) in vec3 pos;
layout (location=6) in vec4 col;

uniform mat4 M;
uniform mat4 V;
uniform mat4 P;

out vData
{
vec4 color;
vec4 pos;
}vertex;

void main()
{
vec4 _col=vec4(col.x/255,col.y/255,col.z/255,col.w/255);
mat4 MVP = P*V*M;
vertex.color=_col;
vertex.pos=vec4(pos,1.0);
gl_Position = MVP * vec4(pos,1);
}


Geometry


#version 330
// THE FOOD CHAIN GOES LIKE THIS
// Vert
// Geom
// Frag

layout (triangles) in;
layout (triangle_strip, max_vertices=3) out;

uniform mat3 normMatrix;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;


in vData
{
vec4 color;
vec4 pos;
}vertex[];

out vec3 normal;
out vec4 color;
out vec4 position;

void main()
{
vec3 calcNorm=normalize(normMatrix*cross(vertex[1].pos.xyz - vertex[0].pos.xyz, vertex[2].pos.xyz - vertex[0].pos.xyz));
normal = calcNorm;
color = vertex[0].color;
position = M*vertex[0].pos;
gl_Position = gl_in[0].gl_Position;
EmitVertex();
normal = calcNorm;
color = vertex[1].color;
position = M*vertex[1].pos;
gl_Position = gl_in[1].gl_Position;
EmitVertex();
normal = calcNorm;
color = vertex[2].color;
position = M*vertex[2].pos;
gl_Position = gl_in[2].gl_Position;
EmitVertex();
EndPrimitive();
}


Fragment


#version 330
// THE FOOD CHAIN GOES LIKE THIS
// Vert
// Geom
// Frag

in vec4 position;
in vec3 normal;
in vec4 color;

layout (location = 0) out vec3 Gdiffuse;
layout (location = 1) out vec3 Gnormal;
layout (location = 2) out vec3 Gposition;
layout (location = 3) out vec3 Gtexcoord;

void main(){

Gdiffuse = color.rgb;
Gnormal = normal;
Gposition = position.rgb;
Gtexcoord = vec3(0);
}


This is how it looks:
http://puu.sh/bzKGC/e289497536.png
(Upper left - diffuse, Lower left - random normals for SSAO, Right - Depth, Normal, Position, UV)

( Yeah, it's a voxel engine, generic. :p But it's really fun learning to optimize and all. :) )