Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 26 Oct 2013
Offline Last Active Oct 07 2016 01:19 PM

Posts I've Made

In Topic: Getting a distance to thr fragment.

18 September 2016 - 08:52 AM

Getting the distance to a 3D object from a depth texture shouldn't be too difficult. The steps to create a depth texture are, enable Depth Testing, bind the depth texture for rendering, perform your operations on (or using data from) the depth texture.


This is how you create a depth texture in OpenGL:

GLuint id;
int width = 512;
int height = 512;

// create a depth texture
glGenTextures(1, &id); 
glBindTexture(GL_TEXTURE_2D, id); 
// set some parameters
glBindTexture(GL_TEXTURE_2D, 0); // unbind

To use the depth texture within your shaders is the exact same way as you would any other texture, bind it to a texture slot and set a uniform for it. Make sure you also have enabled depth testing.


glUniform1i(glGetUniformLocation(shaderProgram, "DepthTexture"), 1);


Within your shader you can access it like so:

uniform sampler2D DepthTexture;


When you sample your depth texture within a shader the Z component will store the depth of the fragment. i.e:

float depth = texture(DepthTexture, UV).z


You might want to bind the depth texture to a Framebuffer, but that is exactly the same as attaching any other texture apart from you need to use 'GL_DEPTH_ATTACHMENT' rather than something like 'GL_COLOR_ATTACHMENT0'.

Thanks! But how to calculate distance from camera eye to the pixel of the depth? And what if I use logarithm z ? Is it not matter?

In Topic: Directional lighting trouble.

08 June 2016 - 08:35 AM

I've solved it! Thanks all. This is becouse I have to multyply direction to the normalMatrix. Mistake is I mul direction to modelView.

In Topic: Directional lighting trouble.

08 June 2016 - 08:12 AM

attribute vec3 aVertexNormal;
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;

uniform mat4 uPMatrix;
uniform mat4 uMVMatrix;
uniform mat3 uNMatrix;

varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;

void main(void) {
        vTransformedNormal = uNMatrix * aVertexNormal;
        vTextureCoord = aTextureCoord;

        vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
        gl_Position = uPMatrix * vPosition;

And frag:

precision highp float;

varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
uniform vec4 uColor;
uniform sampler2D uSampler;

uniform int pointLightsQuantity;
uniform vec4 pointLightsPositions[MAX_POINT_LIGHTS];
uniform vec3 pointLightsParamsv[MAX_POINT_LIGHTS * 3];
uniform float pointLightsParamsf[MAX_POINT_LIGHTS];

void main(void) {

    vec3 lightWeighting;
    vec3 lightDirection;
    vec3 normal;
    vec3 eyeDirection;
    vec3 reflectionDirection;
    float specularLightWeighting;
    float diffuseLightWeighting;

    lightDirection = normalize(pointLightsPositions[0].xyz);
    //float distance = length(dir);
    //float attenuation = 1.0/(1.0+0.1*distance+0.01*distance*distance);
    normal = normalize(vTransformedNormal);
    eyeDirection = normalize(-vPosition.xyz);
    reflectionDirection = reflect(-lightDirection, normal);	
    specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), pointLightsParamsf[0]);
    diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
    lightWeighting = pointLightsParamsv[0] + pointLightsParamsv[1] * diffuseLightWeighting + pointLightsParamsv[2] * specularLightWeighting;

    vec4 cc = texture2D( uSampler, vTextureCoord.st );
    gl_FragColor = vec4(lightWeighting, uColor.a) * cc * uColor;

uNMatrix is normal matrix.

I dont' know..where is a mistake?

In Topic: Directional lighting trouble.

07 June 2016 - 08:42 AM

Ok I dont translate direction, but how to direct the light to fragment now?

In Topic: Directional lighting trouble.

07 June 2016 - 05:06 AM

I made only direction that translate to model view, but nothing.