Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 30 Sep 2012
Offline Last Active Oct 13 2014 11:47 AM

Topics I've Started

Deferred rendering shader spaces issue

17 September 2014 - 12:47 AM

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 the Model's center or OpenGL coordinate center.
Model transform(M) is the so told model center's transform regarding the OpenGL coordinate center. (M*vec)
View transform(V) is the Model's transform according to camera, as the camera doesn't move and is at the (0,0,0). (V*M*vec)
Projection(P) is what projects the View to Screen. (P*V*M*vec)
(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 and Positions in View-Space, for my SSAO implementation. I get that by multiplying normals I have calculated in my Geometry shader by Transpose Inverse View*Model matrix and packing them.
But how do I need to calculate Positions texture?
Also, are there any clarifications what exactly are: Model-Space, View-Space, Camera-Space? Are they exactly as I told?
This is my G-Buffer Shader Set:
#version 330
// 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;
void main()
vec4 _col=vec4(col.x/255,col.y/255,col.z/255,col.w/255);
mat4 MVP = P*V*M;
gl_Position = MVP * vec4(pos,1);
#version 330
// 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;
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))*0.5+0.5;
normal = calcNorm;
color = vertex[0].color;
position = M*vertex[0].pos;
    gl_Position = gl_in[0].gl_Position;
normal = calcNorm;
color = vertex[1].color;
position = M*vertex[1].pos;
    gl_Position = gl_in[1].gl_Position;
normal = calcNorm;
color = vertex[2].color;
position = M*vertex[2].pos;
    gl_Position = gl_in[2].gl_Position;
#version 330
// 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:
(Upper left - diffuse, Lower left - random normals for SSAO, Right - Depth, Normal, Position, UV)
( Yeah, it's a voxel engine, generic. tongue.png But it's really fun learning to optimize and all. smile.png Nothing more satisfying than to see it be faster and more memory efficient. smile.png )

[GL3] VAO and VBO question about dynamic/stream buffers

20 December 2012 - 10:01 AM

Hello, I am writing here with a question which was haunting me lately.

I want to draw some dynamic data using a VAO, but I am not pretty sure how to do it. Doing it the regular dynamic/stream VBO way it works, but when I do it with a VAO I need to bind the buffer I want to change and while using gDebugger I found out that those binds in dynamic/stream functions are pretty expensive and gDebugger doesn't recommend using those, so I want to know how should I draw a dynamic VBOs in a VAO?

Thanks in advance!


[OpenGL 3.3+] Newbie phong shading issues

30 September 2012 - 02:18 AM

Hello, I would like to find a solution for the problem which is haunting me lately.

I am starting to use OpenGL3.3+ and shaders(I've been using immediate mode for 2D rendering most of the time, and Irrlicht 3D rendering engine.), so I am practically new at those. After some time fiddling with the new syntax and shaders, I've finally came to the lighting stage and tried to implement phong lighting, everything seemed okay at first, but then I realised it's screwed. After a day and a half of trying to fix it, intense googling for the same problem, I couldn't find a solution. My problem is simple:
Light is orientating with camera even if it's in a static place(like 0,0,100) so if it's lighting the back of the model, the front is lit, and even more strangely, when I go away from the dark side, it lights up..

Here's a screenshot:
Posted Image
(Light is in front of the model, while the back is being lit brightly as I am far away.)

Here's my phong shader, I've taken it from Anton's OpenGL4 wiki and added texturing support:

#version 400

in vec4 vpeye; // fragment position in eye coords
in vec4 vneye; // surface normal in eye coords
in vec2 UV; // UV coordinates
uniform vec4 lightPosition; // light position in eye coords
uniform vec4 Ka; // ambient coefficient
uniform vec4 Kd; // diffuse coefficient
uniform vec4 Ks; // specular coefficient
uniform float Ns; // specular exponent
uniform vec4 Ld; // diffuse light colour
// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;

layout (location = 0) out vec4 fragmentColour;

void main() {
  vec4 n_eye = normalize(vneye); // normalise just to be on the safe side
  vec4 s_eye = normalize(lightPosition - vpeye); // get direction from surface fragment to light
  vec4 v_eye = normalize(-vpeye); // get direction from surface fragment to camera
  vec4 h_eye = normalize(v_eye + s_eye); // Blinn's half-way vector
  //vec4 r_eye = reflect(-s_eye, vneye); // Phong's full reflection (could use instead of h)

  vec4 Ia = vec4(0.1,0.1,0.1,1) * Ka; // ambient light has a hard-coded colour here, but we could load an La value
  vec4 Id = Ld * Kd * max(dot(s_eye, n_eye), 0.0); // max() is a safety catch to make sure we never get negative colours
  vec4 Is = vec4(1,1,1,1) * Ks * pow(max(dot(h_eye, v_eye), 0), Ns); // my ambient light colour is hard coded white, but could load Ls

  fragmentColour = texture( myTextureSampler, UV ).rgba *(Ia + Id + Is);

#version 400
layout (location = 0) in vec3 vertexPosition;
layout(location = 1) in vec2 vertexUV;
layout (location = 2) in vec3 vertexNormal;

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;

// Output data ; will be interpolated for each fragment.
out vec2 UV;
out vec4 vpeye;
out vec4 vneye;

void main() {
vpeye = modelViewMatrix * vec4(vertexPosition, 1.0);
vneye = vec4(normalMatrix * vertexNormal, 0);
UV = vertexUV;
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);

Also, I think this might be related to my matrices. I load the model with assimp so the normals are correct. The other matrices are set like this:

The ModelView matrix:
glm::mat4 ModelView=cam->getView()*Model;

The NormalMatrix:
//The "normal matrix" is an inverse*transpose of the model*view matrix
glm::mat4 normat=glm::inverse(glm::transpose(ModelView));

How I calculate the view matrix:
// Camera matrix
_view	   = glm::lookAt(
_pos,		   // Camera is here
_pos+direction, // and looks here : at the same position, plus "direction"
up				  // Head is up (set to 0,-1,0 to look upside-down)

Hope you can help a poor noobley out and explain some things. Posted Image